javassist.bytecode

Class ClassFile

public final class ClassFile extends Object

ClassFile represents a Java .class file, which consists of a constant pool, methods, fields, and attributes.

See Also: getClassFile

Constructor Summary
ClassFile(DataInputStream in)
Constructs a class file from a byte stream.
ClassFile(boolean isInterface, String classname, String superclass)
Constructs a class file including no members.
Method Summary
voidaddAttribute(AttributeInfo info)
Appends an attribute.
voidaddField(FieldInfo finfo)
Appends a field to the class.
voidaddInterface(String name)
Appends an interface to the interfaces implemented by the class.
voidaddMethod(MethodInfo minfo)
Appends a method to the class.
voidcompact()
Eliminates dead constant pool items.
intgetAccessFlags()
Returns access flags.
AttributeInfogetAttribute(String name)
Returns the attribute with the specified name.
ListgetAttributes()
Returns all the attributes.
ConstPoolgetConstPool()
Returns a constant pool table.
ListgetFields()
Returns all the fields declared in the class.
intgetInnerAccessFlags()
Returns access and property flags of this nested class.
String[]getInterfaces()
Returns the names of the interfaces implemented by the class.
intgetMajorVersion()
Get the Major version.
MethodInfogetMethod(String name)
Returns the method with the specified name.
ListgetMethods()
Returns all the methods declared in the class.
intgetMinorVersion()
Get the minor version.
StringgetName()
Returns the class name.
StringgetSourceFile()
Returns the source file containing this class.
MethodInfogetStaticInitializer()
Returns a static initializer (class initializer), or null if it does not exist.
StringgetSuperclass()
Returns the super class name.
intgetSuperclassId()
Returns the index of the constant pool entry representing the super class.
booleanisAbstract()
Returns true if this is an abstract class or an interface.
booleanisFinal()
Returns true if this is a final class or interface.
booleanisInterface()
Returns true if this is an interface.
voidprune()
Discards all attributes, associated with both the class file and the members such as a code attribute and exceptions attribute.
voidrenameClass(String oldname, String newname)
Replaces all occurrences of a class name in the class file.
voidrenameClass(Map classnames)
Replaces all occurrences of several class names in the class file.
voidsetAccessFlags(int acc)
Changes access flags.
voidsetInterfaces(String[] nameList)
Sets the interfaces.
voidsetMajorVersion(int major)
Set the major version.
voidsetMinorVersion(int minor)
Set the minor version.
voidsetName(String name)
Sets the class name.
voidsetSuperclass(String superclass)
Sets the super class.
voidsetVersionToJava5()
Sets the major and minor version to Java 5.
voidwrite(DataOutputStream out)
Writes a class file represened by this object into an output stream.

Constructor Detail

ClassFile

public ClassFile(DataInputStream in)
Constructs a class file from a byte stream.

ClassFile

public ClassFile(boolean isInterface, String classname, 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 name superclass a fully-qualified super class name

Method Detail

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

addField

public void addField(FieldInfo finfo)
Appends a field to the class.

Throws: DuplicateMemberException when the field is already included.

addInterface

public void addInterface(String name)
Appends an interface to the interfaces implemented by the class.

addMethod

public void addMethod(MethodInfo minfo)
Appends a method to the class.

Throws: DuplicateMemberException when the method is already included.

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.

getAccessFlags

public int getAccessFlags()
Returns access flags.

See Also: AccessFlag

getAttribute

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.

Parameters: name attribute name

See Also: getAttributes

getAttributes

public List 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: a list of AttributeInfo objects.

See Also: AttributeInfo

getConstPool

public ConstPool getConstPool()
Returns a constant pool table.

getFields

public List getFields()
Returns all the fields declared in the class.

Returns: a list of FieldInfo.

See Also: FieldInfo

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 in InnerClasses_attribute>.

getInterfaces

public String[] getInterfaces()
Returns the names of the interfaces implemented by the class. The returned array is read only.

getMajorVersion

public int getMajorVersion()
Get the Major version.

Returns: the major version

getMethod

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: null if no such a method is found.

getMethods

public List getMethods()
Returns all the methods declared in the class.

Returns: a list of MethodInfo.

See Also: MethodInfo

getMinorVersion

public int getMinorVersion()
Get the minor version.

Returns: the minor version

getName

public String getName()
Returns the class name.

getSourceFile

public String getSourceFile()
Returns the source file containing this class.

Returns: null if this information is not available.

getStaticInitializer

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

getSuperclass

public String getSuperclass()
Returns the super class name.

getSuperclassId

public int getSuperclassId()
Returns the index of the constant pool entry representing the super class.

isAbstract

public boolean isAbstract()
Returns true if this is an abstract class or an interface.

isFinal

public boolean isFinal()
Returns true if this is a final class or interface.

isInterface

public boolean isInterface()
Returns true if this is an interface.

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

renameClass

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 the replaced class name newname the substituted class name

renameClass

public final void renameClass(Map 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 like java/lang/Object.

See Also: renameClass

setAccessFlags

public void setAccessFlags(int acc)
Changes access flags.

See Also: AccessFlag

setInterfaces

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

Parameters: nameList the names of the interfaces.

setMajorVersion

public void setMajorVersion(int major)
Set the major version.

Parameters: major the major version

setMinorVersion

public void setMinorVersion(int minor)
Set the minor version.

Parameters: minor the minor version

setName

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

public void setSuperclass(String superclass)
Sets the super class.

This method modifies constructors so that they call constructors declared in the new super class.

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.

write

public void write(DataOutputStream out)
Writes a class file represened by this object into an output stream.
Javassist, a Java-bytecode translator toolkit.
Copyright (C) 1999-2006 Shigeru Chiba. All Rights Reserved.