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;
}
FieldInfo, MethodInfo, ClassFileWriter, javassist.CtClass#getClassFile() , javassist.ClassPool#makeClass(ClassFile)
| Modifier and Type | Field and Description |
|---|---|
| pack-priv int | |
| pack-priv List | |
| pack-priv String[] | |
| pack-priv String | |
| pack-priv ConstPool | |
| pack-priv List | |
| pack-priv int[] | |
| public static final int | JAVA_1
The major version number of class files for JDK 1.1. |
| public static final int | JAVA_10
The major version number of class files for JDK 10. |
| public static final int | JAVA_11
The major version number of class files for JDK 11. |
| public static final int | JAVA_2
The major version number of class files for JDK 1.2. |
| public static final int | JAVA_3
The major version number of class files for JDK 1.3. |
| public static final int | JAVA_4
The major version number of class files for JDK 1.4. |
| public static final int | JAVA_5
The major version number of class files for JDK 1.5. |
| public static final int | JAVA_6
The major version number of class files for JDK 1.6. |
| public static final int | JAVA_7
The major version number of class files for JDK 1.7. |
| public static final int | JAVA_8
The major version number of class files for JDK 1.8. |
| public static final int | JAVA_9
The major version number of class files for JDK 1.9. |
| pack-priv int | |
| public static final int | MAJOR_VERSION
The major version number of class files created from scratch. |
| pack-priv List | |
| pack-priv int | |
| pack-priv int | |
| pack-priv int | |
| pack-priv String |
| Access | Constructor and Description |
|---|---|
| public | |
| public |
| Modifier and Type | Method and Description |
|---|---|
| public void | |
| public void | |
| public final void | |
| public void | |
| public void | |
| public final void | |
| public void | |
| private ConstPool | |
| public int | |
| public AttributeInfo | |
| public List | |
| public ConstPool | |
| public List | |
| public int | |
| public String[] | |
| public int | |
| public MethodInfo | |
| public List | |
| public int | |
| public String | |
| public final void | |
| public String | Returns: null if this information is not available.Returns the source file containing this class. |
| private static String | |
| public MethodInfo | getStaticInitializer()
Returns a static initializer (class initializer), or null if it does not exist. |
| public String | |
| public int | |
| private void | |
| public boolean | |
| private static boolean | isDuplicated(MethodInfo newMethod, String newName, String newDesc, MethodInfo minfo, ListIterator<MethodInfo> it)
|
| public boolean | |
| public boolean | |
| private static boolean | |
| public void | prune()
Discards all attributes, associated with both the class file and the members such as a code attribute and exceptions attribute. |
| private void | |
| public AttributeInfo | Returns: the removed attribute or null.attribute name. name)Removes an attribute with the specified name. |
| public final void | renameClass(String
the replaced class name oldname, String the substituted class name newname)Replaces all occurrences of a class name in the class file. |
| public final void | renameClass(Map<String, String>
specifies which class name is replaced with which new name.
Class names must be described with the JVM-internal
representation like classnames)java/lang/Object.Replaces all occurrences of several class names in the class file. |
| public void | |
| public void | |
| public void | |
| public void | |
| public void | |
| public void | |
| public void | |
| private void | |
| private void | |
| public void |
| accessFlags | back to summary |
|---|---|
| pack-priv int accessFlags | |
| attributes | back to summary |
|---|---|
| pack-priv List<AttributeInfo> attributes | |
| cachedInterfaces | back to summary |
|---|---|
| pack-priv String[] cachedInterfaces | |
| cachedSuperclass | back to summary |
|---|---|
| pack-priv String cachedSuperclass | |
| constPool | back to summary |
|---|---|
| pack-priv ConstPool constPool | |
| fields | back to summary |
|---|---|
| pack-priv List<FieldInfo> fields | |
| interfaces | back to summary |
|---|---|
| pack-priv int[] interfaces | |
| JAVA_1 | back to summary |
|---|---|
| public static final int JAVA_1 The major version number of class files for JDK 1.1. | |
| JAVA_10 | back to summary |
|---|---|
| public static final int JAVA_10 The major version number of class files for JDK 10. | |
| JAVA_11 | back to summary |
|---|---|
| public static final int JAVA_11 The major version number of class files for JDK 11. | |
| JAVA_2 | back to summary |
|---|---|
| public static final int JAVA_2 The major version number of class files for JDK 1.2. | |
| JAVA_3 | back to summary |
|---|---|
| public static final int JAVA_3 The major version number of class files for JDK 1.3. | |
| JAVA_4 | back to summary |
|---|---|
| public static final int JAVA_4 The major version number of class files for JDK 1.4. | |
| JAVA_5 | back to summary |
|---|---|
| public static final int JAVA_5 The major version number of class files for JDK 1.5. | |
| JAVA_6 | back to summary |
|---|---|
| public static final int JAVA_6 The major version number of class files for JDK 1.6. | |
| JAVA_7 | back to summary |
|---|---|
| public static final int JAVA_7 The major version number of class files for JDK 1.7. | |
| JAVA_8 | back to summary |
|---|---|
| public static final int JAVA_8 The major version number of class files for JDK 1.8. | |
| JAVA_9 | back to summary |
|---|---|
| public static final int JAVA_9 The major version number of class files for JDK 1.9. | |
| major | back to summary |
|---|---|
| pack-priv int major | |
| MAJOR_VERSION | back to summary |
|---|---|
| public static final int MAJOR_VERSION The major version number of class files created
from scratch. The default value is 47 (JDK 1.3).
It is 49 (JDK 1.5)
if the JVM supports | |
| methods | back to summary |
|---|---|
| pack-priv List<MethodInfo> methods | |
| minor | back to summary |
|---|---|
| pack-priv int minor | |
| superClass | back to summary |
|---|---|
| pack-priv int superClass | |
| thisClass | back to summary |
|---|---|
| pack-priv int thisClass | |
| thisclassname | back to summary |
|---|---|
| pack-priv String thisclassname | |
| ClassFile | back to summary |
|---|---|
| public ClassFile(DataInputStream in) throws IOException Constructs a class file from a byte stream. | |
| ClassFile | back to summary |
|---|---|
| public ClassFile(boolean isInterface, String classname, String superclass) Constructs a class file including no members. | |
| addAttribute | back to summary |
|---|---|
| public void addAttribute(AttributeInfo info) Appends an attribute. If there is already an attribute with the same name, the new one substitutes for it.
| |
| addField | back to summary |
|---|---|
| public void addField(FieldInfo finfo) throws DuplicateMemberException Appends a field to the class.
| |
| addField2 | back to summary |
|---|---|
| public final void addField2(FieldInfo finfo) Just appends a field to the class. It does not check field duplication. Use this method only when minimizing performance overheads is seriously required.
| |
| addInterface | back to summary |
|---|---|
| public void addInterface(String name) Appends an interface to the interfaces implemented by the class. | |
| addMethod | back to summary |
|---|---|
| public void addMethod(MethodInfo minfo) throws DuplicateMemberException Appends a method to the class. If there is a bridge method with the same name and signature, then the bridge method is removed before a new method is added.
| |
| addMethod2 | back to summary |
|---|---|
| public final void addMethod2(MethodInfo minfo) Just appends a method to the class. It does not check method duplication or remove a bridge method. Use this method only when minimizing performance overheads is seriously required.
| |
| compact | back to summary |
|---|---|
| public void compact() Eliminates dead constant pool items. If a method or a field is removed, the constant pool items used by that method/field become dead items. This method recreates a constant pool. | |
| compact0 | back to summary |
|---|---|
| private ConstPool compact0() | |
| getAccessFlags | back to summary |
|---|---|
| public int getAccessFlags() Returns access flags.
| |
| getAttribute | back to summary |
|---|---|
| public AttributeInfo getAttribute(String name) Returns the attribute with the specified name. If there are multiple attributes with that name, this method returns either of them. It returns null if the specified attributed is not found. An attribute name can be obtained by, for example,
| |
| getAttributes | back to summary |
|---|---|
| public List Returns all the attributes. The returned
| |
| getConstPool | back to summary |
|---|---|
| public ConstPool getConstPool() Returns a constant pool table. | |
| getFields | back to summary |
|---|---|
| public List Returns all the fields declared in the class. | |
| getInnerAccessFlags | back to summary |
|---|---|
| public int getInnerAccessFlags() Returns access and property flags of this nested class. This method returns -1 if the class is not a nested class. The returned value is obtained from | |
| getInterfaces | back to summary |
|---|---|
| public String[] getInterfaces() Returns the names of the interfaces implemented by the class. The returned array is read only. | |
| getMajorVersion | back to summary |
|---|---|
| public int getMajorVersion() Get the Major version.
| |
| getMethod | back to summary |
|---|---|
| public MethodInfo getMethod(String name) Returns the method with the specified name. If there are multiple methods with that name, this method returns one of them.
| |
| getMethods | back to summary |
|---|---|
| public List Returns all the methods declared in the class.
| |
| getMinorVersion | back to summary |
|---|---|
| public int getMinorVersion() Get the minor version.
| |
| getName | back to summary |
|---|---|
| public String getName() Returns the class name. | |
| getRefClasses | back to summary |
|---|---|
| public final void getRefClasses(Map<String, String> classnames) Internal-use only.
| |
| getSourceFile | back to summary |
|---|---|
| public String getSourceFile() Returns the source file containing this class.
| |
| getSourcefileName | back to summary |
|---|---|
| private static String getSourcefileName(String qname) | |
| getStaticInitializer | back to summary |
|---|---|
| public MethodInfo getStaticInitializer() Returns a static initializer (class initializer), or null if it does not exist. | |
| getSuperclass | back to summary |
|---|---|
| public String getSuperclass() Returns the super class name. | |
| getSuperclassId | back to summary |
|---|---|
| public int getSuperclassId() Returns the index of the constant pool entry representing the super class. | |
| initSuperclass | back to summary |
|---|---|
| private void initSuperclass(String superclass) | |
| isAbstract | back to summary |
|---|---|
| public boolean isAbstract() Returns true if this is an abstract class or an interface. | |
| isDuplicated | back to summary |
|---|---|
| private static boolean isDuplicated(MethodInfo newMethod, String newName, String newDesc, MethodInfo minfo, ListIterator<MethodInfo> it) | |
| isFinal | back to summary |
|---|---|
| public boolean isFinal() Returns true if this is a final class or interface. | |
| isInterface | back to summary |
|---|---|
| public boolean isInterface() Returns true if this is an interface. | |
| notBridgeMethod | back to summary |
|---|---|
| private static boolean notBridgeMethod(MethodInfo minfo) | |
| prune | back to summary |
|---|---|
| public void prune() Discards all attributes, associated with both the class file and the members such as a code attribute and exceptions attribute. The unused constant pool entries are also discarded (a new packed constant pool is constructed). | |
| read | back to summary |
|---|---|
| private void read(DataInputStream in) throws IOException | |
| removeAttribute | back to summary |
|---|---|
| public AttributeInfo removeAttribute(String name) Removes an attribute with the specified name.
| |
| renameClass | back to summary |
|---|---|
| public final void renameClass(String oldname, String newname) Replaces all occurrences of a class name in the class file. 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. | |
| renameClass | back to summary |
|---|---|
| public final void renameClass(Map<String, String> classnames) Replaces all occurrences of several class names in the class file.
| |
| setAccessFlags | back to summary |
|---|---|
| public void setAccessFlags(int acc) Changes access flags.
| |
| setInterfaces | back to summary |
|---|---|
| public void setInterfaces(String[] nameList) Sets the interfaces.
| |
| setMajorVersion | back to summary |
|---|---|
| public void setMajorVersion(int major) Set the major version.
| |
| setMinorVersion | back to summary |
|---|---|
| public void setMinorVersion(int minor) Set the minor version.
| |
| setName | back to summary |
|---|---|
| public void setName(String name) Sets the class name. This method substitutes the new name for all occurrences of the old class name in the class file. | |
| setSuperclass | back to summary |
|---|---|
| public void setSuperclass(String superclass) throws CannotCompileException Sets the super class. 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. | |
| setVersionToJava5 | back to summary |
|---|---|
| public void setVersionToJava5() Sets the major and minor version to Java 5. If the major version is older than 49, Java 5 extensions such as annotations are ignored by the JVM. | |
| testExistingField | back to summary |
|---|---|
| private void testExistingField(String name, String descriptor) throws DuplicateMemberException | |
| testExistingMethod | back to summary |
|---|---|
| private void testExistingMethod(MethodInfo newMinfo) throws DuplicateMemberException | |
| write | back to summary |
|---|---|
| public void write(DataOutputStream out) throws IOException Writes a class file represented by this object into an output stream. | |