Class ClassWriter

java.lang.Object
com.ibm.wala.shrike.shrikeCT.ClassWriter
All Implemented Interfaces:
ClassConstants

public class ClassWriter extends Object implements ClassConstants
This class formats and writes class data into JVM format.
  • Constructor Details Link icon

    • ClassWriter Link icon

      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 Link icon

    • setMajorVersion Link icon

      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 Link icon

      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 Link icon

      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:
      InvalidClassFileException
      IllegalArgumentException
    • setForceAddCPEntries Link icon

      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 Link icon

      protected int addCPEntry(Object o, int size)
    • addCPUtf8 Link icon

      public int addCPUtf8(String s)
      Add a Utf8 string to the constant pool if necessary.
      Returns:
      the index of a constant pool item with the right value
    • addCPInt Link icon

      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 Link icon

      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 Link icon

      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 Link icon

      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 Link icon

      public int addCPMethodHandle(ConstantPoolParser.ReferenceToken c)
    • addCPString Link icon

      public int addCPString(String s)
      Add a String to the constant pool if necessary.
      Returns:
      the index of a constant pool item with the right value
    • addCPClass Link icon

      public int addCPClass(String s)
      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 Link icon

      public int addCPMethodType(String s)
      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 Link icon

      public int addCPFieldRef(String c, String n, String t)
      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 name
      t - 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 Link icon

      public int addCPMethodRef(String c, String n, String t)
      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 name
      t - 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 Link icon

      public int addCPInterfaceMethodRef(String c, String n, String t)
      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 name
      t - 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 Link icon

      public int addCPNAT(String n, String t)
      Add a NameAndType to the constant pool if necessary.
      Parameters:
      n - the name
      t - the type, in JVM format
      Returns:
      the index of a constant pool item with the right value
    • addCPInvokeDynamic Link icon

      public int addCPInvokeDynamic(BootstrapMethodsReader.BootstrapMethod b, String n, String t)
      Add an InvokeDynamic to the constant pool if necessary.
      Parameters:
      n - the name
      t - the type, in JVM format
      Returns:
      the index of a constant pool item with the right value
    • setAccessFlags Link icon

      public void setAccessFlags(int f)
      Set the access flags for the class.
    • setNameIndex Link icon

      public void setNameIndex(int c) throws IllegalArgumentException
      Set the constant pool index for the name of the class.
      Throws:
      IllegalArgumentException
    • setSuperNameIndex Link icon

      public void setSuperNameIndex(int c)
      Set the constant pool index for the name of the superclass.
    • setInterfaceNameIndices Link icon

      public void setInterfaceNameIndices(int[] ifaces)
      Set the constant pool indices for the names of the implemented interfaces.
    • setName Link icon

      public void setName(String c)
      Set the name of the class.
    • setSuperName Link icon

      public void setSuperName(String c)
      Set the name of the superclass; if c is null, then there is no superclass (this must be java/lang/Object).
    • setInterfaceNames Link icon

      public void setInterfaceNames(String[] ifaces)
      Set the names of the implemented interfaces.
    • addRawMethod Link icon

      public void addRawMethod(ClassWriter.Element e)
      Add a method to the class, the method data given as "raw" bytes (probably obtained from a ClassReader).
    • addRawField Link icon

      public void addRawField(ClassWriter.Element e)
      Add a field to the class, the field data given as "raw" bytes (probably obtained from a ClassReader).
    • addMethod Link icon

      public void addMethod(int access, String name, String type, ClassWriter.Element[] attributes)
      Add a method to the class.
      Parameters:
      access - the access flags
      name - the method name
      type - the method type in JVM format (e.g., V(ILjava/lang/Object;) )
      attributes - the attributes in raw form, one Element per attribute
    • addField Link icon

      public void addField(int access, String name, String type, ClassWriter.Element[] attributes)
      Add a field to the class.
      Parameters:
      access - the access flags
      name - the field name
      type - the field type in JVM format (e.g., I, Z, Ljava/lang/Object;)
      attributes - the attributes in raw form, one Element per attribute
    • addMethod Link icon

      public void addMethod(int access, int name, int type, ClassWriter.Element[] attributes)
      Add a method to the class.
      Parameters:
      access - the access flags
      name - the constant pool index of the method name
      type - 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 Link icon

      public void addField(int access, int name, int type, ClassWriter.Element[] attributes)
      Add a field to the class.
      Parameters:
      access - the access flags
      name - the constant pool index of the field name
      type - 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 Link icon

      public void addClassAttribute(ClassWriter.Element attribute)
      Add an atttribute to the class.
      Parameters:
      attribute - the attribute in raw form
    • makeBytes Link icon

      public byte[] makeBytes() throws IllegalArgumentException
      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 Link icon

      public static void setUByte(byte[] buf, int offset, int v) throws IllegalArgumentException
      Set the byte at offset 'offset' in 'buf' to the unsigned 8-bit value in v.
      Throws:
      IllegalArgumentException - if buf is null
    • setInt Link icon

      public static void setInt(byte[] buf, int offset, int v) throws IllegalArgumentException
      Set the 4 bytes at offset 'offset' in 'buf' to the signed 32-bit value in v.
      Throws:
      IllegalArgumentException - if buf is null
    • setLong Link icon

      public static void setLong(byte[] buf, int offset, long v) throws IllegalArgumentException
      Set the 8 bytes at offset 'offset' in 'buf' to the signed 64-bit value in v.
      Throws:
      IllegalArgumentException
    • setFloat Link icon

      public static void setFloat(byte[] buf, int offset, float v) throws IllegalArgumentException
      Set the 4 bytes at offset 'offset' in 'buf' to the float value in v.
      Throws:
      IllegalArgumentException
    • setDouble Link icon

      public static void setDouble(byte[] buf, int offset, double v) throws IllegalArgumentException
      Set the 8 bytes at offset 'offset' in 'buf' to the double value in v.
      Throws:
      IllegalArgumentException
    • setUShort Link icon

      public static void setUShort(byte[] buf, int offset, int v) throws IllegalArgumentException
      Set the 2 bytes at offset 'offset' in 'buf' to the unsigned 16-bit value in v.
      Throws:
      IllegalArgumentException - if buf is null