public class ClassFile extends Object implements Annotatable
ClassFile(InputStream) reads bytecode from an InputStream and creates a ClassFile object
from it.
store(OutputStream) generates JVM bytecode which is suitable for being processed by a Java
virtual machine, and writes it to an OutputStream.
| Modifier and Type | Class and Description |
|---|---|
static class |
ClassFile.Annotation
The structure of the
annotations array as described in JVMS8 4.7.16. |
static class |
ClassFile.AnnotationDefaultAttribute
Representation of an
AnnotationDefault attribute (see JVMS8 4.7.22). |
static class |
ClassFile.AnnotationsAttribute
Representation of a
Runtime[In]visibleAnnotations attribute (see JVMS8 4.7.16/17). |
static class |
ClassFile.ArrayElementValue
Representation of the "array_value" structure.
|
static class |
ClassFile.AttributeInfo
Representation of a class file attribute (see JVMS7 4.7).
|
static class |
ClassFile.BooleanElementValue |
static class |
ClassFile.ByteElementValue |
static class |
ClassFile.CharElementValue |
static class |
ClassFile.ClassElementValue |
static class |
ClassFile.ClassFileException
Unchecked exception that represents an error condition that could occur during processing of class files, e.g.
|
static class |
ClassFile.CodeAttribute
Representation of an unmodifiable
Code attribute, as read from a class file. |
static class |
ClassFile.ConstantClassInfo
See JVMS7 4.4.1.
|
static class |
ClassFile.ConstantElementValue
Convenience class for element values that are constants (as opposed to annotations, enum constants and
arrays).
|
static class |
ClassFile.ConstantFieldrefInfo
See JVMS7 4.4.2.
|
static class |
ClassFile.ConstantInterfaceMethodrefInfo
See JVMS7 4.4.2.
|
static class |
ClassFile.ConstantInvokeDynamicInfo
See JVMS7 4.4.10.
|
static class |
ClassFile.ConstantMethodHandleInfo
See JVMS7 4.4.8.
|
static class |
ClassFile.ConstantMethodrefInfo
See JVMS7 4.4.2.
|
static class |
ClassFile.ConstantMethodTypeInfo
See JVMS7 4.4.9.
|
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.DoubleElementValue |
static interface |
ClassFile.ElementValue
Representation of the "element_value" structure (see JVMS8 4.7.16.1).
|
static class |
ClassFile.EnumConstValue
Representation of the "enum_const_value" element in the "element_value" structure.
|
static class |
ClassFile.ExceptionsAttribute
Representation of an
Exceptions attribute (see JVMS 4.7.4). |
class |
ClassFile.FieldInfo
Representation of a "method_info" structure, as defined by JVMS7 4.5.
|
static class |
ClassFile.FloatElementValue |
static class |
ClassFile.InnerClassesAttribute
Representation of an
InnerClasses attribute (see JVMS 4.7.5). |
static class |
ClassFile.IntElementValue |
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). |
static class |
ClassFile.LongElementValue |
class |
ClassFile.MethodInfo
Representation of a "method_info" structure, as defined by JVMS7 4.6.
|
static class |
ClassFile.ShortElementValue |
static class |
ClassFile.SourceFileAttribute
Representation of a
SourceFile attribute (see JVMS 4.7.7). |
static class |
ClassFile.StackMapTableAttribute
Representation of an unmodifiable
StackMapTable attribute, as read from a class file. |
static class |
ClassFile.StringElementValue |
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.FieldInfos of the field members of this class or interface. |
short[] |
interfaces
The constant pool indexes of
ClassFile.ConstantClassInfo which describe the interfaces that this class implements,
resp. that this interface extends. |
static short |
MAJOR_VERSION_JDK_1_1
Major version number of a class file that was generated by a Java 1.1-compliant compiler.
|
static short |
MAJOR_VERSION_JDK_1_10
Major version number of a class file that was generated by a Java 1.10-compliant compiler.
|
static short |
MAJOR_VERSION_JDK_1_11
Major version number of a class file that was generated by a Java 1.11-compliant compiler.
|
static short |
MAJOR_VERSION_JDK_1_12
Major version number of a class file that was generated by a Java 1.12-compliant compiler.
|
static short |
MAJOR_VERSION_JDK_1_2
Major version number of a class file that was generated by a Java 1.2-compliant compiler.
|
static short |
MAJOR_VERSION_JDK_1_3
Major version number of a class file that was generated by a Java 1.3-compliant compiler.
|
static short |
MAJOR_VERSION_JDK_1_4
Major version number of a class file that was generated by a Java 1.4-compliant compiler.
|
static short |
MAJOR_VERSION_JDK_1_5
Major version number of a class file that was generated by a Java 1.5-compliant compiler.
|
static short |
MAJOR_VERSION_JDK_1_6
Major version number of a class file that was generated by a Java 1.6-compliant compiler.
|
static short |
MAJOR_VERSION_JDK_1_7
Major version number of a class file that was generated by a Java 1.7-compliant compiler.
|
static short |
MAJOR_VERSION_JDK_1_8
Major version number of a class file that was generated by a Java 1.8-compliant compiler.
|
static short |
MAJOR_VERSION_JDK_1_9
Major version number of a class file that was generated by a Java 1.9-compliant compiler.
|
List<ClassFile.MethodInfo> |
methodInfos
The
ClassFile.MethodInfos of the methods of this class or interface. |
static short |
MINOR_VERSION_JDK_1_1
Minor version number of a class file that was generated by a Java 1.1-compliant compiler.
|
static short |
MINOR_VERSION_JDK_1_10
Minor version number of a class file that was generated by a Java 1.10-compliant compiler.
|
static short |
MINOR_VERSION_JDK_1_11
Minor version number of a class file that was generated by a Java 1.11-compliant compiler.
|
static short |
MINOR_VERSION_JDK_1_12
Minor version number of a class file that was generated by a Java 1.12-compliant compiler.
|
static short |
MINOR_VERSION_JDK_1_2
Minor version number of a class file that was generated by a Java 1.2-compliant compiler.
|
static short |
MINOR_VERSION_JDK_1_3
Minor version number of a class file that was generated by a Java 1.3-compliant compiler.
|
static short |
MINOR_VERSION_JDK_1_4
Minor version number of a class file that was generated by a Java 1.4-compliant compiler.
|
static short |
MINOR_VERSION_JDK_1_5
Minor version number of a class file that was generated by a Java 1.5-compliant compiler.
|
static short |
MINOR_VERSION_JDK_1_6
Minor version number of a class file that was generated by a Java 1.6-compliant compiler.
|
static short |
MINOR_VERSION_JDK_1_7
Minor version number of a class file that was generated by a Java 1.7-compliant compiler.
|
static short |
MINOR_VERSION_JDK_1_8
Minor version number of a class file that was generated by a Java 1.8-compliant compiler.
|
static short |
MINOR_VERSION_JDK_1_9
Minor version number of a class file that was generated by a Java 1.9-compliant compiler.
|
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)
Reads "class file" data from the inputStream and construct a
ClassFile object from it. |
ClassFile(short accessFlags,
String thisClassFd,
String superclassFd,
String[] interfaceFds)
Constructs a class with no fields and methods.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addAnnotationsAttributeEntry(boolean runtimeVisible,
String fieldDescriptor,
Map<Short,ClassFile.ElementValue> elementValuePairs)
Creates a
Runtime[In]visibleAnnotations attribute on the class (if it does not yet exist) and adds an
entry to it. |
short |
addConstantClassInfo(String typeFd)
Returns the constant index number for a "CONSTANT_Class_info" structure to the class file.
|
short |
addConstantDoubleInfo(double value)
Adds a "CONSTANT_Double_info" structure to the class file.
|
short |
addConstantFieldrefInfo(String classFd,
String fieldName,
String fieldFd)
Adds a "CONSTANT_Fieldref_info" structure to the class file.
|
short |
addConstantFloatInfo(float value)
Adds a "CONSTANT_Float_info" structure to the class file.
|
short |
addConstantIntegerInfo(int value)
Adds a "CONSTANT_Integer_info" structure to the class file.
|
short |
addConstantInterfaceMethodrefInfo(String classFd,
String methodName,
String methodMd)
Adds a "CONSTANT_InterfaceMethodref_info" structure to the class file.
|
short |
addConstantLongInfo(long value)
Adds a "CONSTANT_Long_info" structure to the class file.
|
short |
addConstantMethodrefInfo(String classFd,
String methodName,
String methodMd)
Adds a "CONSTANT_Methodref_info" structure to the class file.
|
short |
addConstantStringInfo(String string)
Adds 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(short accessFlags,
String fieldName,
String fieldTypeFd,
Object optionalConstantValue)
Creates a
ClassFile.FieldInfo and adds it to this class. |
void |
addInnerClassesAttributeEntry(ClassFile.InnerClassesAttribute.Entry entry)
Creates an
InnerClasses attribute if it does not exist, then adds the entry to the InnerClasses attribute. |
ClassFile.MethodInfo |
addMethodInfo(short accessFlags,
String methodName,
MethodDescriptor methodMd)
Creates a
ClassFile.MethodInfo and adds it to this class. |
void |
addSourceFileAttribute(String sourceFileName)
Adds a
SourceFile attribute to this class file. |
ClassFile.Annotation[] |
getAnnotations(boolean runtimeVisible) |
static String |
getClassFileResourceName(String className)
Constructs the name of a resource that could contain the class file of the class with the className.
|
ClassFile.ConstantClassInfo |
getConstantClassInfo(short index) |
ClassFile.ConstantFieldrefInfo |
getConstantFieldrefInfo(short index) |
ClassFile.ConstantInterfaceMethodrefInfo |
getConstantInterfaceMethodrefInfo(short index) |
ClassFile.ConstantInvokeDynamicInfo |
getConstantInvokeDynamicInfo(short index) |
ClassFile.ConstantMethodHandleInfo |
getConstantMethodHandleInfo(short index) |
ClassFile.ConstantMethodrefInfo |
getConstantMethodrefInfo(short index) |
ClassFile.ConstantMethodTypeInfo |
getConstantMethodTypeInfo(short index) |
ClassFile.ConstantNameAndTypeInfo |
getConstantNameAndTypeInfo(short index) |
ClassFile.ConstantPoolInfo |
getConstantPoolInfo(short index) |
int |
getConstantPoolSize() |
String |
getConstantUtf8(short index)
Shorthand for
getConstantUtf8Info(index).s. |
ClassFile.ConstantUtf8Info |
getConstantUtf8Info(short index) |
ClassFile.ConstantValuePoolInfo |
getConstantValuePoolInfo(short index) |
ClassFile.InnerClassesAttribute |
getInnerClassesAttribute()
Finds the
InnerClasses attribute of this class file. |
short |
getMajorVersion() |
short |
getMinorVersion() |
static String |
getSourceResourceName(String className)
Constructs the name of a resource that could contain the source code of the class with the className.
|
String |
getThisClassName() |
void |
setVersion(short majorVersion,
short minorVersion)
Sets the major and minor class file version numbers (JVMS 4.1).
|
void |
store(OutputStream os)
Writes
ClassFile to an OutputStream, in "class file" format. |
byte[] |
toByteArray()
Returns 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 static final short MAJOR_VERSION_JDK_1_6
public static final short MINOR_VERSION_JDK_1_6
public static final short MAJOR_VERSION_JDK_1_7
public static final short MINOR_VERSION_JDK_1_7
public static final short MAJOR_VERSION_JDK_1_8
public static final short MINOR_VERSION_JDK_1_8
public static final short MAJOR_VERSION_JDK_1_9
public static final short MINOR_VERSION_JDK_1_9
public static final short MAJOR_VERSION_JDK_1_10
public static final short MINOR_VERSION_JDK_1_10
public static final short MAJOR_VERSION_JDK_1_11
public static final short MINOR_VERSION_JDK_1_11
public static final short MAJOR_VERSION_JDK_1_12
public static final short MINOR_VERSION_JDK_1_12
public final short accessFlags
and consortspublic 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 describe the interfaces that this class implements,
resp. that this interface extends.public final List<ClassFile.FieldInfo> fieldInfos
ClassFile.FieldInfos of the field members of this class or interface.public final List<ClassFile.MethodInfo> methodInfos
ClassFile.MethodInfos of the methods of this class or interface.public ClassFile(short accessFlags,
String thisClassFd,
@Nullable
String superclassFd,
String[] interfaceFds)
addFieldInfo(short, String, String, Object)
and addMethodInfo(short, String, MethodDescriptor) before saving it.accessFlags - as defined by ModthisClassFd - 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
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.
IOExceptionpublic void addSourceFileAttribute(String sourceFileName)
SourceFile attribute to this class file. (Does not check whether one exists already.)sourceFileName - public void addDeprecatedAttribute()
Deprecated attribute to this class.@Nullable public ClassFile.InnerClassesAttribute getInnerClassesAttribute()
InnerClasses attribute of this class file.null if this class has no "InnerClasses" attributepublic void addInnerClassesAttributeEntry(ClassFile.InnerClassesAttribute.Entry entry)
InnerClasses attribute if it does not exist, then adds the entry to the InnerClasses attribute.public ClassFile.Annotation[] getAnnotations(boolean runtimeVisible)
getAnnotations in interface Annotatablepublic void addAnnotationsAttributeEntry(boolean runtimeVisible,
String fieldDescriptor,
Map<Short,ClassFile.ElementValue> elementValuePairs)
Runtime[In]visibleAnnotations attribute on the class (if it does not yet exist) and adds an
entry to it.addAnnotationsAttributeEntry in interface AnnotatableelementValuePairs - Maps element-name constant-pool-index (ClassFile.ConstantUtf8Info) to element valuepublic 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(short accessFlags, String fieldName, String fieldTypeFd, @Nullable 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(short accessFlags, String methodName, MethodDescriptor 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)
public ClassFile.ConstantClassInfo getConstantClassInfo(short index)
public ClassFile.ConstantFieldrefInfo getConstantFieldrefInfo(short index)
public ClassFile.ConstantInterfaceMethodrefInfo getConstantInterfaceMethodrefInfo(short index)
public ClassFile.ConstantInvokeDynamicInfo getConstantInvokeDynamicInfo(short index)
public ClassFile.ConstantMethodHandleInfo getConstantMethodHandleInfo(short index)
public ClassFile.ConstantMethodrefInfo getConstantMethodrefInfo(short index)
public ClassFile.ConstantMethodTypeInfo getConstantMethodTypeInfo(short index)
public ClassFile.ConstantNameAndTypeInfo getConstantNameAndTypeInfo(short index)
public ClassFile.ConstantUtf8Info getConstantUtf8Info(short index)
public ClassFile.ConstantValuePoolInfo getConstantValuePoolInfo(short index)
public int getConstantPoolSize()
public String getConstantUtf8(short index)
getConstantUtf8Info(index).s.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.
IOExceptionpublic 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""pkg1/pkg2/Outer.java"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 © 2019. All rights reserved.