Class ClassWriter
java.lang.Object
com.ibm.wala.shrike.shrikeCT.ClassWriter
- All Implemented Interfaces:
ClassConstants
This class formats and writes class data into JVM format.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classstatic classAn Element is an object that can be serialized into a byte buffer.static final classA RawElement is an Element that is already available as some chunk of a byte buffer. -
Field Summary
Fields inherited from interface ClassConstants
ACC_ABSTRACT, ACC_FINAL, ACC_INTERFACE, ACC_NATIVE, ACC_PRIVATE, ACC_PROTECTED, ACC_PUBLIC, ACC_STATIC, ACC_STRICT, ACC_SUPER, ACC_SYNCHRONIZED, ACC_TRANSIENT, ACC_VOLATILE, CONSTANT_Class, CONSTANT_Double, CONSTANT_FieldRef, CONSTANT_Float, CONSTANT_Integer, CONSTANT_InterfaceMethodRef, CONSTANT_InvokeDynamic, CONSTANT_Long, CONSTANT_MethodHandle, CONSTANT_MethodRef, CONSTANT_MethodType, CONSTANT_Module, CONSTANT_NameAndType, CONSTANT_Package, CONSTANT_String, CONSTANT_Utf8, MAGIC, REF_getField, REF_getStatic, REF_invokeInterface, REF_invokeSpecial, REF_invokeStatic, REF_invokeVirtual, REF_newInvokeSpecial, REF_putField, REF_putStatic -
Constructor Summary
ConstructorsConstructorDescriptionCreate a blank ClassWriter with no methods, fields, or attributes, an empty constant pool, no super class, no implemented interfaces, no name, majorVersion 46, and minorVersion 0. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddClassAttribute(ClassWriter.Element attribute) Add an attribute to the class.intaddCPClass(String s) Add a Class to the constant pool if necessary.intaddCPDouble(double d) Add a Double to the constant pool if necessary.protected intaddCPEntry(Object o, int size) intaddCPFieldRef(String c, String n, String t) Add a FieldRef to the constant pool if necessary.intaddCPFloat(float f) Add a Float to the constant pool if necessary.intaddCPInt(int i) Add an Integer to the constant pool if necessary.intaddCPInterfaceMethodRef(String c, String n, String t) Add an InterfaceMethodRef to the constant pool if necessary.intAdd an InvokeDynamic to the constant pool if necessary.intaddCPLong(long l) Add a Long to the constant pool if necessary.intintaddCPMethodRef(String c, String n, String t) Add a MethodRef to the constant pool if necessary.intAdd a Class to the constant pool if necessary.intAdd a NameAndType to the constant pool if necessary.intAdd a String to the constant pool if necessary.intAdd a Utf8 string to the constant pool if necessary.voidaddField(int access, int name, int type, ClassWriter.Element[] attributes) Add a field to the class.voidaddField(int access, String name, String type, ClassWriter.Element[] attributes) Add a field to the class.voidaddMethod(int access, int name, int type, ClassWriter.Element[] attributes) Add a method to the class.voidaddMethod(int access, String name, String type, ClassWriter.Element[] attributes) Add a method to the class.voidAdd a field to the class, the field data given as "raw" bytes (probably obtained from a ClassReader).voidAdd a method to the class, the method data given as "raw" bytes (probably obtained from a ClassReader).byte[]After you've added everything you need to the class, call this method to generate the actual class file data.voidsetAccessFlags(int f) Set the access flags for the class.static voidsetDouble(byte[] buf, int offset, double v) Set the 8 bytes at offset 'offset' in 'buf' to the double value in v.static voidsetFloat(byte[] buf, int offset, float v) Set the 4 bytes at offset 'offset' in 'buf' to the float value in v.voidsetForceAddCPEntries(boolean force) static voidsetInt(byte[] buf, int offset, int v) Set the 4 bytes at offset 'offset' in 'buf' to the signed 32-bit value in v.voidsetInterfaceNameIndices(int[] interfaces) Set the constant pool indices for the names of the implemented interfaces.voidsetInterfaceNames(String[] interfaces) Set the names of the implemented interfaces.static voidsetLong(byte[] buf, int offset, long v) Set the 8 bytes at offset 'offset' in 'buf' to the signed 64-bit value in v.voidsetMajorVersion(int major) Set the class file format major version.voidsetMinorVersion(int minor) Set the class file format minor version.voidSet the name of the class.voidsetNameIndex(int c) Set the constant pool index for the name of the class.voidsetRawCP(ConstantPoolParser cp, boolean cacheEntries) Copy a constant pool from some ClassReader into this class.voidSet the name of the superclass; if c is null, then there is no superclass (this must be java/lang/Object).voidsetSuperNameIndex(int c) Set the constant pool index for the name of the superclass.static voidsetUByte(byte[] buf, int offset, int v) Set the byte at offset 'offset' in 'buf' to the unsigned 8-bit value in v.static voidsetUShort(byte[] buf, int offset, int v) Set the 2 bytes at offset 'offset' in 'buf' to the unsigned 16-bit value in v.
-
Constructor Details
-
ClassWriter
public ClassWriter()Create a blank ClassWriter with no methods, fields, or attributes, an empty constant pool, no super class, no implemented interfaces, no name, majorVersion 46, and minorVersion 0.
-
-
Method Details
-
setMajorVersion
public void setMajorVersion(int major) Set the class file format major version. You probably don't want to use this unless you really know what you are doing. -
setMinorVersion
public void setMinorVersion(int minor) Set the class file format minor version. You probably don't want to use this unless you really know what you are doing. -
setRawCP
public void setRawCP(ConstantPoolParser cp, boolean cacheEntries) throws InvalidClassFileException, IllegalArgumentException Copy a constant pool from some ClassReader into this class. This must be done before any entries are allocated in this ClassWriter's constant pool, and it can only be done once. If and only if this is done, it is safe to copy "raw" fields, methods and attributes from the ClassReader into this class, because the constant pool references in those fields, methods and attributes are guaranteed to point to the same constant pool items in this new class.- Parameters:
cacheEntries- records whether to parse the raw constant pool completely so that if new entries are required which are the same as entries already in the raw pool, the existing entries in the raw pool are used instead. Setting this to 'true' produces smaller constant pools but may slow down performance because the raw pool must be completely parsed- Throws:
InvalidClassFileExceptionIllegalArgumentException
-
setForceAddCPEntries
public void setForceAddCPEntries(boolean force) - Parameters:
force- true iff you want the addCP methods to always create a new constant pool entry and never reuse an existing constant pool entry
-
addCPEntry
-
addCPUtf8
Add a Utf8 string to the constant pool if necessary.- Returns:
- the index of a constant pool item with the right value
-
addCPInt
public int addCPInt(int i) Add an Integer to the constant pool if necessary.- Returns:
- the index of a constant pool item with the right value
-
addCPFloat
public int addCPFloat(float f) Add a Float to the constant pool if necessary.- Returns:
- the index of a constant pool item with the right value
-
addCPLong
public int addCPLong(long l) Add a Long to the constant pool if necessary.- Returns:
- the index of a constant pool item with the right value
-
addCPDouble
public int addCPDouble(double d) Add a Double to the constant pool if necessary.- Returns:
- the index of a constant pool item with the right value
-
addCPMethodHandle
-
addCPString
Add a String to the constant pool if necessary.- Returns:
- the index of a constant pool item with the right value
-
addCPClass
Add a Class to the constant pool if necessary.- Parameters:
s- the class name, in JVM format (e.g., java/lang/Object)- Returns:
- the index of a constant pool item with the right value
-
addCPMethodType
Add a Class to the constant pool if necessary.- Parameters:
s- the class name, in JVM format (e.g., java/lang/Object)- Returns:
- the index of a constant pool item with the right value
-
addCPFieldRef
Add a FieldRef to the constant pool if necessary.- Parameters:
c- the class name, in JVM format (e.g., java/lang/Object)n- the field namet- the field type, in JVM format (e.g., I, Z, or Ljava/lang/Object;)- Returns:
- the index of a constant pool item with the right value
-
addCPMethodRef
Add a MethodRef to the constant pool if necessary.- Parameters:
c- the class name, in JVM format (e.g., java/lang/Object)n- the method namet- the method type, in JVM format (e.g., V(ILjava/lang/Object;) )- Returns:
- the index of a constant pool item with the right value
-
addCPInterfaceMethodRef
Add an InterfaceMethodRef to the constant pool if necessary.- Parameters:
c- the class name, in JVM format (e.g., java/lang/Object)n- the field namet- the method type, in JVM format (e.g., V(ILjava/lang/Object;) )- Returns:
- the index of a constant pool item with the right value
-
addCPNAT
-
addCPInvokeDynamic
Add an InvokeDynamic to the constant pool if necessary.- Parameters:
n- the namet- the type, in JVM format- Returns:
- the index of a constant pool item with the right value
-
setAccessFlags
public void setAccessFlags(int f) Set the access flags for the class. -
setNameIndex
Set the constant pool index for the name of the class.- Throws:
IllegalArgumentException
-
setSuperNameIndex
public void setSuperNameIndex(int c) Set the constant pool index for the name of the superclass. -
setInterfaceNameIndices
public void setInterfaceNameIndices(int[] interfaces) Set the constant pool indices for the names of the implemented interfaces. -
setName
Set the name of the class. -
setSuperName
Set the name of the superclass; if c is null, then there is no superclass (this must be java/lang/Object). -
setInterfaceNames
Set the names of the implemented interfaces. -
addRawMethod
Add a method to the class, the method data given as "raw" bytes (probably obtained from a ClassReader). -
addRawField
Add a field to the class, the field data given as "raw" bytes (probably obtained from a ClassReader). -
addMethod
Add a method to the class.- Parameters:
access- the access flagsname- the method nametype- the method type in JVM format (e.g., V(ILjava/lang/Object;) )attributes- the attributes in raw form, one Element per attribute
-
addField
Add a field to the class.- Parameters:
access- the access flagsname- the field nametype- the field type in JVM format (e.g., I, Z, Ljava/lang/Object;)attributes- the attributes in raw form, one Element per attribute
-
addMethod
Add a method to the class.- Parameters:
access- the access flagsname- the constant pool index of the method nametype- the constant pool index of the method type in JVM format (e.g., V(ILjava/lang/Object;) )attributes- the attributes in raw form, one Element per attribute
-
addField
Add a field to the class.- Parameters:
access- the access flagsname- the constant pool index of the field nametype- the constant pool index of the field type in JVM format (e.g., I, Z, Ljava/lang/Object;)attributes- the attributes in raw form, one Element per attribute
-
addClassAttribute
Add an attribute to the class.- Parameters:
attribute- the attribute in raw form
-
makeBytes
After you've added everything you need to the class, call this method to generate the actual class file data. This can only be called once.- Throws:
IllegalArgumentException
-
setUByte
Set the byte at offset 'offset' in 'buf' to the unsigned 8-bit value in v.- Throws:
IllegalArgumentException- if buf is null
-
setInt
Set the 4 bytes at offset 'offset' in 'buf' to the signed 32-bit value in v.- Throws:
IllegalArgumentException- if buf is null
-
setLong
Set the 8 bytes at offset 'offset' in 'buf' to the signed 64-bit value in v.- Throws:
IllegalArgumentException
-
setFloat
Set the 4 bytes at offset 'offset' in 'buf' to the float value in v.- Throws:
IllegalArgumentException
-
setDouble
Set the 8 bytes at offset 'offset' in 'buf' to the double value in v.- Throws:
IllegalArgumentException
-
setUShort
Set the 2 bytes at offset 'offset' in 'buf' to the unsigned 16-bit value in v.- Throws:
IllegalArgumentException- if buf is null
-