public class CtField extends CtMember
CtClass.getDeclaredFields()| Modifier and Type | Class and Description | 
|---|---|
| static class  | CtField.InitializerInstances of this class specify how to initialize a field. | 
| Modifier and Type | Field and Description | 
|---|---|
| protected FieldInfo | fieldInfo | 
declaringClass| Constructor and Description | 
|---|
| CtField(CtClass type,
       String name,
       CtClass declaring)Creates a  CtFieldobject. | 
| CtField(CtField src,
       CtClass declaring)Creates a copy of the given field. | 
| Modifier and Type | Method and Description | 
|---|---|
| protected void | extendToString(StringBuffer buffer)Invoked by  CtMember.toString()to add to the buffer and provide the
 complete value. | 
| Object | getAnnotation(Class clz)Returns the annotation if the class has the specified annotation class. | 
| Object[] | getAnnotations()Returns the annotations associated with this field. | 
| byte[] | getAttribute(String name)Obtains an attribute with the given name. | 
| Object[] | getAvailableAnnotations()Returns the annotations associated with this field. | 
| Object | getConstantValue()Returns the value of this field if it is a constant field. | 
| CtClass | getDeclaringClass()Returns the class declaring the field. | 
| FieldInfo | getFieldInfo()Returns the FieldInfo representing the field in the class file. | 
| FieldInfo | getFieldInfo2()Returns the FieldInfo representing the field in the class
 file (read only). | 
| String | getGenericSignature()Returns the generic signature of the field. | 
| protected ASTree | getInitAST() | 
| int | getModifiers()Returns the encoded modifiers of the field. | 
| String | getName()Returns the name of the field. | 
| String | getSignature()Returns the character string representing the type of the field. | 
| CtClass | getType()Returns the type of the field. | 
| boolean | hasAnnotation(String typeName)Returns true if the class has the specified annotation type. | 
| static CtField | make(String src,
    CtClass declaring)Compiles the given source code and creates a field. | 
| void | setAttribute(String name,
            byte[] data)Adds an attribute. | 
| void | setGenericSignature(String sig)Set the generic signature of the field. | 
| void | setModifiers(int mod)Sets the encoded modifiers of the field. | 
| void | setName(String newName)Changes the name of the field. | 
| void | setType(CtClass clazz)Sets the type of the field. | 
| String | toString()Returns a String representation of the object. | 
hasAnnotation, visibleFromprotected FieldInfo fieldInfo
public CtField(CtClass type, String name, CtClass declaring) throws CannotCompileException
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().
type - field typename - field namedeclaring - the class to which the field will be added.CannotCompileExceptionCtClass.addField(CtField), 
CtNewMethod.getter(String,CtField), 
CtNewMethod.setter(String,CtField), 
CtField.Initializerpublic CtField(CtField src, CtClass declaring) throws CannotCompileException
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().
src - the original fielddeclaring - the class to which the field will be added.CannotCompileExceptionCtNewMethod.getter(String,CtField), 
CtNewMethod.setter(String,CtField), 
CtField.Initializerpublic String toString()
protected void extendToString(StringBuffer buffer)
CtMemberCtMember.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.extendToString in class CtMemberprotected ASTree getInitAST()
public static CtField make(String src, CtClass declaring) throws CannotCompileException
"public String name;" "public int k = 3;"
Note that the source code ends with ';'
 (semicolon).
src - the source text.declaring - the class to which the created field is added.CannotCompileExceptionpublic FieldInfo getFieldInfo()
public FieldInfo getFieldInfo2()
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.
getFieldInfo(), 
CtClass.isFrozen(), 
CtClass.prune()public CtClass getDeclaringClass()
getDeclaringClass in class CtMemberpublic String getName()
getName in class CtMemberCtConstructor.getName()public void setName(String newName)
public int getModifiers()
getModifiers in class CtMemberjavassist.Modifier.Modifierpublic void setModifiers(int mod)
setModifiers in class CtMemberModifierpublic boolean hasAnnotation(String typeName)
hasAnnotation in class CtMembertypeName - the name of annotation type.true if the annotation is found, otherwise false.public Object getAnnotation(Class clz) throws ClassNotFoundException
@Author is associated
 with this field, an Author object is returned.
 The member values can be obtained by calling methods on
 the Author object.getAnnotation in class CtMemberclz - the annotation class.null.ClassNotFoundExceptionpublic Object[] getAnnotations() throws ClassNotFoundException
getAnnotations in class CtMemberClassNotFoundExceptiongetAvailableAnnotations()public Object[] getAvailableAnnotations()
getAvailableAnnotations in class CtMembergetAnnotations()public String getSignature()
getSignature() returns the same string.
 Note that the returned string is not the type signature
 contained in the SignatureAttirbute.  It is
 a descriptor.
getSignature in class CtMemberDescriptor, 
getGenericSignature()public String getGenericSignature()
getGenericSignature in class CtMemberSignatureAttribute.toFieldSignature(String)public void setGenericSignature(String sig)
CtClass.setGenericSignature(String)
 for a code sample.setGenericSignature in class CtMembersig - a new generic signature.SignatureAttribute.ObjectType.encode()public CtClass getType() throws NotFoundException
NotFoundExceptionpublic void setType(CtClass clazz)
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 variable t is changed by setType(CtClass)
 from int to double, then t.value has to be modified
 as well since the bytecode of t.value contains the type information.
 
CodeConverter, 
ExprEditorpublic Object getConstantValue()
String type.  Otherwise, it returns null.
 A constant field is static and final.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 byte[] getAttribute(String name)
Note that an attribute is a data block specified by
 the class file format.
 See AttributeInfo.
getAttribute in class CtMembername - attribute namepublic void setAttribute(String name, byte[] data)
Note that an attribute is a data block specified by
 the class file format.
 See AttributeInfo.
setAttribute in class CtMembername - attribute namedata - attribute valueCopyright © 2016 Shigeru Chiba, www.javassist.org. All Rights Reserved.