Class CtField
- java.lang.Object
-
- javassist.CtMember
-
- javassist.CtField
-
- Direct Known Subclasses:
Javac.CtFieldWithInit
public class CtField extends CtMember
An instance of CtField represents a field.- See Also:
CtClass.getDeclaredFields()
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static classCtField.ArrayInitializer(package private) static classCtField.CodeInitializer(package private) static classCtField.CodeInitializer0(package private) static classCtField.DoubleInitializer(package private) static classCtField.FloatInitializerstatic classCtField.InitializerInstances of this class specify how to initialize a field.(package private) static classCtField.IntInitializer(package private) static classCtField.LongInitializer(package private) static classCtField.MethodInitializerA field initialized with the result of a static method call.(package private) static classCtField.MultiArrayInitializer(package private) static classCtField.NewInitializerA field initialized with an object created by the new operator.(package private) static classCtField.ParamInitializerA field initialized with a parameter passed to the constructor of the class containing that field.(package private) static classCtField.PtreeInitializer(package private) static classCtField.StringInitializer-
Nested classes/interfaces inherited from class javassist.CtMember
CtMember.Cache
-
-
Field Summary
Fields Modifier and Type Field Description protected FieldInfofieldInfo(package private) static java.lang.StringjavaLangString-
Fields inherited from class javassist.CtMember
declaringClass, next
-
-
Constructor Summary
Constructors Modifier Constructor Description privateCtField(java.lang.String typeDesc, java.lang.String name, CtClass clazz)(package private)CtField(FieldInfo fi, CtClass clazz)CtField(CtClass type, java.lang.String name, CtClass declaring)Creates aCtFieldobject.CtField(CtField src, CtClass declaring)Creates a copy of the given field.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidextendToString(java.lang.StringBuffer buffer)Invoked byCtMember.toString()to add to the buffer and provide the complete value.java.lang.ObjectgetAnnotation(java.lang.Class clz)Returns the annotation if the class has the specified annotation class.java.lang.Object[]getAnnotations()Returns the annotations associated with this field.private java.lang.Object[]getAnnotations(boolean ignoreNotFound)byte[]getAttribute(java.lang.String name)Obtains an attribute with the given name.java.lang.Object[]getAvailableAnnotations()Returns the annotations associated with this field.java.lang.ObjectgetConstantValue()Returns the value of this field if it is a constant field.CtClassgetDeclaringClass()Returns the class declaring the field.FieldInfogetFieldInfo()Returns the FieldInfo representing the field in the class file.FieldInfogetFieldInfo2()Returns the FieldInfo representing the field in the class file (read only).java.lang.StringgetGenericSignature()Returns the generic signature of the field.(package private) CtField.InitializergetInit()protected ASTreegetInitAST()intgetModifiers()Returns the encoded modifiers of the field.java.lang.StringgetName()Returns the name of the field.java.lang.StringgetSignature()Returns the character string representing the type of the field.CtClassgetType()Returns the type of the field.booleanhasAnnotation(java.lang.String typeName)Returns true if the class has the specified annotation type.static CtFieldmake(java.lang.String src, CtClass declaring)Compiles the given source code and creates a field.voidsetAttribute(java.lang.String name, byte[] data)Adds an attribute.voidsetGenericSignature(java.lang.String sig)Set the generic signature of the field.voidsetModifiers(int mod)Sets the encoded modifiers of the field.voidsetName(java.lang.String newName)Changes the name of the field.voidsetType(CtClass clazz)Sets the type of the field.java.lang.StringtoString()Returns a String representation of the object.-
Methods inherited from class javassist.CtMember
hasAnnotation, nameReplaced, next, visibleFrom
-
-
-
-
Field Detail
-
javaLangString
static final java.lang.String javaLangString
- See Also:
- Constant Field Values
-
fieldInfo
protected FieldInfo fieldInfo
-
-
Constructor Detail
-
CtField
public CtField(CtClass type, java.lang.String name, CtClass declaring) throws CannotCompileException
Creates aCtFieldobject. The created field must be added to a class withCtClass.addField(). An initial value of the field is specified by aCtField.Initializerobject.If getter and setter methods are needed, call
CtNewMethod.getter()andCtNewMethod.setter().- Parameters:
type- field typename- field namedeclaring- the class to which the field will be added.- Throws:
CannotCompileException- See Also:
CtClass.addField(CtField),CtNewMethod.getter(String,CtField),CtNewMethod.setter(String,CtField),CtField.Initializer
-
CtField
public CtField(CtField src, CtClass declaring) throws CannotCompileException
Creates a copy of the given field. The created field must be added to a class withCtClass.addField(). An initial value of the field is specified by aCtField.Initializerobject.If getter and setter methods are needed, call
CtNewMethod.getter()andCtNewMethod.setter().- Parameters:
src- the original fielddeclaring- the class to which the field will be added.- Throws:
CannotCompileException- See Also:
CtNewMethod.getter(String,CtField),CtNewMethod.setter(String,CtField),CtField.Initializer
-
CtField
private CtField(java.lang.String typeDesc, java.lang.String name, CtClass clazz) throws CannotCompileException- Throws:
CannotCompileException
-
-
Method Detail
-
toString
public java.lang.String toString()
Returns a String representation of the object.
-
extendToString
protected void extendToString(java.lang.StringBuffer buffer)
Description copied from class:CtMemberInvoked byCtMember.toString()to add to the buffer and provide the complete value. Subclasses should invoke this method, adding a space before each token. The modifiers for the member are provided first; subclasses should provide additional data such as return type, field or method name, etc.- Specified by:
extendToStringin classCtMember
-
getInitAST
protected ASTree getInitAST()
-
getInit
CtField.Initializer getInit()
-
make
public static CtField make(java.lang.String src, CtClass declaring) throws CannotCompileException
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).- Parameters:
src- the source text.declaring- the class to which the created field is added.- Throws:
CannotCompileException
-
getFieldInfo
public FieldInfo getFieldInfo()
Returns the FieldInfo representing the field in the class file.
-
getFieldInfo2
public FieldInfo getFieldInfo2()
Returns the FieldInfo representing the field in the class file (read only). Normal applications do not need calling this method. UsegetFieldInfo().The
FieldInfoobject obtained by this method is read only. Changes to this object might not be reflected on a class file generated bytoBytecode(),toClass(), etc inCtClass.This method is available even if the
CtClasscontaining this field is frozen. However, if the class is frozen, theFieldInfomight be also pruned.- See Also:
getFieldInfo(),CtClass.isFrozen(),CtClass.prune()
-
getDeclaringClass
public CtClass getDeclaringClass()
Returns the class declaring the field.- Overrides:
getDeclaringClassin classCtMember
-
getName
public java.lang.String getName()
Returns the name of the field.- Specified by:
getNamein classCtMember- See Also:
CtConstructor.getName()
-
setName
public void setName(java.lang.String newName)
Changes the name of the field.
-
getModifiers
public int getModifiers()
Returns the encoded modifiers of the field.- Specified by:
getModifiersin classCtMember- Returns:
- modifiers encoded with
javassist.Modifier. - See Also:
Modifier
-
setModifiers
public void setModifiers(int mod)
Sets the encoded modifiers of the field.- Specified by:
setModifiersin classCtMember- See Also:
Modifier
-
hasAnnotation
public boolean hasAnnotation(java.lang.String typeName)
Returns true if the class has the specified annotation type.- Specified by:
hasAnnotationin classCtMember- Parameters:
typeName- the name of annotation type.- Returns:
trueif the annotation is found, otherwisefalse.- Since:
- 3.21
-
getAnnotation
public java.lang.Object getAnnotation(java.lang.Class clz) throws java.lang.ClassNotFoundExceptionReturns the annotation if the class has the specified annotation class. For example, if an annotation@Authoris associated with this field, anAuthorobject is returned. The member values can be obtained by calling methods on theAuthorobject.- Specified by:
getAnnotationin classCtMember- Parameters:
clz- the annotation class.- Returns:
- the annotation if found, otherwise
null. - Throws:
java.lang.ClassNotFoundException- Since:
- 3.11
-
getAnnotations
public java.lang.Object[] getAnnotations() throws java.lang.ClassNotFoundExceptionReturns the annotations associated with this field.- Specified by:
getAnnotationsin classCtMember- Returns:
- an array of annotation-type objects.
- Throws:
java.lang.ClassNotFoundException- Since:
- 3.1
- See Also:
getAvailableAnnotations()
-
getAvailableAnnotations
public java.lang.Object[] getAvailableAnnotations()
Returns the annotations associated with this field. If any annotations are not on the classpath, they are not included in the returned array.- Specified by:
getAvailableAnnotationsin classCtMember- Returns:
- an array of annotation-type objects.
- Since:
- 3.3
- See Also:
getAnnotations()
-
getAnnotations
private java.lang.Object[] getAnnotations(boolean ignoreNotFound) throws java.lang.ClassNotFoundException- Throws:
java.lang.ClassNotFoundException
-
getSignature
public java.lang.String getSignature()
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.- Specified by:
getSignaturein classCtMember- See Also:
Descriptor,getGenericSignature()
-
getGenericSignature
public java.lang.String getGenericSignature()
Returns the generic signature of the field. It represents a type including type variables.- Specified by:
getGenericSignaturein classCtMember- Since:
- 3.17
- See Also:
SignatureAttribute.toFieldSignature(String)
-
setGenericSignature
public void setGenericSignature(java.lang.String sig)
Set the generic signature of the field. It represents a type including type variables. SeeCtClass.setGenericSignature(String)for a code sample.- Specified by:
setGenericSignaturein classCtMember- Parameters:
sig- a new generic signature.- Since:
- 3.17
- See Also:
SignatureAttribute.ObjectType.encode()
-
getType
public CtClass getType() throws NotFoundException
Returns the type of the field.- Throws:
NotFoundException
-
setType
public void setType(CtClass clazz)
Sets the type of the field.This method does not automatically update method bodies that access this field. They have to be explicitly updated. For example, if some method contains an expression
t.valueand the type of the variabletis changed bysetType(CtClass)frominttodouble, thent.valuehas to be modified as well since the bytecode oft.valuecontains the type information.- See Also:
CodeConverter,ExprEditor
-
getConstantValue
public java.lang.Object getConstantValue()
Returns the value of this field if it is a constant field. This method works only if the field type is a primitive type orStringtype. Otherwise, it returnsnull. A constant field isstaticandfinal.- Returns:
- a
Integer,Long,Float,Double,Boolean, orStringobject representing the constant value.nullif it is not a constant field or if the field type is not a primitive type orString.
-
getAttribute
public byte[] getAttribute(java.lang.String name)
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
AttributeInfo.- Specified by:
getAttributein classCtMember- Parameters:
name- attribute name
-
setAttribute
public void setAttribute(java.lang.String name, byte[] data)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
AttributeInfo.- Specified by:
setAttributein classCtMember- Parameters:
name- attribute namedata- attribute value
-
-