org.codehaus.janino.util

Class ClassFile

public class ClassFile extends Object

An object that represents the JavaTM "class file" format.

ClassFile creates a ClassFile object from the bytecode read from the given InputStream.

store generates JavaTM bytecode which is suitable for being processed by a JavaTM virtual machine.

Nested Class Summary
abstract static classClassFile.AttributeInfo
Representation of a class file attribute (see JVMS 4.7).
static classClassFile.ConstantClassInfo
static classClassFile.ConstantFieldrefInfo
static classClassFile.ConstantInterfaceMethodrefInfo
static classClassFile.ConstantMethodrefInfo
static classClassFile.ConstantNameAndTypeInfo
abstract static classClassFile.ConstantPoolInfo
static classClassFile.ConstantUtf8Info
static classClassFile.ConstantValueAttribute
Representation of a "ConstantValue" attribute (see JVMS 4.7.2).
abstract static classClassFile.ConstantValuePoolInfo
static classClassFile.DeprecatedAttribute
Representation of a "Deprecated" attribute (see JVMS 4.7.10).
static classClassFile.ExceptionsAttribute
Representation of an "Exceptions" attribute (see JVMS 4.7.4).
static classClassFile.FieldInfo
static classClassFile.InnerClassesAttribute
Representation of an "InnerClasses" attribute (see JVMS 4.7.5).
static classClassFile.LineNumberTableAttribute
Representation of a "LineNumberTable" attribute (see JVMS 4.7.8).
static classClassFile.LocalVariableTableAttribute
Representation of a "LocalVariableTable" attribute (see JVMS 4.7.9).
classClassFile.MethodInfo
This class represents a "method_info" structure, as defined by the JVM specification.
static classClassFile.SourceFileAttribute
Representation of a "SourceFile" attribute (see JVMS 4.7.7).
static classClassFile.SyntheticAttribute
Representation of a "Synthetic" attribute (see JVMS 4.7.6).
Field Summary
shortaccessFlags
ListconstantPool
ListfieldInfos
short[]interfaces
ListmethodInfos
static shortMAJOR_VERSION_JDK_1_1
static shortMAJOR_VERSION_JDK_1_2
static shortMAJOR_VERSION_JDK_1_3
static shortMAJOR_VERSION_JDK_1_4
static shortMAJOR_VERSION_JDK_1_5
static shortMINOR_VERSION_JDK_1_1
static shortMINOR_VERSION_JDK_1_2
static shortMINOR_VERSION_JDK_1_3
static shortMINOR_VERSION_JDK_1_4
static shortMINOR_VERSION_JDK_1_5
shortsuperclass
shortthisClass
Constructor Summary
ClassFile(short accessFlags, String thisClassFD, String superclassFD, String[] interfaceFDs)
Construct from parsed components.
ClassFile(InputStream inputStream)
Read "class file" data from a InputStream and construct a ClassFile object from it.
Method Summary
shortaddConstantClassInfo(String typeFD)
Add a "CONSTANT_Class_info" structure to the class file.
shortaddConstantDoubleInfo(double value)
Add a "CONSTANT_Double_info" structure to the class file.
shortaddConstantFieldrefInfo(String classFD, String fieldName, String fieldFD)
Add a "CONSTANT_Fieldref_info" structure to the class file.
shortaddConstantFloatInfo(float value)
Add a "CONSTANT_Float_info" structure to the class file.
shortaddConstantIntegerInfo(int value)
Add a "CONSTANT_Integer_info" structure to the class file.
shortaddConstantInterfaceMethodrefInfo(String classFD, String methodName, String methodMD)
Add a "CONSTANT_InterfaceMethodref_info" structure to the class file.
shortaddConstantLongInfo(long value)
Add a "CONSTANT_Long_info" structure to the class file.
shortaddConstantMethodrefInfo(String classFD, String methodName, String methodMD)
Add a "CONSTANT_Methodref_info" structure to the class file.
shortaddConstantStringInfo(String string)
Add a "CONSTANT_String_info" structure to the class file.
shortaddConstantUtf8Info(String s)
Add a "CONSTANT_Utf8_info" structure to the class file.
voidaddDeprecatedAttribute()
ClassFile.FieldInfoaddFieldInfo(short accessFlags, String fieldName, String fieldTypeFD, Object optionalConstantValue)
voidaddInnerClassesAttributeEntry(ClassFile.InnerClassesAttribute.Entry e)
Create an "InnerClasses" attribute if it does not exist, then add the given entry to the "InnerClasses" attribute.
ClassFile.MethodInfoaddMethodInfo(short accessFlags, String methodName, String methodMD)
voidaddSourceFileAttribute(String sourceFileName)
Adds a "SourceFile" attribute to this class file.
static StringgetClassFileResourceName(String className)
Construct the name of a resource that could contain the class file of the class with the given name.
StringgetConstantClassName(short index)
ClassFile.ConstantPoolInfogetConstantPoolInfo(short index)
StringgetConstantUtf8(short index)
ClassFile.InnerClassesAttributegetInnerClassesAttribute()
Find the "InnerClasses" attribute of this class file
shortgetMajorVersion()
Returns the current major class file version number.
shortgetMinorVersion()
Returns the current minor class file version number.
static StringgetSourceResourceName(String className)
Construct the name of a resource that could contain the source code of the class with the given name.
StringgetThisClassName()
static booleanisRecognizedVersion(short majorVersion, short minorVersion)
voidsetVersion(short majorVersion, short minorVersion)
Sets the major and minor class file version numbers (JVMS 4.1).
voidstore(OutputStream os)
Write ClassFile to an OutputStream, in "class file" format.
byte[]toByteArray()
Return the byte code of this ClassFile as a byte array.

