Class ClassFile
- java.lang.Object
-
- javassist.bytecode.ClassFile
-
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; }
-
-
Field Summary
Fields Modifier and Type Field Description static int
JAVA_1
The major version number of class files for JDK 1.1.static int
JAVA_10
The major version number of class files for JDK 10.static int
JAVA_11
The major version number of class files for JDK 11.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
JAVA_9
The major version number of class files for JDK 1.9.static int
MAJOR_VERSION
The major version number of class files created from scratch.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method 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<AttributeInfo>
getAttributes()
Returns all the attributes.ConstPool
getConstPool()
Returns a constant pool table.java.util.List<FieldInfo>
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<MethodInfo>
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<java.lang.String,java.lang.String> 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.AttributeInfo
removeAttribute(java.lang.String name)
Removes an attribute with the specified name.void
renameClass(java.lang.String oldname, java.lang.String newname)
Replaces all occurrences of a class name in the class file.void
renameClass(java.util.Map<java.lang.String,java.lang.String> classnames)
Replaces all occurrences of several class names 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.
-
-
-
Field Detail
-
JAVA_1
public static final int JAVA_1
The major version number of class files for JDK 1.1.- See Also:
- Constant Field Values
-
JAVA_2
public static final int JAVA_2
The major version number of class files for JDK 1.2.- See Also:
- Constant Field Values
-
JAVA_3
public static final int JAVA_3
The major version number of class files for JDK 1.3.- See Also:
- Constant Field Values
-
JAVA_4
public static final int JAVA_4
The major version number of class files for JDK 1.4.- See Also:
- Constant Field Values
-
JAVA_5
public static final int JAVA_5
The major version number of class files for JDK 1.5.- See Also:
- Constant Field Values
-
JAVA_6
public static final int JAVA_6
The major version number of class files for JDK 1.6.- See Also:
- Constant Field Values
-
JAVA_7
public static final int JAVA_7
The major version number of class files for JDK 1.7.- See Also:
- Constant Field Values
-
JAVA_8
public static final int JAVA_8
The major version number of class files for JDK 1.8.- See Also:
- Constant Field Values
-
JAVA_9
public static final int JAVA_9
The major version number of class files for JDK 1.9.- See Also:
- Constant Field Values
-
JAVA_10
public static final int JAVA_10
The major version number of class files for JDK 10.- See Also:
- Constant Field Values
-
JAVA_11
public static final int JAVA_11
The major version number of class files for JDK 11.- See Also:
- Constant Field Values
-
MAJOR_VERSION
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 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 Detail
-
ClassFile
public ClassFile(java.io.DataInputStream in) throws java.io.IOException
Constructs a class file from a byte stream.- Throws:
java.io.IOException
-
ClassFile
public ClassFile(boolean isInterface, java.lang.String classname, java.lang.String superclass)
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 Detail
-
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
public ConstPool 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:
AccessFlag
-
setAccessFlags
public void setAccessFlags(int acc)
Changes access flags.- See Also:
AccessFlag
-
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
public java.lang.String getName()
Returns the class name.
-
setName
public void setName(java.lang.String name)
Sets the class name. This method substitutes the new name for all occurrences of the old class name in the class file.
-
getSuperclass
public java.lang.String getSuperclass()
Returns the super class name.
-
getSuperclassId
public int getSuperclassId()
Returns the index of the constant pool entry representing the super class.
-
setSuperclass
public void setSuperclass(java.lang.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.
- Throws:
CannotCompileException
-
renameClass
public final void renameClass(java.lang.String oldname, java.lang.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.
- Parameters:
oldname
- the replaced class namenewname
- the substituted class name
-
renameClass
public final void renameClass(java.util.Map<java.lang.String,java.lang.String> classnames)
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:
renameClass(String,String)
-
getRefClasses
public final void getRefClasses(java.util.Map<java.lang.String,java.lang.String> classnames)
Internal-use only.CtClass.getRefClasses()
calls this method.
-
getInterfaces
public java.lang.String[] getInterfaces()
Returns the names of the interfaces implemented by the class. The returned array is read only.
-
setInterfaces
public void setInterfaces(java.lang.String[] nameList)
Sets the interfaces.- Parameters:
nameList
- the names of the interfaces.
-
addInterface
public void addInterface(java.lang.String name)
Appends an interface to the interfaces implemented by the class.
-
getFields
public java.util.List<FieldInfo> getFields()
Returns all the fields declared in the class.- Returns:
- a list of
FieldInfo
. - See Also:
FieldInfo
-
addField
public void addField(FieldInfo finfo) throws DuplicateMemberException
Appends a field to the class.- Throws:
DuplicateMemberException
- when the field is already included.
-
addField2
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.- Since:
- 3.13
-
getMethods
public java.util.List<MethodInfo> getMethods()
Returns all the methods declared in the class.- Returns:
- a list of
MethodInfo
. - See Also:
MethodInfo
-
getMethod
public MethodInfo getMethod(java.lang.String name)
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
public MethodInfo getStaticInitializer()
Returns a static initializer (class initializer), or null if it does not exist.
-
addMethod
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.- Throws:
DuplicateMemberException
- when the method is already included.
-
addMethod2
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.- Since:
- 3.13
-
getAttributes
public java.util.List<AttributeInfo> 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:
AttributeInfo
-
getAttribute
public AttributeInfo getAttribute(java.lang.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,
AnnotationsAttribute.visibleTag
orAnnotationsAttribute.invisibleTag
.- Parameters:
name
- attribute name- See Also:
getAttributes()
-
removeAttribute
public AttributeInfo removeAttribute(java.lang.String name)
Removes an attribute with the specified name.- Parameters:
name
- attribute name.- Returns:
- the removed attribute or null.
- Since:
- 3.21
-
addAttribute
public void addAttribute(AttributeInfo info)
Appends an attribute. If there is already an attribute with the same name, the new one substitutes for it.- See Also:
getAttributes()
-
getSourceFile
public java.lang.String getSourceFile()
Returns the source file containing this class.- Returns:
- null if this information is not available.
-
write
public void write(java.io.DataOutputStream out) throws java.io.IOException
Writes a class file represented by this object into an output stream.- Throws:
java.io.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.
-
-