com.go.trove.classfile
Class ClassFile

java.lang.Object
  |
  +--com.go.trove.classfile.ClassFile

public class ClassFile
extends java.lang.Object

A class used to create Java class files. Call the writeTo method to produce a class file.

See The Java Virtual Machine Specification (ISBN 0-201-63452-X) for information on how class files are structured. Section 4.1 describes the ClassFile structure.

Version:
38 , 01/05/17
Author:
Brian S O'Neill

Constructor Summary
ClassFile(java.lang.String className)
          By default, the ClassFile defines public, non-final, concrete classes.
ClassFile(java.lang.String className, java.lang.Class superClass)
          By default, the ClassFile defines public, non-final, concrete classes.
ClassFile(java.lang.String className, java.lang.String superClassName)
          By default, the ClassFile defines public, non-final, concrete classes.
 
Method Summary
 void addAttribute(Attribute attr)
          Add an attribute to this class.
 MethodInfo addConstructor(AccessFlags flags, TypeDescriptor[] params)
          Add a constructor to this class.
 FieldInfo addField(AccessFlags flags, java.lang.String fieldName, TypeDescriptor type)
          Add a field to this class.
 MethodInfo addInitializer()
          Add a static initializer to this class.
 ClassFile addInnerClass(java.lang.String innerClassName)
          Add an inner class to this class.
 ClassFile addInnerClass(java.lang.String innerClassName, java.lang.Class superClass)
          Add an inner class to this class.
 ClassFile addInnerClass(java.lang.String innerClassName, java.lang.String superClassName)
          Add an inner class to this class.
 void addInterface(java.lang.Class i)
          Add an interface that this class implements.
 void addInterface(java.lang.String interfaceName)
          Add an interface that this class implements.
 MethodInfo addMethod(AccessFlags flags, java.lang.String methodName, MethodDescriptor md)
          Add a method to this class.
 MethodInfo addMethod(AccessFlags flags, java.lang.String methodName, TypeDescriptor ret, TypeDescriptor[] params)
          Add a method to this class.
 MethodInfo addMethod(java.lang.reflect.Method method)
          Add a method to this class.
 AccessFlags getAccessFlags()
           
 Attribute[] getAttributes()
           
 int getClassDepth()
          Returns a value indicating how deeply nested an inner class is with respect to its outermost enclosing class.
 java.lang.String getClassName()
           
 ConstantPool getConstantPool()
          Provides access to the ClassFile's ContantPool.
 MethodInfo[] getConstructors()
          Returns all the constructors defined in this class.
 FieldInfo[] getFields()
          Returns all the fields defined in this class.
 MethodInfo getInitializer()
          Returns the static initializer defined in this class or null if there isn't one.
 ClassFile[] getInnerClasses()
          Returns all the inner classes defined in this class.
 java.lang.String getInnerClassName()
          If this ClassFile represents a non-anonymous inner class, returns its short inner class name.
 java.lang.String[] getInterfaces()
          Returns the names of all the interfaces that this class implements.
 MethodInfo[] getMethods()
          Returns all the methods defined in this class, not including constructors and static initializers.
 ClassFile getOuterClass()
          Returns null if this ClassFile does not represent an inner class.
 java.lang.String getSourceFile()
          Returns the source file of this class file or null if not set.
 java.lang.String getSuperClassName()
           
 TypeDescriptor getType()
          Returns a TypeDescriptor for the type of this ClassFile.
 boolean isDeprecated()
           
 boolean isInnerClass()
          Returns true if this ClassFile represents an inner class.
 boolean isSynthetic()
           
 void markDeprecated()
          Mark this class as being deprecated by adding a special attribute.
 void markSynthetic()
          Mark this class as being synthetic by adding a special attribute.
static ClassFile readFrom(java.io.DataInput din)
          Reads a ClassFile from the given DataInput.
static ClassFile readFrom(java.io.DataInput din, ClassFileDataLoader loader, AttributeFactory attrFactory)
          Reads a ClassFile from the given DataInput.
static ClassFile readFrom(java.io.InputStream in)
          Reads a ClassFile from the given InputStream.
static ClassFile readFrom(java.io.InputStream in, ClassFileDataLoader loader, AttributeFactory attrFactory)
          Reads a ClassFile from the given InputStream.
 void setSourceFile(java.lang.String fileName)
          Set the source file of this class file by adding a source file attribute.
 void setVersion(int major, int minor)
          Sets the version to use when writing the generated ClassFile.
 void writeTo(java.io.DataOutput dout)
          Writes the ClassFile to the given DataOutput.
 void writeTo(java.io.OutputStream out)
          Writes the ClassFile to the given OutputStream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ClassFile

