/*
* Javassist, a Java-bytecode translator toolkit.
* Copyright (C) 1999- Shigeru Chiba. All Rights Reserved.
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. Alternatively, the contents of this file may be used under
* the terms of the GNU Lesser General Public License Version 2.1 or later,
* or the Apache License Version 2.0.
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*/
package com.fr.third.javassist;
import com.fr.third.javassist.bytecode.ConstPool;
import com.fr.third.javassist.compiler.Javac;
import com.fr.third.javassist.compiler.SymbolTable;
import com.fr.third.javassist.compiler.CompileError;
import com.fr.third.javassist.compiler.ast.ASTree;
import com.fr.third.javassist.compiler.ast.IntConst;
import com.fr.third.javassist.compiler.ast.DoubleConst;
import com.fr.third.javassist.compiler.ast.StringL;
/**
* An instance of CtField represents a field.
*
* @see com.fr.third.javassist.CtClass#getDeclaredFields()
*/
public class CtField extends CtMember {
static final String javaLangString = "java.lang.String";
protected com.fr.third.javassist.bytecode.FieldInfo fieldInfo;
/**
* Creates a CtField
object.
* The created field must be added to a class
* with CtClass.addField()
.
* An initial value of the field is specified
* by a CtField.Initializer
object.
*
*
If getter and setter methods are needed,
* call CtNewMethod.getter()
and
* CtNewMethod.setter()
.
*
* @param type field type
* @param name field name
* @param declaring the class to which the field will be added.
*
* @see com.fr.third.javassist.CtClass#addField(CtField)
* @see CtNewMethod#getter(String,CtField)
* @see CtNewMethod#setter(String,CtField)
* @see CtField.Initializer
*/
public CtField(com.fr.third.javassist.CtClass type, String name, com.fr.third.javassist.CtClass declaring)
throws com.fr.third.javassist.CannotCompileException
{
this(com.fr.third.javassist.bytecode.Descriptor.of(type), name, declaring);
}
/**
* Creates a copy of the given field.
* The created field must be added to a class
* with CtClass.addField()
.
* An initial value of the field is specified
* by a CtField.Initializer
object.
*
*
If getter and setter methods are needed,
* call CtNewMethod.getter()
and
* CtNewMethod.setter()
.
*
* @param src the original field
* @param declaring the class to which the field will be added.
* @see CtNewMethod#getter(String,CtField)
* @see CtNewMethod#setter(String,CtField)
* @see CtField.Initializer
*/
public CtField(CtField src, com.fr.third.javassist.CtClass declaring)
throws com.fr.third.javassist.CannotCompileException
{
this(src.fieldInfo.getDescriptor(), src.fieldInfo.getName(),
declaring);
java.util.ListIterator iterator
= src.fieldInfo.getAttributes().listIterator();
com.fr.third.javassist.bytecode.FieldInfo fi = fieldInfo;
fi.setAccessFlags(src.fieldInfo.getAccessFlags());
com.fr.third.javassist.bytecode.ConstPool cp = fi.getConstPool();
while (iterator.hasNext()) {
com.fr.third.javassist.bytecode.AttributeInfo ainfo = (com.fr.third.javassist.bytecode.AttributeInfo)iterator.next();
fi.addAttribute(ainfo.copy(cp, null));
}
}
private CtField(String typeDesc, String name, com.fr.third.javassist.CtClass clazz)
throws com.fr.third.javassist.CannotCompileException
{
super(clazz);
com.fr.third.javassist.bytecode.ClassFile cf = clazz.getClassFile2();
if (cf == null)
throw new com.fr.third.javassist.CannotCompileException("bad declaring class: "
+ clazz.getName());
fieldInfo = new com.fr.third.javassist.bytecode.FieldInfo(cf.getConstPool(), name, typeDesc);
}
CtField(com.fr.third.javassist.bytecode.FieldInfo fi, com.fr.third.javassist.CtClass clazz) {
super(clazz);
fieldInfo = fi;
}
/**
* Returns a String representation of the object.
*/
public String toString() {
return getDeclaringClass().getName() + "." + getName()
+ ":" + fieldInfo.getDescriptor();
}
protected void extendToString(StringBuffer buffer) {
buffer.append(' ');
buffer.append(getName());
buffer.append(' ');
buffer.append(fieldInfo.getDescriptor());
}
/* Javac.CtFieldWithInit overrides.
*/
protected ASTree getInitAST() { return null; }
/* Called by CtClassType.addField().
*/
Initializer getInit() {
ASTree tree = getInitAST();
if (tree == null)
return null;
else
return Initializer.byExpr(tree);
}
/**
* Compiles the given source code and creates a field.
* Examples of the source code are:
*
*
* "public String name;" * "public int k = 3;"* *
Note that the source code ends with ';'
* (semicolon).
*
* @param src the source text.
* @param declaring the class to which the created field is added.
*/
public static CtField make(String src, com.fr.third.javassist.CtClass declaring)
throws com.fr.third.javassist.CannotCompileException
{
Javac compiler = new Javac(declaring);
try {
CtMember obj = compiler.compile(src);
if (obj instanceof CtField)
return (CtField)obj; // an instance of Javac.CtFieldWithInit
}
catch (CompileError e) {
throw new com.fr.third.javassist.CannotCompileException(e);
}
throw new com.fr.third.javassist.CannotCompileException("not a field");
}
/**
* Returns the FieldInfo representing the field in the class file.
*/
public com.fr.third.javassist.bytecode.FieldInfo getFieldInfo() {
declaringClass.checkModify();
return fieldInfo;
}
/**
* Returns the FieldInfo representing the field in the class
* file (read only).
* Normal applications do not need calling this method. Use
* getFieldInfo()
.
*
*
The FieldInfo
object obtained by this method
* is read only. Changes to this object might not be reflected
* on a class file generated by toBytecode()
,
* toClass()
, etc in CtClass
.
*
*
This method is available even if the CtClass
* containing this field is frozen. However, if the class is
* frozen, the FieldInfo
might be also pruned.
*
* @see #getFieldInfo()
* @see com.fr.third.javassist.CtClass#isFrozen()
* @see com.fr.third.javassist.CtClass#prune()
*/
public com.fr.third.javassist.bytecode.FieldInfo getFieldInfo2() { return fieldInfo; }
/**
* Returns the class declaring the field.
*/
public com.fr.third.javassist.CtClass getDeclaringClass() {
// this is redundant but for javadoc.
return super.getDeclaringClass();
}
/**
* Returns the name of the field.
*/
public String getName() {
return fieldInfo.getName();
}
/**
* Changes the name of the field.
*/
public void setName(String newName) {
declaringClass.checkModify();
fieldInfo.setName(newName);
}
/**
* Returns the encoded modifiers of the field.
*
* @see Modifier
*/
public int getModifiers() {
return com.fr.third.javassist.bytecode.AccessFlag.toModifier(fieldInfo.getAccessFlags());
}
/**
* Sets the encoded modifiers of the field.
*
* @see Modifier
*/
public void setModifiers(int mod) {
declaringClass.checkModify();
fieldInfo.setAccessFlags(com.fr.third.javassist.bytecode.AccessFlag.of(mod));
}
/**
* Returns true if the class has the specified annotation class.
*
* @param clz the annotation class.
* @return true
if the annotation is found, otherwise false
.
* @since 3.11
*/
public boolean hasAnnotation(Class clz) {
com.fr.third.javassist.bytecode.FieldInfo fi = getFieldInfo2();
com.fr.third.javassist.bytecode.AnnotationsAttribute ainfo = (com.fr.third.javassist.bytecode.AnnotationsAttribute)
fi.getAttribute(com.fr.third.javassist.bytecode.AnnotationsAttribute.invisibleTag);
com.fr.third.javassist.bytecode.AnnotationsAttribute ainfo2 = (com.fr.third.javassist.bytecode.AnnotationsAttribute)
fi.getAttribute(com.fr.third.javassist.bytecode.AnnotationsAttribute.visibleTag);
return com.fr.third.javassist.CtClassType.hasAnnotationType(clz, getDeclaringClass().getClassPool(),
ainfo, ainfo2);
}
/**
* Returns the annotation if the class has the specified annotation class.
* For example, if an annotation @Author
is associated
* with this field, an Author
object is returned.
* The member values can be obtained by calling methods on
* the Author
object.
*
* @param clz the annotation class.
* @return the annotation if found, otherwise null
.
* @since 3.11
*/
public Object getAnnotation(Class clz) throws ClassNotFoundException {
com.fr.third.javassist.bytecode.FieldInfo fi = getFieldInfo2();
com.fr.third.javassist.bytecode.AnnotationsAttribute ainfo = (com.fr.third.javassist.bytecode.AnnotationsAttribute)
fi.getAttribute(com.fr.third.javassist.bytecode.AnnotationsAttribute.invisibleTag);
com.fr.third.javassist.bytecode.AnnotationsAttribute ainfo2 = (com.fr.third.javassist.bytecode.AnnotationsAttribute)
fi.getAttribute(com.fr.third.javassist.bytecode.AnnotationsAttribute.visibleTag);
return com.fr.third.javassist.CtClassType.getAnnotationType(clz, getDeclaringClass().getClassPool(),
ainfo, ainfo2);
}
/**
* Returns the annotations associated with this field.
*
* @return an array of annotation-type objects.
* @see #getAvailableAnnotations()
* @since 3.1
*/
public Object[] getAnnotations() throws ClassNotFoundException {
return getAnnotations(false);
}
/**
* Returns the annotations associated with this field.
* If any annotations are not on the classpath, they are not included
* in the returned array.
*
* @return an array of annotation-type objects.
* @see #getAnnotations()
* @since 3.3
*/
public Object[] getAvailableAnnotations(){
try {
return getAnnotations(true);
}
catch (ClassNotFoundException e) {
throw new RuntimeException("Unexpected exception", e);
}
}
private Object[] getAnnotations(boolean ignoreNotFound) throws ClassNotFoundException {
com.fr.third.javassist.bytecode.FieldInfo fi = getFieldInfo2();
com.fr.third.javassist.bytecode.AnnotationsAttribute ainfo = (com.fr.third.javassist.bytecode.AnnotationsAttribute)
fi.getAttribute(com.fr.third.javassist.bytecode.AnnotationsAttribute.invisibleTag);
com.fr.third.javassist.bytecode.AnnotationsAttribute ainfo2 = (com.fr.third.javassist.bytecode.AnnotationsAttribute)
fi.getAttribute(com.fr.third.javassist.bytecode.AnnotationsAttribute.visibleTag);
return com.fr.third.javassist.CtClassType.toAnnotationType(ignoreNotFound, getDeclaringClass().getClassPool(),
ainfo, ainfo2);
}
/**
* Returns the character string representing the type of the field.
* The field signature is represented by a character string
* called a field descriptor, which is defined in the JVM specification.
* If two fields have the same type,
* getSignature()
returns the same string.
*
*
Note that the returned string is not the type signature
* contained in the SignatureAttirbute
. It is
* a descriptor.
*
* @see com.fr.third.javassist.bytecode.Descriptor
* @see #getGenericSignature()
*/
public String getSignature() {
return fieldInfo.getDescriptor();
}
/**
* Returns the generic signature of the field.
* It represents a type including type variables.
*
* @see com.fr.third.javassist.bytecode.SignatureAttribute#toFieldSignature(String)
* @since 3.17
*/
public String getGenericSignature() {
com.fr.third.javassist.bytecode.SignatureAttribute sa
= (com.fr.third.javassist.bytecode.SignatureAttribute)fieldInfo.getAttribute(com.fr.third.javassist.bytecode.SignatureAttribute.tag);
return sa == null ? null : sa.getSignature();
}
/**
* Set the generic signature of the field.
* It represents a type including type variables.
* See {@link com.fr.third.javassist.CtClass#setGenericSignature(String)}
* for a code sample.
*
* @param sig a new generic signature.
* @see com.fr.third.javassist.bytecode.SignatureAttribute.ObjectType#encode()
* @since 3.17
*/
public void setGenericSignature(String sig) {
declaringClass.checkModify();
fieldInfo.addAttribute(new com.fr.third.javassist.bytecode.SignatureAttribute(fieldInfo.getConstPool(), sig));
}
/**
* Returns the type of the field.
*/
public com.fr.third.javassist.CtClass getType() throws NotFoundException {
return com.fr.third.javassist.bytecode.Descriptor.toCtClass(fieldInfo.getDescriptor(),
declaringClass.getClassPool());
}
/**
* Sets the type of the field.
*/
public void setType(com.fr.third.javassist.CtClass clazz) {
declaringClass.checkModify();
fieldInfo.setDescriptor(com.fr.third.javassist.bytecode.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 String
type. Otherwise, it returns null
.
* A constant field is static
and final
.
*
* @return a Integer
, Long
, Float
,
* Double
, Boolean
,
* or String
object
* representing the constant value.
* null
if it is not a constant field
* or if the field type is not a primitive type
* or String
.
*/
public Object getConstantValue() {
// When this method is modified,
// see also getConstantFieldValue() in TypeChecker.
int index = fieldInfo.getConstantValue();
if (index == 0)
return null;
com.fr.third.javassist.bytecode.ConstPool cp = fieldInfo.getConstPool();
switch (cp.getTag(index)) {
case com.fr.third.javassist.bytecode.ConstPool.CONST_Long :
return new Long(cp.getLongInfo(index));
case com.fr.third.javassist.bytecode.ConstPool.CONST_Float :
return new Float(cp.getFloatInfo(index));
case com.fr.third.javassist.bytecode.ConstPool.CONST_Double :
return new Double(cp.getDoubleInfo(index));
case com.fr.third.javassist.bytecode.ConstPool.CONST_Integer :
int value = cp.getIntegerInfo(index);
// "Z" means boolean type.
if ("Z".equals(fieldInfo.getDescriptor()))
return new Boolean(value != 0);
else
return new Integer(value);
case com.fr.third.javassist.bytecode.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.
* If that attribute is not found in the class file, this
* method returns null.
*
*
Note that an attribute is a data block specified by * the class file format. * See {@link com.fr.third.javassist.bytecode.AttributeInfo}. * * @param name attribute name */ public byte[] getAttribute(String name) { com.fr.third.javassist.bytecode.AttributeInfo ai = fieldInfo.getAttribute(name); if (ai == null) return null; else return ai.get(); } /** * Adds an attribute. The attribute is saved in the class file. * *
Note that an attribute is a data block specified by
* the class file format.
* See {@link com.fr.third.javassist.bytecode.AttributeInfo}.
*
* @param name attribute name
* @param data attribute value
*/
public void setAttribute(String name, byte[] data) {
declaringClass.checkModify();
fieldInfo.addAttribute(new com.fr.third.javassist.bytecode.AttributeInfo(fieldInfo.getConstPool(),
name, data));
}
// inner classes
/**
* Instances of this class specify how to initialize a field.
* Initializer
is passed to
* CtClass.addField()
with a CtField
.
*
*
This class cannot be instantiated with the new
operator.
* Factory methods such as byParameter()
and
* byNew
* must be used for the instantiation. They create a new instance with
* the given parameters and return it.
*
* @see com.fr.third.javassist.CtClass#addField(CtField,CtField.Initializer)
*/
public static abstract class Initializer {
/**
* Makes an initializer that assigns a constant integer value.
* 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.
*/
public static Initializer constant(long l) {
return new LongInitializer(l);
}
/**
* Makes an initializer that assigns a constant float value.
* The field must be float type.
*/
public static Initializer constant(float l) {
return new FloatInitializer(l);
}
/**
* Makes an initializer that assigns a constant double value.
* The field must be double type.
*/
public static Initializer constant(double d) {
return new DoubleInitializer(d);
}
/**
* Makes an initializer that assigns a constant string value.
* The field must be java.lang.String
type.
*/
public static Initializer constant(String s) {
return new StringInitializer(s);
}
/**
* Makes an initializer using a constructor parameter.
*
*
The initial value is the * N-th parameter given to the constructor of the object including * the field. If the constructor takes less than N parameters, * the field is not initialized. * If the field is static, it is never initialized. * * @param nth the n-th (>= 0) parameter is used as * the initial value. * If nth is 0, then the first parameter is * used. */ public static Initializer byParameter(int nth) { ParamInitializer i = new ParamInitializer(); i.nthParam = nth; return i; } /** * Makes an initializer creating a new object. * *
This initializer creates a new object and uses it as the initial * value of the field. The constructor of the created object receives * the parameter: * *
Object obj
- the object including the field.If the initialized field is static, then the constructor does * not receive any parameters. * * @param objectType the class instantiated for the initial value. */ public static Initializer byNew(com.fr.third.javassist.CtClass objectType) { NewInitializer i = new NewInitializer(); i.objectType = objectType; i.stringParams = null; i.withConstructorParams = false; return i; } /** * Makes an initializer creating a new object. * *
This initializer creates a new object and uses it as the initial * value of the field. The constructor of the created object receives * the parameters: * *
Object obj
- the object including the field.String[] strs
- the character strings specified
* by stringParams
If the initialized field is static, then the constructor
* receives only strs
.
*
* @param objectType the class instantiated for the initial value.
* @param stringParams the array of strings passed to the
* constructor.
*/
public static Initializer byNew(com.fr.third.javassist.CtClass objectType,
String[] stringParams) {
NewInitializer i = new NewInitializer();
i.objectType = objectType;
i.stringParams = stringParams;
i.withConstructorParams = false;
return i;
}
/**
* Makes an initializer creating a new object.
*
*
This initializer creates a new object and uses it as the initial * value of the field. The constructor of the created object receives * the parameters: * *
Object obj
- the object including the field.Object[] args
- the parameters passed to the
* constructor of the object including the
* filed.
* If the initialized field is static, then the constructor does * not receive any parameters. * * @param objectType the class instantiated for the initial value. * * @see CtField.Initializer#byNewArray(com.fr.third.javassist.CtClass,int) * @see CtField.Initializer#byNewArray(com.fr.third.javassist.CtClass,int[]) */ public static Initializer byNewWithParams(com.fr.third.javassist.CtClass objectType) { NewInitializer i = new NewInitializer(); i.objectType = objectType; i.stringParams = null; i.withConstructorParams = true; return i; } /** * Makes an initializer creating a new object. * *
This initializer creates a new object and uses it as the initial * value of the field. The constructor of the created object receives * the parameters: * *
Object obj
- the object including the field.String[] strs
- the character strings specified
* by stringParams
Object[] args
- the parameters passed to the
* constructor of the object including the
* filed.
* If the initialized field is static, then the constructor receives
* only strs
.
*
* @param objectType the class instantiated for the initial value.
* @param stringParams the array of strings passed to the
* constructor.
*/
public static Initializer byNewWithParams(com.fr.third.javassist.CtClass objectType,
String[] stringParams) {
NewInitializer i = new NewInitializer();
i.objectType = objectType;
i.stringParams = stringParams;
i.withConstructorParams = true;
return i;
}
/**
* Makes an initializer calling a static method.
*
*
This initializer calls a static method and uses the returned * value as the initial value of the field. * The called method receives the parameters: * *
Object obj
- the object including the field.If the initialized field is static, then the method does * not receive any parameters. * *
The type of the returned value must be the same as the field * type. * * @param methodClass the class that the static method is * declared in. * @param methodName the name of the satic method. */ public static Initializer byCall(com.fr.third.javassist.CtClass methodClass, String methodName) { MethodInitializer i = new MethodInitializer(); i.objectType = methodClass; i.methodName = methodName; i.stringParams = null; i.withConstructorParams = false; return i; } /** * Makes an initializer calling a static method. * *
This initializer calls a static method and uses the returned * value as the initial value of the field. The called method * receives the parameters: * *
Object obj
- the object including the field.String[] strs
- the character strings specified
* by stringParams
If the initialized field is static, then the method
* receive only strs
.
*
*
The type of the returned value must be the same as the field * type. * * @param methodClass the class that the static method is * declared in. * @param methodName the name of the satic method. * @param stringParams the array of strings passed to the * static method. */ public static Initializer byCall(com.fr.third.javassist.CtClass methodClass, String methodName, String[] stringParams) { MethodInitializer i = new MethodInitializer(); i.objectType = methodClass; i.methodName = methodName; i.stringParams = stringParams; i.withConstructorParams = false; return i; } /** * Makes an initializer calling a static method. * *
This initializer calls a static method and uses the returned * value as the initial value of the field. The called method * receives the parameters: * *
Object obj
- the object including the field.Object[] args
- the parameters passed to the
* constructor of the object including the
* filed.
* If the initialized field is static, then the method does * not receive any parameters. * *
The type of the returned value must be the same as the field * type. * * @param methodClass the class that the static method is * declared in. * @param methodName the name of the satic method. */ public static Initializer byCallWithParams(com.fr.third.javassist.CtClass methodClass, String methodName) { MethodInitializer i = new MethodInitializer(); i.objectType = methodClass; i.methodName = methodName; i.stringParams = null; i.withConstructorParams = true; return i; } /** * Makes an initializer calling a static method. * *
This initializer calls a static method and uses the returned * value as the initial value of the field. The called method * receives the parameters: * *
Object obj
- the object including the field.String[] strs
- the character strings specified
* by stringParams
Object[] args
- the parameters passed to the
* constructor of the object including the
* filed.
* If the initialized field is static, then the method
* receive only strs
.
*
*
The type of the returned value must be the same as the field
* type.
*
* @param methodClass the class that the static method is
* declared in.
* @param methodName the name of the satic method.
* @param stringParams the array of strings passed to the
* static method.
*/
public static Initializer byCallWithParams(com.fr.third.javassist.CtClass methodClass,
String methodName, String[] stringParams) {
MethodInitializer i = new MethodInitializer();
i.objectType = methodClass;
i.methodName = methodName;
i.stringParams = stringParams;
i.withConstructorParams = true;
return i;
}
/**
* Makes an initializer creating a new array.
*
* @param type the type of the array.
* @param size the size of the array.
* @throws NotFoundException if the type of the array components
* is not found.
*/
public static Initializer byNewArray(com.fr.third.javassist.CtClass type, int size)
throws NotFoundException
{
return new ArrayInitializer(type.getComponentType(), size);
}
/**
* Makes an initializer creating a new multi-dimensional array.
*
* @param type the type of the array.
* @param sizes an int
array of the size in every
* dimension.
* The first element is the size in the first
* dimension. The second is in the second, etc.
*/
public static Initializer byNewArray(com.fr.third.javassist.CtClass type, int[] sizes) {
return new MultiArrayInitializer(type, sizes);
}
/**
* Makes an initializer.
*
* @param source initializer expression.
*/
public static Initializer byExpr(String source) {
return new CodeInitializer(source);
}
static Initializer byExpr(ASTree source) {
return new PtreeInitializer(source);
}
// Check whether this initializer is valid for the field type.
// If it is invaild, this method throws an exception.
void check(String desc) throws com.fr.third.javassist.CannotCompileException {}
// produce codes for initialization
abstract int compile(com.fr.third.javassist.CtClass type, String name, com.fr.third.javassist.bytecode.Bytecode code,
com.fr.third.javassist.CtClass[] parameters, Javac drv)
throws com.fr.third.javassist.CannotCompileException;
// produce codes for initialization
abstract int compileIfStatic(com.fr.third.javassist.CtClass type, String name,
com.fr.third.javassist.bytecode.Bytecode code, Javac drv) throws com.fr.third.javassist.CannotCompileException;
// returns the index of CONSTANT_Integer_info etc
// if the value is constant. Otherwise, 0.
int getConstantValue(com.fr.third.javassist.bytecode.ConstPool cp, com.fr.third.javassist.CtClass type) { return 0; }
}
static abstract class CodeInitializer0 extends Initializer {
abstract void compileExpr(Javac drv) throws CompileError;
int compile(com.fr.third.javassist.CtClass type, String name, com.fr.third.javassist.bytecode.Bytecode code,
com.fr.third.javassist.CtClass[] parameters, Javac drv)
throws com.fr.third.javassist.CannotCompileException
{
try {
code.addAload(0);
compileExpr(drv);
code.addPutfield(com.fr.third.javassist.bytecode.Bytecode.THIS, name, com.fr.third.javassist.bytecode.Descriptor.of(type));
return code.getMaxStack();
}
catch (CompileError e) {
throw new com.fr.third.javassist.CannotCompileException(e);
}
}
int compileIfStatic(com.fr.third.javassist.CtClass type, String name, com.fr.third.javassist.bytecode.Bytecode code,
Javac drv) throws com.fr.third.javassist.CannotCompileException
{
try {
compileExpr(drv);
code.addPutstatic(com.fr.third.javassist.bytecode.Bytecode.THIS, name, com.fr.third.javassist.bytecode.Descriptor.of(type));
return code.getMaxStack();
}
catch (CompileError e) {
throw new com.fr.third.javassist.CannotCompileException(e);
}
}
int getConstantValue2(com.fr.third.javassist.bytecode.ConstPool cp, com.fr.third.javassist.CtClass type, ASTree tree) {
if (type.isPrimitive()) {
if (tree instanceof IntConst) {
long value = ((IntConst)tree).get();
if (type == com.fr.third.javassist.CtClass.doubleType)
return cp.addDoubleInfo((double)value);
else if (type == com.fr.third.javassist.CtClass.floatType)
return cp.addFloatInfo((float)value);
else if (type == com.fr.third.javassist.CtClass.longType)
return cp.addLongInfo(value);
else if (type != com.fr.third.javassist.CtClass.voidType)
return cp.addIntegerInfo((int)value);
}
else if (tree instanceof DoubleConst) {
double value = ((DoubleConst)tree).get();
if (type == com.fr.third.javassist.CtClass.floatType)
return cp.addFloatInfo((float)value);
else if (type == com.fr.third.javassist.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 {
private String expression;
CodeInitializer(String expr) { expression = expr; }
void compileExpr(Javac drv) throws CompileError {
drv.compileExpr(expression);
}
int getConstantValue(com.fr.third.javassist.bytecode.ConstPool cp, com.fr.third.javassist.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 {
private ASTree expression;
PtreeInitializer(ASTree expr) { expression = expr; }
void compileExpr(Javac drv) throws CompileError {
drv.compileExpr(expression);
}
int getConstantValue(com.fr.third.javassist.bytecode.ConstPool cp, com.fr.third.javassist.CtClass type) {
return getConstantValue2(cp, type, expression);
}
}
/**
* A field initialized with a parameter passed to the constructor
* of the class containing that field.
*/
static class ParamInitializer extends Initializer {
int nthParam;
ParamInitializer() {}
int compile(com.fr.third.javassist.CtClass type, String name, com.fr.third.javassist.bytecode.Bytecode code,
com.fr.third.javassist.CtClass[] parameters, Javac drv)
throws com.fr.third.javassist.CannotCompileException
{
if (parameters != null && nthParam < parameters.length) {
code.addAload(0);
int nth = nthParamToLocal(nthParam, parameters, false);
int s = code.addLoad(nth, type) + 1;
code.addPutfield(com.fr.third.javassist.bytecode.Bytecode.THIS, name, com.fr.third.javassist.bytecode.Descriptor.of(type));
return s; // stack size
}
else
return 0; // do not initialize
}
/**
* Computes the index of the local variable that the n-th parameter
* is assigned to.
*
* @param nth n-th parameter
* @param params list of parameter types
* @param isStatic true if the method is static.
*/
static int nthParamToLocal(int nth, com.fr.third.javassist.CtClass[] params,
boolean isStatic) {
com.fr.third.javassist.CtClass longType = com.fr.third.javassist.CtClass.longType;
com.fr.third.javassist.CtClass doubleType = com.fr.third.javassist.CtClass.doubleType;
int k;
if (isStatic)
k = 0;
else
k = 1; // 0 is THIS.
for (int i = 0; i < nth; ++i) {
com.fr.third.javassist.CtClass type = params[i];
if (type == longType || type == doubleType)
k += 2;
else
++k;
}
return k;
}
int compileIfStatic(com.fr.third.javassist.CtClass type, String name, com.fr.third.javassist.bytecode.Bytecode code,
Javac drv) throws com.fr.third.javassist.CannotCompileException
{
return 0;
}
}
/**
* A field initialized with an object created by the new operator.
*/
static class NewInitializer extends Initializer {
com.fr.third.javassist.CtClass objectType;
String[] stringParams;
boolean withConstructorParams;
NewInitializer() {}
/**
* Produces codes in which a new object is created and assigned to
* the field as the initial value.
*/
int compile(com.fr.third.javassist.CtClass type, String name, com.fr.third.javassist.bytecode.Bytecode code,
com.fr.third.javassist.CtClass[] parameters, Javac drv)
throws com.fr.third.javassist.CannotCompileException
{
int stacksize;
code.addAload(0);
code.addNew(objectType);
code.add(com.fr.third.javassist.bytecode.Bytecode.DUP);
code.addAload(0);
if (stringParams == null)
stacksize = 4;
else
stacksize = compileStringParameter(code) + 4;
if (withConstructorParams)
stacksize += CtNewWrappedMethod.compileParameterList(code,
parameters, 1);
code.addInvokespecial(objectType, "