aboutsummaryrefslogtreecommitdiffstats
path: root/src
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
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')
-rw-r--r--src/main/javassist/CtClassType.java15
-rw-r--r--src/main/javassist/CtField.java84
-rw-r--r--src/main/javassist/CtNewConstructor.java1
-rw-r--r--src/main/javassist/bytecode/annotation/Annotation.java11
-rw-r--r--src/main/javassist/bytecode/annotation/MemberValue.java1
-rw-r--r--src/main/javassist/bytecode/annotation/MemberValueVisitor.java1
-rw-r--r--src/main/javassist/compiler/Javac.java15
-rw-r--r--src/main/javassist/compiler/Parser.java21
8 files changed, 141 insertions, 8 deletions
diff --git a/src/main/javassist/CtClassType.java b/src/main/javassist/CtClassType.java
index 44014641..b400d8b1 100644
--- a/src/main/javassist/CtClassType.java
+++ b/src/main/javassist/CtClassType.java
@@ -32,6 +32,7 @@ import javassist.bytecode.BadBytecode;
import javassist.bytecode.Bytecode;
import javassist.bytecode.ClassFile;
import javassist.bytecode.CodeAttribute;
+import javassist.bytecode.ConstantAttribute;
import javassist.bytecode.CodeIterator;
import javassist.bytecode.ConstPool;
import javassist.bytecode.Descriptor;
@@ -834,6 +835,20 @@ class CtClassType extends CtClass {
if (init == null)
init = f.getInit();
+ if (init != null) {
+ int mod = f.getModifiers();
+ if (Modifier.isStatic(mod) && Modifier.isFinal(mod))
+ try {
+ ConstPool cp = getClassFile2().getConstPool();
+ int index = init.getConstantValue(cp, f.getType());
+ if (index != 0) {
+ f.getFieldInfo2().addAttribute(new ConstantAttribute(cp, index));
+ init = null;
+ }
+ }
+ catch (NotFoundException e) {}
+ }
+
getFieldsCache();
fieldsCache = CtField.append(fieldsCache, f);
getClassFile2().addField(f.getFieldInfo2());
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 {
diff --git a/src/main/javassist/CtNewConstructor.java b/src/main/javassist/CtNewConstructor.java
index 43d64ebc..f3f03d00 100644
--- a/src/main/javassist/CtNewConstructor.java
+++ b/src/main/javassist/CtNewConstructor.java
@@ -30,6 +30,7 @@ import javassist.CtMethod.ConstParameter;
* initializer obtained by <code>makeClassInitializer()</code>.
*
* @see CtClass#addConstructor(CtConstructor)
+ * @see CtClass#makeClassInitializer()
*/
public class CtNewConstructor {
/**
diff --git a/src/main/javassist/bytecode/annotation/Annotation.java b/src/main/javassist/bytecode/annotation/Annotation.java
index e542d8cb..f4f8ac61 100644
--- a/src/main/javassist/bytecode/annotation/Annotation.java
+++ b/src/main/javassist/bytecode/annotation/Annotation.java
@@ -243,7 +243,16 @@ public class Annotation {
/**
* Obtains the member value with the given name.
*
- * @return null if the member cannot be found.
+ * <p>If this annotation does not have a value for the
+ * specified member,
+ * this method returns null. It does not return a
+ * <code>MemberValue</code> with the default value.
+ * The default value can be obtained from the annotation type.
+ *
+ * @return null if the member cannot be found or if the value is
+ * the default value.
+ *
+ * @see AnnotationDefaultAttribute
*/
public MemberValue getMemberValue(String name) {
if (members == null)
diff --git a/src/main/javassist/bytecode/annotation/MemberValue.java b/src/main/javassist/bytecode/annotation/MemberValue.java
index 4d0c3554..84d0eb38 100644
--- a/src/main/javassist/bytecode/annotation/MemberValue.java
+++ b/src/main/javassist/bytecode/annotation/MemberValue.java
@@ -21,6 +21,7 @@ import java.io.IOException;
/**
* The value of a member declared in an annotation.
*
+ * @see Annotation#getMemberValue(String)
* @author <a href="mailto:bill@jboss.org">Bill Burke</a>
* @author Shigeru Chiba
*/
diff --git a/src/main/javassist/bytecode/annotation/MemberValueVisitor.java b/src/main/javassist/bytecode/annotation/MemberValueVisitor.java
index 55aabb8f..6944bd0a 100644
--- a/src/main/javassist/bytecode/annotation/MemberValueVisitor.java
+++ b/src/main/javassist/bytecode/annotation/MemberValueVisitor.java
@@ -18,6 +18,7 @@ package javassist.bytecode.annotation;
/**
* Visitor for traversing member values included in an annotation.
*
+ * @see MemberValue#accept(MemberValueVisitor)
* @author <a href="mailto:bill@jboss.org">Bill Burke</a>
*/
public interface MemberValueVisitor {
diff --git a/src/main/javassist/compiler/Javac.java b/src/main/javassist/compiler/Javac.java
index 88ed0ab5..7a9dbe83 100644
--- a/src/main/javassist/compiler/Javac.java
+++ b/src/main/javassist/compiler/Javac.java
@@ -569,12 +569,21 @@ public class Javac {
* have been invoked.
*/
public void compileExpr(String src) throws CompileError {
- Parser p = new Parser(new Lex(src));
- ASTree e = p.parseExpression(stable);
+ ASTree e = parseExpr(src, stable);
compileExpr(e);
}
/**
+ * Parsers an expression.
+ */
+ public static ASTree parseExpr(String src, SymbolTable st)
+ throws CompileError
+ {
+ Parser p = new Parser(new Lex(src));
+ return p.parseExpression(st);
+ }
+
+ /**
* Compiles an exression. <code>recordParams()</code> must be
* called before invoking this method.
*
@@ -585,6 +594,6 @@ public class Javac {
*/
public void compileExpr(ASTree e) throws CompileError {
if (e != null)
- e.accept(gen);
+ gen.compileExpr(e);
}
}
diff --git a/src/main/javassist/compiler/Parser.java b/src/main/javassist/compiler/Parser.java
index ed6e9dd3..e1a0d5b2 100644
--- a/src/main/javassist/compiler/Parser.java
+++ b/src/main/javassist/compiler/Parser.java
@@ -852,6 +852,23 @@ public final class Parser implements TokenId {
case '!' :
case '~' :
t = lex.get();
+ if (t == '-') {
+ int t2 = lex.lookAhead();
+ switch (t2) {
+ case LongConstant :
+ case IntConstant :
+ case CharConstant :
+ lex.get();
+ return new IntConst(-lex.getLong(), t2);
+ case DoubleConstant :
+ case FloatConstant :
+ lex.get();
+ return new DoubleConst(-lex.getDouble(), t2);
+ default :
+ break;
+ }
+ }
+
return Expr.make(t, parseUnaryExpr(tbl));
case '(' :
return parseCast(tbl);
@@ -977,12 +994,12 @@ public final class Parser implements TokenId {
* a class name and a member name in an expression for static member
* access. For example,
* java.lang.Integer.toString(3) in regular Java
- * must be written like this:
+ * can be written like this:
* java.lang.Integer#toString(3) for this compiler.
*/
private ASTree parsePostfix(SymbolTable tbl) throws CompileError {
int token = lex.lookAhead();
- switch (token) {
+ switch (token) { // see also parseUnaryExpr()
case LongConstant :
case IntConstant :
case CharConstant :