Class CtField
- See Also:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
Instances of this class specify how to initialize a field. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptiongetAnnotation
(Class<?> clz) Returns the annotation if the class has the specified annotation class.Object[]
Returns the annotations associated with this field.byte[]
getAttribute
(String name) Obtains an attribute with the given name.Object[]
Returns the annotations associated with this field.Returns the value of this field if it is a constant field.Returns the class declaring the field.Returns the FieldInfo representing the field in the class file.Returns the FieldInfo representing the field in the class file (read only).Returns the generic signature of the field.int
Returns the encoded modifiers of the field.getName()
Returns the name of the field.Returns the character string representing the type of the field.getType()
Returns the type of the field.boolean
hasAnnotation
(String typeName) Returns true if the class has the specified annotation type.static CtField
Compiles the given source code and creates a field.void
setAttribute
(String name, byte[] data) Adds an attribute.void
Set the generic signature of the field.void
setModifiers
(int mod) Sets the encoded modifiers of the field.void
Changes the name of the field.void
Sets the type of the field.toString()
Returns a String representation of the object.Methods inherited from class javassist.CtMember
hasAnnotation, visibleFrom
-
Constructor Details
-
CtField
Creates aCtField
object. The created field must be added to a class withCtClass.addField()
. An initial value of the field is specified by aCtField.Initializer
object.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:
-
CtField
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.Initializer
object.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:
-
-
Method Details
-
toString
Returns a String representation of the object. -
make
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
Returns the FieldInfo representing the field in the class file. -
getFieldInfo2
Returns the FieldInfo representing the field in the class file (read only). Normal applications do not need calling this method. UsegetFieldInfo()
.The
FieldInfo
object 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
CtClass
containing this field is frozen. However, if the class is frozen, theFieldInfo
might be also pruned.- See Also:
-
getDeclaringClass
Returns the class declaring the field.- Overrides:
getDeclaringClass
in classCtMember
-
getName
Returns the name of the field. -
setName
Changes the name of the field. -
getModifiers
public int getModifiers()Returns the encoded modifiers of the field.- Specified by:
getModifiers
in classCtMember
- Returns:
- modifiers encoded with
javassist.Modifier
. - See Also:
-
setModifiers
public void setModifiers(int mod) Sets the encoded modifiers of the field.- Specified by:
setModifiers
in classCtMember
- See Also:
-
hasAnnotation
Returns true if the class has the specified annotation type.- Specified by:
hasAnnotation
in classCtMember
- Parameters:
typeName
- the name of annotation type.- Returns:
true
if the annotation is found, otherwisefalse
.- Since:
- 3.21
-
getAnnotation
Returns the annotation if the class has the specified annotation class. For example, if an annotation@Author
is associated with this field, anAuthor
object is returned. The member values can be obtained by calling methods on theAuthor
object.- Specified by:
getAnnotation
in classCtMember
- Parameters:
clz
- the annotation class.- Returns:
- the annotation if found, otherwise
null
. - Throws:
ClassNotFoundException
- Since:
- 3.11
-
getAnnotations
Returns the annotations associated with this field.- Specified by:
getAnnotations
in classCtMember
- Returns:
- an array of annotation-type objects.
- Throws:
ClassNotFoundException
- Since:
- 3.1
- See Also:
-
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:
getAvailableAnnotations
in classCtMember
- Returns:
- an array of annotation-type objects.
- Since:
- 3.3
- See Also:
-
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:
getSignature
in classCtMember
- See Also:
-
getGenericSignature
Returns the generic signature of the field. It represents a type including type variables.- Specified by:
getGenericSignature
in classCtMember
- Since:
- 3.17
- See Also:
-
setGenericSignature
Set the generic signature of the field. It represents a type including type variables. SeeCtClass.setGenericSignature(String)
for a code sample.- Specified by:
setGenericSignature
in classCtMember
- Parameters:
sig
- a new generic signature.- Since:
- 3.17
- See Also:
-
getType
Returns the type of the field.- Throws:
NotFoundException
-
setType
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.value
and the type of the variablet
is changed bysetType(CtClass)
fromint
todouble
, thent.value
has to be modified as well since the bytecode oft.value
contains the type information.- See Also:
-
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 orString
type. Otherwise, it returnsnull
. A constant field isstatic
andfinal
.- Returns:
- a
Integer
,Long
,Float
,Double
,Boolean
, orString
object representing the constant value.null
if it is not a constant field or if the field type is not a primitive type orString
.
-
getAttribute
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:
getAttribute
in classCtMember
- Parameters:
name
- attribute name
-
setAttribute
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:
setAttribute
in classCtMember
- Parameters:
name
- attribute namedata
- attribute value
-