public class ClassFile extends Object
See The Java Virtual Machine Specification for information on how class files are structured.
Constructor and Description |
---|
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.
|
Modifier and Type | Method and Description |
---|---|
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.
|
public ClassFile(String className)
Use the setModifiers(org.cojen.classfile.Modifiers)
method to change the access modifiers of
this class or to turn it into an interface.
className
- Full class name of the form ex: "java.lang.String".public ClassFile(String className, Class superClass)
Use the setModifiers(org.cojen.classfile.Modifiers)
method to change the access modifiers of
this class or to turn it into an interface.
className
- Full class name of the form ex: "java.lang.String".superClass
- Super class or interface.public ClassFile(String className, String superClassName)
Use the setModifiers(org.cojen.classfile.Modifiers)
method to change the access modifiers of
this class or to turn it into an interface.
className
- Full class name of the form ex: "java.lang.String".superClassName
- Full super class name.public static ClassFile readFrom(InputStream in) throws IOException
in
- source of class file dataIOException
- 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.public static ClassFile readFrom(DataInput din) throws IOException
din
- source of class file dataIOException
- 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.public static ClassFile readFrom(InputStream in, ClassFileDataLoader loader, AttributeFactory attrFactory) throws IOException
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.in
- source of class file dataloader
- optional loader for reading inner class definitionsattrFactory
- optional factory for reading custom attributesIOException
- 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.public static ClassFile readFrom(DataInput din, ClassFileDataLoader loader, AttributeFactory attrFactory) throws IOException
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.din
- source of class file dataloader
- optional loader for reading inner class definitionsattrFactory
- optional factory for reading custom attributesIOException
- 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.public String getClassName()
public String getSuperClassName()
public TypeDesc getType()
public Modifiers getModifiers()
public String[] getInterfaces()
public FieldInfo[] getFields()
public MethodInfo[] getMethods()
public MethodInfo[] getConstructors()
public MethodInfo getInitializer()
public ClassFile[] getInnerClasses()
public boolean isInnerClass()
public String getInnerClassName()
public ClassFile getOuterClass()
isInnerClass()
public int getClassDepth()
public String getSourceFile()
public boolean isSynthetic()
public boolean isDeprecated()
public Annotation[] getRuntimeInvisibleAnnotations()
public Annotation[] getRuntimeVisibleAnnotations()
public Annotation addRuntimeInvisibleAnnotation(TypeDesc type)
public Annotation addRuntimeVisibleAnnotation(TypeDesc type)
public SignatureAttr getSignatureAttr()
public ConstantPool getConstantPool()
public void setModifiers(Modifiers modifiers)
public void addInterface(String interfaceName)
interfaceName
- Full interface name.public void addInterface(Class i)
public FieldInfo addField(Modifiers modifiers, String fieldName, TypeDesc type)
public MethodInfo addMethod(Modifiers modifiers, String methodName, TypeDesc ret, TypeDesc[] params)
ret
- Is null if method returns void.params
- May be null if method accepts no parameters.public MethodInfo addMethod(Modifiers modifiers, String methodName, MethodDesc md)
public MethodInfo addMethod(Method method)
public MethodInfo addMethod(String declaration)
IllegalArgumentException
- if declaration syntax is wrongMethodDeclarationParser
public MethodInfo addConstructor(Modifiers modifiers, TypeDesc[] params)
params
- May be null if constructor accepts no parameters.public MethodInfo addDefaultConstructor()
public MethodInfo addInitializer()
public ClassFile addInnerClass(String fullInnerClassName, String innerClassName)
fullInnerClassName
- Optional full inner class name.innerClassName
- Optional short inner class name.public ClassFile addInnerClass(String fullInnerClassName, String innerClassName, Class superClass)
fullInnerClassName
- Optional full inner class name.innerClassName
- Optional short inner class name.superClass
- Super class.public ClassFile addInnerClass(String fullInnerClassName, String innerClassName, String superClassName)
fullInnerClassName
- Optional full inner class name.innerClassName
- Optional short inner class name.superClassName
- Full super class name.public void setSourceFile(String fileName)
public void markSynthetic()
public void markDeprecated()
public void addAttribute(Attribute attr)
public Attribute[] getAttributes()
public void setTarget(String target) throws IllegalArgumentException
target
- VM version, 1.0, 1.1, etc.IllegalArgumentException
- if target is not supportedpublic String getTarget()
public void setVersion(int major, int minor)
public int getMajorVersion()
public int getMinorVersion()
public void writeTo(OutputStream out) throws IOException
IOException
public void writeTo(DataOutput dout) throws IOException
IOException
Copyright © 2004-2015 Cojen. All Rights Reserved.