org.apache.bcel.generic

Class InstructionFactory

public class InstructionFactory extends Object implements InstructionConstants, Serializable

Instances of this class may be used, e.g., to generate typed versions of instructions. Its main purpose is to be used as the byte code generating backend of a compiler. You can subclass it to add your own create methods.

See Also: Constants

Field Summary
protected ClassGencg
protected ConstantPoolGencp
Constructor Summary
InstructionFactory(ClassGen cg, ConstantPoolGen cp)
InstructionFactory(ClassGen cg)
Initialize with ClassGen object
InstructionFactory(ConstantPoolGen cp)
Initialize just with ConstantPoolGen object
Method Summary
InstructioncreateAppend(Type type)
static ArrayInstructioncreateArrayLoad(Type type)
static ArrayInstructioncreateArrayStore(Type type)
static ArithmeticInstructioncreateBinaryOperation(String op, Type type)
Create binary operation for simple basic types, such as int and float.
static BranchInstructioncreateBranchInstruction(short opcode, InstructionHandle target)
Create branch instruction by given opcode, except LOOKUPSWITCH and TABLESWITCH.
InstructioncreateCast(Type src_type, Type dest_type)
Create conversion operation for two stack operands, this may be an I2C, instruction, e.g., if the operands are basic types and CHECKCAST if they are reference types.
CHECKCASTcreateCheckCast(ReferenceType t)
InstructioncreateConstant(Object value)
Uses PUSH to push a constant value onto the stack.
static StackInstructioncreateDup(int size)
static StackInstructioncreateDup_1(int size)
static StackInstructioncreateDup_2(int size)
FieldInstructioncreateFieldAccess(String class_name, String name, Type type, short kind)
Create a field instruction.
GETFIELDcreateGetField(String class_name, String name, Type t)
GETSTATICcreateGetStatic(String class_name, String name, Type t)
INSTANCEOFcreateInstanceOf(ReferenceType t)
InvokeInstructioncreateInvoke(String class_name, String name, Type ret_type, Type[] arg_types, short kind)
Create an invoke instruction.
static LocalVariableInstructioncreateLoad(Type type, int index)
NEWcreateNew(ObjectType t)
NEWcreateNew(String s)
InstructioncreateNewArray(Type t, short dim)
Create new array of given size and type.
static InstructioncreateNull(Type type)
Create "null" value for reference types, 0 for basic types like int
static StackInstructioncreatePop(int size)
InstructionListcreatePrintln(String s)
Create a call to the most popular System.out.println() method.
PUTFIELDcreatePutField(String class_name, String name, Type t)
PUTSTATICcreatePutStatic(String class_name, String name, Type t)
static ReturnInstructioncreateReturn(Type type)
Create typed return
static LocalVariableInstructioncreateStore(Type type, int index)
static InstructioncreateThis()
Create reference to `this'
ClassGengetClassGen()
ConstantPoolGengetConstantPool()
voidsetClassGen(ClassGen c)
voidsetConstantPool(ConstantPoolGen c)

Field Detail

cg

protected ClassGen cg

cp

protected ConstantPoolGen cp

Constructor Detail

InstructionFactory

public InstructionFactory(ClassGen cg, ConstantPoolGen cp)

InstructionFactory

public InstructionFactory(ClassGen cg)
Initialize with ClassGen object

InstructionFactory

public InstructionFactory(ConstantPoolGen cp)
Initialize just with ConstantPoolGen object

Method Detail

createAppend

public Instruction createAppend(Type type)

createArrayLoad

public static ArrayInstruction createArrayLoad(Type type)

Parameters: type type of elements of array, i.e., array.getElementType()

createArrayStore

public static ArrayInstruction createArrayStore(Type type)

Parameters: type type of elements of array, i.e., array.getElementType()

createBinaryOperation

public static ArithmeticInstruction createBinaryOperation(String op, Type type)
Create binary operation for simple basic types, such as int and float.

Parameters: op operation, such as "+", "*", "<<", etc.

createBranchInstruction

public static BranchInstruction createBranchInstruction(short opcode, InstructionHandle target)
Create branch instruction by given opcode, except LOOKUPSWITCH and TABLESWITCH. For those you should use the SWITCH compound instruction.

createCast

public Instruction createCast(Type src_type, Type dest_type)
Create conversion operation for two stack operands, this may be an I2C, instruction, e.g., if the operands are basic types and CHECKCAST if they are reference types.

createCheckCast

public CHECKCAST createCheckCast(ReferenceType t)

createConstant

public Instruction createConstant(Object value)
Uses PUSH to push a constant value onto the stack.

Parameters: value must be of type Number, Boolean, Character or String

createDup

public static StackInstruction createDup(int size)

Parameters: size size of operand, either 1 (int, e.g.) or 2 (double)

createDup_1

public static StackInstruction createDup_1(int size)

Parameters: size size of operand, either 1 (int, e.g.) or 2 (double)

createDup_2

public static StackInstruction createDup_2(int size)

Parameters: size size of operand, either 1 (int, e.g.) or 2 (double)

createFieldAccess

public FieldInstruction createFieldAccess(String class_name, String name, Type type, short kind)
Create a field instruction.

Parameters: class_name name of the accessed class name name of the referenced field type type of field kind how to access, i.e., GETFIELD, PUTFIELD, GETSTATIC, PUTSTATIC

See Also: Constants

createGetField

public GETFIELD createGetField(String class_name, String name, Type t)

createGetStatic

public GETSTATIC createGetStatic(String class_name, String name, Type t)

createInstanceOf

public INSTANCEOF createInstanceOf(ReferenceType t)

createInvoke

public InvokeInstruction createInvoke(String class_name, String name, Type ret_type, Type[] arg_types, short kind)
Create an invoke instruction.

Parameters: class_name name of the called class name name of the called method ret_type return type of method arg_types argument types of method kind how to invoke, i.e., INVOKEINTERFACE, INVOKESTATIC, INVOKEVIRTUAL, or INVOKESPECIAL

See Also: Constants

createLoad

public static LocalVariableInstruction createLoad(Type type, int index)

Parameters: index index of local variable

createNew

public NEW createNew(ObjectType t)

createNew

public NEW createNew(String s)

createNewArray

public Instruction createNewArray(Type t, short dim)
Create new array of given size and type.

Returns: an instruction that creates the corresponding array at runtime, i.e. is an AllocationInstruction

createNull

public static Instruction createNull(Type type)
Create "null" value for reference types, 0 for basic types like int

createPop

public static StackInstruction createPop(int size)

Parameters: size size of operand, either 1 (int, e.g.) or 2 (double)

createPrintln

public InstructionList createPrintln(String s)
Create a call to the most popular System.out.println() method.

Parameters: s the string to print

createPutField

public PUTFIELD createPutField(String class_name, String name, Type t)

createPutStatic

public PUTSTATIC createPutStatic(String class_name, String name, Type t)

createReturn

public static ReturnInstruction createReturn(Type type)
Create typed return

createStore

public static LocalVariableInstruction createStore(Type type, int index)

Parameters: index index of local variable

createThis

public static Instruction createThis()
Create reference to `this'

getClassGen

public ClassGen getClassGen()

getConstantPool

public ConstantPoolGen getConstantPool()

setClassGen

public void setClassGen(ClassGen c)

setConstantPool

public void setConstantPool(ConstantPoolGen c)