aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/javassist/CtField.java
diff options
context:
space:
mode:
authorchiba <chiba@30ef5769-5b8d-40dd-aea6-55b5d6557bb3>2005-08-17 16:07:45 +0000
committerchiba <chiba@30ef5769-5b8d-40dd-aea6-55b5d6557bb3>2005-08-17 16:07:45 +0000
commitf6faf5fde703853cfe3ea27a46db4b8523746d0d (patch)
tree4da71a834733c02f4600792abf823e9aad5a4c35 /src/main/javassist/CtField.java
parent6dc6b9945eb0d3b49113d6c512d35ed1a16dd870 (diff)
downloadjavassist-f6faf5fde703853cfe3ea27a46db4b8523746d0d.tar.gz
javassist-f6faf5fde703853cfe3ea27a46db4b8523746d0d.zip
fixed a compilation problem of static final fields.
git-svn-id: http://anonsvn.jboss.org/repos/javassist/trunk@192 30ef5769-5b8d-40dd-aea6-55b5d6557bb3
Diffstat (limited to 'src/main/javassist/CtField.java')
-rw-r--r--src/main/javassist/CtField.java84
1 files changed, 82 insertions, 2 deletions
diff --git a/src/main/javassist/CtField.java b/src/main/javassist/CtField.java
index 931abf92..710f8c50 100644
--- a/src/main/javassist/CtField.java
+++ b/src/main/javassist/CtField.java
@@ -17,8 +17,12 @@ package javassist;
import javassist.bytecode.*;
import javassist.compiler.Javac;
+import javassist.compiler.SymbolTable;
import javassist.compiler.CompileError;
import javassist.compiler.ast.ASTree;
+import javassist.compiler.ast.IntConst;
+import javassist.compiler.ast.DoubleConst;
+import javassist.compiler.ast.StringL;
/**
* An instance of CtField represents a field.
@@ -26,6 +30,8 @@ import javassist.compiler.ast.ASTree;
* @see CtClass#getDeclaredFields()
*/
public class CtField extends CtMember {
+ static final String javaLangString = "java.lang.String";
+
protected FieldInfo fieldInfo;
/**
@@ -665,6 +671,10 @@ public class CtField extends CtMember {
// produce codes for initialization
abstract int compileIfStatic(CtClass type, String name,
Bytecode code, Javac drv) throws CannotCompileException;
+
+ // returns the index of CONSTANT_Integer_info etc
+ // if the value is constant. Otherwise, 0.
+ int getConstantValue(ConstPool cp, CtClass type) { return 0; }
}
static abstract class CodeInitializer0 extends Initializer {
@@ -697,6 +707,34 @@ public class CtField extends CtMember {
throw new CannotCompileException(e);
}
}
+
+ int getConstantValue2(ConstPool cp, CtClass type, ASTree tree) {
+ if (type.isPrimitive()) {
+ if (tree instanceof IntConst) {
+ long value = ((IntConst)tree).get();
+ if (type == CtClass.doubleType)
+ return cp.addDoubleInfo((double)value);
+ else if (type == CtClass.floatType)
+ return cp.addFloatInfo((float)value);
+ else if (type == CtClass.longType)
+ return cp.addLongInfo(value);
+ else if (type != CtClass.voidType)
+ return cp.addIntegerInfo((int)value);
+ }
+ else if (tree instanceof DoubleConst) {
+ double value = ((DoubleConst)tree).get();
+ if (type == CtClass.floatType)
+ return cp.addFloatInfo((float)value);
+ else if (type == CtClass.doubleType)
+ return cp.addDoubleInfo(value);
+ }
+ }
+ else if (tree instanceof StringL
+ && type.getName().equals(javaLangString))
+ return cp.addStringInfo(((StringL)tree).get());
+
+ return 0;
+ }
}
static class CodeInitializer extends CodeInitializer0 {
@@ -707,6 +745,16 @@ public class CtField extends CtMember {
void compileExpr(Javac drv) throws CompileError {
drv.compileExpr(expression);
}
+
+ int getConstantValue(ConstPool cp, CtClass type) {
+ try {
+ ASTree t = Javac.parseExpr(expression, new SymbolTable());
+ return getConstantValue2(cp, type, t);
+ }
+ catch (CompileError e) {
+ return 0;
+ }
+ }
}
static class PtreeInitializer extends CodeInitializer0 {
@@ -717,6 +765,10 @@ public class CtField extends CtMember {
void compileExpr(Javac drv) throws CompileError {
drv.compileExpr(expression);
}
+
+ int getConstantValue(ConstPool cp, CtClass type) {
+ return getConstantValue2(cp, type, expression);
+ }
}
/**
@@ -863,7 +915,7 @@ public class CtField extends CtMember {
{
int nparam = stringParams.length;
code.addIconst(nparam);
- code.addAnewarray("java.lang.String");
+ code.addAnewarray(javaLangString);
for (int j = 0; j < nparam; ++j) {
code.add(Bytecode.DUP); // dup
code.addIconst(j); // iconst_<j>
@@ -980,6 +1032,13 @@ public class CtField extends CtMember {
code.addPutstatic(Bytecode.THIS, name, Descriptor.of(type));
return 1; // stack size
}
+
+ int getConstantValue(ConstPool cp, CtClass type) {
+ if (type == CtClass.intType)
+ return cp.addIntegerInfo(value);
+ else
+ return 0;
+ }
}
static class LongInitializer extends Initializer {
@@ -1009,6 +1068,13 @@ public class CtField extends CtMember {
code.addPutstatic(Bytecode.THIS, name, Descriptor.of(type));
return 2; // stack size
}
+
+ int getConstantValue(ConstPool cp, CtClass type) {
+ if (type == CtClass.longType)
+ return cp.addLongInfo(value);
+ else
+ return 0;
+ }
}
static class DoubleInitializer extends Initializer {
@@ -1038,6 +1104,13 @@ public class CtField extends CtMember {
code.addPutstatic(Bytecode.THIS, name, Descriptor.of(type));
return 2; // stack size
}
+
+ int getConstantValue(ConstPool cp, CtClass type) {
+ if (type == CtClass.doubleType)
+ return cp.addDoubleInfo(value);
+ else
+ return 0;
+ }
}
static class StringInitializer extends Initializer {
@@ -1046,7 +1119,7 @@ public class CtField extends CtMember {
StringInitializer(String v) { value = v; }
void check(CtClass type) throws CannotCompileException {
- if (!type.getName().equals("java.lang.String"))
+ if (!type.getName().equals(javaLangString))
throw new CannotCompileException("type mismatch");
}
@@ -1067,6 +1140,13 @@ public class CtField extends CtMember {
code.addPutstatic(Bytecode.THIS, name, Descriptor.of(type));
return 1; // stack size
}
+
+ int getConstantValue(ConstPool cp, CtClass type) {
+ if (type.getName().equals(javaLangString))
+ return cp.addStringInfo(value);
+ else
+ return 0;
+ }
}
static class ArrayInitializer extends Initializer {