Class ClassFile
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; }
-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
The major version number of class files for JDK 1.1.static final int
The major version number of class files for JDK 10.static final int
The major version number of class files for JDK 11.static final int
The major version number of class files for JDK 1.2.static final int
The major version number of class files for JDK 1.3.static final int
The major version number of class files for JDK 1.4.static final int
The major version number of class files for JDK 1.5.static final int
The major version number of class files for JDK 1.6.static final int
The major version number of class files for JDK 1.7.static final int
The major version number of class files for JDK 1.8.static final int
The major version number of class files for JDK 1.9.static final int
The major version number of class files created from scratch. -
Constructor Summary
ConstructorDescriptionConstructs a class file including no members.Constructs a class file from a byte stream. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addAttribute
(AttributeInfo info) Appends an attribute.void
Appends a field to the class.final void
Just appends a field to the class.void
addInterface
(String name) Appends an interface to the interfaces implemented by the class.void
addMethod
(MethodInfo minfo) Appends a method to the class.final void
addMethod2
(MethodInfo minfo) Just appends a method to the class.void
compact()
Eliminates dead constant pool items.int
Returns access flags.getAttribute
(String name) Returns the attribute with the specified name.Returns all the attributes.Returns a constant pool table.Returns all the fields declared in the class.int
Returns access and property flags of this nested class.String[]
Returns the names of the interfaces implemented by the class.int
Get the Major version.Returns the method with the specified name.Returns all the methods declared in the class.int
Get the minor version.getName()
Returns the class name.final void
getRefClasses
(Map<String, String> classnames) Internal-use only.Returns the source file containing this class.Returns a static initializer (class initializer), or null if it does not exist.Returns the super class name.int
Returns the index of the constant pool entry representing the super class.boolean
Returns true if this is an abstract class or an interface.boolean
isFinal()
Returns true if this is a final class or interface.boolean
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.removeAttribute
(String name) Removes an attribute with the specified name.final void
renameClass
(String oldname, String newname) Replaces all occurrences of a class name in the class file.final void
renameClass
(Map<String, String> classnames) Replaces all occurrences of several class names in the class file.void
setAccessFlags
(int acc) Changes access flags.void
setInterfaces
(String[] nameList) Sets the interfaces.void
setMajorVersion
(int major) Set the major version.void
setMinorVersion
(int minor) Set the minor version.void
Sets the class name.void
setSuperclass
(String superclass) Sets the super class.void
Sets the major and minor version to Java 5.void
write
(DataOutputStream out) Writes a class file represented by this object into an output stream.
-
Field Details
-
JAVA_1
public static final int JAVA_1The major version number of class files for JDK 1.1.- See Also:
-
JAVA_2
public static final int JAVA_2The major version number of class files for JDK 1.2.- See Also:
-
JAVA_3
public static final int JAVA_3The major version number of class files for JDK 1.3.- See Also:
-
JAVA_4
public static final int JAVA_4The major version number of class files for JDK 1.4.- See Also:
-
JAVA_5
public static final int JAVA_5The major version number of class files for JDK 1.5.- See Also:
-
JAVA_6
public static final int JAVA_6The major version number of class files for JDK 1.6.- See Also:
-
JAVA_7
public static final int JAVA_7The major version number of class files for JDK 1.7.- See Also:
-
JAVA_8
public static final int JAVA_8The major version number of class files for JDK 1.8.- See Also:
-
JAVA_9
public static final int JAVA_9The major version number of class files for JDK 1.9.- See Also:
-
JAVA_10
public static final int JAVA_10The major version number of class files for JDK 10.- See Also:
-
JAVA_11
public static final int JAVA_11The major version number of class files for JDK 11.- See Also:
-
MAJOR_VERSION
public static final int MAJOR_VERSIONThe 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 supportsjava.lang.StringBuilder
. It is 50 (JDK 1.6) if the JVM supportsjava.util.zip.DeflaterInputStream
. It is 51 (JDK 1.7) if the JVM supportsjava.lang.invoke.CallSite
. It is 52 (JDK 1.8) if the JVM supportsjava.util.function.Function
. It is 53 (JDK 1.9) if the JVM supportsjava.lang.reflect.Module
. It is 54 (JDK 10) if the JVM supportsjava.util.List.copyOf(Collection)
. It is 55 (JDK 11) if the JVM supportsjava.util.Optional.isEmpty()
.
-
-
Constructor Details
-
ClassFile
Constructs a class file from a byte stream.- Throws:
IOException
-
ClassFile
Constructs a class file including no members.- Parameters:
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.
-
-
Method Details
-
compact
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. -
prune
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). -
getConstPool
Returns a constant pool table. -
isInterface
public boolean isInterface()Returns true if this is an interface. -
isFinal
public boolean isFinal()Returns true if this is a final class or interface. -
isAbstract
public boolean isAbstract()Returns true if this is an abstract class or an interface. -
getAccessFlags
public int getAccessFlags()Returns access flags.- See Also:
-
setAccessFlags
public void setAccessFlags(int acc) Changes access flags.- See Also:
-
getInnerAccessFlags
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
inner_class_access_flags
of the entry representing this nested class itself inInnerClasses_attribute
. -
getName
Returns the class name. -
setName
Sets the class name. This method substitutes the new name for all occurrences of the old class name in the class file. -
getSuperclass
Returns the super class name. -
getSuperclassId
public int getSuperclassId()Returns the index of the constant pool entry representing the super class. -
setSuperclass
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.
- Throws:
CannotCompileException
-
renameClass
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.
- Parameters:
oldname
- the replaced class namenewname
- the substituted class name
-
renameClass
Replaces all occurrences of several class names in the class file.- Parameters:
classnames
- specifies which class name is replaced with which new name. Class names must be described with the JVM-internal representation likejava/lang/Object
.- See Also:
-
getRefClasses
Internal-use only.CtClass.getRefClasses()
calls this method. -
getInterfaces
Returns the names of the interfaces implemented by the class. The returned array is read only. -
setInterfaces
Sets the interfaces.- Parameters:
nameList
- the names of the interfaces.
-
addInterface
Appends an interface to the interfaces implemented by the class. -
getFields
Returns all the fields declared in the class.- Returns:
- a list of
FieldInfo
. - See Also:
-
addField
Appends a field to the class.- Throws:
DuplicateMemberException
- when the field is already included.
-
addField2
Just appends a field to the class. It does not check field duplication. Use this method only when minimizing performance overheads is seriously required.- Since:
- 3.13
-
getMethods
Returns all the methods declared in the class.- Returns:
- a list of
MethodInfo
. - See Also:
-
getMethod
Returns the method with the specified name. If there are multiple methods with that name, this method returns one of them.- Returns:
- null if no such method is found.
-
getStaticInitializer
Returns a static initializer (class initializer), or null if it does not exist. -
addMethod
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.- Throws:
DuplicateMemberException
- when the method is already included.
-
addMethod2
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.- Since:
- 3.13
-
getAttributes
Returns all the attributes. The returnedList
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.- Returns:
- a list of
AttributeInfo
objects. - See Also:
-
getAttribute
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,
AnnotationsAttribute.visibleTag
orAnnotationsAttribute.invisibleTag
.- Parameters:
name
- attribute name- See Also:
-
removeAttribute
Removes an attribute with the specified name.- Parameters:
name
- attribute name.- Returns:
- the removed attribute or null.
- Since:
- 3.21
-
addAttribute
Appends an attribute. If there is already an attribute with the same name, the new one substitutes for it.- See Also:
-
getSourceFile
Returns the source file containing this class.- Returns:
- null if this information is not available.
-
write
Writes a class file represented by this object into an output stream.- Throws:
IOException
-
getMajorVersion
public int getMajorVersion()Get the Major version.- Returns:
- the major version
-
setMajorVersion
public void setMajorVersion(int major) Set the major version.- Parameters:
major
- the major version
-
getMinorVersion
public int getMinorVersion()Get the minor version.- Returns:
- the minor version
-
setMinorVersion
public void setMinorVersion(int minor) Set the minor version.- Parameters:
minor
- the minor version
-
setVersionToJava5
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.
-