Top Description Fields Constructors Methods
javassist.bytecode

public final Class ClassFile

extends Object
Class Inheritance
Imports
java.io.DataInputStream, .DataOutputStream, .IOException, java.util.ArrayList, .Collection, .List, .ListIterator, .Map, javassist.CannotCompileException

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;
}
See Also
FieldInfo, MethodInfo, ClassFileWriter, javassist.CtClass#getClassFile(), javassist.ClassPool#makeClass(ClassFile)

Field Summary

Modifier and TypeField and Description
pack-priv int
pack-priv List<AttributeInfo>
pack-priv String[]
pack-priv String
pack-priv ConstPool
pack-priv List<FieldInfo>
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<MethodInfo>
pack-priv int
pack-priv int
pack-priv int
pack-priv String

Constructor Summary

AccessConstructor and Description
public
ClassFile(DataInputStream in)

Constructs a class file from a byte stream.

public
ClassFile(boolean
true if this is an interface. false if this is a class.
isInterface
,
String
a fully-qualified class name
classname
,
String
a fully-qualified super class name or null.
superclass
)

Constructs a class file including no members.

Method Summary

Modifier and TypeMethod and Description
public void
addAttribute(AttributeInfo info)

Appends an attribute.

public void
addField(FieldInfo finfo)

Appends a field to the class.

public final void
addField2(FieldInfo finfo)

Just appends a field to the class.

public void
addInterface(String name)

Appends an interface to the interfaces implemented by the class.

public void
addMethod(MethodInfo minfo)

Appends a method to the class.

public final void
addMethod2(MethodInfo minfo)

Just appends a method to the class.

public void
compact()

Eliminates dead constant pool items.

private ConstPool
public int
getAccessFlags()

Returns access flags.

public AttributeInfo
getAttribute(String
attribute name
name
)

Returns the attribute with the specified name.

public List<AttributeInfo>

Returns:

a list of AttributeInfo objects.
getAttributes
()

Returns all the attributes.

public ConstPool
getConstPool()

Returns a constant pool table.

public List<FieldInfo>

Returns:

a list of FieldInfo.
getFields
()

Returns all the fields declared in the class.

public int
getInnerAccessFlags()

Returns access and property flags of this nested class.

public String[]
getInterfaces()

Returns the names of the interfaces implemented by the class.

public int

Returns:

the major version
getMajorVersion
()

Get the Major version.

public MethodInfo

Returns:

null if no such method is found.
getMethod
(String name)

Returns the method with the specified name.

public List<MethodInfo>

Returns:

a list of MethodInfo.
getMethods
()

Returns all the methods declared in the class.

public int

Returns:

the minor version
getMinorVersion
()

Get the minor version.

public String
getName()

Returns the class name.

public final void
getRefClasses(Map<String, String> classnames)

Internal-use only.

public String

Returns:

null if this information is not available.
getSourceFile
()

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
getSuperclass()

Returns the super class name.

public int
getSuperclassId()

Returns the index of the constant pool entry representing the super class.

private void
initSuperclass(String superclass)

public boolean
isAbstract()

Returns true if this is an abstract class or an interface.

private static boolean
isDuplicated(MethodInfo newMethod, String newName, String newDesc, MethodInfo minfo, ListIterator<MethodInfo> it)

public boolean
isFinal()

Returns true if this is a final class or interface.

public boolean
isInterface()

Returns true if this is an interface.

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.
removeAttribute
(String
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 java/lang/Object.
classnames
)

Replaces all occurrences of several class names in the class file.

public void
setAccessFlags(int acc)

Changes access flags.

public void
setInterfaces(String[]
the names of the interfaces.
nameList
)

Sets the interfaces.

public void
setMajorVersion(int
the major version
major
)

Set the major version.

public void
setMinorVersion(int
the minor version
minor
)

Set the minor version.

public void
setName(String name)

Sets the class name.

public void
setSuperclass(String superclass)

Sets the super class.

