Browse Source

fixed a compilation problem of static final fields.


git-svn-id: http://anonsvn.jboss.org/repos/javassist/trunk@192 30ef5769-5b8d-40dd-aea6-55b5d6557bb3
tags/rel_3_17_1_ga
chiba 18 years ago
parent
commit
f6faf5fde7

+ 1
- 1
Readme.html View File

@@ -52,7 +52,7 @@ other editors.
</tr>

<tr>
<td><li><tt><a href="html/index.html">html/index.html</a></tt></tr>
<td><li><tt><a href="html/index.html">html/index.html</a></tt></td>
<td>The top page of the Javassist API document.</td>
</tr>


+ 15
- 0
src/main/javassist/CtClassType.java View File

@@ -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());

+ 82
- 2
src/main/javassist/CtField.java View File

@@ -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 {

+ 1
- 0
src/main/javassist/CtNewConstructor.java View File

@@ -30,6 +30,7 @@ import javassist.CtMethod.ConstParameter;
* initializer obtained by <code>makeClassInitializer()</code>.
*
* @see CtClass#addConstructor(CtConstructor)
* @see CtClass#makeClassInitializer()
*/
public class CtNewConstructor {
/**

+ 10
- 1
src/main/javassist/bytecode/annotation/Annotation.java View File

@@ -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)

+ 1
- 0
src/main/javassist/bytecode/annotation/MemberValue.java View File

@@ -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
*/

+ 1
- 0
src/main/javassist/bytecode/annotation/MemberValueVisitor.java View File

@@ -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 {

+ 12
- 3
src/main/javassist/compiler/Javac.java View File

@@ -569,11 +569,20 @@ 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);
}
}

+ 19
- 2
src/main/javassist/compiler/Parser.java View File

@@ -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 :

Loading…
Cancel
Save