Class ClassFile
- java.lang.Object
-
- org.codehaus.janino.util.ClassFile
-
public class ClassFile extends Object
An object that represents the Java™ "class file" format.ClassFile(InputStream)creates aClassFileobject from the bytecode read from the givenInputStream.store(OutputStream)generates Java™ bytecode which is suitable for being processed by a Java™ virtual machine.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classClassFile.AttributeInfoRepresentation of a class file attribute (see JVMS7 4.7).static classClassFile.ConstantClassInfoSee JVMS7 4.4.1.static classClassFile.ConstantFieldrefInfoSee JVMS7 4.4.2.static classClassFile.ConstantInterfaceMethodrefInfoSee JVMS7 4.4.2.static classClassFile.ConstantMethodrefInfoSee JVMS7 4.4.2.static classClassFile.ConstantNameAndTypeInfoSee JVMS7 4.4.6.static classClassFile.ConstantPoolInfoBase for various the constant pool table entry types.static classClassFile.ConstantUtf8InfoSee JVMS7 4.4.7.static classClassFile.ConstantValueAttributeRepresentation of a "ConstantValue" attribute (see JVMS 4.7.2).static classClassFile.ConstantValuePoolInfoIntermediate base class for constant pool table entry types that have 'value' semantics: Double, Float, Integer, Long, Stringstatic classClassFile.DeprecatedAttributeRepresentation of a "Deprecated" attribute (see JVMS 4.7.10).static classClassFile.ExceptionsAttributeRepresentation of an "Exceptions" attribute (see JVMS 4.7.4).static classClassFile.FieldInfoRepresentation of a "method_info" structure, as defined by JVMS7 4.5.static classClassFile.InnerClassesAttributeRepresentation of an "InnerClasses" attribute (see JVMS 4.7.5).static classClassFile.LineNumberTableAttributeRepresentation of a "LineNumberTable" attribute (see JVMS 4.7.8).static classClassFile.LocalVariableTableAttributeRepresentation of a "LocalVariableTable" attribute (see JVMS 4.7.9).classClassFile.MethodInfoRepresentation of a "method_info" structure, as defined by JVMS7 4.6.static classClassFile.SourceFileAttributeRepresentation of a "SourceFile" attribute (see JVMS 4.7.7).static classClassFile.SyntheticAttributeRepresentation of a "Synthetic" attribute (see JVMS 4.7.6).
-
Field Summary
Fields Modifier and Type Field Description shortaccessFlagsThe access flags of the class.List<ClassFile.FieldInfo>fieldInfosTheClassFile.FieldInfos of the field members of this class or interface.short[]interfacesThe constant pool indexes ofClassFile.ConstantClassInfowhich describes the interfaces that this class implements, resp.static shortMAJOR_VERSION_JDK_1_1static shortMAJOR_VERSION_JDK_1_2static shortMAJOR_VERSION_JDK_1_3static shortMAJOR_VERSION_JDK_1_4static shortMAJOR_VERSION_JDK_1_5List<ClassFile.MethodInfo>methodInfosTheClassFile.MethodInfos of the methods of this class or interface.static shortMINOR_VERSION_JDK_1_1static shortMINOR_VERSION_JDK_1_2static shortMINOR_VERSION_JDK_1_3static shortMINOR_VERSION_JDK_1_4static shortMINOR_VERSION_JDK_1_5shortsuperclassThe constant pool index of theClassFile.ConstantClassInfothat describes the superclass of THIS class.shortthisClassThe constant pool index of theClassFile.ConstantClassInfothat describes THIS class.
-
Constructor Summary
Constructors Constructor Description ClassFile(short accessFlags, String thisClassFd, String superclassFd, String[] interfaceFds)Construct from parsed components.ClassFile(InputStream inputStream)Read "class file" data from aInputStreamand construct aClassFileobject from it.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description shortaddConstantClassInfo(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.shortaddConstantUtf8Info(String s)Adds a "CONSTANT_Utf8_info" structure to the class file if no equal entry exists.voidaddDeprecatedAttribute()Adds the "Deprecated" attribute to this class.ClassFile.FieldInfoaddFieldInfo(Java.Modifiers modifiers, String fieldName, String fieldTypeFd, Object optionalConstantValue)Creates aClassFile.FieldInfoand adds it to this class.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(Java.Modifiers modifiers, String methodName, String methodMd)Creates aClassFile.MethodInfoand adds it to this class.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.ClassFile.ConstantPoolInfogetConstantPoolInfo(short index)intgetConstantPoolSize()StringgetConstantUtf8(short index)ClassFile.InnerClassesAttributegetInnerClassesAttribute()Find the "InnerClasses" attribute of this class fileshortgetMajorVersion()shortgetMinorVersion()static StringgetSourceResourceName(String className)Construct the name of a resource that could contain the source code of the class with the given name.StringgetThisClassName()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[]toByteArray()Return the byte code of thisClassFileas a byte array.
-
-
-
Field Detail
-
MAJOR_VERSION_JDK_1_1
public static final short MAJOR_VERSION_JDK_1_1
- See Also:
- Constant Field Values
-
MINOR_VERSION_JDK_1_1
public static final short MINOR_VERSION_JDK_1_1
- See Also:
- Constant Field Values
-
MAJOR_VERSION_JDK_1_2
public static final short MAJOR_VERSION_JDK_1_2
- See Also:
- Constant Field Values
-
MINOR_VERSION_JDK_1_2
public static final short MINOR_VERSION_JDK_1_2
- See Also:
- Constant Field Values
-
MAJOR_VERSION_JDK_1_3
public static final short MAJOR_VERSION_JDK_1_3
- See Also:
- Constant Field Values
-
MINOR_VERSION_JDK_1_3
public static final short MINOR_VERSION_JDK_1_3
- See Also:
- Constant Field Values
-
MAJOR_VERSION_JDK_1_4
public static final short MAJOR_VERSION_JDK_1_4
- See Also:
- Constant Field Values
-
MINOR_VERSION_JDK_1_4
public static final short MINOR_VERSION_JDK_1_4
- See Also:
- Constant Field Values
-
MAJOR_VERSION_JDK_1_5
public static final short MAJOR_VERSION_JDK_1_5
- See Also:
- Constant Field Values
-
MINOR_VERSION_JDK_1_5
public static final short MINOR_VERSION_JDK_1_5
- See Also:
- Constant Field Values
-
accessFlags
public final short accessFlags
The access flags of the class.- See Also:
and consorts
-
thisClass
public final short thisClass
The constant pool index of theClassFile.ConstantClassInfothat describes THIS class.
-
superclass
public final short superclass
The constant pool index of theClassFile.ConstantClassInfothat describes the superclass of THIS class. Zero for classObject,Objectfor interfaces.
-
interfaces
public final short[] interfaces
The constant pool indexes ofClassFile.ConstantClassInfowhich describes the interfaces that this class implements, resp. that this interface extends.
-
fieldInfos
public final List<ClassFile.FieldInfo> fieldInfos
TheClassFile.FieldInfos of the field members of this class or interface.
-
methodInfos
public final List<ClassFile.MethodInfo> methodInfos
TheClassFile.MethodInfos of the methods of this class or interface.
-
-
Constructor Detail
-
ClassFile
public ClassFile(short accessFlags, String thisClassFd, String superclassFd, String[] interfaceFds)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
public ClassFile(InputStream inputStream) throws IOException
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.- Throws:
IOException
-
-
Method Detail
-
addSourceFileAttribute
public void addSourceFileAttribute(String sourceFileName)
Adds a "SourceFile" attribute to this class file. (Does not check whether one exists already.)- Parameters:
sourceFileName-
-
addDeprecatedAttribute
public void addDeprecatedAttribute()
Adds the "Deprecated" attribute to this class.
-
getInnerClassesAttribute
public ClassFile.InnerClassesAttribute getInnerClassesAttribute()
Find the "InnerClasses" attribute of this class file- Returns:
nullif this class has no "InnerClasses" attribute
-
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-
-
getThisClassName
public String 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.
-
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
public short addConstantClassInfo(String typeFd)
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:
- JVM specification, section 4.4.1
-
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
-
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
-
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
-
addConstantStringInfo
public short addConstantStringInfo(String string)
Add a "CONSTANT_String_info" structure to the class file.- See Also:
- JVM specification, section 4.4.3
-
addConstantIntegerInfo
public short addConstantIntegerInfo(int value)
Add a "CONSTANT_Integer_info" structure to the class file.- See Also:
- JVM specification, section 4.4.4
-
addConstantFloatInfo
public short addConstantFloatInfo(float value)
Add a "CONSTANT_Float_info" structure to the class file.- See Also:
- JVM specification, section 4.4.4
-
addConstantLongInfo
public short addConstantLongInfo(long value)
Add a "CONSTANT_Long_info" structure to the class file.- See Also:
- JVM specification, section 4.4.5
-
addConstantDoubleInfo
public short addConstantDoubleInfo(double value)
Add a "CONSTANT_Double_info" structure to the class file.- See Also:
- JVM specification, section 4.4.5
-
addConstantUtf8Info
public short addConstantUtf8Info(String s)
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:
- JVM specification, section 4.4.7
-
addFieldInfo
public ClassFile.FieldInfo addFieldInfo(Java.Modifiers modifiers, String fieldName, String fieldTypeFd, Object optionalConstantValue)
Creates aClassFile.FieldInfoand adds it to this class. The return value can be used e.g. to add attributes (Deprecated, ...) to the field.
-
addMethodInfo
public ClassFile.MethodInfo addMethodInfo(Java.Modifiers modifiers, String methodName, String methodMd)
Creates aClassFile.MethodInfoand adds it to this class. The return value can be used e.g. to add attributes (Code,Deprecated,Exceptions, ...) to the method.
-
getConstantPoolInfo
public ClassFile.ConstantPoolInfo getConstantPoolInfo(short index)
- Returns:
- The (read-only) constant pool entry indexed by
index
-
getConstantPoolSize
public int getConstantPoolSize()
- Returns:
- The size of the constant pool
-
getConstantUtf8
public String getConstantUtf8(short index)
- Parameters:
index- Index to aCONSTANT_Utf8_infoin the constant pool- Returns:
- The string represented by the structure
-
store
public void store(OutputStream os) throws IOException
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
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"
-
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"
-
toByteArray
public byte[] toByteArray()
Return the byte code of thisClassFileas a byte array.
-
-