Package javassist

Class CtField

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  CtField.Initializer
      Instances of this class specify how to initialize a field.
    • Constructor Summary

      Constructors 
      Constructor Description
      CtField​(CtClass type, java.lang.String name, CtClass declaring)
      Creates a CtField object.
      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
      java.lang.Object getAnnotation​(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.
      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.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).
      java.lang.String getGenericSignature()
      Returns the generic signature of the field.
      int getModifiers()
      Returns the encoded modifiers of the field.
      java.lang.String getName()
      Returns the name of the field.
      java.lang.String getSignature()
      Returns the character string representing the type of the field.
      CtClass getType()
      Returns the type of the field.
      boolean hasAnnotation​(java.lang.String typeName)
      Returns true if the class has the specified annotation type.
      static CtField make​(java.lang.String src, CtClass declaring)
      Compiles the given source code and creates a field.
      void setAttribute​(java.lang.String name, byte[] data)
      Adds an attribute.
      void setGenericSignature​(java.lang.String sig)
      Set the generic signature of the field.
      void setModifiers​(int mod)
      Sets the encoded modifiers of the field.
      void setName​(java.lang.String newName)
      Changes the name of the field.
      void setType​(CtClass clazz)
      Sets the type of the field.
      java.lang.String toString()
      Returns a String representation of the object.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Method Detail

      • toString

        public java.lang.String toString()
        Returns a String representation of the object.
        Overrides:
        toString in class CtMember
      • 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. 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 Also:
        getFieldInfo(), CtClass.isFrozen(), CtClass.prune()
      • 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:
        getModifiers in class CtMember
        Returns:
        modifiers encoded with javassist.Modifier.
        See Also:
        Modifier
      • setModifiers

        public void setModifiers​(int mod)
        Sets the encoded modifiers of the field.
        Specified by:
        setModifiers in class CtMember
        See Also:
        Modifier
      • hasAnnotation

        public boolean hasAnnotation​(java.lang.String typeName)
        Returns true if the class has the specified annotation type.
        Specified by:
        hasAnnotation in class CtMember
        Parameters:
        typeName - the name of annotation type.
        Returns:
        true if the annotation is found, otherwise false.
        Since:
        3.21
      • getAnnotation

        public java.lang.Object getAnnotation​(java.lang.Class<?> clz)
                                       throws java.lang.ClassNotFoundException
        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.
        Specified by:
        getAnnotation in class CtMember
        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.ClassNotFoundException
        Returns the annotations associated with this field.
        Specified by:
        getAnnotations in class CtMember
        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:
        getAvailableAnnotations in class CtMember
        Returns:
        an array of annotation-type objects.
        Since:
        3.3
        See Also:
        getAnnotations()
      • 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:
        getSignature in class CtMember
        See Also:
        Descriptor, getGenericSignature()
      • 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.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.

        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 or String type. Otherwise, it returns null. A constant field is static and final.
        Returns:
        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.
      • 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:
        getAttribute in class CtMember
        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:
        setAttribute in class CtMember
        Parameters:
        name - attribute name
        data - attribute value