Field Detail

accessFlags

public short accessFlags

constantPool

public List constantPool

fieldInfos

public List fieldInfos

interfaces

public short[] interfaces

methodInfos

public List methodInfos

MAJOR_VERSION_JDK_1_1

public static final short MAJOR_VERSION_JDK_1_1

MAJOR_VERSION_JDK_1_2

public static final short MAJOR_VERSION_JDK_1_2

MAJOR_VERSION_JDK_1_3

public static final short MAJOR_VERSION_JDK_1_3

MAJOR_VERSION_JDK_1_4

public static final short MAJOR_VERSION_JDK_1_4

MAJOR_VERSION_JDK_1_5

public static final short MAJOR_VERSION_JDK_1_5

MINOR_VERSION_JDK_1_1

public static final short MINOR_VERSION_JDK_1_1

MINOR_VERSION_JDK_1_2

public static final short MINOR_VERSION_JDK_1_2

MINOR_VERSION_JDK_1_3

public static final short MINOR_VERSION_JDK_1_3

MINOR_VERSION_JDK_1_4

public static final short MINOR_VERSION_JDK_1_4

MINOR_VERSION_JDK_1_5

public static final short MINOR_VERSION_JDK_1_5

superclass

public short superclass

thisClass

public short thisClass

Constructor Detail

ClassFile

public ClassFile(short accessFlags, String thisClassFD, String superclassFD, String[] interfaceFDs)
Construct from parsed components.

Parameters: accessFlags as defined by Mod thisClassFD the field descriptor for this class superclassFD the field descriptor for the extended class (e.g. "Ljava/lang/Object;") interfaceFDs the field descriptors for the implemented interfaces

ClassFile

public ClassFile(InputStream inputStream)
Read "class file" data from a InputStream and construct a ClassFile object from it.

If the ClassFile is created with this constructor, then most modifying operations lead to a UnsupportedOperationException; only fields, methods and attributes can be added.

Parameters: inputStream

Throws: IOException ClassFormatError

Method Detail

addConstantClassInfo

public short addConstantClassInfo(String typeFD)
Add a "CONSTANT_Class_info" structure to the class file.

See Also: JVM specification, section 4.4.1

addConstantDoubleInfo

public short addConstantDoubleInfo(double value)
Add a "CONSTANT_Double_info" structure to the class file.

See Also: JVM specification, section 4.4.5

addConstantFieldrefInfo

public short addConstantFieldrefInfo(String classFD, String fieldName, String fieldFD)
Add a "CONSTANT_Fieldref_info" structure to the class file.

See Also: JVM specification, section 4.4.2

addConstantFloatInfo

public short addConstantFloatInfo(float value)
Add a "CONSTANT_Float_info" structure to the class file.

See Also: JVM specification, section 4.4.4

addConstantIntegerInfo

public short addConstantIntegerInfo(int value)
Add a "CONSTANT_Integer_info" structure to the class file.

