| java.lang.Object org.cojen.classfile.ClassFile
ClassFile | public class ClassFile (Code) | | 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 | |
public | ClassFile(String className) By default, the ClassFile defines public, non-final, concrete classes. | public | ClassFile(String className, Class superClass) By default, the ClassFile defines public, non-final, concrete classes. | public | ClassFile(String className, String superClassName) By default, the ClassFile defines public, non-final, concrete classes. |
Method Summary | |
public void | addAttribute(Attribute attr) Add an attribute to this class. | public MethodInfo | addConstructor(Modifiers modifiers, TypeDesc[] params) Add a constructor to this class. | public MethodInfo | addDefaultConstructor() Adds a public, no-arg constructor with the code buffer properly defined. | public FieldInfo | addField(Modifiers modifiers, String fieldName, TypeDesc type) Add a field to this class. | public MethodInfo | addInitializer() Add a static initializer to this class. | public ClassFile | addInnerClass(String fullInnerClassName, String innerClassName) Add an inner class to this class. | public ClassFile | addInnerClass(String fullInnerClassName, String innerClassName, Class superClass) Add an inner class to this class. | public ClassFile | addInnerClass(String fullInnerClassName, String innerClassName, String superClassName) Add an inner class to this class. | public void | addInterface(String interfaceName) Add an interface that this class implements. | public void | addInterface(Class i) Add an interface that this class implements. | public MethodInfo | addMethod(Modifiers modifiers, String methodName, TypeDesc ret, TypeDesc[] params) Add a method to this class. | public MethodInfo | addMethod(Modifiers modifiers, String methodName, MethodDesc md) Add a method to this class. | public MethodInfo | addMethod(Method method) Add a method to this class. | public MethodInfo | addMethod(String declaration) Add a method to this class by declaration. | public Annotation | addRuntimeInvisibleAnnotation(TypeDesc type) Add a runtime invisible annotation. | public Annotation | addRuntimeVisibleAnnotation(TypeDesc type) Add a runtime visible annotation. | public Attribute[] | getAttributes() | public int | getClassDepth() Returns a value indicating how deeply nested an inner class is with
respect to its outermost enclosing class. | public String | getClassName() | public ConstantPool | getConstantPool() Provides access to the ClassFile's ContantPool. | public MethodInfo[] | getConstructors() Returns all the constructors defined in this class. | public FieldInfo[] | getFields() Returns all the fields defined in this class. | public MethodInfo | getInitializer() Returns the static initializer defined in this class or null if there
isn't one. | public String | getInnerClassName() If this ClassFile represents a non-anonymous inner class, returns its
short inner class name. | public ClassFile[] | getInnerClasses() Returns all the inner classes defined in this class. | public String[] | getInterfaces() Returns the names of all the interfaces that this class implements. | public int | getMajorVersion() Returns the major version number of the classfile format. | public MethodInfo[] | getMethods() Returns all the methods defined in this class, not including
constructors and static initializers. | public int | getMinorVersion() Returns the minor version number of the classfile format. | public Modifiers | getModifiers() | public ClassFile | getOuterClass() Returns null if this ClassFile does not represent an inner class. | public Annotation[] | getRuntimeInvisibleAnnotations() Returns all the runtime invisible annotations defined for this class
file, or an empty array if none. | public Annotation[] | getRuntimeVisibleAnnotations() Returns all the runtime visible annotations defined for this class file,
or an empty array if none. | public SignatureAttr | getSignatureAttr() Returns the signature attribute of this classfile, or null if none is
defined. | public String | getSourceFile() Returns the source file of this class file or null if not set. | public String | getSuperClassName() | public String | getTarget() Returns the target virtual machine version, or null if unknown. | public TypeDesc | getType() Returns a TypeDesc for the type of this ClassFile. | public boolean | isDeprecated() | public boolean | isInnerClass() Returns true if this ClassFile represents an inner class. | public boolean | isSynthetic() | public void | markDeprecated() Mark this class as being deprecated by adding a special attribute. | public void | markSynthetic() Mark this class as being synthetic by adding a special attribute. | public static ClassFile | readFrom(InputStream in) Reads a ClassFile from the given InputStream. | public static ClassFile | readFrom(DataInput din) Reads a ClassFile from the given DataInput. | public static ClassFile | readFrom(InputStream in, ClassFileDataLoader loader, AttributeFactory attrFactory) Reads a ClassFile from the given InputStream. | public static ClassFile | readFrom(DataInput din, ClassFileDataLoader loader, AttributeFactory attrFactory) Reads a ClassFile from the given DataInput. | public void | setModifiers(Modifiers modifiers) | public void | setSourceFile(String fileName) Set the source file of this class file by adding a source file
attribute. | public void | setTarget(String target) Specify what target virtual machine version classfile should generate
for. | public void | setVersion(int major, int minor) Sets the version to use when writing the generated classfile, overriding
the target. | public String | toString() | public void | writeTo(OutputStream out) Writes the ClassFile to the given OutputStream. | public void | writeTo(DataOutput dout) Writes the ClassFile to the given DataOutput. |
ClassFile | public ClassFile(String className)(Code) | | 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
ClassFile.setModifiers 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)(Code) | | By default, the ClassFile defines public, non-final, concrete classes.
Use the
ClassFile.setModifiers 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". Parameters: superClass - Super class or interface. |
ClassFile | public ClassFile(String className, String superClassName)(Code) | | By default, the ClassFile defines public, non-final, concrete classes.
Use the
ClassFile.setModifiers 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". Parameters: superClassName - Full super class name. |
addAttribute | public void addAttribute(Attribute attr)(Code) | | Add an attribute to this class.
|
addConstructor | public MethodInfo addConstructor(Modifiers modifiers, TypeDesc[] params)(Code) | | Add a constructor to this class.
Parameters: params - May be null if constructor accepts no parameters. |
addDefaultConstructor | public MethodInfo addDefaultConstructor()(Code) | | Adds a public, no-arg constructor with the code buffer properly defined.
|
addInitializer | public MethodInfo addInitializer()(Code) | | Add a static initializer to this class.
|
addInnerClass | public ClassFile addInnerClass(String fullInnerClassName, String innerClassName)(Code) | | Add an inner class to this class. By default, inner classes are private
static.
Parameters: fullInnerClassName - Optional full inner class name. Parameters: innerClassName - Optional short inner class name. |
addInnerClass | public ClassFile addInnerClass(String fullInnerClassName, String innerClassName, Class superClass)(Code) | | Add an inner class to this class. By default, inner classes are private
static.
Parameters: fullInnerClassName - Optional full inner class name. Parameters: innerClassName - Optional short inner class name. Parameters: superClass - Super class. |
addInnerClass | public ClassFile addInnerClass(String fullInnerClassName, String innerClassName, String superClassName)(Code) | | Add an inner class to this class. By default, inner classes are private
static.
Parameters: fullInnerClassName - Optional full inner class name. Parameters: innerClassName - Optional short inner class name. Parameters: superClassName - Full super class name. |
addInterface | public void addInterface(String interfaceName)(Code) | | Add an interface that this class implements.
Parameters: interfaceName - Full interface name. |
addInterface | public void addInterface(Class i)(Code) | | Add an interface that this class implements.
|
addMethod | public MethodInfo addMethod(Modifiers modifiers, String methodName, TypeDesc ret, TypeDesc[] params)(Code) | | Add a method to this class.
Parameters: ret - Is null if method returns void. Parameters: params - May be null if method accepts no parameters. |
addMethod | public MethodInfo addMethod(Method method)(Code) | | Add a method to this class. This method is handy for implementing
methods defined by a pre-existing interface.
|
addRuntimeInvisibleAnnotation | public Annotation addRuntimeInvisibleAnnotation(TypeDesc type)(Code) | | Add a runtime invisible annotation.
|
addRuntimeVisibleAnnotation | public Annotation addRuntimeVisibleAnnotation(TypeDesc type)(Code) | | Add a runtime visible annotation.
|
getClassDepth | public int getClassDepth()(Code) | | 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.
|
getConstantPool | public ConstantPool getConstantPool()(Code) | | Provides access to the ClassFile's ContantPool.
The constant pool for this class file. |
getConstructors | public MethodInfo[] getConstructors()(Code) | | Returns all the constructors defined in this class.
|
getFields | public FieldInfo[] getFields()(Code) | | Returns all the fields defined in this class.
|
getInitializer | public MethodInfo getInitializer()(Code) | | Returns the static initializer defined in this class or null if there
isn't one.
|
getInnerClassName | public String getInnerClassName()(Code) | | If this ClassFile represents a non-anonymous inner class, returns its
short inner class name.
|
getInnerClasses | public ClassFile[] getInnerClasses()(Code) | | Returns all the inner classes defined in this class. If no inner classes
are defined, then an array of length zero is returned.
|
getInterfaces | public String[] getInterfaces()(Code) | | Returns the names of all the interfaces that this class implements.
|
getMajorVersion | public int getMajorVersion()(Code) | | Returns the major version number of the classfile format.
|
getMethods | public MethodInfo[] getMethods()(Code) | | Returns all the methods defined in this class, not including
constructors and static initializers.
|
getMinorVersion | public int getMinorVersion()(Code) | | Returns the minor version number of the classfile format.
|
getRuntimeInvisibleAnnotations | public Annotation[] getRuntimeInvisibleAnnotations()(Code) | | Returns all the runtime invisible annotations defined for this class
file, or an empty array if none.
|
getRuntimeVisibleAnnotations | public Annotation[] getRuntimeVisibleAnnotations()(Code) | | Returns all the runtime visible annotations defined for this class file,
or an empty array if none.
|
getSignatureAttr | public SignatureAttr getSignatureAttr()(Code) | | Returns the signature attribute of this classfile, or null if none is
defined.
|
getSourceFile | public String getSourceFile()(Code) | | Returns the source file of this class file or null if not set.
|
getTarget | public String getTarget()(Code) | | Returns the target virtual machine version, or null if unknown.
|
getType | public TypeDesc getType()(Code) | | Returns a TypeDesc for the type of this ClassFile.
|
isDeprecated | public boolean isDeprecated()(Code) | | |
isInnerClass | public boolean isInnerClass()(Code) | | Returns true if this ClassFile represents an inner class.
|
isSynthetic | public boolean isSynthetic()(Code) | | |
markDeprecated | public void markDeprecated()(Code) | | Mark this class as being deprecated by adding a special attribute.
|
markSynthetic | public void markSynthetic()(Code) | | Mark this class as being synthetic by adding a special attribute.
|
readFrom | public static ClassFile readFrom(InputStream in) throws IOException(Code) | | 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. throws: ArrayIndexOutOfBoundsException - if a constant pool index is outof range. throws: ClassCastException - if a constant pool index references thewrong type. |
readFrom | public static ClassFile readFrom(DataInput din) throws IOException(Code) | | 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. throws: ArrayIndexOutOfBoundsException - if a constant pool index is outof range. throws: ClassCastException - if a constant pool index references thewrong type. |
readFrom | public static ClassFile readFrom(InputStream in, ClassFileDataLoader loader, AttributeFactory attrFactory) throws IOException(Code) | | 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 Parameters: loader - optional loader for reading inner class definitions Parameters: attrFactory - optional factory for reading custom attributes throws: IOException - for I/O error or if classfile is invalid. throws: ArrayIndexOutOfBoundsException - if a constant pool index is outof range. throws: ClassCastException - if a constant pool index references thewrong type. |
readFrom | public static ClassFile readFrom(DataInput din, ClassFileDataLoader loader, AttributeFactory attrFactory) throws IOException(Code) | | 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 Parameters: loader - optional loader for reading inner class definitions Parameters: attrFactory - optional factory for reading custom attributes throws: IOException - for I/O error or if classfile is invalid. throws: ArrayIndexOutOfBoundsException - if a constant pool index is outof range. throws: ClassCastException - if a constant pool index references thewrong type. |
setSourceFile | public void setSourceFile(String fileName)(Code) | | 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".
|
setTarget | public void setTarget(String target) throws IllegalArgumentException(Code) | | 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 |
setVersion | public void setVersion(int major, int minor)(Code) | | Sets the version to use when writing the generated classfile, overriding
the target.
|
|
|