public ClassFile(java.lang.String className)
By default, the ClassFile defines public, non-final, concrete classes. This constructor creates a ClassFile for a class that extends java.lang.Object.

Use the access flags to change the default modifiers for this class or to turn it into an interface.

Parameters:
className - Full class name of the form ex: "java.lang.String".

ClassFile

public ClassFile(java.lang.String className,
                 java.lang.Class superClass)
By default, the ClassFile defines public, non-final, concrete classes.

Use the access flags to change the default modifiers for this class or to turn it into an interface.

Parameters:
className - Full class name of the form ex: "java.lang.String".
superClass - Super class.

ClassFile

public ClassFile(java.lang.String className,
                 java.lang.String superClassName)
By default, the ClassFile defines public, non-final, concrete classes.

Use the access flags to change the default modifiers for this class or to turn it into an interface.

Parameters:
className - Full class name of the form ex: "java.lang.String".
superClassName - Full super class name.
Method Detail

getClassName

public java.lang.String getClassName()

getSuperClassName

public java.lang.String getSuperClassName()

getType

public TypeDescriptor getType()
Returns a TypeDescriptor for the type of this ClassFile.

getAccessFlags

public AccessFlags getAccessFlags()

getInterfaces

public java.lang.String[] getInterfaces()
Returns the names of all the interfaces that this class implements.

getFields

public FieldInfo[] getFields()
Returns all the fields defined in this class.

getMethods

public MethodInfo[] getMethods()
Returns all the methods defined in this class, not including constructors and static initializers.

getConstructors

public MethodInfo[] getConstructors()
Returns all the constructors defined in this class.

getInitializer

public MethodInfo getInitializer()
Returns the static initializer defined in this class or null if there isn't one.

getInnerClasses

public ClassFile[] getInnerClasses()
Returns all the inner classes defined in this class. If no inner classes are defined, then an array of length zero is returned.

isInnerClass

public boolean isInnerClass()
Returns true if this ClassFile represents an inner class.

getInnerClassName

public java.lang.String getInnerClassName()
If this ClassFile represents a non-anonymous inner class, returns its short inner class name.

getOuterClass

public ClassFile getOuterClass()
Returns null if this ClassFile does not represent an inner class.
See Also:
isInnerClass()

getClassDepth

public int getClassDepth()
Returns a value indicating how deeply nested an inner class is with respect to its outermost enclosing class. For top level classes, 0 is returned. For first level inner classes, 1 is returned, etc.

getSourceFile

public java.lang.String getSourceFile()
Returns the source file of this class file or null if not set.

isSynthetic

public boolean isSynthetic()

isDeprecated

public boolean isDeprecated()

getConstantPool

public ConstantPool getConstantPool()
Provides access to the ClassFile's ContantPool.
Returns:
The constant pool for this class file.

addInterface

public void addInterface(java.lang.String interfaceName)
Add an interface that this class implements.
Parameters:
interfaceName - Full interface name.

addInterface

public void addInterface(java.lang.Class i)
Add an interface that this class implements.

addField

public FieldInfo addField(AccessFlags flags,
                          java.lang.String fieldName,
                          TypeDescriptor type)
Add a field to this class.

addMethod

public MethodInfo addMethod(AccessFlags flags,
                            java.lang.String methodName,
                            TypeDescriptor ret,
                            TypeDescriptor[] params)
Add a method to this class.
Parameters:
ret - Is null if method returns void.
params - May be null if method accepts no parameters.

addMethod

public MethodInfo addMethod(AccessFlags flags,
                            java.lang.String methodName,
                            MethodDescriptor md)
Add a method to this class.

addMethod

public MethodInfo addMethod(java.lang.reflect.Method method)
Add a method to this class. This method is handy for implementing methods defined by a pre-existing interface.

addConstructor

public MethodInfo addConstructor(AccessFlags flags,
                                 TypeDescriptor[] params)
Add a constructor to this class.
Parameters:
params - May be null if constructor accepts no parameters.

addInitializer

public MethodInfo addInitializer()
Add a static initializer to this class.

addInnerClass

