org.cojen.classfile
Class ClassFile

java.lang.Object
  extended by org.cojen.classfile.ClassFile

public class ClassFile
extends 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.

Author:
Brian S O'Neill

Constructor Summary
ClassFile(String className)
          By default, the ClassFile defines public, non-final, concrete classes.
ClassFile(String className, Class superClass)
          By default, the ClassFile defines public, non-final, concrete classes.
ClassFile(String className, 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(Modifiers modifiers, TypeDesc[] params)
          Add a constructor to this class.
 MethodInfo addDefaultConstructor()
          Adds a public, no-arg constructor with the code buffer properly defined.
 FieldInfo addField(Modifiers modifiers, String fieldName, TypeDesc type)
          Add a field to this class.
 MethodInfo addInitializer()
          Add a static initializer to this class.
 ClassFile addInnerClass(String fullInnerClassName, String innerClassName)
          Add an inner class to this class.
 ClassFile addInnerClass(String fullInnerClassName, String innerClassName, Class superClass)
          Add an inner class to this class.
 ClassFile addInnerClass(String fullInnerClassName, String innerClassName, String superClassName)
          Add an inner class to this class.
 void addInterface(Class i)
          Add an interface that this class implements.
 void addInterface(String interfaceName)
          Add an interface that this class implements.
 MethodInfo addMethod(Method method)
          Add a method to this class.
 MethodInfo addMethod(Modifiers modifiers, String methodName, MethodDesc md)
          Add a method to this class.
 MethodInfo addMethod(Modifiers modifiers, String methodName, TypeDesc ret, TypeDesc[] params)
          Add a method to this class.
 MethodInfo addMethod(String declaration)
          Add a method to this class by declaration.
 Annotation addRuntimeInvisibleAnnotation(TypeDesc type)
          Add a runtime invisible annotation.
 Annotation addRuntimeVisibleAnnotation(TypeDesc type)
          Add a runtime visible annotation.
 Attribute[] getAttributes()
           
 int getClassDepth()
          Returns a value indicating how deeply nested an inner class is with respect to its outermost enclosing class.
 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.
 String getInnerClassName()
          If this ClassFile represents a non-anonymous inner class, returns its short inner class name.
 String[] getInterfaces()
          Returns the names of all the interfaces that this class implements.
 int getMajorVersion()
          Returns the major version number of the classfile format.
 MethodInfo[] getMethods()
          Returns all the methods defined in this class, not including constructors and static initializers.
 int getMinorVersion()
          Returns the minor version number of the classfile format.
 Modifiers getModifiers()
           
 ClassFile getOuterClass()
          Returns null if this ClassFile does not represent an inner class.
 Annotation[] getRuntimeInvisibleAnnotations()
          Returns all the runtime invisible annotations defined for this class file, or an empty array if none.
 Annotation[] getRuntimeVisibleAnnotations()
          Returns all the runtime visible annotations defined for this class file, or an empty array if none.
 SignatureAttr getSignatureAttr()
          Returns the signature attribute of this classfile, or null if none is defined.
 String getSourceFile()
          Returns the source file of this class file or null if not set.
 String getSuperClassName()
           
 String getTarget()
          Returns the target virtual machine version, or null if unknown.
 TypeDesc getType()
          Returns a TypeDesc 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(DataInput din)
          Reads a ClassFile from the given DataInput.
static ClassFile readFrom(DataInput din, ClassFileDataLoader loader, AttributeFactory attrFactory)
          Reads a ClassFile from the given DataInput.
static ClassFile readFrom(InputStream in)
          Reads a ClassFile from the given InputStream.
static ClassFile readFrom(InputStream in, ClassFileDataLoader loader, AttributeFactory attrFactory)
          Reads a ClassFile from the given InputStream.
 void setModifiers(Modifiers modifiers)
           
 void setSourceFile(String fileName)
          Set the source file of this class file by adding a source file attribute.
 void setTarget(String target)
          Specify what target virtual machine version classfile should generate for.
 void setVersion(int major, int minor)
          Sets the version to use when writing the generated classfile, overriding the target.
 String toString()
           
 void writeTo(DataOutput dout)
          Writes the ClassFile to the given DataOutput.
 void writeTo(OutputStream out)
          Writes the ClassFile to the given OutputStream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ClassFile

public ClassFile(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 setModifiers(org.cojen.classfile.Modifiers) method to change the access modifiers of this class or to turn it into an interface.

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

ClassFile

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

Use the setModifiers(org.cojen.classfile.Modifiers) method to change the access modifiers of this class or to turn it into an interface.

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

ClassFile

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

Use the setModifiers(org.cojen.classfile.Modifiers) method to change the access modifiers of 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

readFrom

public static ClassFile readFrom(InputStream in)
                          throws 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:
IOException - for I/O error or if classfile is invalid.
ArrayIndexOutOfBoundsException - if a constant pool index is out of range.
ClassCastException - if a constant pool index references the wrong type.

readFrom

public static ClassFile readFrom(DataInput din)
                          throws 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:
IOException - for I/O error or if classfile is invalid.
ArrayIndexOutOfBoundsException - if a constant pool index is out of range.
ClassCastException - if a constant pool index references the wrong type.

readFrom

public static ClassFile readFrom(InputStream in,
                                 ClassFileDataLoader loader,
                                 AttributeFactory attrFactory)
                          throws 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:
IOException - for I/O error or if classfile is invalid.
ArrayIndexOutOfBoundsException - if a constant pool index is out of range.
ClassCastException - if a constant pool index references the wrong type.

readFrom

public static ClassFile readFrom(DataInput din,
                                 ClassFileDataLoader loader,
                                 AttributeFactory attrFactory)
                          throws 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:
IOException - for I/O error or if classfile is invalid.
ArrayIndexOutOfBoundsException - if a constant pool index is out of range.
ClassCastException - if a constant pool index references the wrong type.

getClassName

public String getClassName()

getSuperClassName

public String getSuperClassName()

getType

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


getModifiers

public Modifiers getModifiers()

getInterfaces

public 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 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 String getSourceFile()
Returns the source file of this class file or null if not set.


isSynthetic

public boolean isSynthetic()

isDeprecated

public boolean isDeprecated()

getRuntimeInvisibleAnnotations

public Annotation[] getRuntimeInvisibleAnnotations()
Returns all the runtime invisible annotations defined for this class file, or an empty array if none.


getRuntimeVisibleAnnotations

public Annotation[] getRuntimeVisibleAnnotations()
Returns all the runtime visible annotations defined for this class file, or an empty array if none.


addRuntimeInvisibleAnnotation

public Annotation addRuntimeInvisibleAnnotation(TypeDesc type)
Add a runtime invisible annotation.


addRuntimeVisibleAnnotation

public Annotation addRuntimeVisibleAnnotation(TypeDesc type)
Add a runtime visible annotation.


getSignatureAttr

public SignatureAttr getSignatureAttr()
Returns the signature attribute of this classfile, or null if none is defined.


getConstantPool

public ConstantPool getConstantPool()
Provides access to the ClassFile's ContantPool.

Returns:
The constant pool for this class file.

setModifiers

public void setModifiers(Modifiers modifiers)

addInterface

public void addInterface(String interfaceName)
Add an interface that this class implements.

Parameters:
interfaceName - Full interface name.

addInterface

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


addField

public FieldInfo addField(Modifiers modifiers,
                          String fieldName,
                          TypeDesc type)
Add a field to this class.


addMethod

public MethodInfo addMethod(Modifiers modifiers,
                            String methodName,
                            TypeDesc ret,
                            TypeDesc[] 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(Modifiers modifiers,
                            String methodName,
                            MethodDesc md)
Add a method to this class.


addMethod

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


addMethod

public MethodInfo addMethod(String declaration)
Add a method to this class by declaration.

Throws:
IllegalArgumentException - if declaration syntax is wrong
See Also:
MethodDeclarationParser

addConstructor

public MethodInfo addConstructor(Modifiers modifiers,
                                 TypeDesc[] params)
Add a constructor to this class.

Parameters:
params - May be null if constructor accepts no parameters.

addDefaultConstructor

public MethodInfo addDefaultConstructor()
Adds a public, no-arg constructor with the code buffer properly defined.


addInitializer

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


addInnerClass

public ClassFile addInnerClass(String fullInnerClassName,
                               String innerClassName)
Add an inner class to this class. By default, inner classes are private static.

Parameters:
fullInnerClassName - Optional full inner class name.
innerClassName - Optional short inner class name.

addInnerClass

public ClassFile addInnerClass(String fullInnerClassName,
                               String innerClassName,
                               Class superClass)
Add an inner class to this class. By default, inner classes are private static.

Parameters:
fullInnerClassName - Optional full inner class name.
innerClassName - Optional short inner class name.
superClass - Super class.

addInnerClass

public ClassFile addInnerClass(String fullInnerClassName,
                               String innerClassName,
                               String superClassName)
Add an inner class to this class. By default, inner classes are private static.

Parameters:
fullInnerClassName - Optional full inner class name.
innerClassName - Optional short inner class name.
superClassName - Full super class name.

setSourceFile

public void setSourceFile(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()

setTarget

public void setTarget(String target)
               throws IllegalArgumentException
Specify what target virtual machine version classfile should generate for. Calling this method changes the major and minor version of the classfile format.

Parameters:
target - VM version, 1.0, 1.1, etc.
Throws:
IllegalArgumentException - if target is not supported

getTarget

public String getTarget()
Returns the target virtual machine version, or null if unknown.


setVersion

public void setVersion(int major,
                       int minor)
Sets the version to use when writing the generated classfile, overriding the target.


getMajorVersion

public int getMajorVersion()
Returns the major version number of the classfile format.


getMinorVersion

public int getMinorVersion()
Returns the minor version number of the classfile format.


writeTo

public void writeTo(OutputStream out)
             throws IOException
Writes the ClassFile to the given OutputStream.

Throws:
IOException

writeTo

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

Throws:
IOException

toString

public String toString()
Overrides:
toString in class Object


Copyright © 2004-2011 Brian S O'Neill. All Rights Reserved.