public void
setVersionToJava5()

Sets the major and minor version to Java 5.

private void
testExistingField(String name, String descriptor)

private void
public void
write(DataOutputStream out)

Writes a class file represented by this object into an output stream.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

accessFlagsback to summary
pack-priv int accessFlags
attributesback to summary
pack-priv List<AttributeInfo> attributes
cachedInterfacesback to summary
pack-priv String[] cachedInterfaces
cachedSuperclassback to summary
pack-priv String cachedSuperclass
constPoolback to summary
pack-priv ConstPool constPool
fieldsback to summary
pack-priv List<FieldInfo> fields
interfacesback to summary
pack-priv int[] interfaces
JAVA_1back to summary
public static final int JAVA_1

The major version number of class files for JDK 1.1.

JAVA_10back to summary
public static final int JAVA_10

The major version number of class files for JDK 10.

JAVA_11back to summary
public static final int JAVA_11

The major version number of class files for JDK 11.

JAVA_2back to summary
public static final int JAVA_2

The major version number of class files for JDK 1.2.

JAVA_3back to summary
public static final int JAVA_3

The major version number of class files for JDK 1.3.

JAVA_4back to summary
public static final int JAVA_4

The major version number of class files for JDK 1.4.

JAVA_5back to summary
public static final int JAVA_5

The major version number of class files for JDK 1.5.

JAVA_6back to summary
public static final int JAVA_6

The major version number of class files for JDK 1.6.

JAVA_7back to summary
public static final int JAVA_7

The major version number of class files for JDK 1.7.

JAVA_8back to summary
public static final int JAVA_8

The major version number of class files for JDK 1.8.

JAVA_9back to summary
public static final int JAVA_9

The major version number of class files for JDK 1.9.

majorback to summary
pack-priv int major
MAJOR_VERSIONback 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 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. It is 52 (JDK 1.8) if the JVM supports java.util.function.Function. It is 53 (JDK 1.9) if the JVM supports java.lang.reflect.Module. It is 54 (JDK 10) if the JVM supports java.util.List.copyOf(Collection). It is 55 (JDK 11) if the JVM supports java.util.Optional.isEmpty().

methodsback to summary
pack-priv List<MethodInfo> methods
minorback to summary
pack-priv int minor
superClassback to summary
pack-priv int superClass
thisClassback to summary
pack-priv int thisClass
thisclassnameback to summary
pack-priv String thisclassname

Constructor Detail

ClassFileback to summary
public ClassFile(DataInputStream in) throws IOException

Constructs a class file from a byte stream.

ClassFileback to summary
public ClassFile(boolean isInterface, String classname, String superclass)

Constructs a class file including no members.

Parameters
isInterface:boolean

true if this is an interface. false if this is a class.

classname:String

a fully-qualified class name

superclass:String

a fully-qualified super class name or null.

Method Detail

addAttributeback 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.

See Also
getAttributes()
addFieldback to summary
public void addField(FieldInfo finfo) throws DuplicateMemberException

Appends a field to the class.

Exceptions
DuplicateMemberException:
when the field is already included.
addField2back 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.

Since
3.13
addInterfaceback to summary
public void addInterface(String name)

Appends an interface to the interfaces implemented by the class.

addMethodback 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.

Exceptions
DuplicateMemberException:
when the method is already included.
addMethod2back 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.

Since
3.13
compactback 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.

compact0back to summary
private ConstPool compact0()
getAccessFlagsback to summary
public int getAccessFlags()

Returns access flags.

See Also
javassist.bytecode.AccessFlag
getAttributeback 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, AnnotationsAttribute#visibleTag or AnnotationsAttribute#invisibleTag.

Parameters
name:String

attribute name

See Also
getAttributes()
getAttributesback to summary
public List<AttributeInfo> getAttributes()

Returns all the attributes. The returned 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.

Returns:List<AttributeInfo>

a list of AttributeInfo objects.

See Also
AttributeInfo
getConstPoolback to summary
public ConstPool getConstPool()

