Top Description Inners Fields Constructors Methods
org.codehaus.janino.util

public Class ClassFile

extends Object
implements Annotatable
Class Inheritance
All Implemented Interfaces
org.codehaus.janino.util.Annotatable
Imports
java.io.ByteArrayInputStream, .ByteArrayOutputStream, .DataInputStream, .DataOutputStream, .IOException, .InputStream, .OutputStream, .UTFDataFormatException, java.util.ArrayList, .Arrays, .Collections, .HashMap, .List, .Map, java.util.regex.Matcher, .Pattern, org.codehaus.commons.nullanalysis.Nullable, org.codehaus.janino.Descriptor, .MethodDescriptor, .Mod

An object that implements the Java "class file" format.

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.

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
ClassFile.Annotation

The structure of the annotations array as described in JVMS8 4.7.16.

public static class
ClassFile.AnnotationDefaultAttribute

Representation of an AnnotationDefault attribute (see JVMS8 4.7.22).

public static class
ClassFile.AnnotationsAttribute

Representation of a Runtime[In]visibleAnnotations attribute (see JVMS8 4.7.16/17).

public static class
ClassFile.ArrayElementValue

Representation of the "array_value" structure.

public abstract static class
ClassFile.AttributeInfo

Representation of a class file attribute (see JVMS7 4.7).

public static class
public static class
public static class
public static class
public static class
ClassFile.ClassFileException

Unchecked exception that represents an error condition that could occur during processing of class files, e.g. reading a corrupt class file, or an overflow of the constant pool.

public static class
ClassFile.CodeAttribute

Representation of an unmodifiable Code attribute, as read from a class file.

public static class
private static class
public abstract static class
ClassFile.ConstantElementValue

Convenience class for element values that are constants (as opposed to annotations, enum constants and arrays).

public static class
private static class
private static class
public static class
public static class
private static class
public static class
public static class
public static class
public static class
public abstract static class
ClassFile.ConstantPoolInfo

Base for various the constant pool table entry types.

pack-priv static class
public static class
public static class
ClassFile.ConstantValueAttribute

Representation of a ConstantValue attribute (see JVMS 4.7.2).

public abstract static class
ClassFile.ConstantValuePoolInfo

Intermediate base class for constant pool table entry types that have 'value' semantics: Double, Float, Integer, Long, String

public static class
ClassFile.DeprecatedAttribute

Representation of a Deprecated attribute (see JVMS 4.7.10).

public static class
public static interface
ClassFile.ElementValue

Representation of the "element_value" structure (see JVMS8 4.7.16.1).

public static class
ClassFile.EnumConstValue

Representation of the "enum_const_value" element in the "element_value" structure.

public static class
ClassFile.ExceptionsAttribute

Representation of an Exceptions attribute (see JVMS 4.7.4).

public class
ClassFile.FieldInfo

Representation of a "method_info" structure, as defined by JVMS7 4.5.

public static class
public static class
ClassFile.InnerClassesAttribute

Representation of an InnerClasses attribute (see JVMS 4.7.5).

public static class
public static class
ClassFile.LineNumberTableAttribute

Representation of a LineNumberTable attribute (see JVMS 4.7.8).

public static class
ClassFile.LocalVariableTableAttribute

Representation of a LocalVariableTable attribute (see JVMS 4.7.9).

public static class
public class
ClassFile.MethodInfo

Representation of a "method_info" structure, as defined by JVMS7 4.6.

public static class
public static class
ClassFile.SignatureAttribute

Representation of a Signature attribute (see JVMS 4.7.9).

public static class
ClassFile.SourceFileAttribute

Representation of a SourceFile attribute (see JVMS 4.7.7).

public static class
ClassFile.StackMapTableAttribute

Representation of an unmodifiable StackMapTable attribute, as read from a class file.

public static class
public static class
ClassFile.SyntheticAttribute

Representation of a Synthetic attribute (see JVMS 4.7.6).

Field Summary

Modifier and TypeField and Description
public final short
accessFlags

The access flags of the class.

private final List<ClassFile.AttributeInfo>
attributes

The AttributeInfos of the attributes of this class or interface.

private static final int
private final List<ClassFile.ConstantPoolInfo>
private final Map<ClassFile.ConstantPoolInfo, Short>
public final List<ClassFile.FieldInfo>
fieldInfos

The FieldInfos of the field members of this class or interface.

public final short[]
interfaces

