org.curjent.impl.asm
Class ClassWriter

java.lang.Object
  extended by org.curjent.impl.asm.ClassWriter

public final class ClassWriter
extends Object

A { @link ClassVisitor} that generates classes in bytecode form. More precisely this visitor generates a byte array conforming to the Java class file format. It can be used alone, to generate a Java class "from scratch", or with one or more { @link ClassReader ClassReader} and adapter class visitor to generate a modified class from one or more existing Java classes.

Author:
Eric Bruneton

Field Summary
private  int access
          The access flags of this class.
(package private) static int CLASS
          The type of CONSTANT_Class constant pool items.
(package private) static int DOUBLE
          The type of CONSTANT_Double constant pool items.
(package private) static int FIELD
          The type of CONSTANT_Fieldref constant pool items.
(package private)  FieldWriter firstField
          The fields of this class.
(package private)  MethodWriter firstMethod
          The methods of this class.
(package private) static int FLOAT
          The type of CONSTANT_Float constant pool items.
(package private) static int IMETH
          The type of CONSTANT_InterfaceMethodref constant pool items.
private  int index
          Index of the next item to be added in the constant pool.
(package private) static int INT
          The type of CONSTANT_Integer constant pool items.
private  int interfaceCount
          Number of interfaces implemented or extended by this class or interface.
private  int[] interfaces
          The interfaces implemented or extended by this class or interface.
private  Item[] items
          The constant pool's hash table data.
