Class ClassFile

    • Constructor Detail

      • ClassFile

        public ClassFile​(short accessFlags,
                         String thisClassFd,
                         String superclassFd,
                         String[] interfaceFds)
        Construct from parsed components.
        Parameters:
        accessFlags - as defined by Mod
        thisClassFd - the field descriptor for this class
        superclassFd - the field descriptor for the extended class (e.g. "Ljava/lang/Object;")
        interfaceFds - the field descriptors for the implemented interfaces
    • 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

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

        public void addInnerClassesAttributeEntry​(ClassFile.InnerClassesAttribute.Entry e)
        Create an "InnerClasses" attribute if it does not exist, then add the given entry to the "InnerClasses" attribute.
        Parameters:
        e -
      • 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). The class file version defaults to the JDK 1.1 values (45.3) which execute on virtually every JVM.
      • 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)
        Return the constant index number for a "CONSTANT_Class_info" structure to the class file. If the class hasn't been added before, add it to the constant pool. Otherwise return 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)
        Add a "CONSTANT_InterfaceMethodref_info" structure to the class file.
        See Also:
        JVM specification, section 4.4.2
      • addConstantIntegerInfo

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

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

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

        public short addConstantDoubleInfo​(double value)
        Add 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
      • getConstantPoolInfo

        public ClassFile.ConstantPoolInfo getConstantPoolInfo​(short index)
        Returns:
        The (read-only) constant pool entry indexed by index
      • getConstantPoolSize

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

        public String getConstantUtf8​(short index)
        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
        Write 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.

        Parameters:
        os -
        Throws:
        IOException
      • getSourceResourceName

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

        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)
        Construct the name of a resource that could contain the class file of the class with the given name.
        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()
        Return the byte code of this ClassFile as a byte array.