The constant pool indexes of ConstantClassInfo which describe the interfaces that this class implements, resp.

public static final short
MAJOR_VERSION_JDK_1_1

Major version number of a class file that was generated by a Java 1.1-compliant compiler.

public static final short
MAJOR_VERSION_JDK_1_10

Major version number of a class file that was generated by a Java 1.10-compliant compiler.

public static final short
MAJOR_VERSION_JDK_1_11

Major version number of a class file that was generated by a Java 1.11-compliant compiler.

public static final short
MAJOR_VERSION_JDK_1_12

Major version number of a class file that was generated by a Java 1.12-compliant compiler.

public static final short
MAJOR_VERSION_JDK_1_2

Major version number of a class file that was generated by a Java 1.2-compliant compiler.

public static final short
MAJOR_VERSION_JDK_1_3

Major version number of a class file that was generated by a Java 1.3-compliant compiler.

public static final short
MAJOR_VERSION_JDK_1_4

Major version number of a class file that was generated by a Java 1.4-compliant compiler.

public static final short
MAJOR_VERSION_JDK_1_5

Major version number of a class file that was generated by a Java 1.5-compliant compiler.

public static final short
MAJOR_VERSION_JDK_1_6

Major version number of a class file that was generated by a Java 1.6-compliant compiler.

public static final short
MAJOR_VERSION_JDK_1_7

Major version number of a class file that was generated by a Java 1.7-compliant compiler.

public static final short
MAJOR_VERSION_JDK_1_8

Major version number of a class file that was generated by a Java 1.8-compliant compiler.

public static final short
MAJOR_VERSION_JDK_1_9

Major version number of a class file that was generated by a Java 1.9-compliant compiler.

private short
public final List<ClassFile.MethodInfo>
methodInfos

The MethodInfos of the methods of this class or interface.

public static final short
MINOR_VERSION_JDK_1_1

Minor version number of a class file that was generated by a Java 1.1-compliant compiler.

public static final short
MINOR_VERSION_JDK_1_10

Minor version number of a class file that was generated by a Java 1.10-compliant compiler.

public static final short
MINOR_VERSION_JDK_1_11

Minor version number of a class file that was generated by a Java 1.11-compliant compiler.

public static final short
MINOR_VERSION_JDK_1_12

Minor version number of a class file that was generated by a Java 1.12-compliant compiler.

public static final short
MINOR_VERSION_JDK_1_2

Minor version number of a class file that was generated by a Java 1.2-compliant compiler.

public static final short
MINOR_VERSION_JDK_1_3

Minor version number of a class file that was generated by a Java 1.3-compliant compiler.

public static final short
MINOR_VERSION_JDK_1_4

Minor version number of a class file that was generated by a Java 1.4-compliant compiler.

public static final short
MINOR_VERSION_JDK_1_5

Minor version number of a class file that was generated by a Java 1.5-compliant compiler.

public static final short
MINOR_VERSION_JDK_1_6

Minor version number of a class file that was generated by a Java 1.6-compliant compiler.

public static final short
MINOR_VERSION_JDK_1_7

Minor version number of a class file that was generated by a Java 1.7-compliant compiler.

public static final short
MINOR_VERSION_JDK_1_8

Minor version number of a class file that was generated by a Java 1.8-compliant compiler.

public static final short
MINOR_VERSION_JDK_1_9

Minor version number of a class file that was generated by a Java 1.9-compliant compiler.

private short
public final short
superclass

The constant pool index of the ConstantClassInfo that describes the superclass of this class.

public final short
thisClass

The constant pool index of the ConstantClassInfo that describes this class.

Constructor Summary

AccessConstructor and Description
public
ClassFile(short accessFlags, String
The field descriptor for this class
thisClassFd
,
String
The field descriptor for the extended class (e.g. "Ljava/lang/Object;"); null for Object
superclassFd
,
String[]
The field descriptors for the implemented interfaces
interfaceFds
)

Constructs a class with no fields and methods.

public
ClassFile(InputStream inputStream)

Reads "class file" data from the inputStream and construct a ClassFile object from it.

Method Summary

Modifier and TypeMethod and Description
public void
addAnnotationsAttributeEntry(boolean runtimeVisible, String fieldDescriptor, Map<Short, ClassFile.ElementValue>
Maps element-name constant-pool-index (ConstantUtf8Info) to element value
elementValuePairs
)