See Also: JVM specification, section 4.4.4

addConstantInterfaceMethodrefInfo

public short addConstantInterfaceMethodrefInfo(String classFD, String methodName, String methodMD)
Add a "CONSTANT_InterfaceMethodref_info" structure to the class file.

See Also: JVM specification, section 4.4.2

addConstantLongInfo

public short addConstantLongInfo(long value)
Add a "CONSTANT_Long_info" structure to the class file.

See Also: JVM specification, section 4.4.5

addConstantMethodrefInfo

public short addConstantMethodrefInfo(String classFD, String methodName, String methodMD)
Add a "CONSTANT_Methodref_info" structure to the class file.

See Also: JVM specification, section 4.4.2

addConstantStringInfo

public short addConstantStringInfo(String string)
Add a "CONSTANT_String_info" structure to the class file.

See Also: JVM specification, section 4.4.3

addConstantUtf8Info

public short addConstantUtf8Info(String s)
Add a "CONSTANT_Utf8_info" structure to the class file.

See Also: JVM specification, section 4.4.7

addDeprecatedAttribute

public void addDeprecatedAttribute()

addFieldInfo

public ClassFile.FieldInfo addFieldInfo(short accessFlags, String fieldName, String fieldTypeFD, Object optionalConstantValue)

addInnerClassesAttributeEntry

public void addInnerClassesAttributeEntry(ClassFile.InnerClassesAttribute.Entry e)
Create an "InnerClasses" attribute if it does not exist, then add the given entry to the "InnerClasses" attribute.

Parameters: e

addMethodInfo

public ClassFile.MethodInfo addMethodInfo(short accessFlags, String methodName, String methodMD)

addSourceFileAttribute

public void addSourceFileAttribute(String sourceFileName)
Adds a "SourceFile" attribute to this class file. (Does not check whether one exists already.)

Parameters: sourceFileName

getClassFileResourceName

public static String getClassFileResourceName(String className)
Construct the name of a resource that could contain the class file of the class with the given name.

Parameters: className Fully qualified class name, e.g. "pkg1.pkg2.Outer$Inner"

Returns: the name of the resource, e.g. "pkg1/pkg2/Outer$Inner.class"

getConstantClassName

public String getConstantClassName(short index)

Parameters: index Index to a CONSTANT_Class_info in the constant pool

Returns: The name of the denoted class in "internal form" (see JVMS 4.2)

getConstantPoolInfo

public ClassFile.ConstantPoolInfo getConstantPoolInfo(short index)

getConstantUtf8

public String getConstantUtf8(short index)

Parameters: index Index to a CONSTANT_Utf8_info in the constant pool

Returns: The string represented by the structure

getInnerClassesAttribute

public ClassFile.InnerClassesAttribute getInnerClassesAttribute()
Find the "InnerClasses" attribute of this class file

Returns: null if this class has no "InnerClasses" attribute

getMajorVersion

public short getMajorVersion()
Returns the current major class file version number.

getMinorVersion

public short getMinorVersion()
Returns the current minor class file version number.

getSourceResourceName

public static String getSourceResourceName(String className)
Construct the name of a resource that could contain the source code of the class with the given name.

Notice that member types are declared inside a different type, so the relevant source file is that of the outermost declaring class.

Parameters: className Fully qualified class name, e.g. "pkg1.pkg2.Outer$Inner"

Returns: the name of the resource, e.g. "pkg1/pkg2/Outer.java"

getThisClassName

public String getThisClassName()

Returns: The fully qualified name of this class, e.g. "pkg1.pkg2.Outer$Inner"

isRecognizedVersion

public static boolean isRecognizedVersion(short majorVersion, short minorVersion)

setVersion

public void setVersion(short majorVersion, short minorVersion)
Sets the major and minor class file version numbers (JVMS 4.1). The class file version defaults to the JDK 1.1 values (45.3) which execute on virtually every JVM.

Parameters: majorVersion minorVersion

store

public void store(OutputStream os)
Write ClassFile to an OutputStream, in "class file" format.

Notice that if an IOException is thrown, the class file is probably written incompletely and thus invalid. The calling method must take care of this situation, e.g. by closing the output stream and then deleting the file.

Parameters: os

Throws: IOException

toByteArray

public byte[] toByteArray()
Return the byte code of this ClassFile as a byte array.