public ClassFile addInnerClass(java.lang.String innerClassName)
Add an inner class to this class. By default, inner classes are private static.
Parameters:
innerClassName - Optional short inner class name.

addInnerClass

public ClassFile addInnerClass(java.lang.String innerClassName,
                               java.lang.Class superClass)
Add an inner class to this class. By default, inner classes are private static.
Parameters:
innerClassName - Optional short inner class name.
superClass - Super class.

addInnerClass

public ClassFile addInnerClass(java.lang.String innerClassName,
                               java.lang.String superClassName)
Add an inner class to this class. By default, inner classes are private static.
Parameters:
innerClassName - Optional short inner class name.
superClassName - Full super class name.
isStatic - True specifies a static inner class.

setSourceFile

public void setSourceFile(java.lang.String fileName)
Set the source file of this class file by adding a source file attribute. The source doesn't actually have to be a file, but the virtual machine spec names the attribute "SourceFile_attribute".

markSynthetic

public void markSynthetic()
Mark this class as being synthetic by adding a special attribute.

markDeprecated

public void markDeprecated()
Mark this class as being deprecated by adding a special attribute.

addAttribute

public void addAttribute(Attribute attr)
Add an attribute to this class.

getAttributes

public Attribute[] getAttributes()

setVersion

public void setVersion(int major,
                       int minor)
                throws java.lang.IllegalArgumentException
Sets the version to use when writing the generated ClassFile. Currently, only version 45, 3 is supported, and is set by default.
Throws:
java.lang.IllegalArgumentException - when the version isn't supported

writeTo

public void writeTo(java.io.OutputStream out)
             throws java.io.IOException
Writes the ClassFile to the given OutputStream. When finished, the stream is flushed, but not closed.

writeTo

public void writeTo(java.io.DataOutput dout)
             throws java.io.IOException
Writes the ClassFile to the given DataOutput.

readFrom

public static ClassFile readFrom(java.io.InputStream in)
                          throws java.io.IOException
Reads a ClassFile from the given InputStream. With this method, inner classes cannot be loaded, and custom attributes cannot be defined.
Parameters:
in - source of class file data
Throws:
java.io.IOException - for I/O error or if classfile is invalid.
java.lang.ArrayIndexOutOfBoundsException - if a constant pool index is out of range.
java.lang.ClassCastException - if a constant pool index references the wrong type.

readFrom

public static ClassFile readFrom(java.io.DataInput din)
                          throws java.io.IOException
Reads a ClassFile from the given DataInput. With this method, inner classes cannot be loaded, and custom attributes cannot be defined.
Parameters:
din - source of class file data
Throws:
java.io.IOException - for I/O error or if classfile is invalid.
java.lang.ArrayIndexOutOfBoundsException - if a constant pool index is out of range.
java.lang.ClassCastException - if a constant pool index references the wrong type.

readFrom

public static ClassFile readFrom(java.io.InputStream in,
                                 ClassFileDataLoader loader,
                                 AttributeFactory attrFactory)
                          throws java.io.IOException
Reads a ClassFile from the given InputStream. A ClassFileDataLoader may be provided, which allows inner class definitions to be loaded. Also, an AttributeFactory may be provided, which allows non-standard attributes to be read. All remaining unknown attribute types are captured, but are not decoded.
Parameters:
in - source of class file data
loader - optional loader for reading inner class definitions
attrFactory - optional factory for reading custom attributes
Throws:
java.io.IOException - for I/O error or if classfile is invalid.
java.lang.ArrayIndexOutOfBoundsException - if a constant pool index is out of range.
java.lang.ClassCastException - if a constant pool index references the wrong type.

readFrom

public static ClassFile readFrom(java.io.DataInput din,
                                 ClassFileDataLoader loader,
                                 AttributeFactory attrFactory)
                          throws java.io.IOException
Reads a ClassFile from the given DataInput. A ClassFileDataLoader may be provided, which allows inner class definitions to be loaded. Also, an AttributeFactory may be provided, which allows non-standard attributes to be read. All remaining unknown attribute types are captured, but are not decoded.
Parameters:
din - source of class file data
loader - optional loader for reading inner class definitions
attrFactory - optional factory for reading custom attributes
Throws:
java.io.IOException - for I/O error or if classfile is invalid.
java.lang.ArrayIndexOutOfBoundsException - if a constant pool index is out of range.
java.lang.ClassCastException - if a constant pool index references the wrong type.