public class ClassFile extends Object
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.
Modifier and Type | Class and Description |
---|---|
static class |
ClassFile.AttributeInfo
Representation of a class file attribute (see JVMS7 4.7).
|
static class |
ClassFile.ConstantClassInfo
See JVMS7 4.4.1.
|
static class |
ClassFile.ConstantFieldrefInfo
See JVMS7 4.4.2.
|
static class |
ClassFile.ConstantInterfaceMethodrefInfo
See JVMS7 4.4.2.
|
static class |
ClassFile.ConstantMethodrefInfo
See JVMS7 4.4.2.
|
static class |
ClassFile.ConstantNameAndTypeInfo
See JVMS7 4.4.6.
|
static class |
ClassFile.ConstantPoolInfo
Base for various the constant pool table entry types.
|
static class |
ClassFile.ConstantUtf8Info
See JVMS7 4.4.7.
|
static class |
ClassFile.ConstantValueAttribute
Representation of a "ConstantValue" attribute (see JVMS 4.7.2).
|
static class |
ClassFile.ConstantValuePoolInfo
Intermediate base class for constant pool table entry types that have 'value' semantics: Double, Float,
Integer, Long, String
|
static class |
ClassFile.DeprecatedAttribute
Representation of a "Deprecated" attribute (see JVMS 4.7.10).
|
static class |
ClassFile.ExceptionsAttribute
Representation of an "Exceptions" attribute (see JVMS 4.7.4).
|
static class |
ClassFile.FieldInfo
Representation of a "method_info" structure, as defined by JVMS7 4.5.
|
static class |
ClassFile.InnerClassesAttribute
Representation of an "InnerClasses" attribute (see JVMS 4.7.5).
|
static class |
ClassFile.LineNumberTableAttribute
Representation of a "LineNumberTable" attribute (see JVMS 4.7.8).
|
static class |
ClassFile.LocalVariableTableAttribute
Representation of a "LocalVariableTable" attribute (see JVMS 4.7.9).
|
class |
ClassFile.MethodInfo
Representation of a "method_info" structure, as defined by JVMS7 4.6.
|
static class |
ClassFile.SourceFileAttribute
Representation of a "SourceFile" attribute (see JVMS 4.7.7).
|
static class |
ClassFile.SyntheticAttribute
Representation of a "Synthetic" attribute (see JVMS 4.7.6).
|
Modifier and Type | Field and Description |
---|---|
short |
accessFlags
The access flags of the class.
|
List<ClassFile.FieldInfo> |
fieldInfos
The
ClassFile.FieldInfo s of the field members of this class or interface. |
short[] |
interfaces
The constant pool indexes of
ClassFile.ConstantClassInfo which describes the interfaces that this class
implements, resp. |
static short |
MAJOR_VERSION_JDK_1_1 |
static short |
MAJOR_VERSION_JDK_1_2 |
static short |
MAJOR_VERSION_JDK_1_3 |
static short |
MAJOR_VERSION_JDK_1_4 |
static short |
MAJOR_VERSION_JDK_1_5 |
List<ClassFile.MethodInfo> |
methodInfos
The
ClassFile.MethodInfo s of the methods of this class or interface. |
static short |
MINOR_VERSION_JDK_1_1 |
static short |
MINOR_VERSION_JDK_1_2 |
static short |
MINOR_VERSION_JDK_1_3 |
static short |
MINOR_VERSION_JDK_1_4 |
static short |
MINOR_VERSION_JDK_1_5 |
short |
superclass
The constant pool index of the
ClassFile.ConstantClassInfo that describes the superclass of THIS class. |
short |
thisClass
The constant pool index of the
ClassFile.ConstantClassInfo that describes THIS class. |
Constructor and Description |
---|
ClassFile(InputStream inputStream)
Read "class file" data from a
InputStream and construct a ClassFile object from it. |
ClassFile(short accessFlags,
String thisClassFd,
String superclassFd,
String[] interfaceFds)
Construct from parsed components.
|
Modifier and Type | Method and Description |
---|---|
short |
addConstantClassInfo(String typeFd)
Return the constant index number for a "CONSTANT_Class_info" structure to the class file.
|
short |
addConstantDoubleInfo(double value)
Add a "CONSTANT_Double_info" structure to the class file.
|
short |
addConstantFieldrefInfo(String classFd,
String fieldName,
String fieldFd)
Add a "CONSTANT_Fieldref_info" structure to the class file.
|
short |
addConstantFloatInfo(float value)
Add a "CONSTANT_Float_info" structure to the class file.
|
short |
addConstantIntegerInfo(int value)
Add a "CONSTANT_Integer_info" structure to the class file.
|
short |
addConstantInterfaceMethodrefInfo(String classFd,
String methodName,
String methodMd)
Add a "CONSTANT_InterfaceMethodref_info" structure to the class file.
|
short |
addConstantLongInfo(long value)
Add a "CONSTANT_Long_info" structure to the class file.
|
short |
addConstantMethodrefInfo(String classFd,
String methodName,
String methodMd)
Add a "CONSTANT_Methodref_info" structure to the class file.
|
short |
addConstantStringInfo(String string)
Add a "CONSTANT_String_info" structure to the class file.
|
short |
addConstantUtf8Info(String s)
Adds a "CONSTANT_Utf8_info" structure to the class file if no equal entry exists.
|
void |
addDeprecatedAttribute()
Adds the "Deprecated" attribute to this class.
|
ClassFile.FieldInfo |
addFieldInfo(Java.Modifiers modifiers,
String fieldName,
String fieldTypeFd,
Object optionalConstantValue)
Creates a
ClassFile.FieldInfo and adds it to this class. |
void |
addInnerClassesAttributeEntry(ClassFile.InnerClassesAttribute.Entry e)
Create an "InnerClasses" attribute if it does not exist, then add the given entry
to the "InnerClasses" attribute.
|
ClassFile.MethodInfo |
addMethodInfo(Java.Modifiers modifiers,
String methodName,
String methodMd)
Creates a
ClassFile.MethodInfo and adds it to this class. |
void |
addSourceFileAttribute(String sourceFileName)
Adds a "SourceFile" attribute to this class file.
|
static String |
getClassFileResourceName(String className)
Construct the name of a resource that could contain the class file of the
class with the given name.
|
ClassFile.ConstantPoolInfo |
getConstantPoolInfo(short index) |
int |
getConstantPoolSize() |
String |
getConstantUtf8(short index) |
ClassFile.InnerClassesAttribute |
getInnerClassesAttribute()
Find the "InnerClasses" attribute of this class file
|
short |
getMajorVersion() |
short |
getMinorVersion() |
static String |
getSourceResourceName(String className)
Construct the name of a resource that could contain the source code of
the class with the given name.
|
String |
getThisClassName() |
void |
setVersion(short majorVersion,
short minorVersion)
Sets the major and minor class file version numbers (JVMS 4.1).
|
void |
store(OutputStream os)
Write
ClassFile to an OutputStream , in "class file" format. |
byte[] |
toByteArray()
Return the byte code of this
ClassFile as a byte array. |
public static final short MAJOR_VERSION_JDK_1_1
public static final short MINOR_VERSION_JDK_1_1
public static final short MAJOR_VERSION_JDK_1_2
public static final short MINOR_VERSION_JDK_1_2
public static final short MAJOR_VERSION_JDK_1_3
public static final short MINOR_VERSION_JDK_1_3
public static final short MAJOR_VERSION_JDK_1_4
public static final short MINOR_VERSION_JDK_1_4
public static final short MAJOR_VERSION_JDK_1_5
public static final short MINOR_VERSION_JDK_1_5
public final short accessFlags
and consorts
public final short thisClass
ClassFile.ConstantClassInfo
that describes THIS class.public final short superclass
ClassFile.ConstantClassInfo
that describes the superclass of THIS class. Zero
for class Object
, Object
for interfaces.public final short[] interfaces
ClassFile.ConstantClassInfo
which describes the interfaces that this class
implements, resp. that this interface extends.public final List<ClassFile.FieldInfo> fieldInfos
ClassFile.FieldInfo
s of the field members of this class or interface.public final List<ClassFile.MethodInfo> methodInfos
ClassFile.MethodInfo
s of the methods of this class or interface.public ClassFile(short accessFlags, String thisClassFd, String superclassFd, String[] interfaceFds)
accessFlags
- as defined by Mod
thisClassFd
- 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 interfacespublic ClassFile(InputStream inputStream) throws IOException
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.
IOException
public void addSourceFileAttribute(String sourceFileName)
sourceFileName
- public void addDeprecatedAttribute()
public ClassFile.InnerClassesAttribute getInnerClassesAttribute()
null
if this class has no "InnerClasses" attributepublic void addInnerClassesAttributeEntry(ClassFile.InnerClassesAttribute.Entry e)
e
- public String getThisClassName()
public void setVersion(short majorVersion, short minorVersion)
public short getMajorVersion()
public short getMinorVersion()
public short addConstantClassInfo(String typeFd)
public short addConstantFieldrefInfo(String classFd, String fieldName, String fieldFd)
public short addConstantMethodrefInfo(String classFd, String methodName, String methodMd)
public short addConstantInterfaceMethodrefInfo(String classFd, String methodName, String methodMd)
public short addConstantStringInfo(String string)
public short addConstantIntegerInfo(int value)
public short addConstantFloatInfo(float value)
public short addConstantLongInfo(long value)
public short addConstantDoubleInfo(double value)
public short addConstantUtf8Info(String s)
public ClassFile.FieldInfo addFieldInfo(Java.Modifiers modifiers, String fieldName, String fieldTypeFd, Object optionalConstantValue)
ClassFile.FieldInfo
and adds it to this class. The return value can be used e.g. to add attributes
(Deprecated
, ...) to the field.public ClassFile.MethodInfo addMethodInfo(Java.Modifiers modifiers, String methodName, String methodMd)
ClassFile.MethodInfo
and adds it to this class. The return value can be used e.g. to add attributes
(Code
, Deprecated
, Exceptions
, ...) to the method.public ClassFile.ConstantPoolInfo getConstantPoolInfo(short index)
index
public int getConstantPoolSize()
public String getConstantUtf8(short index)
index
- Index to a CONSTANT_Utf8_info
in the constant poolpublic void store(OutputStream os) throws IOException
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.
os
- IOException
public static String getSourceResourceName(String className)
Notice that member types are declared inside a different type, so the relevant source file is that of the outermost declaring class.
className
- Fully qualified class name, e.g. "pkg1.pkg2.Outer$Inner"public static String getClassFileResourceName(String className)
className
- Fully qualified class name, e.g. "pkg1.pkg2.Outer$Inner"public byte[] toByteArray()
ClassFile
as a byte array.Copyright © 2001–2016. All rights reserved.