diff options
author | chiba <chiba@30ef5769-5b8d-40dd-aea6-55b5d6557bb3> | 2007-10-20 16:47:00 +0000 |
---|---|---|
committer | chiba <chiba@30ef5769-5b8d-40dd-aea6-55b5d6557bb3> | 2007-10-20 16:47:00 +0000 |
commit | 1d637574afe2089c8a9c0dfa52ea825721da5ce2 (patch) | |
tree | e38b6ea56471f9ad8de7d430682e85342992a46c | |
parent | 58420fe406ca4cb4afec2fb6fc0f65cd11161e5c (diff) | |
download | javassist-1d637574afe2089c8a9c0dfa52ea825721da5ce2.tar.gz javassist-1d637574afe2089c8a9c0dfa52ea825721da5ce2.zip |
for JASSIST-37
git-svn-id: http://anonsvn.jboss.org/repos/javassist/trunk@411 30ef5769-5b8d-40dd-aea6-55b5d6557bb3
-rw-r--r-- | src/main/javassist/CtClass.java | 5 | ||||
-rw-r--r-- | src/main/javassist/CtClassType.java | 1 | ||||
-rw-r--r-- | src/main/javassist/CtField.java | 39 |
3 files changed, 26 insertions, 19 deletions
diff --git a/src/main/javassist/CtClass.java b/src/main/javassist/CtClass.java index e7aafc26..e5033335 100644 --- a/src/main/javassist/CtClass.java +++ b/src/main/javassist/CtClass.java @@ -898,6 +898,11 @@ public abstract class CtClass { * <p>Here, the type of variable <code>cc</code> is <code>CtClass</code>. * The type of <code>f</code> is <code>CtField</code>. * + * <p>Note: do not change the modifier of the field + * (in particular, do not add or remove <code>static</code> + * to/from the modifier) + * after it is added to the class by <code>addField()</code>. + * * @param init an expression for the initial value. * * @see javassist.CtField.Initializer#byExpr(String) diff --git a/src/main/javassist/CtClassType.java b/src/main/javassist/CtClassType.java index d8182d10..13a2b871 100644 --- a/src/main/javassist/CtClassType.java +++ b/src/main/javassist/CtClassType.java @@ -1146,6 +1146,7 @@ class CtClassType extends CtClass { init = f.getInit(); if (init != null) { + init.check(f.getSignature()); int mod = f.getModifiers(); if (Modifier.isStatic(mod) && Modifier.isFinal(mod)) try { diff --git a/src/main/javassist/CtField.java b/src/main/javassist/CtField.java index ce9c6ddd..efc6b94b 100644 --- a/src/main/javassist/CtField.java +++ b/src/main/javassist/CtField.java @@ -413,13 +413,21 @@ public class CtField extends CtMember { public static abstract class Initializer { /** * Makes an initializer that assigns a constant integer value. - * The field must be integer type. + * The field must be integer, short, char, or byte type. */ public static Initializer constant(int i) { return new IntInitializer(i); } /** + * Makes an initializer that assigns a constant boolean value. + * The field must be boolean type. + */ + public static Initializer constant(boolean b) { + return new IntInitializer(b ? 1 : 0); + } + + /** * Makes an initializer that assigns a constant long value. * The field must be long type. */ @@ -751,7 +759,7 @@ public class CtField extends CtMember { // Check whether this initializer is valid for the field type. // If it is invaild, this method throws an exception. - void check(CtClass type) throws CannotCompileException {} + void check(String desc) throws CannotCompileException {} // produce codes for initialization abstract int compile(CtClass type, String name, Bytecode code, @@ -1100,8 +1108,9 @@ public class CtField extends CtMember { IntInitializer(int v) { value = v; } - void check(CtClass type) throws CannotCompileException { - if (type != CtClass.intType) + void check(String desc) throws CannotCompileException { + char c = desc.charAt(0); + if (c != 'I' && c != 'S' && c != 'B' && c != 'C' && c != 'Z') throw new CannotCompileException("type mismatch"); } @@ -1124,10 +1133,7 @@ public class CtField extends CtMember { } int getConstantValue(ConstPool cp, CtClass type) { - if (type == CtClass.intType) - return cp.addIntegerInfo(value); - else - return 0; + return cp.addIntegerInfo(value); } } @@ -1136,8 +1142,8 @@ public class CtField extends CtMember { LongInitializer(long v) { value = v; } - void check(CtClass type) throws CannotCompileException { - if (type != CtClass.longType) + void check(String desc) throws CannotCompileException { + if (!desc.equals("J")) throw new CannotCompileException("type mismatch"); } @@ -1172,8 +1178,8 @@ public class CtField extends CtMember { DoubleInitializer(double v) { value = v; } - void check(CtClass type) throws CannotCompileException { - if (type != CtClass.doubleType) + void check(String desc) throws CannotCompileException { + if (!desc.equals("D")) throw new CannotCompileException("type mismatch"); } @@ -1208,11 +1214,6 @@ public class CtField extends CtMember { StringInitializer(String v) { value = v; } - void check(CtClass type) throws CannotCompileException { - if (!type.getName().equals(javaLangString)) - throw new CannotCompileException("type mismatch"); - } - int compile(CtClass type, String name, Bytecode code, CtClass[] parameters, Javac drv) throws CannotCompileException @@ -1278,8 +1279,8 @@ public class CtField extends CtMember { MultiArrayInitializer(CtClass t, int[] d) { type = t; dim = d; } - void check(CtClass type) throws CannotCompileException { - if (!type.isArray()) + void check(String desc) throws CannotCompileException { + if (desc.charAt(0) != '[') throw new CannotCompileException("type mismatch"); } |