Class ClassFile


  • public final class ClassFile
    extends Object
    ClassFile represents a Java .class file, which consists of a constant pool, methods, fields, and attributes.

    For example,

     ClassFile cf = new ClassFile(false, "test.Foo", null);
     cf.setInterfaces(new String[] { "java.lang.Cloneable" });
    
     FieldInfo f = new FieldInfo(cf.getConstPool(), "width", "I");
     f.setAccessFlags(AccessFlag.PUBLIC);
     cf.addField(f);
    
     cf.write(new DataOutputStream(new FileOutputStream("Foo.class")));
     

    This code generates a class file Foo.class for the following class:

     package test;
     class Foo implements Cloneable {
         public int width;
     }
     
    See Also:
    FieldInfo, MethodInfo, ClassFileWriter, CtClass.getClassFile(), ClassPool.makeClass(ClassFile)
    • Field Detail

      • JAVA_1

        public static final int JAVA_1
        The major version number of class files for JDK 1.1.
        See Also:
        Constant Field Values
      • JAVA_2

        public static final int JAVA_2
        The major version number of class files for JDK 1.2.
        See Also:
        Constant Field Values
      • JAVA_3

        public static final int JAVA_3
        The major version number of class files for JDK 1.3.
        See Also:
        Constant Field Values
      • JAVA_4

        public static final int JAVA_4
        The major version number of class files for JDK 1.4.
        See Also:
        Constant Field Values
      • JAVA_5

        public static final int JAVA_5
        The major version number of class files for JDK 1.5.
        See Also:
        Constant Field Values
      • JAVA_6

        public static final int JAVA_6
        The major version number of class files for JDK 1.6.
        See Also:
        Constant Field Values
      • JAVA_7

        public static final int JAVA_7
        The major version number of class files for JDK 1.7.
        See Also:
        Constant Field Values
      • JAVA_8

        public static final int JAVA_8
        The major version number of class files for JDK 1.8.
        See Also:
        Constant Field Values
      • JAVA_9

        public static final int JAVA_9
        The major version number of class files for JDK 1.9.
        See Also:
        Constant Field Values
      • JAVA_10

        public static final int JAVA_10
        The major version number of class files for JDK 10.
        See Also:
        Constant Field Values
      • JAVA_11

        public static final int JAVA_11
        The major version number of class files for JDK 11.
        See Also:
        Constant Field Values
      • MAJOR_VERSION

        public static final int MAJOR_VERSION
        The major version number of class files created from scratch. The default value is 47 (JDK 1.3). It is 49 (JDK 1.5) if the JVM supports java.lang.StringBuilder. It is 50 (JDK 1.6) if the JVM supports java.util.zip.DeflaterInputStream. It is 51 (JDK 1.7) if the JVM supports java.lang.invoke.CallSite. It is 52 (JDK 1.8) if the JVM supports java.util.function.Function. It is 53 (JDK 1.9) if the JVM supports java.lang.reflect.Module. It is 54 (JDK 10) if the JVM supports java.util.List.copyOf(Collection). It is 55 (JDK 11) if the JVM supports java.util.Optional.isEmpty().
    • Constructor Detail

      • ClassFile

        public ClassFile​(boolean isInterface,
                         String classname,
                         String superclass)
        Constructs a class file including no members.
        Parameters:
        isInterface - true if this is an interface. false if this is a class.
        classname - a fully-qualified class name
        superclass - a fully-qualified super class name or null.
    • Method Detail

      • compact

        public void compact()
        Eliminates dead constant pool items. If a method or a field is removed, the constant pool items used by that method/field become dead items. This method recreates a constant pool.
      • prune

        public void prune()
        Discards all attributes, associated with both the class file and the members such as a code attribute and exceptions attribute. The unused constant pool entries are also discarded (a new packed constant pool is constructed).
      • getConstPool

        public ConstPool getConstPool()
        Returns a constant pool table.
      • isInterface

        public boolean isInterface()
        Returns true if this is an interface.
      • isFinal

        public boolean isFinal()
        Returns true if this is a final class or interface.
      • isAbstract

        public boolean isAbstract()
        Returns true if this is an abstract class or an interface.
      • getAccessFlags

        public int getAccessFlags()
        Returns access flags.
        See Also:
        AccessFlag
      • setAccessFlags

        public void setAccessFlags​(int acc)
        Changes access flags.
        See Also:
        AccessFlag
      • getInnerAccessFlags

        public int getInnerAccessFlags()
        Returns access and property flags of this nested class. This method returns -1 if the class is not a nested class.

        The returned value is obtained from inner_class_access_flags of the entry representing this nested class itself in InnerClasses_attribute.

      • getName

        public String getName()
        Returns the class name.
      • setName

        public void setName​(String name)
        Sets the class name. This method substitutes the new name for all occurrences of the old class name in the class file.
      • getSuperclass

        public String getSuperclass()
        Returns the super class name.
      • getSuperclassId

        public int getSuperclassId()
        Returns the index of the constant pool entry representing the super class.
      • setSuperclass

        public void setSuperclass​(String superclass)
                           throws CannotCompileException
        Sets the super class.

        The new super class should inherit from the old super class. This method modifies constructors so that they call constructors declared in the new super class.

        Throws:
        CannotCompileException
      • renameClass

        public final void renameClass​(String oldname,
                                      String newname)
        Replaces all occurrences of a class name in the class file.

        If class X is substituted for class Y in the class file, X and Y must have the same signature. If Y provides a method m(), X must provide it even if X inherits m() from the super class. If this fact is not guaranteed, the bytecode verifier may cause an error.

        Parameters:
        oldname - the replaced class name
        newname - the substituted class name
      • renameClass

        public final void renameClass​(Map<String,​String> classnames)
        Replaces all occurrences of several class names in the class file.
        Parameters:
        classnames - specifies which class name is replaced with which new name. Class names must be described with the JVM-internal representation like java/lang/Object.
        See Also:
        renameClass(String,String)
      • getRefClasses

        public final void getRefClasses​(Map<String,​String> classnames)
        Internal-use only. CtClass.getRefClasses() calls this method.
      • getInterfaces

        public String[] getInterfaces()
        Returns the names of the interfaces implemented by the class. The returned array is read only.
      • setInterfaces

        public void setInterfaces​(String[] nameList)
        Sets the interfaces.
        Parameters:
        nameList - the names of the interfaces.
      • addInterface

        public void addInterface​(String name)
        Appends an interface to the interfaces implemented by the class.
      • getFields

        public List<FieldInfo> getFields()
        Returns all the fields declared in the class.
        Returns:
        a list of FieldInfo.
        See Also:
        FieldInfo
      • addField2

        public final void addField2​(FieldInfo finfo)
        Just appends a field to the class. It does not check field duplication. Use this method only when minimizing performance overheads is seriously required.
        Since:
        3.13
      • getMethods

        public List<MethodInfo> getMethods()
        Returns all the methods declared in the class.
        Returns:
        a list of MethodInfo.
        See Also:
        MethodInfo
      • getMethod

        public MethodInfo getMethod​(String name)
        Returns the method with the specified name. If there are multiple methods with that name, this method returns one of them.
        Returns:
        null if no such method is found.
      • getStaticInitializer

        public MethodInfo getStaticInitializer()
        Returns a static initializer (class initializer), or null if it does not exist.
      • addMethod

        public void addMethod​(MethodInfo minfo)
                       throws DuplicateMemberException
        Appends a method to the class. If there is a bridge method with the same name and signature, then the bridge method is removed before a new method is added.
        Throws:
        DuplicateMemberException - when the method is already included.
      • addMethod2

        public final void addMethod2​(MethodInfo minfo)
        Just appends a method to the class. It does not check method duplication or remove a bridge method. Use this method only when minimizing performance overheads is seriously required.
        Since:
        3.13
      • getAttributes

        public List<AttributeInfo> getAttributes()
        Returns all the attributes. The returned List object is shared with this object. If you add a new attribute to the list, the attribute is also added to the classs file represented by this object. If you remove an attribute from the list, it is also removed from the class file.
        Returns:
        a list of AttributeInfo objects.
        See Also:
        AttributeInfo
      • removeAttribute

        public AttributeInfo removeAttribute​(String name)
        Removes an attribute with the specified name.
        Parameters:
        name - attribute name.
        Returns:
        the removed attribute or null.
        Since:
        3.21
      • addAttribute

        public void addAttribute​(AttributeInfo info)
        Appends an attribute. If there is already an attribute with the same name, the new one substitutes for it.
        See Also:
        getAttributes()
      • getSourceFile

        public String getSourceFile()
        Returns the source file containing this class.
        Returns:
        null if this information is not available.
      • getMajorVersion

        public int getMajorVersion()
        Get the Major version.
        Returns:
        the major version
      • setMajorVersion

        public void setMajorVersion​(int major)
        Set the major version.
        Parameters:
        major - the major version
      • getMinorVersion

        public int getMinorVersion()
        Get the minor version.
        Returns:
        the minor version
      • setMinorVersion

        public void setMinorVersion​(int minor)
        Set the minor version.
        Parameters:
        minor - the minor version
      • setVersionToJava5

        public void setVersionToJava5()
        Sets the major and minor version to Java 5. If the major version is older than 49, Java 5 extensions such as annotations are ignored by the JVM.