Implements org.codehaus.janino.util.Annotatable.addAnnotationsAttributeEntry.

Creates a Runtime[In]visibleAnnotations attribute on the class (if it does not yet exist) and adds an entry to it.
private void
addAnnotationsAttributeEntry(boolean runtimeVisible, String fieldDescriptor, Map<Short, ClassFile.ElementValue>
Maps "elemant_name_index" (ConstantUtf8Info) to "element_value", see JVMS8 4.7.16
elementValuePairs
,
List<ClassFile.AttributeInfo> target)

Adds a Runtime[In]visibleAnnotations attribute to the target (if it does not yet exist) and adds an entry to it.

public short
addConstantClassInfo(String typeFd)

Returns the constant index number for a "CONSTANT_Class_info" structure to the class file.

public short
addConstantDoubleInfo(final double value)

Adds a "CONSTANT_Double_info" structure to the class file.

public short
addConstantFieldrefInfo(String classFd, String fieldName, String fieldFd)

Adds a "CONSTANT_Fieldref_info" structure to the class file.

public short
addConstantFloatInfo(final float value)

Adds a "CONSTANT_Float_info" structure to the class file.

public short
addConstantIntegerInfo(final int value)

Adds a "CONSTANT_Integer_info" structure to the class file.

public short
addConstantInterfaceMethodrefInfo(String classFd, String methodName, String methodMd)

Adds a "CONSTANT_InterfaceMethodref_info" structure to the class file.

public short
addConstantLongInfo(final long value)

Adds a "CONSTANT_Long_info" structure to the class file.

public short
addConstantMethodrefInfo(String classFd, String methodName, String methodMd)

Adds a "CONSTANT_Methodref_info" structure to the class file.

private short
addConstantNameAndTypeInfo(String name, String descriptor)

Adds a "CONSTANT_NameAndType_info" structure to the class file.

private short
addConstantSifldInfo(Object cv)

Convenience method that adds a String, Integer, Float, Long or Double ConstantInfo.

public short
addConstantStringInfo(String string)

Adds a "CONSTANT_String_info" structure to the class file.

public short

Returns:

The index of the already existing or newly created entry
addConstantUtf8Info
(final String s)

Adds a "CONSTANT_Utf8_info" structure to the class file if no equal entry exists.

public void
addDeprecatedAttribute()

Adds the Deprecated attribute to this class.

public ClassFile.FieldInfo
addFieldInfo(short accessFlags, String fieldName, String fieldTypeFd, Object constantValue)

Creates a FieldInfo and adds it to this class.

public void
addInnerClassesAttributeEntry(ClassFile.InnerClassesAttribute.Entry entry)

Creates an InnerClasses attribute if it does not exist, then adds the entry to the InnerClasses attribute.

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

Creates a MethodInfo and adds it to this class.

public void
addSourceFileAttribute(String sourceFileName)

Adds a SourceFile attribute to this class file.

private short
addToConstantPool(ClassFile.ConstantPoolInfo cpi)

Adds an entry to the constant pool and returns its index, or, if an equal entry already exists in the constant pool, returns the index of that entry.

private ClassFile.AttributeInfo

Returns:

null iff the attributes constain no attribute with that name
findAttribute
(List<ClassFile.AttributeInfo> attributes, String attributeName)

Finds the named attribute in the attributes.

public ClassFile.Annotation[]
private ClassFile.AnnotationsAttribute

Returns:

null if attributes constains no such attribute
getAnnotationsAttribute
(boolean runtimeVisible, List<ClassFile.AttributeInfo> attributes)

Finds the Runtime[In]visibleAnnotations attribute in the attributes.

public static String

Returns:

the name of the resource, e.g. "pkg1/pkg2/Outer$Inner.class"
getClassFileResourceName
(String
Fully qualified class name, e.g. "pkg1.pkg2.Outer$Inner"
className
)

Constructs the name of a resource that could contain the class file of the class with the className.

public ClassFile.ConstantClassInfo

Returns:

The (read-only) constant class info indexed by index
getConstantClassInfo
(short index)

public ClassFile.ConstantFieldrefInfo

Returns:

The (read-only) constant fieldref info indexed by index
getConstantFieldrefInfo
(short index)

public ClassFile.ConstantInterfaceMethodrefInfo

Returns:

The (read-only) constant interface methodref info indexed by index
getConstantInterfaceMethodrefInfo
(short index)

