Class ClassFile

    • Field Detail

      • MAJOR_VERSION_JDK_1_1

        public static final short MAJOR_VERSION_JDK_1_1
        Major version number of a class file that was generated by a Java 1.1-compliant compiler.
        See Also:
        Constant Field Values
      • MINOR_VERSION_JDK_1_1

        public static final short MINOR_VERSION_JDK_1_1
        Minor version number of a class file that was generated by a Java 1.1-compliant compiler.
        See Also:
        Constant Field Values
      • MAJOR_VERSION_JDK_1_2

        public static final short MAJOR_VERSION_JDK_1_2
        Major version number of a class file that was generated by a Java 1.2-compliant compiler.
        See Also:
        Constant Field Values
      • MINOR_VERSION_JDK_1_2

        public static final short MINOR_VERSION_JDK_1_2
        Minor version number of a class file that was generated by a Java 1.2-compliant compiler.
        See Also:
        Constant Field Values
      • MAJOR_VERSION_JDK_1_3

        public static final short MAJOR_VERSION_JDK_1_3
        Major version number of a class file that was generated by a Java 1.3-compliant compiler.
        See Also:
        Constant Field Values
      • MINOR_VERSION_JDK_1_3

        public static final short MINOR_VERSION_JDK_1_3
        Minor version number of a class file that was generated by a Java 1.3-compliant compiler.
        See Also:
        Constant Field Values
      • MAJOR_VERSION_JDK_1_4

        public static final short MAJOR_VERSION_JDK_1_4
        Major version number of a class file that was generated by a Java 1.4-compliant compiler.
        See Also:
        Constant Field Values
      • MINOR_VERSION_JDK_1_4

        public static final short MINOR_VERSION_JDK_1_4
        Minor version number of a class file that was generated by a Java 1.4-compliant compiler.
        See Also:
        Constant Field Values
      • MAJOR_VERSION_JDK_1_5

        public static final short MAJOR_VERSION_JDK_1_5
        Major version number of a class file that was generated by a Java 1.5-compliant compiler.
        See Also:
        Constant Field Values
      • MINOR_VERSION_JDK_1_5

        public static final short MINOR_VERSION_JDK_1_5
        Minor version number of a class file that was generated by a Java 1.5-compliant compiler.
        See Also:
        Constant Field Values
      • MAJOR_VERSION_JDK_1_6

        public static final short MAJOR_VERSION_JDK_1_6
        Major version number of a class file that was generated by a Java 1.6-compliant compiler.
        See Also:
        Constant Field Values
      • MINOR_VERSION_JDK_1_6

        public static final short MINOR_VERSION_JDK_1_6
        Minor version number of a class file that was generated by a Java 1.6-compliant compiler.
        See Also:
        Constant Field Values
      • MAJOR_VERSION_JDK_1_7

        public static final short MAJOR_VERSION_JDK_1_7
        Major version number of a class file that was generated by a Java 1.7-compliant compiler.
        See Also:
        Constant Field Values
      • MINOR_VERSION_JDK_1_7

        public static final short MINOR_VERSION_JDK_1_7
        Minor version number of a class file that was generated by a Java 1.7-compliant compiler.
        See Also:
        Constant Field Values
      • MAJOR_VERSION_JDK_1_8

        public static final short MAJOR_VERSION_JDK_1_8
        Major version number of a class file that was generated by a Java 1.8-compliant compiler.
        See Also:
        Constant Field Values
      • MINOR_VERSION_JDK_1_8

        public static final short MINOR_VERSION_JDK_1_8
        Minor version number of a class file that was generated by a Java 1.8-compliant compiler.
        See Also:
        Constant Field Values
      • MAJOR_VERSION_JDK_1_9

        public static final short MAJOR_VERSION_JDK_1_9
        Major version number of a class file that was generated by a Java 1.9-compliant compiler.
        See Also:
        Constant Field Values
      • MINOR_VERSION_JDK_1_9

        public static final short MINOR_VERSION_JDK_1_9
        Minor version number of a class file that was generated by a Java 1.9-compliant compiler.
        See Also:
        Constant Field Values
      • MAJOR_VERSION_JDK_1_10

        public static final short MAJOR_VERSION_JDK_1_10
        Major version number of a class file that was generated by a Java 1.10-compliant compiler.
        See Also:
        Constant Field Values
      • MINOR_VERSION_JDK_1_10

        public static final short MINOR_VERSION_JDK_1_10
        Minor version number of a class file that was generated by a Java 1.10-compliant compiler.
        See Also:
        Constant Field Values
      • MAJOR_VERSION_JDK_1_11

        public static final short MAJOR_VERSION_JDK_1_11
        Major version number of a class file that was generated by a Java 1.11-compliant compiler.
        See Also:
        Constant Field Values
      • MINOR_VERSION_JDK_1_11

        public static final short MINOR_VERSION_JDK_1_11
        Minor version number of a class file that was generated by a Java 1.11-compliant compiler.
        See Also:
        Constant Field Values
      • MAJOR_VERSION_JDK_1_12

        public static final short MAJOR_VERSION_JDK_1_12
        Major version number of a class file that was generated by a Java 1.12-compliant compiler.
        See Also:
        Constant Field Values
      • MINOR_VERSION_JDK_1_12

        public static final short MINOR_VERSION_JDK_1_12
        Minor version number of a class file that was generated by a Java 1.12-compliant compiler.
        See Also:
        Constant Field Values
      • accessFlags

        public final short accessFlags
        The access flags of the class.
        See Also:
        and consorts
      • superclass

        public final short superclass
        The constant pool index of the ClassFile.ConstantClassInfo that describes the superclass of this class. Zero for class Object, Object for interfaces.
        See Also:
        "JVMS11 4.1, The ClassFile Structure"
      • interfaces

        public final short[] interfaces
        The constant pool indexes of ClassFile.ConstantClassInfo which describe the interfaces that this class implements, resp. that this interface extends.
    • Method Detail

      • addSourceFileAttribute

        public void addSourceFileAttribute​(String sourceFileName)
        Adds a SourceFile attribute to this class file. (Does not check whether one exists already.)
        Parameters:
        sourceFileName -
      • addDeprecatedAttribute

        public void addDeprecatedAttribute()
        Adds the Deprecated attribute to this class.
      • getInnerClassesAttribute

        @Nullable
        public ClassFile.InnerClassesAttribute getInnerClassesAttribute()
        Finds the InnerClasses attribute of this class file.
        Returns:
        null if this class has no "InnerClasses" attribute
      • addInnerClassesAttributeEntry

        public void addInnerClassesAttributeEntry​(ClassFile.InnerClassesAttribute.Entry entry)
        Creates an InnerClasses attribute if it does not exist, then adds the entry to the InnerClasses attribute.
      • getAnnotations

        public ClassFile.Annotation[] getAnnotations​(boolean runtimeVisible)
        Specified by:
        getAnnotations in interface Annotatable
        Returns:
        The annotations on this element; an empty array iff there are no annotations
      • getThisClassName

        public String getThisClassName()
        Returns:
        The fully qualified name of this class, e.g. "pkg1.pkg2.Outer$Inner"
      • setVersion

        public void setVersion​(short majorVersion,
                               short minorVersion)
        Sets the major and minor class file version numbers (JVMS 4.1).

        ClassFile declares a set of valid major-minor version number pairs, e.g. MAJOR_VERSION_JDK_1_6 and MINOR_VERSION_JDK_1_6.

      • getMajorVersion

        public short getMajorVersion()
        Returns:
        The current major class file version number
      • getMinorVersion

        public short getMinorVersion()
        Returns:
        The current minor class file version number
      • addConstantClassInfo

        public short addConstantClassInfo​(String typeFd)
        Returns the constant index number for a "CONSTANT_Class_info" structure to the class file. If the class hasn't been added before, adds it to the constant pool. Otherwise returns the constant number for that element of the pool.
        See Also:
        JVM specification, section 4.4.1
      • addConstantInterfaceMethodrefInfo

        public short addConstantInterfaceMethodrefInfo​(String classFd,
                                                       String methodName,
                                                       String methodMd)
        Adds a "CONSTANT_InterfaceMethodref_info" structure to the class file.
        See Also:
        JVM specification, section 4.4.2
      • addConstantIntegerInfo

        public short addConstantIntegerInfo​(int value)
        Adds a "CONSTANT_Integer_info" structure to the class file.
        See Also:
        JVM specification, section 4.4.4
      • addConstantFloatInfo

        public short addConstantFloatInfo​(float value)
        Adds a "CONSTANT_Float_info" structure to the class file.
        See Also:
        JVM specification, section 4.4.4
      • addConstantLongInfo

        public short addConstantLongInfo​(long value)
        Adds a "CONSTANT_Long_info" structure to the class file.
        See Also:
        JVM specification, section 4.4.5
      • addConstantDoubleInfo

        public short addConstantDoubleInfo​(double value)
        Adds a "CONSTANT_Double_info" structure to the class file.
        See Also:
        JVM specification, section 4.4.5
      • addConstantUtf8Info

        public short addConstantUtf8Info​(String s)
        Adds a "CONSTANT_Utf8_info" structure to the class file if no equal entry exists.
        Returns:
        The index of the already existing or newly created entry
        See Also:
        JVM specification, section 4.4.7
      • getConstantClassInfo

        public ClassFile.ConstantClassInfo getConstantClassInfo​(short index)
        Returns:
        The (read-only) constant class info indexed by index
      • getConstantFieldrefInfo

        public ClassFile.ConstantFieldrefInfo getConstantFieldrefInfo​(short index)
        Returns:
        The (read-only) constant fieldref info indexed by index
      • getConstantInterfaceMethodrefInfo

        public ClassFile.ConstantInterfaceMethodrefInfo getConstantInterfaceMethodrefInfo​(short index)
        Returns:
        The (read-only) constant interface methodref info indexed by index
      • getConstantInvokeDynamicInfo

        public ClassFile.ConstantInvokeDynamicInfo getConstantInvokeDynamicInfo​(short index)
        Returns:
        The (read-only) constant invoke dynamic info indexed by index
      • getConstantMethodHandleInfo

        public ClassFile.ConstantMethodHandleInfo getConstantMethodHandleInfo​(short index)
        Returns:
        The (read-only) constant method handle info indexed by index
      • getConstantMethodrefInfo

        public ClassFile.ConstantMethodrefInfo getConstantMethodrefInfo​(short index)
        Returns:
        The (read-only) constant methodref info indexed by index
      • getConstantMethodTypeInfo

        public ClassFile.ConstantMethodTypeInfo getConstantMethodTypeInfo​(short index)
        Returns:
        The (read-only) constant method type info indexed by index
      • getConstantNameAndTypeInfo

        public ClassFile.ConstantNameAndTypeInfo getConstantNameAndTypeInfo​(short index)
        Returns:
        The (read-only) constant name and type info indexed by index
      • getConstantUtf8Info

        public ClassFile.ConstantUtf8Info getConstantUtf8Info​(short index)
        Returns:
        The (read-only) constant utf8 info indexed by index
      • getConstantValuePoolInfo

        public ClassFile.ConstantValuePoolInfo getConstantValuePoolInfo​(short index)
        Returns:
        The (read-only) constant value pool info indexed by index
      • getConstantPoolSize

        public int getConstantPoolSize()
        Returns:
        The size of the constant pool
      • getConstantUtf8

        public String getConstantUtf8​(short index)
        Shorthand for getConstantUtf8Info(index).s.
        Parameters:
        index - Index to a CONSTANT_Utf8_info in the constant pool
        Returns:
        The string represented by the structure
      • store

        public void store​(OutputStream os)
                   throws IOException
        Writes ClassFile to an OutputStream, in "class file" format.

        Notice that if an IOException is thrown, the class file is probably written incompletely and thus invalid. The calling method must take care of this situation, e.g. by closing the output stream and then deleting the file.

        Throws:
        IOException
      • getSourceResourceName

        public static String getSourceResourceName​(String className)
        Constructs the name of a resource that could contain the source code of the class with the className.

        Notice that member types are declared inside a different type, so the relevant source file is that of the outermost declaring class.

        Parameters:
        className - Fully qualified class name, e.g. "pkg1.pkg2.Outer$Inner"
        Returns:
        the name of the resource, e.g. "pkg1/pkg2/Outer.java"
      • getClassFileResourceName

        public static String getClassFileResourceName​(String className)
        Constructs the name of a resource that could contain the class file of the class with the className.
        Parameters:
        className - Fully qualified class name, e.g. "pkg1.pkg2.Outer$Inner"
        Returns:
        the name of the resource, e.g. "pkg1/pkg2/Outer$Inner.class"
      • toByteArray

        public byte[] toByteArray()
        Returns the byte code of this ClassFile as a byte array.