Returns a constant pool table.

getFieldsback to summary
public List<FieldInfo> getFields()

Returns all the fields declared in the class.

Returns:List<FieldInfo>

a list of FieldInfo.

See Also
FieldInfo
getInnerAccessFlagsback 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 inner_class_access_flags of the entry representing this nested class itself in InnerClasses_attribute.

getInterfacesback to summary
public String[] getInterfaces()

Returns the names of the interfaces implemented by the class. The returned array is read only.

getMajorVersionback to summary
public int getMajorVersion()

Get the Major version.

Returns:int

the major version

getMethodback 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.

Returns:MethodInfo

null if no such method is found.

getMethodsback to summary
public List<MethodInfo> getMethods()

Returns all the methods declared in the class.

Returns:List<MethodInfo>

a list of MethodInfo.

See Also
MethodInfo
getMinorVersionback to summary
public int getMinorVersion()

Get the minor version.

Returns:int

the minor version

getNameback to summary
public String getName()

Returns the class name.

getRefClassesback to summary
public final void getRefClasses(Map<String, String> classnames)

Internal-use only. CtClass.getRefClasses() calls this method.

getSourceFileback to summary
public String getSourceFile()

Returns the source file containing this class.

Returns:String

null if this information is not available.

getSourcefileNameback to summary
private static String getSourcefileName(String qname)
getStaticInitializerback to summary
public MethodInfo getStaticInitializer()

Returns a static initializer (class initializer), or null if it does not exist.

getSuperclassback to summary
public String getSuperclass()

Returns the super class name.

getSuperclassIdback to summary
public int getSuperclassId()

Returns the index of the constant pool entry representing the super class.

initSuperclassback to summary
private void initSuperclass(String superclass)
isAbstractback to summary
public boolean isAbstract()

Returns true if this is an abstract class or an interface.

isDuplicatedback to summary
private static boolean isDuplicated(MethodInfo newMethod, String newName, String newDesc, MethodInfo minfo, ListIterator<MethodInfo> it)
isFinalback to summary
public boolean isFinal()

Returns true if this is a final class or interface.

isInterfaceback to summary
public boolean isInterface()

Returns true if this is an interface.

notBridgeMethodback to summary
private static boolean notBridgeMethod(MethodInfo minfo)
pruneback 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).

readback to summary
private void read(DataInputStream in) throws IOException
removeAttributeback to summary
public AttributeInfo removeAttribute(String name)

Removes an attribute with the specified name.

Parameters
name:String

attribute name.

Returns:AttributeInfo

the removed attribute or null.

Since
3.21
renameClassback 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.

Parameters
oldname:String

the replaced class name

newname:String

the substituted class name

renameClassback to summary
public final void renameClass(Map<String, String> classnames)

Replaces all occurrences of several class names in the class file.

Parameters
classnames:Map<String, String>

specifies which class name is replaced with which new name. Class names must be described with the JVM-internal representation like java/lang/Object.

See Also
renameClass(String, String)
setAccessFlagsback to summary
public void setAccessFlags(int acc)

Changes access flags.

See Also
javassist.bytecode.AccessFlag
setInterfacesback to summary
public void setInterfaces(String[] nameList)

Sets the interfaces.

Parameters
nameList:String[]

the names of the interfaces.

setMajorVersionback to summary
public void setMajorVersion(int major)

Set the major version.

Parameters
major:int

the major version

setMinorVersionback to summary
public void setMinorVersion(int minor)

Set the minor version.

Parameters
minor:int

the minor version

setNameback 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.

setSuperclassback 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.

setVersionToJava5back 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.

testExistingFieldback to summary
private void testExistingField(String name, String descriptor) throws DuplicateMemberException
testExistingMethodback to summary
private void testExistingMethod(MethodInfo newMinfo) throws DuplicateMemberException
writeback to summary
public void write(DataOutputStream out) throws IOException

Writes a class file represented by this object into an output stream.