public ClassFile.ConstantInvokeDynamicInfo

Returns:

The (read-only) constant invoke dynamic info indexed by index
getConstantInvokeDynamicInfo
(short index)

public ClassFile.ConstantMethodHandleInfo

Returns:

The (read-only) constant method handle info indexed by index
getConstantMethodHandleInfo
(short index)

public ClassFile.ConstantMethodrefInfo

Returns:

The (read-only) constant methodref info indexed by index
getConstantMethodrefInfo
(short index)

public ClassFile.ConstantMethodTypeInfo

Returns:

The (read-only) constant method type info indexed by index
getConstantMethodTypeInfo
(short index)

public ClassFile.ConstantNameAndTypeInfo

Returns:

The (read-only) constant name and type info indexed by index
getConstantNameAndTypeInfo
(short index)

public ClassFile.ConstantPoolInfo

Returns:

The (read-only) constant pool entry indexed by index
getConstantPoolInfo
(short index)

public int

Returns:

The size of the constant pool
getConstantPoolSize
()

public String

Returns:

The string represented by the structure
getConstantUtf8
(short
Index to a CONSTANT_Utf8_info in the constant pool
index
)

Shorthand for getConstantUtf8Info(index).s.

public ClassFile.ConstantUtf8Info

Returns:

The (read-only) constant utf8 info indexed by index
getConstantUtf8Info
(short index)

public ClassFile.ConstantValuePoolInfo

Returns:

The (read-only) constant value pool info indexed by index
getConstantValuePoolInfo
(short index)

public ClassFile.InnerClassesAttribute

Returns:

null if this class has no "InnerClasses" attribute
getInnerClassesAttribute
()

Finds the InnerClasses attribute of this class file.

public short

Returns:

The current major class file version number
getMajorVersion
()

public short

Returns:

The current minor class file version number
getMinorVersion
()

public ClassFile.SignatureAttribute
public static String

Returns:

the name of the resource, e.g. "pkg1/pkg2/Outer.java"
getSourceResourceName
(String
Fully qualified class name, e.g. "pkg1.pkg2.Outer$Inner"
className
)

Constructs the name of a resource that could contain the source code of the class with the className.

public String

Returns:

The fully qualified name of this class, e.g. "pkg1.pkg2.Outer$Inner"
getThisClassName
()

private ClassFile.AttributeInfo
loadAttribute(DataInputStream dis)

Loads one class file attribute.

private List<ClassFile.AttributeInfo>
loadAttributes(DataInputStream dis)

u2 attributes_count, attributes[attributes_count]

private void
loadConstantPool(DataInputStream dis)

u2 constant_pool_count, constant_pool[constant_pool_count]

private static ClassFile.ElementValue
private List<ClassFile.FieldInfo>
loadFields(DataInputStream dis)

u2 fields_count, fields[fields_count]

private ClassFile.MethodInfo
private List<ClassFile.MethodInfo>
loadMethods(DataInputStream dis)

u2 methods_count, methods[methods_count]

public ClassFile.StackMapTableAttribute.ObjectVariableInfo
newObjectVariableInfo(String fieldDescriptor)

public ClassFile.StackMapTableAttribute.UninitializedVariableInfo
private static byte[]
readLengthAndBytes(DataInputStream dis)

u4 length, u1[length]

private static short[]
readShortArray(DataInputStream dis)

u2 length, u2[length]

public void
setVersion(short majorVersion, short minorVersion)

Sets the major and minor class file version numbers (JVMS 4.1).

public void
store(OutputStream os)

Writes ClassFile to an OutputStream, in "class file" format.

private static void
storeAttributes(DataOutputStream dos, List<ClassFile.AttributeInfo> attributeInfos)

u2 attributes_count, attributes[attributes_count]

private static void
storeConstantPool(DataOutputStream dos, List<ClassFile.ConstantPoolInfo> constantPool)

u2 constant_pool_count, constant_pool[constant_pool_count - 1]

private static void
storeFields(DataOutputStream dos, List<ClassFile.FieldInfo> fieldInfos)

u2 fields_count, fields[fields_count]

private static void
storeMethods(DataOutputStream dos, List<ClassFile.MethodInfo> methodInfos)

u2 methods_count, methods[methods_count]

private static void
storeShortArray(DataOutputStream dos, short[] sa)

u2 count, u2[count]

public byte[]
toByteArray()

Returns the byte code of this ClassFile as a byte array.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait