Browse Source

implemented CtField#getConstantValue().


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

+ 36
- 0
src/main/javassist/CtField.java View File

fieldInfo.setDescriptor(Descriptor.of(clazz)); fieldInfo.setDescriptor(Descriptor.of(clazz));
} }


/**
* Returns the value of this field if it is a constant field.
* This method works only if the field type is a primitive type
* or <code>String</code> type. Otherwise, it returns <code>null</code>.
* A constant field is <code>static</code> and <code>final</code>.
*
* @return a <code>Integer</code>, <code>Long</code>, <code>Float</code>,
* <code>Double</code>, or <code>String</code> object
* representing the constant value.
* <code>null</code> if it is not a constant field
* or if the field type is not a primitive type
* or <code>String</code>.
*/
public Object getConstantValue() {
int index = fieldInfo.getConstantValue();
if (index == 0)
return null;

ConstPool cp = fieldInfo.getConstPool();
switch (cp.getTag(index)) {
case ConstPool.CONST_Long :
return new Long(cp.getLongInfo(index));
case ConstPool.CONST_Float :
return new Float(cp.getFloatInfo(index));
case ConstPool.CONST_Double :
return new Double(cp.getDoubleInfo(index));
case ConstPool.CONST_Integer :
return new Integer(cp.getIntegerInfo(index));
case ConstPool.CONST_String :
return cp.getStringInfo(index);
default :
throw new RuntimeException("bad tag: " + cp.getTag(index)
+ " at " + index);
}
}

/** /**
* Obtains an attribute with the given name. * Obtains an attribute with the given name.
* If that attribute is not found in the class file, this * If that attribute is not found in the class file, this

+ 1
- 1
src/main/javassist/bytecode/ConstPool.java View File

/** /**
* <code>CONSTANT_Float</code> * <code>CONSTANT_Float</code>
*/ */
public static final int CONST_Float = IntegerInfo.tag;
public static final int CONST_Float = FloatInfo.tag;


/** /**
* <code>CONSTANT_Long</code> * <code>CONSTANT_Long</code>

+ 27
- 1
src/main/javassist/bytecode/FieldInfo.java View File

} }


void prune(ConstPool cp) { void prune(ConstPool cp) {
attribute = null;
int index = getConstantValue();
if (index == 0)
attribute = null;
else {
index = constPool.copy(index, cp, null);
attribute = new LinkedList();
attribute.add(new ConstantAttribute(cp, index));
}

name = cp.addUtf8Info(getName()); name = cp.addUtf8Info(getName());
descriptor = cp.addUtf8Info(getDescriptor()); descriptor = cp.addUtf8Info(getDescriptor());
constPool = cp; constPool = cp;
descriptor = constPool.addUtf8Info(desc); descriptor = constPool.addUtf8Info(desc);
} }


/**
* Finds a ConstantValue attribute and returns the index into
* the <code>constant_pool</code> table.
*
* @return 0 if a ConstantValue attribute is not found.
*/
public int getConstantValue() {
if ((accessFlags & AccessFlag.STATIC) == 0)
return 0;

ConstantAttribute attr
= (ConstantAttribute)getAttribute(ConstantAttribute.tag);
if (attr == null)
return 0;
else
return attr.getConstantValue();
}

/** /**
* Returns all the attributes. * Returns all the attributes.
* A new element can be added to the returned list * A new element can be added to the returned list

+ 2
- 0
src/main/javassist/compiler/ast/CastExpr.java View File



public ASTree getOprand() { return getRight().getLeft(); } public ASTree getOprand() { return getRight().getLeft(); }


public void setOprand(ASTree t) { getRight().setLeft(t); }

public String getTag() { return "cast:" + castType + ":" + arrayDim; } public String getTag() { return "cast:" + castType + ":" + arrayDim; }


public void accept(Visitor v) throws CompileError { v.atCastExpr(this); } public void accept(Visitor v) throws CompileError { v.atCastExpr(this); }

+ 2
- 0
src/main/javassist/compiler/ast/CondExpr.java View File



public ASTree condExpr() { return head(); } public ASTree condExpr() { return head(); }


public void setCond(ASTree t) { setHead(t); }

public ASTree thenExpr() { return tail().head(); } public ASTree thenExpr() { return tail().head(); }


public void setThen(ASTree t) { tail().setHead(t); } public void setThen(ASTree t) { tail().setHead(t); }

+ 54
- 0
src/main/javassist/compiler/ast/DoubleConst.java View File

package javassist.compiler.ast; package javassist.compiler.ast;


import javassist.compiler.CompileError; import javassist.compiler.CompileError;
import javassist.compiler.TokenId;


/** /**
* Double constant. * Double constant.


public double get() { return value; } public double get() { return value; }


public void set(double v) { value = v; }

/* Returns DoubleConstant or FloatConstant /* Returns DoubleConstant or FloatConstant
*/ */
public int getType() { return type; } public int getType() { return type; }
public void accept(Visitor v) throws CompileError { public void accept(Visitor v) throws CompileError {
v.atDoubleConst(this); v.atDoubleConst(this);
} }

public ASTree compute(int op, ASTree right) {
if (right instanceof IntConst)
return compute0(op, (IntConst)right);
else if (right instanceof DoubleConst)
return compute0(op, (DoubleConst)right);
else
return null;
}

private DoubleConst compute0(int op, DoubleConst right) {
int newType;
if (this.type == TokenId.DoubleConstant
|| right.type == TokenId.DoubleConstant)
newType = TokenId.DoubleConstant;
else
newType = TokenId.FloatConstant;

return compute(op, this.value, right.value, newType);
}

private DoubleConst compute0(int op, IntConst right) {
return compute(op, this.value, (double)right.value, this.type);
}

private static DoubleConst compute(int op, double value1, double value2,
int newType)
{
double newValue;
switch (op) {
case '+' :
newValue = value1 + value2;
break;
case '-' :
newValue = value1 - value2;
break;
case '*' :
newValue = value1 * value2;
break;
case '/' :
newValue = value1 / value2;
break;
case '%' :
newValue = value1 % value2;
break;
default :
return null;
}

return new DoubleConst(newValue, newType);
}
} }

+ 4
- 0
src/main/javassist/compiler/ast/Expr.java View File



public ASTree oprand1() { return getLeft(); } public ASTree oprand1() { return getLeft(); }


public void setOprand1(ASTree expr) {
setLeft(expr);
}

public ASTree oprand2() { return getRight().getLeft(); } public ASTree oprand2() { return getRight().getLeft(); }


public void setOprand2(ASTree expr) { public void setOprand2(ASTree expr) {

+ 98
- 0
src/main/javassist/compiler/ast/IntConst.java View File

package javassist.compiler.ast; package javassist.compiler.ast;


import javassist.compiler.CompileError; import javassist.compiler.CompileError;
import javassist.compiler.TokenId;


/** /**
* Integer constant. * Integer constant.


public long get() { return value; } public long get() { return value; }


public void set(long v) { value = v; }

/* Returns IntConstant, CharConstant, or LongConstant. /* Returns IntConstant, CharConstant, or LongConstant.
*/ */
public int getType() { return type; } public int getType() { return type; }
public void accept(Visitor v) throws CompileError { public void accept(Visitor v) throws CompileError {
v.atIntConst(this); v.atIntConst(this);
} }

public ASTree compute(int op, ASTree right) {
if (right instanceof IntConst)
return compute0(op, (IntConst)right);
else if (right instanceof DoubleConst)
return compute0(op, (DoubleConst)right);
else
return null;
}

private IntConst compute0(int op, IntConst right) {
int type1 = this.type;
int type2 = right.type;
int newType;
if (type1 == TokenId.LongConstant || type2 == TokenId.LongConstant)
newType = TokenId.LongConstant;
else if (type1 == TokenId.CharConstant
&& type2 == TokenId.CharConstant)
newType = TokenId.CharConstant;
else
newType = TokenId.IntConstant;

long value1 = this.value;
long value2 = right.value;
long newValue;
switch (op) {
case '+' :
newValue = value1 + value2;
break;
case '-' :
newValue = value1 - value2;
break;
case '*' :
newValue = value1 * value2;
break;
case '/' :
newValue = value1 / value2;
break;
case '%' :
newValue = value1 % value2;
break;
case '|' :
newValue = value1 | value2;
break;
case '^' :
newValue = value1 ^ value2;
break;
case '&' :
newValue = value1 & value2;
break;
case TokenId.LSHIFT :
newValue = value << (int)value2;
newType = type1;
break;
case TokenId.RSHIFT :
newValue = value >> (int)value2;
newType = type1;
break;
case TokenId.ARSHIFT :
newValue = value >>> (int)value2;
newType = type1;
break;
default :
return null;
}

return new IntConst(newValue, newType);
}

private DoubleConst compute0(int op, DoubleConst right) {
double value1 = (double)this.value;
double value2 = right.value;
double newValue;
switch (op) {
case '+' :
newValue = value1 + value2;
break;
case '-' :
newValue = value1 - value2;
break;
case '*' :
newValue = value1 * value2;
break;
case '/' :
newValue = value1 / value2;
break;
case '%' :
newValue = value1 % value2;
break;
default :
return null;
}

return new DoubleConst(newValue, right.type);
}
} }

Loading…
Cancel
Save