Class ClassFile


  • public class ClassFile
    extends java.lang.Object
    A Java class file, whose content can be returned as a verbose, human "readable" string. As an example, the string representation of the HelloWorld class, obtained with the toString() method, is:
     magic: -889275714
     minor_version: 0
     major_version: 49
     access_flags: 33
     this_class: ConstantClassInfo HelloWorld
     super_class: ConstantClassInfo java/lang/Object
     interfaces_count: 0
     fields_count: 0
     methods_count: 2
     access_flags: 1
     name_index: <init>
     descriptor_index: ()V
     attributes_count: 1
     attribute_name_index: Code
     max_stack: 1
     max_locals: 1
     0: 25 0
     1: 183 ConstantMethodRefInfo java/lang/Object.<init>()V
     2: 177
     exception_table_length: 0
     attributes_count: 2
     attribute_name_index: LineNumberTable
     line_number_table_length: 1
     start_pc: <0>
     line_number: 31
     attribute_name_index: LocalVariableTable
     local_variable_table_length: 1
     start_pc: <0>
     length: <3>
     name_index: this
     descriptor_index: LHelloWorld;
     index: 0
     access_flags: 9
     name_index: main
     descriptor_index: ([Ljava/lang/String;)V
     attributes_count: 1
     attribute_name_index: Code
     max_stack: 2
     max_locals: 1
     0: 178 ConstantFieldRefInfo java/lang/System.outLjava/io/PrintStream;
     1: 18 ConstantStringInfo Hello, world!
     2: 182 ConstantMethodRefInfo java/io/PrintStream.println(Ljava/lang/String;)V
     3: 177
     exception_table_length: 0
     attributes_count: 2
     attribute_name_index: LineNumberTable
     line_number_table_length: 2
     start_pc: <0>
     line_number: 33
     start_pc: <3>
     line_number: 34
     attribute_name_index: LocalVariableTable
     local_variable_table_length: 1
     start_pc: <0>
     length: <4>
     name_index: args
     descriptor_index: [Ljava/lang/String;
     index: 0
     attributes_count: 1
     attribute_name_index: SourceFile
     sourcefile_index: HelloWorld.java
     

    This class is used to compare classes in unit tests. Its source code is as close as possible to the Java Virtual Machine specification for ease of reference. The constant pool and bytecode offsets are abstracted away so that two classes which differ only by their constant pool or low level byte code instruction representation (e.g. a ldc vs. a ldc_w) are still considered equal. Likewise, attributes (resp. type annotations) are re-ordered into alphabetical order, so that two classes which differ only via the ordering of their attributes (resp. type annotations) are still considered equal.

    • Field Detail

      • MODULE_INFO

        static final java.lang.String MODULE_INFO
        The name of JDK9 module classes.
        See Also:
        Constant Field Values
      • classBytes

        private final byte[] classBytes
        The binary content of a Java class file.
      • className

        private java.lang.String className
        The name of the class contained in this class file, lazily computed.
      • constantPoolDump

        private java.lang.String constantPoolDump
        The dump of the constant pool of classBytes, lazily computed.
      • dump

        private java.lang.String dump
        The dump of classBytes, lazily computed.
    • Constructor Detail

      • ClassFile

        public ClassFile​(byte[] classBytes)
        Constructs a new ClassFile instance.
        Parameters:
        classBytes - the binary content of a Java class file.
    • Method Detail

      • getConstantPoolDump

        public java.lang.String getConstantPoolDump()
        Returns a string representation of the constant pool of the class contained in this class file.
        Returns:
        a string representation of the constant pool of the class contained in this class file.
      • newInstance

        public java.lang.Object newInstance()
                                     throws java.lang.ReflectiveOperationException
        Returns a new instance of the class contained in this class file. The class is loaded in a new class loader.
        Returns:
        a new instance of the class, or null if the class is abstract, is an enum, or a module info.
        Throws:
        java.lang.ReflectiveOperationException - if the class is invalid or if an error occurs in its constructor.
      • newInstance

        static java.lang.Object newInstance​(java.lang.String className,
                                            byte[] classContent)
                                     throws java.lang.ReflectiveOperationException
        Returns a new instance of the given class. The class is loaded in a new class loader.
        Parameters:
        className - the name of the class to load.
        classContent - the content of the class to load.
        Returns:
        a new instance of the class, or null if the class is abstract, is an enum, or a module info.
        Throws:
        java.lang.ReflectiveOperationException - if the class is invalid or if an error occurs in its constructor.
      • equals

        public boolean equals​(java.lang.Object other)
        Returns whether the given class file is the same as this one.
        Overrides:
        equals in class java.lang.Object
        Returns:
        true if 'other' is a ClassFile with the same string representation.
        Throws:
        ClassFormatException - if the class content can't be parsed.
      • hashCode

        public int hashCode()
        Returns the hashcode of this class file.
        Overrides:
        hashCode in class java.lang.Object
        Returns:
        the hashcode of the string representation of this class file.
        Throws:
        ClassFormatException - if the class content can't be parsed.
      • toString

        public java.lang.String toString()
        Returns a string representation of this class file.
        Overrides:
        toString in class java.lang.Object
        Returns:
        a string representation of this class file (see the class comments for more details).
        Throws:
        ClassFormatException - if the class content can't be parsed.
      • computeNameAndDumps

        private void computeNameAndDumps()
        Computes the name and the string representation of the class (and of its constant pool) contained in this class file.
        Throws:
        ClassFormatException - if the class content can't be parsed.
      • dumpClassFile

        private static ClassFile.ConstantClassInfo dumpClassFile​(ClassFile.Parser parser,
                                                                 ClassFile.Builder builder,
                                                                 ClassFile.Builder constantPoolBuilder)
                                                          throws java.io.IOException
        Parses and dumps the high level structure of the class.
        Parameters:
        parser - a class parser.
        builder - a dump builder.
        constantPoolBuilder - a dump builder for the constant pool.
        Returns:
        the ConstantClassInfo corresponding to the parsed class.
        Throws:
        java.io.IOException - if the class can't be parsed.
        See Also:
        JVMS 4.1
      • dumpAttributeList

        private static void dumpAttributeList​(ClassFile.Parser parser,
                                              ClassFile.Builder builder)
                                       throws java.io.IOException
        Parses and dumps a list of attributes.
        Parameters:
        parser - a class parser.
        builder - a dump builder.
        Throws:
        java.io.IOException - if the class can't be parsed.
        See Also:
        JVMS 4.1
      • parseCpInfo

        private static ClassFile.CpInfo parseCpInfo​(ClassFile.Parser parser,
                                                    ClassFile.ClassContext classContext)
                                             throws java.io.IOException
        Parses a cp_info structure.
        Parameters:
        parser - a class parser.
        classContext - a context to lookup constant pool items from their index.
        Returns:
        the parsed constant pool item.
        Throws:
        java.io.IOException - if the class can't be parsed.
        See Also:
        JVMS 4.4
      • dumpFieldInfo

        private static void dumpFieldInfo​(ClassFile.Parser parser,
                                          ClassFile.Builder builder)
                                   throws java.io.IOException
        Parses and dumps a field_info structure.
        Parameters:
        parser - a class parser.
        builder - a dump builder.
        Throws:
        java.io.IOException - if the class can't be parsed.
        See Also:
        JVMS 4.5
      • dumpMethodInfo

        private static void dumpMethodInfo​(ClassFile.Parser parser,
                                           ClassFile.Builder builder)
                                    throws java.io.IOException
        Parses and dumps a method_info structure.
        Parameters:
        parser - a class parser.
        builder - a dump builder.
        Throws:
        java.io.IOException - if the class can't be parsed.
        See Also:
        JVMS 4.6
      • dumpAttributeInfo

        private static void dumpAttributeInfo​(ClassFile.Parser parser,
                                              ClassFile.SortedBuilder sortedBuilder)
                                       throws java.io.IOException
        Parses and dumps an attribute_info structure.
        Parameters:
        parser - a class parser.
        sortedBuilder - a dump builder.
        Throws:
        java.io.IOException - if the class can't be parsed.
        See Also:
        JVMS 4.7
      • dumpConstantValueAttribute

        private static void dumpConstantValueAttribute​(ClassFile.Parser parser,
                                                       ClassFile.Builder builder)
                                                throws java.io.IOException
        Parses and dumps a ConstantValue attribute.
        Parameters:
        parser - a class parser.
        builder - a dump builder.
        Throws:
        java.io.IOException - if the class can't be parsed.
        See Also:
        JVMS 4.7.2
      • dumpCodeAttribute

        private static void dumpCodeAttribute​(ClassFile.Parser parser,
                                              ClassFile.Builder builder)
                                       throws java.io.IOException
        Parses and dumps a Code attribute.
        Parameters:
        parser - a class parser.
        builder - a dump builder.
        Throws:
        java.io.IOException - if the class can't be parsed.
        See Also:
        JVMS 4.7.3
      • dumpInstructions

        private static void dumpInstructions​(int codeLength,
                                             ClassFile.Parser parser,
                                             ClassFile.Builder builder)
                                      throws java.io.IOException
        Parses and dumps the bytecode instructions of a method.
        Parameters:
        codeLength - the number of bytes to parse.
        parser - a class parser.
        builder - a dump builder.
        Throws:
        java.io.IOException - if the class can't be parsed.
        See Also:
        JVMS 6.5
      • dumpStackMapTableAttribute

        private static void dumpStackMapTableAttribute​(ClassFile.Parser parser,
                                                       ClassFile.Builder builder)
                                                throws java.io.IOException
        Parses and dumps a StackMapTable attribute.
        Parameters:
        parser - a class parser.
        builder - a dump builder.
        Throws:
        java.io.IOException - if the class can't be parsed.
        See Also:
        JVMS 4.7.4
      • dumpVerificationTypeInfo

        private static void dumpVerificationTypeInfo​(ClassFile.Parser parser,
                                                     ClassFile.Builder builder)
                                              throws java.io.IOException
        Parses and dumps a verification_type_info structure.
        Parameters:
        parser - a class parser.
        builder - a dump builder.
        Throws:
        java.io.IOException - if the class can't be parsed.
        See Also:
        JVMS 4.7.2
      • dumpExceptionsAttribute

        private static void dumpExceptionsAttribute​(ClassFile.Parser parser,
                                                    ClassFile.Builder builder)
                                             throws java.io.IOException
        Parses and dumps an Exception attribute.
        Parameters:
        parser - a class parser.
        builder - a dump builder.
        Throws:
        java.io.IOException - if the class can't be parsed.
        See Also:
        JVMS 4.7.5
      • dumpInnerClassesAttribute

        private static void dumpInnerClassesAttribute​(ClassFile.Parser parser,
                                                      ClassFile.Builder builder)
                                               throws java.io.IOException
        Parses and dumps an InnerClasses attribute.
        Parameters:
        parser - a class parser.
        builder - a dump builder.
        Throws:
        java.io.IOException - if the class can't be parsed.
        See Also:
        JVMS 4.7.6
      • dumpEnclosingMethodAttribute

        private static void dumpEnclosingMethodAttribute​(ClassFile.Parser parser,
                                                         ClassFile.Builder builder)
                                                  throws java.io.IOException
        Parses and dumps an EnclosingMethod attribute.
        Parameters:
        parser - a class parser.
        builder - a dump builder.
        Throws:
        java.io.IOException - if the class can't be parsed.
        See Also:
        JVMS 4.7.7
      • dumpSyntheticAttribute

        private static void dumpSyntheticAttribute()
        Parses and dumps a Synthetic attribute.
        See Also:
        JVMS 4.7.8
      • dumpSignatureAttribute

        private static void dumpSignatureAttribute​(ClassFile.Parser parser,
                                                   ClassFile.Builder builder)
                                            throws java.io.IOException
        Parses and dumps a Signature attribute.
        Parameters:
        parser - a class parser.
        builder - a dump builder.
        Throws:
        java.io.IOException - if the class can't be parsed.
        See Also:
        JVMS 4.7.9
      • dumpSourceFileAttribute

        private static void dumpSourceFileAttribute​(ClassFile.Parser parser,
                                                    ClassFile.Builder builder)
                                             throws java.io.IOException
        Parses and dumps a SourceFile attribute.
        Parameters:
        parser - a class parser.
        builder - a dump builder.
        Throws:
        java.io.IOException - if the class can't be parsed.
        See Also:
        JVMS 4.7.10
      • dumpSourceDebugAttribute

        private static void dumpSourceDebugAttribute​(int attributeLength,
                                                     ClassFile.Parser parser,
                                                     ClassFile.Builder builder)
                                              throws java.io.IOException
        Parses and dumps a SourceDebug attribute.
        Parameters:
        attributeLength - the length of the SourceDebug attribute (excluding its 6 header bytes).
        parser - a class parser.
        builder - a dump builder.
        Throws:
        java.io.IOException - if the class can't be parsed.
        See Also:
        JVMS 4.7.11
      • dumpLineNumberTableAttribute

        private static void dumpLineNumberTableAttribute​(ClassFile.Parser parser,
                                                         ClassFile.Builder builder)
                                                  throws java.io.IOException
        Parses and dumps a LineNumberTable attribute.
        Parameters:
        parser - a class parser.
        builder - a dump builder.
        Throws:
        java.io.IOException - if the class can't be parsed.
        See Also:
        JVMS 4.7.12
      • dumpLocalVariableTableAttribute

        private static void dumpLocalVariableTableAttribute​(ClassFile.Parser parser,
                                                            ClassFile.Builder builder)
                                                     throws java.io.IOException
        Parses and dumps a LocalVariableTable attribute.
        Parameters:
        parser - a class parser.
        builder - a dump builder.
        Throws:
        java.io.IOException - if the class can't be parsed.
        See Also:
        JVMS 4.7.13
      • dumpLocalVariableTypeTableAttribute

        private static void dumpLocalVariableTypeTableAttribute​(ClassFile.Parser parser,
                                                                ClassFile.Builder builder)
                                                         throws java.io.IOException
        Parses and dumps a LocalVariableTypeTable attribute.
        Parameters:
        parser - a class parser.
        builder - a dump builder.
        Throws:
        java.io.IOException - if the class can't be parsed.
        See Also:
        JVMS 4.7.14
      • dumpDeprecatedAttribute

        private static void dumpDeprecatedAttribute()
        Parses and dumps a Deprecated attribute.
        See Also:
        JVMS 4.7.15
      • dumpRuntimeVisibleAnnotationsAttribute

        private static void dumpRuntimeVisibleAnnotationsAttribute​(ClassFile.Parser parser,
                                                                   ClassFile.Builder builder)
                                                            throws java.io.IOException
        Parses and dumps a RuntimeVisibleAnnotations attribute.
        Parameters:
        parser - a class parser.
        builder - a dump builder.
        Throws:
        java.io.IOException - if the class can't be parsed.
        See Also:
        JVMS 4.7.16
      • dumpAnnotation

        private static void dumpAnnotation​(ClassFile.Parser parser,
                                           ClassFile.Builder builder)
                                    throws java.io.IOException
        Parses and dumps an annotations structure.
        Parameters:
        parser - a class parser.
        builder - a dump builder.
        Throws:
        java.io.IOException - if the class can't be parsed.
        See Also:
        JVMS 4.7.16
      • dumpElementValue

        private static void dumpElementValue​(ClassFile.Parser parser,
                                             ClassFile.Builder builder)
                                      throws java.io.IOException
        Parses and dumps an element_value structure.
        Parameters:
        parser - a class parser.
        builder - a dump builder.
        Throws:
        java.io.IOException - if the class can't be parsed.
        See Also:
        JVMS 4.7.16.1
      • dumpRuntimeInvisibleAnnotationsAttribute

        private static void dumpRuntimeInvisibleAnnotationsAttribute​(ClassFile.Parser parser,
                                                                     ClassFile.Builder builder)
                                                              throws java.io.IOException
        Parses and dumps a RuntimeInvisibleAnnotations attribute.
        Parameters:
        parser - a class parser.
        builder - a dump builder.
        Throws:
        java.io.IOException - if the class can't be parsed.
        See Also:
        JVMS 4.7.17
      • dumpRuntimeVisibleParameterAnnotationsAttribute

        private static void dumpRuntimeVisibleParameterAnnotationsAttribute​(ClassFile.Parser parser,
                                                                            ClassFile.Builder builder)
                                                                     throws java.io.IOException
        Parses and dumps a RuntimeVisibleParameterAnnotations attribute.
        Parameters:
        parser - a class parser.
        builder - a dump builder.
        Throws:
        java.io.IOException - if the class can't be parsed.
        See Also:
        JVMS 4.7.18
      • dumpRuntimeInvisibleParameterAnnotationsAttribute

        private static void dumpRuntimeInvisibleParameterAnnotationsAttribute​(ClassFile.Parser parser,
                                                                              ClassFile.Builder builder)
                                                                       throws java.io.IOException
        Parses and dumps a RuntimeInvisibleParameterAnnotations attribute.
        Parameters:
        parser - a class parser.
        builder - a dump builder.
        Throws:
        java.io.IOException - if the class can't be parsed.
        See Also:
        JVMS 4.7.19
      • dumpRuntimeVisibleTypeAnnotationsAttribute

        private static void dumpRuntimeVisibleTypeAnnotationsAttribute​(ClassFile.Parser parser,
                                                                       ClassFile.Builder builder)
                                                                throws java.io.IOException
        Parses and dumps a RuntimeVisibleTypeAnnotations attribute.
        Parameters:
        parser - a class parser.
        builder - a dump builder.
        Throws:
        java.io.IOException - if the class can't be parsed.
        See Also:
        JVMS 4.7.20
      • dumpTypeAnnotation

        private static void dumpTypeAnnotation​(ClassFile.Parser parser,
                                               ClassFile.SortedBuilder sortedBuilder)
                                        throws java.io.IOException
        Parses and dumps a type_annotation structure.
        Parameters:
        parser - a class parser.
        sortedBuilder - a dump builder.
        Throws:
        java.io.IOException - if the class can't be parsed.
        See Also:
        JVMS 4.7.20
      • dumpTypePath

        private static void dumpTypePath​(ClassFile.Parser parser,
                                         ClassFile.Builder builder)
                                  throws java.io.IOException
        Parses and dumps a type_path structure.
        Parameters:
        parser - a class parser.
        builder - a dump builder.
        Throws:
        java.io.IOException - if the class can't be parsed.
        See Also:
        JVMS 4.7.20.2
      • dumpRuntimeInvisibleTypeAnnotationsAttribute

        private static void dumpRuntimeInvisibleTypeAnnotationsAttribute​(ClassFile.Parser parser,
                                                                         ClassFile.Builder builder)
                                                                  throws java.io.IOException
        Parses and dumps a RuntimeInvisibleTypeAnnotations attribute.
        Parameters:
        parser - a class parser.
        builder - a dump builder.
        Throws:
        java.io.IOException - if the class can't be parsed.
        See Also:
        JVMS 4.7.21
      • dumpAnnotationDefaultAttribute

        private static void dumpAnnotationDefaultAttribute​(ClassFile.Parser parser,
                                                           ClassFile.Builder builder)
                                                    throws java.io.IOException
        Parses and dumps an AnnotationDefault attribute.
        Parameters:
        parser - a class parser.
        builder - a dump builder.
        Throws:
        java.io.IOException - if the class can't be parsed.
        See Also:
        JVMS 4.7.22
      • dumpBootstrapMethodsAttribute

        private static void dumpBootstrapMethodsAttribute​(ClassFile.Parser parser,
                                                          ClassFile.Builder builder)
                                                   throws java.io.IOException
        Parses and dumps a BootstrapMethods attribute.
        Parameters:
        parser - a class parser.
        builder - a dump builder.
        Throws:
        java.io.IOException - if the class can't be parsed.
        See Also:
        JVMS 4.7.23
      • dumpMethodParametersAttribute

        private static void dumpMethodParametersAttribute​(ClassFile.Parser parser,
                                                          ClassFile.Builder builder)
                                                   throws java.io.IOException
        Parses and dumps a MethodParameters attribute.
        Parameters:
        parser - a class parser.
        builder - a dump builder.
        Throws:
        java.io.IOException - if the class can't be parsed.
        See Also:
        JVMS 4.7.24
      • dumpModuleAttribute

        private static void dumpModuleAttribute​(ClassFile.Parser parser,
                                                ClassFile.Builder builder)
                                         throws java.io.IOException
        Parses and dumps a Module attribute.
        Parameters:
        parser - a class parser.
        builder - a dump builder.
        Throws:
        java.io.IOException - if the class can't be parsed.
        See Also:
        JVMS 4.7.25
      • dumpModulePackagesAttribute

        private static void dumpModulePackagesAttribute​(ClassFile.Parser parser,
                                                        ClassFile.Builder builder)
                                                 throws java.io.IOException
        Parses and dumps a ModulePackages attribute.
        Parameters:
        parser - a class parser.
        builder - a dump builder.
        Throws:
        java.io.IOException - if the class can't be parsed.
        See Also:
        JVMS 4.7.26
      • dumpModuleMainClassAttribute

        private static void dumpModuleMainClassAttribute​(ClassFile.Parser parser,
                                                         ClassFile.Builder builder)
                                                  throws java.io.IOException
        Parses and dumps a ModuleMainClass attribute.
        Parameters:
        parser - a class parser.
        builder - a dump builder.
        Throws:
        java.io.IOException - if the class can't be parsed.
        See Also:
        JVMS 4.7.27
      • dumpNestHostAttribute

        private static void dumpNestHostAttribute​(ClassFile.Parser parser,
                                                  ClassFile.Builder builder)
                                           throws java.io.IOException
        Parses and dumps a NestHost attribute.
        Parameters:
        parser - a class parser.
        builder - a dump builder.
        Throws:
        java.io.IOException - if the class can't be parsed.
        See Also:
        JVMS 4.7.28
      • dumpNestMembersAttribute

        private static void dumpNestMembersAttribute​(ClassFile.Parser parser,
                                                     ClassFile.Builder builder)
                                              throws java.io.IOException
        Parses and dumps a NestMembers attribute.
        Parameters:
        parser - a class parser.
        builder - a dump builder.
        Throws:
        java.io.IOException - if the class can't be parsed.
        See Also:
        JVMS 4.7.29
      • dumpPermittedSubtypesAttribute

        private static void dumpPermittedSubtypesAttribute​(ClassFile.Parser parser,
                                                           ClassFile.Builder builder)
                                                    throws java.io.IOException
        Parses and dumps a PermittedSubtypes attribute.
        Parameters:
        parser - a class parser.
        builder - a dump builder.
        Throws:
        java.io.IOException - if the class can't be parsed.
        See Also:
        JEP 360
      • dumpRecordAttribute

        private static void dumpRecordAttribute​(ClassFile.Parser parser,
                                                ClassFile.Builder builder)
                                         throws java.io.IOException
        Parses and dumps a Record attribute.
        Parameters:
        parser - a class parser.
        builder - a dump builder.
        Throws:
        java.io.IOException - if the class can't be parsed.
        See Also:
        JEP 360
      • dumpStackMapAttribute

        private static void dumpStackMapAttribute​(ClassFile.Parser parser,
                                                  ClassFile.Builder builder)
                                           throws java.io.IOException
        Parses and dumps a StackMap attribute.
        Parameters:
        parser - a class parser.
        builder - a dump builder.
        Throws:
        java.io.IOException - if the class can't be parsed.
        See Also:
        CLDC