Class ClassFile
ClassFile(InputStream) creates a ClassFile object from the bytecode
read from the given InputStream.
store(OutputStream) generates Java™ bytecode
which is suitable for being processed by a Java™ virtual
machine.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classRepresentation of a class file attribute (see JVMS 4.7).static classstatic classstatic classstatic classstatic classstatic classstatic classstatic classRepresentation of a "ConstantValue" attribute (see JVMS 4.7.2).static classstatic classRepresentation of a "Deprecated" attribute (see JVMS 4.7.10).static classRepresentation of an "Exceptions" attribute (see JVMS 4.7.4).static classstatic classRepresentation of an "InnerClasses" attribute (see JVMS 4.7.5).static classRepresentation of a "LineNumberTable" attribute (see JVMS 4.7.8).static classRepresentation of a "LocalVariableTable" attribute (see JVMS 4.7.9).classThis class represents a "method_info" structure, as defined by the JVM specification.static classRepresentation of a "SourceFile" attribute (see JVMS 4.7.7).static classRepresentation of a "Synthetic" attribute (see JVMS 4.7.6). -
Field Summary
FieldsModifier and TypeFieldDescriptionfinal shortfinal Listfinal Listfinal short[]static final shortstatic final shortstatic final shortstatic final shortstatic final shortfinal Liststatic final shortstatic final shortstatic final shortstatic final shortstatic final shortfinal shortfinal short -
Constructor Summary
ConstructorsConstructorDescriptionConstruct from parsed components.ClassFile(InputStream inputStream) Read "class file" data from aInputStreamand construct aClassFileobject from it. -
Method Summary
Modifier and TypeMethodDescriptionshortaddConstantClassInfo(String typeFd) Return the constant index number for 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.shortAdds a "CONSTANT_Utf8_info" structure to the class file if no equal entry exists.voidaddFieldInfo(short accessFlags, String fieldName, String fieldTypeFd, Object optionalConstantValue) voidCreate an "InnerClasses" attribute if it does not exist, then add the given entry to the "InnerClasses" attribute.addMethodInfo(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.getConstantClassName(short index) getConstantPoolInfo(short index) getConstantUtf8(short index) Find the "InnerClasses" attribute of this class fileshortReturns the current major class file version number.shortReturns 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.voidsetVersion(short majorVersion, short minorVersion) Sets the major and minor class file version numbers (JVMS 4.1).voidstore(OutputStream os) WriteClassFileto anOutputStream, in "class file" format.byte[]Return the byte code of thisClassFileas a byte array.
-
Field Details
-
MAJOR_VERSION_JDK_1_1
public static final short MAJOR_VERSION_JDK_1_1- See Also:
-
MINOR_VERSION_JDK_1_1
public static final short MINOR_VERSION_JDK_1_1- See Also:
-
MAJOR_VERSION_JDK_1_2
public static final short MAJOR_VERSION_JDK_1_2- See Also:
-
MINOR_VERSION_JDK_1_2
public static final short MINOR_VERSION_JDK_1_2- See Also:
-
MAJOR_VERSION_JDK_1_3
public static final short MAJOR_VERSION_JDK_1_3- See Also:
-
MINOR_VERSION_JDK_1_3
public static final short MINOR_VERSION_JDK_1_3- See Also:
-
MAJOR_VERSION_JDK_1_4
public static final short MAJOR_VERSION_JDK_1_4- See Also:
-
MINOR_VERSION_JDK_1_4
public static final short MINOR_VERSION_JDK_1_4- See Also:
-
MAJOR_VERSION_JDK_1_5
public static final short MAJOR_VERSION_JDK_1_5- See Also:
-
MINOR_VERSION_JDK_1_5
public static final short MINOR_VERSION_JDK_1_5- See Also:
-
constantPool
-
accessFlags
public final short accessFlags -
thisClass
public final short thisClass -
superclass
public final short superclass -
interfaces
public final short[] interfaces -
fieldInfos
-
methodInfos
-
-
Constructor Details
-
ClassFile
Construct from parsed components.- Parameters:
accessFlags- as defined byModthisClassFd- the field descriptor for this classsuperclassFd- the field descriptor for the extended class (e.g. "Ljava/lang/Object;")interfaceFds- the field descriptors for the implemented interfaces
-
ClassFile
Read "class file" data from aInputStreamand construct aClassFileobject from it.If the
ClassFileis created with this constructor, then most modifying operations lead to aUnsupportedOperationException; only fields, methods and attributes can be added.- Parameters:
inputStream-- Throws:
IOExceptionClassFormatError
-
-
Method Details
-
addSourceFileAttribute
Adds a "SourceFile" attribute to this class file. (Does not check whether one exists already.)- Parameters:
sourceFileName-
-
addDeprecatedAttribute
public void addDeprecatedAttribute() -
getInnerClassesAttribute
Find the "InnerClasses" attribute of this class file- Returns:
nullif this class has no "InnerClasses" attribute
-
addInnerClassesAttributeEntry
Create an "InnerClasses" attribute if it does not exist, then add the given entry to the "InnerClasses" attribute.- Parameters:
e-
-
getThisClassName
- Returns:
- The fully qualified name of this class, e.g. "pkg1.pkg2.Outer$Inner"
-
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-
-
getMajorVersion
public short getMajorVersion()Returns the current major class file version number. -
getMinorVersion
public short getMinorVersion()Returns the current minor class file version number. -
addConstantClassInfo
Return the constant index number for a "CONSTANT_Class_info" structure to the class file. If the class hasn't been added before, add it to the constant pool. Otherwise return the constant number for that element of the pool.- See Also:
-
addConstantFieldrefInfo
Add a "CONSTANT_Fieldref_info" structure to the class file.- See Also:
-
addConstantMethodrefInfo
Add a "CONSTANT_Methodref_info" structure to the class file.- See Also:
-
addConstantInterfaceMethodrefInfo
Add a "CONSTANT_InterfaceMethodref_info" structure to the class file.- See Also:
-
addConstantStringInfo
Add a "CONSTANT_String_info" structure to the class file.- See Also:
-
addConstantIntegerInfo
public short addConstantIntegerInfo(int value) Add a "CONSTANT_Integer_info" structure to the class file.- See Also:
-
addConstantFloatInfo
public short addConstantFloatInfo(float value) Add a "CONSTANT_Float_info" structure to the class file.- See Also:
-
addConstantLongInfo
public short addConstantLongInfo(long value) Add a "CONSTANT_Long_info" structure to the class file.- See Also:
-
addConstantDoubleInfo
public short addConstantDoubleInfo(double value) Add a "CONSTANT_Double_info" structure to the class file.- See Also:
-
addConstantUtf8Info
Adds a "CONSTANT_Utf8_info" structure to the class file if no equal entry exists.- Returns:
- The index of the already existing or newly created entry
- See Also:
-
addFieldInfo
public ClassFile.FieldInfo addFieldInfo(short accessFlags, String fieldName, String fieldTypeFd, Object optionalConstantValue) -
addMethodInfo
-
getConstantPoolInfo
-
getConstantClassName
- Parameters:
index- Index to aCONSTANT_Class_infoin the constant pool- Returns:
- The name of the denoted class in "internal form" (see JVMS 4.2)
-
getConstantUtf8
- Parameters:
index- Index to aCONSTANT_Utf8_infoin the constant pool- Returns:
- The string represented by the structure
-
store
WriteClassFileto anOutputStream, in "class file" format.Notice that if an
IOExceptionis 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
-
getSourceResourceName
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"
-
getClassFileResourceName
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"
-
toByteArray
public byte[] toByteArray()Return the byte code of thisClassFileas a byte array.
-