private  Item key
          A reusable key used to look for items in the { @link #items} hash table.
private  Item key2
          A reusable key used to look for items in the { @link #items} hash table.
private  Item key3
          A reusable key used to look for items in the { @link #items} hash table.
(package private)  FieldWriter lastField
          The fields of this class.
(package private)  MethodWriter lastMethod
          The methods of this class.
(package private) static int LONG
          The type of CONSTANT_Long constant pool items.
(package private) static int METH
          The type of CONSTANT_Methodref constant pool items.
private  int name
          The constant pool item that contains the internal name of this class.
(package private) static int NAME_TYPE
          The type of CONSTANT_NameAndType constant pool items.
private  ByteVector pool
          The constant pool of this class.
(package private) static int STR
          The type of CONSTANT_String constant pool items.
private  int superName
          The constant pool item that contains the internal name of the super class of this class.
private  int threshold
          The threshold of the constant pool's hash table.
(package private) static int TYPE_MERGED
          Merged type Item stored in the ClassWriter { @link ClassWriter#typeTable}, instead of the constant pool, in order to avoid clashes with normal constant pool items in the ClassWriter constant pool's hash table.
(package private) static int TYPE_NORMAL
          Normal type Item stored in the ClassWriter { @link ClassWriter#typeTable}, instead of the constant pool, in order to avoid clashes with normal constant pool items in the ClassWriter constant pool's hash table.
(package private) static int TYPE_UNINIT
          Uninitialized type Item stored in the ClassWriter { @link ClassWriter#typeTable}, instead of the constant pool, in order to avoid clashes with normal constant pool items in the ClassWriter constant pool's hash table.
(package private) static int UTF8
          The type of CONSTANT_Utf8 constant pool items.
private  int version
          Minor and major version numbers of the class to be generated.
 
Constructor Summary
ClassWriter(int flags)
          Constructs a new { @link ClassWriter} object.
 
Method Summary
private  Item get(Item key)
          Returns the constant pool's hash table item which is equal to the given item.
(package private)  int newClass(String value)
          Adds a class reference to the constant pool of the class being build.
(package private)  Item newClassItem(String value)
          Adds a class reference to the constant pool of the class being build.
(package private)  Item newConstItem(Object cst)
          Adds a number or string constant to the constant pool of the class being build.
(package private)  Item newDouble(double value)
          Adds a double to the constant pool of the class being build.
(package private)  Item newFieldItem(String owner, String name, String desc)
          Adds a field reference to the constant pool of the class being build.
(package private)  Item newFloat(float value)
          Adds a float to the constant pool of the class being build.
(package private)  Item newInteger(int value)
          Adds an integer to the constant pool of the class being build.
(package private)  Item newLong(long value)
          Adds a long to the constant pool of the class being build.
(package private)  Item newMethodItem(String owner, String name, String desc, boolean itf)
          Adds a method reference to the constant pool of the class being build.
private  int newNameType(String name, String desc)
          Adds a name and type to the constant pool of the class being build.
private  Item newString(String value)
          Adds a string to the constant pool of the class being build.
(package private)  int newUTF8(String value)
          Adds an UTF8 string to the constant pool of the class being build.
private  void put(Item i)
          Puts the given item in the constant pool's hash table.
private  void put122(int b, int s1, int s2)
          Puts one byte and two shorts into the constant pool.
 byte[] toByteArray()
          Returns the bytecode of the class that was build with this class writer.
 void visit(int version, int access, String name, String signature, String superName, String[] interfaces)
           
 void visitEnd()
           
 FieldWriter visitField(int access, String name, String desc, String signature, Object value)
           
 MethodWriter visitMethod(int access, String name, String desc, String signature, String[] exceptions)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CLASS

static final int CLASS
The type of CONSTANT_Class constant pool items.

See Also:
Constant Field Values

FIELD

static final int FIELD
The type of CONSTANT_Fieldref constant pool items.

See Also:
Constant Field Values

METH

static final int METH
The type of CONSTANT_Methodref constant pool items.

See Also:
Constant Field Values

IMETH

static final int IMETH
The type of CONSTANT_InterfaceMethodref constant pool items.

See Also:
Constant Field Values

STR

static final int STR
The type of CONSTANT_String constant pool items.

See Also:
Constant Field Values

INT

static final int INT
The type of CONSTANT_Integer constant pool items.

See Also:
Constant Field Values

FLOAT

static final int FLOAT
The type of CONSTANT_Float constant pool items.

See Also:
Constant Field Values

LONG

static final int LONG
The type of CONSTANT_Long constant pool items.

See Also:
Constant Field Values

DOUBLE

static final int DOUBLE
The type of CONSTANT_Double constant pool items.

See Also:
Constant Field Values

NAME_TYPE

static final int NAME_TYPE
The type of CONSTANT_NameAndType constant pool items.

See Also:
Constant Field Values

UTF8

static final int UTF8
The type of CONSTANT_Utf8 constant pool items.

See Also:
Constant Field Values

TYPE_NORMAL

static final int TYPE_NORMAL
Normal type Item stored in the ClassWriter { @link ClassWriter#typeTable}, instead of the constant pool, in order to avoid clashes with normal constant pool items in the ClassWriter constant pool's hash table.

See Also:
Constant Field Values

TYPE_UNINIT

static final int TYPE_UNINIT
Uninitialized type Item stored in the ClassWriter { @link ClassWriter#typeTable}, instead of the constant pool, in order to avoid clashes with normal constant pool items in the ClassWriter constant pool's hash table.

See Also:
Constant Field Values

TYPE_MERGED

static final int TYPE_MERGED
Merged type Item stored in the ClassWriter { @link ClassWriter#typeTable}, instead of the constant pool, in order to avoid clashes with normal constant pool items in the ClassWriter constant pool's hash table.

See Also:
Constant Field Values

version

private int version
Minor and major version numbers of the class to be generated.


index

private int index
Index of the next item to be added in the constant pool.


pool

private final ByteVector pool
The constant pool of this class.


items

private Item[] items
The constant pool's hash table data.


threshold

private int threshold
The threshold of the constant pool's hash table.


key

private final Item key
A reusable key used to look for items in the { @link #items} hash table.


key2

private final Item key2
A reusable key used to look for items in the { @link #items} hash table.


key3

private final Item key3
A reusable key used to look for items in the { @link #items} hash table.


access

private int access
The access flags of this class.


name

private int name
The constant pool item that contains the internal name of this class.


superName

private int superName
The constant pool item that contains the internal name of the super class of this class.


interfaceCount

private int interfaceCount
Number of interfaces implemented or extended by this class or interface.


interfaces

private int[] interfaces
The interfaces implemented or extended by this class or interface. More precisely, this array contains the indexes of the constant pool items that contain the internal names of these interfaces.


firstField

FieldWriter firstField
The fields of this class. These fields are stored in a linked list of { @link FieldWriter} objects, linked to each other by their { @link FieldWriter#next} field. This field stores the first element of this list.


lastField

FieldWriter lastField
The fields of this class. These fields are stored in a linked list of { @link FieldWriter} objects, linked to each other by their { @link FieldWriter#next} field. This field stores the last element of this list.


firstMethod

MethodWriter firstMethod
The methods of this class. These methods are stored in a linked list of { @link MethodWriter} objects, linked to each other by their { @link MethodWriter#next} field. This field stores the first element of this list.


lastMethod

MethodWriter lastMethod
The methods of this class. These methods are stored in a linked list of { @link MethodWriter} objects, linked to each other by their { @link MethodWriter#next} field. This field stores the last element of this list.

Constructor Detail

ClassWriter

public ClassWriter(int flags)
Constructs a new { @link ClassWriter} object.

Parameters:
flags - option flags that can be used to modify the default behavior of this class. See { @link #COMPUTE_MAXS}, { @link #COMPUTE_FRAMES}.
Method Detail

visit

public void visit(int version,
                  int access,
                  String name,
                  String signature,
                  String superName,
                  String[] interfaces)

visitField

public FieldWriter visitField(int access,
                              String name,
                              String desc,
                              String signature,
                              Object value)

visitMethod

public MethodWriter visitMethod(int access,
                                String name,
                                String desc,
                                String signature,
                                String[] exceptions)

visitEnd

public void visitEnd()

toByteArray

public byte[] toByteArray()
Returns the bytecode of the class that was build with this class writer.

Returns:
the bytecode of the class that was build with this class writer.

newConstItem

Item newConstItem(Object cst)
Adds a number or string constant to the constant pool of the class being build. Does nothing if the constant pool already contains a similar item.

Parameters:
cst - the value of the constant to be added to the constant pool. This parameter must be an { @link Integer}, a { @link Float}, a { @link Long}, a { @link Double}, a { @link String} or a { @link Type}.
Returns:
a new or already existing constant item with the given value.

newUTF8

int newUTF8(String value)
Adds an UTF8 string to the constant pool of the class being build. Does nothing if the constant pool already contains a similar item. This method is intended for { @link Attribute} sub classes, and is normally not needed by class generators or adapters.

Parameters:
value - the String value.
Returns:
the index of a new or already existing UTF8 item.

newClassItem

Item newClassItem(String value)
Adds a class reference to the constant pool of the class being build. Does nothing if the constant pool already contains a similar item. This method is intended for { @link Attribute} sub classes, and is normally not needed by class generators or adapters.

Parameters:
value - the internal name of the class.
Returns:
a new or already existing class reference item.

newClass

int newClass(String value)
Adds a class reference to the constant pool of the class being build. Does nothing if the constant pool already contains a similar item. This method is intended for { @link Attribute} sub classes, and is normally not needed by class generators or adapters.

Parameters:
value - the internal name of the class.
Returns:
the index of a new or already existing class reference item.

newFieldItem

Item newFieldItem(String owner,
                  String name,
                  String desc)
Adds a field reference to the constant pool of the class being build. Does nothing if the constant pool already contains a similar item.

Parameters:
owner - the internal name of the field's owner class.
name - the field's name.
desc - the field's descriptor.
Returns:
a new or already existing field reference item.

newMethodItem

Item newMethodItem(String owner,
                   String name,
                   String desc,
                   boolean itf)
Adds a method reference to the constant pool of the class being build. Does nothing if the constant pool already contains a similar item.

Parameters:
owner - the internal name of the method's owner class.
name - the method's name.
desc - the method's descriptor.
itf - true if owner is an interface.
Returns:
a new or already existing method reference item.

newInteger

Item newInteger(int value)
Adds an integer to the constant pool of the class being build. Does nothing if the constant pool already contains a similar item.

Parameters:
value - the int value.
Returns:
a new or already existing int item.

newFloat

Item newFloat(float value)
Adds a float to the constant pool of the class being build. Does nothing if the constant pool already contains a similar item.

Parameters:
value - the float value.
Returns:
a new or already existing float item.

newLong

Item newLong(long value)
Adds a long to the constant pool of the class being build. Does nothing if the constant pool already contains a similar item.

Parameters:
value - the long value.
Returns:
a new or already existing long item.

newDouble

Item newDouble(double value)
Adds a double to the constant pool of the class being build. Does nothing if the constant pool already contains a similar item.

Parameters:
value - the double value.
Returns:
a new or already existing double item.

newString

private Item newString(String value)
Adds a string to the constant pool of the class being build. Does nothing if the constant pool already contains a similar item.

Parameters:
value - the String value.
Returns:
a new or already existing string item.

newNameType

private int newNameType(String name,
                        String desc)
Adds a name and type to the constant pool of the class being build. Does nothing if the constant pool already contains a similar item. This method is intended for { @link Attribute} sub classes, and is normally not needed by class generators or adapters.

Parameters:
name - a name.
desc - a type descriptor.
Returns:
the index of a new or already existing name and type item.

get

private Item get(Item key)
Returns the constant pool's hash table item which is equal to the given item.

Parameters:
key - a constant pool item.
Returns:
the constant pool's hash table item which is equal to the given item, or null if there is no such item.

put

private void put(Item i)
Puts the given item in the constant pool's hash table. The hash table must not already contains this item.

Parameters:
i - the item to be added to the constant pool's hash table.

put122

private void put122(int b,
                    int s1,
                    int s2)
Puts one byte and two shorts into the constant pool.

Parameters:
b - a byte.
s1 - a short.
s2 - another short.


Copyright 2009-2011 Tom Landon
Apache License 2.0