public final class ClassFile
extends java.lang.Object
ClassFile
represents a Java .class
file, which
consists of a constant pool, methods, fields, and attributes.
For example,
ClassFile cf = new ClassFile(false, "test.Foo", null); cf.setInterfaces(new String[] { "java.lang.Cloneable" }); FieldInfo f = new FieldInfo(cf.getConstPool(), "width", "I"); f.setAccessFlags(AccessFlag.PUBLIC); cf.addField(f); cf.write(new DataOutputStream(new FileOutputStream("Foo.class")));
This code generates a class file Foo.class
for the following class:
package test; class Foo implements Cloneable { public int width; }
Modifier and Type | Field and Description |
---|---|
static int |
JAVA_1
The major version number of class files
for JDK 1.1.
|
static int |
JAVA_2
The major version number of class files
for JDK 1.2.
|
static int |
JAVA_3
The major version number of class files
for JDK 1.3.
|
static int |
JAVA_4
The major version number of class files
for JDK 1.4.
|
static int |
JAVA_5
The major version number of class files
for JDK 1.5.
|
static int |
JAVA_6
The major version number of class files
for JDK 1.6.
|
static int |
JAVA_7
The major version number of class files
for JDK 1.7.
|
static int |
JAVA_8
The major version number of class files
for JDK 1.8.
|
static int |
MAJOR_VERSION
The major version number of class files created
from scratch.
|
Constructor and Description |
---|
ClassFile(boolean isInterface,
java.lang.String classname,
java.lang.String superclass)
Constructs a class file including no members.
|
ClassFile(java.io.DataInputStream in)
Constructs a class file from a byte stream.
|
Modifier and Type | Method and Description |
---|---|
void |
addAttribute(AttributeInfo info)
Appends an attribute.
|
void |
addField(FieldInfo finfo)
Appends a field to the class.
|
void |
addField2(FieldInfo finfo)
Just appends a field to the class.
|
void |
addInterface(java.lang.String name)
Appends an interface to the interfaces implemented by the class.
|
void |
addMethod(MethodInfo minfo)
Appends a method to the class.
|
void |
addMethod2(MethodInfo minfo)
Just appends a method to the class.
|
void |
compact()
Eliminates dead constant pool items.
|
int |
getAccessFlags()
Returns access flags.
|
AttributeInfo |
getAttribute(java.lang.String name)
Returns the attribute with the specified name.
|
java.util.List |
getAttributes()
Returns all the attributes.
|
ConstPool |
getConstPool()
Returns a constant pool table.
|
java.util.List |
getFields()
Returns all the fields declared in the class.
|
int |
getInnerAccessFlags()
Returns access and property flags of this nested class.
|
java.lang.String[] |
getInterfaces()
Returns the names of the interfaces implemented by the class.
|
int |
getMajorVersion()
Get the Major version.
|
MethodInfo |
getMethod(java.lang.String name)
Returns the method with the specified name.
|
java.util.List |
getMethods()
Returns all the methods declared in the class.
|
int |
getMinorVersion()
Get the minor version.
|
java.lang.String |
getName()
Returns the class name.
|
void |
getRefClasses(java.util.Map classnames)
Internal-use only.
|
java.lang.String |
getSourceFile()
Returns the source file containing this class.
|
MethodInfo |
getStaticInitializer()
Returns a static initializer (class initializer), or null if it does not
exist.
|
java.lang.String |
getSuperclass()
Returns the super class name.
|
int |
getSuperclassId()
Returns the index of the constant pool entry representing the super
class.
|
boolean |
isAbstract()
Returns true if this is an abstract class or an interface.
|
boolean |
isFinal()
Returns true if this is a final class or interface.
|
boolean |
isInterface()
Returns true if this is an interface.
|
void |
prune()
Discards all attributes, associated with both the class file and the
members such as a code attribute and exceptions attribute.
|
void |
renameClass(java.util.Map classnames)
Replaces all occurrences of several class names in the class file.
|
void |
renameClass(java.lang.String oldname,
java.lang.String newname)
Replaces all occurrences of a class name in the class file.
|
void |
setAccessFlags(int acc)
Changes access flags.
|
void |
setInterfaces(java.lang.String[] nameList)
Sets the interfaces.
|
void |
setMajorVersion(int major)
Set the major version.
|
void |
setMinorVersion(int minor)
Set the minor version.
|
void |
setName(java.lang.String name)
Sets the class name.
|
void |
setSuperclass(java.lang.String superclass)
Sets the super class.
|
void |
setVersionToJava5()
Sets the major and minor version to Java 5.
|
void |
write(java.io.DataOutputStream out)
Writes a class file represented by this object into an output stream.
|
public static final int JAVA_1
public static final int JAVA_2
public static final int JAVA_3
public static final int JAVA_4
public static final int JAVA_5
public static final int JAVA_6
public static final int JAVA_7
public static final int JAVA_8
public static int MAJOR_VERSION
java.lang.StringBuilder
.
It is 50 (JDK 1.6)
if the JVM supports java.util.zip.DeflaterInputStream
.
It is 51 (JDK 1.7)
if the JVM supports java.lang.invoke.CallSite
.public ClassFile(java.io.DataInputStream in) throws java.io.IOException
java.io.IOException
public ClassFile(boolean isInterface, java.lang.String classname, java.lang.String superclass)
isInterface
- true if this is an interface. false if this is a class.classname
- a fully-qualified class namesuperclass
- a fully-qualified super class name or null.public void compact()
public void prune()
public ConstPool getConstPool()
public boolean isInterface()
public boolean isFinal()
public boolean isAbstract()
public int getAccessFlags()
AccessFlag
public void setAccessFlags(int acc)
AccessFlag
public int getInnerAccessFlags()
The returned value is obtained from inner_class_access_flags
of the entry representing this nested class itself
in InnerClasses_attribute
.
public java.lang.String getName()
public void setName(java.lang.String name)
public java.lang.String getSuperclass()
public int getSuperclassId()
public void setSuperclass(java.lang.String superclass) throws CannotCompileException
The new super class should inherit from the old super class. This method modifies constructors so that they call constructors declared in the new super class.
CannotCompileException
public final void renameClass(java.lang.String oldname, java.lang.String newname)
If class X is substituted for class Y in the class file, X and Y must have the same signature. If Y provides a method m(), X must provide it even if X inherits m() from the super class. If this fact is not guaranteed, the bytecode verifier may cause an error.
oldname
- the replaced class namenewname
- the substituted class namepublic final void renameClass(java.util.Map classnames)
classnames
- specifies which class name is replaced with which new name.
Class names must be described with the JVM-internal
representation like java/lang/Object
.renameClass(String,String)
public final void getRefClasses(java.util.Map classnames)
CtClass.getRefClasses()
calls this method.public java.lang.String[] getInterfaces()
public void setInterfaces(java.lang.String[] nameList)
nameList
- the names of the interfaces.public void addInterface(java.lang.String name)
public java.util.List getFields()
FieldInfo
.FieldInfo
public void addField(FieldInfo finfo) throws DuplicateMemberException
DuplicateMemberException
- when the field is already included.public final void addField2(FieldInfo finfo)
public java.util.List getMethods()
MethodInfo
.MethodInfo
public MethodInfo getMethod(java.lang.String name)
public MethodInfo getStaticInitializer()
public void addMethod(MethodInfo minfo) throws DuplicateMemberException
DuplicateMemberException
- when the method is already included.public final void addMethod2(MethodInfo minfo)
public java.util.List getAttributes()
List
object
is shared with this object. If you add a new attribute to the list,
the attribute is also added to the classs file represented by this
object. If you remove an attribute from the list, it is also removed
from the class file.AttributeInfo
objects.AttributeInfo
public AttributeInfo getAttribute(java.lang.String name)
name
- attribute namegetAttributes()
public void addAttribute(AttributeInfo info)
getAttributes()
public java.lang.String getSourceFile()
public void write(java.io.DataOutputStream out) throws java.io.IOException
java.io.IOException
public int getMajorVersion()
public void setMajorVersion(int major)
major
- the major versionpublic int getMinorVersion()
public void setMinorVersion(int minor)
minor
- the minor versionpublic void setVersionToJava5()
Javassist, a Java-bytecode translator toolkit.
Copyright (C) 1999- Shigeru Chiba. All Rights Reserved.