Package org.apache.bcel.generic
Class InstructionFactory
- java.lang.Object
-
- org.apache.bcel.generic.InstructionFactory
-
- All Implemented Interfaces:
InstructionConstants
public class InstructionFactory extends java.lang.Object implements InstructionConstants
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.Note: The static createXXX methods return singleton instances from the
InstructionConstclass.- See Also:
Const,InstructionConst
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.apache.bcel.generic.InstructionConstants
InstructionConstants.Clinit
-
-
Field Summary
Fields Modifier and Type Field Description protected ClassGencgDeprecated.(since 6.0) will be made private; do not access directly, use getter/setterprotected ConstantPoolGencpDeprecated.(since 6.0) will be made private; do not access directly, use getter/setter-
Fields inherited from interface org.apache.bcel.generic.InstructionConstants
AALOAD, AASTORE, ACONST_NULL, ALOAD_0, ALOAD_1, ALOAD_2, ARETURN, ARRAYLENGTH, ASTORE_0, ASTORE_1, ASTORE_2, ATHROW, BALOAD, BASTORE, bla, CALOAD, CASTORE, D2F, D2I, D2L, DADD, DALOAD, DASTORE, DCMPG, DCMPL, DCONST_0, DCONST_1, DDIV, DMUL, DNEG, DREM, DRETURN, DSUB, DUP, DUP_X1, DUP_X2, DUP2, DUP2_X1, DUP2_X2, F2D, F2I, F2L, FADD, FALOAD, FASTORE, FCMPG, FCMPL, FCONST_0, FCONST_1, FCONST_2, FDIV, FMUL, FNEG, FREM, FRETURN, FSUB, I2B, I2C, I2D, I2F, I2L, I2S, IADD, IALOAD, IAND, IASTORE, ICONST_0, ICONST_1, ICONST_2, ICONST_3, ICONST_4, ICONST_5, ICONST_M1, IDIV, ILOAD_0, ILOAD_1, ILOAD_2, IMUL, INEG, INSTRUCTIONS, IOR, IREM, IRETURN, ISHL, ISHR, ISTORE_0, ISTORE_1, ISTORE_2, ISUB, IUSHR, IXOR, L2D, L2F, L2I, LADD, LALOAD, LAND, LASTORE, LCMP, LCONST_0, LCONST_1, LDIV, LMUL, LNEG, LOR, LREM, LRETURN, LSHL, LSHR, LSUB, LUSHR, LXOR, MONITORENTER, MONITOREXIT, NOP, POP, POP2, RETURN, SALOAD, SASTORE, SWAP, THIS
-
-
Constructor Summary
Constructors Constructor Description InstructionFactory(ClassGen cg)Initialize with ClassGen objectInstructionFactory(ClassGen cg, ConstantPoolGen cp)InstructionFactory(ConstantPoolGen cp)Initialize just with ConstantPoolGen object
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description InstructioncreateAppend(Type type)static ArrayInstructioncreateArrayLoad(Type type)static ArrayInstructioncreateArrayStore(Type type)static ArithmeticInstructioncreateBinaryOperation(java.lang.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(java.lang.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(java.lang.String class_name, java.lang.String name, Type type, short kind)Create a field instruction.GETFIELDcreateGetField(java.lang.String class_name, java.lang.String name, Type t)GETSTATICcreateGetStatic(java.lang.String class_name, java.lang.String name, Type t)INSTANCEOFcreateInstanceOf(ReferenceType t)InvokeInstructioncreateInvoke(java.lang.String class_name, java.lang.String name, Type ret_type, Type[] arg_types, short kind)Create an invoke instruction.InvokeInstructioncreateInvoke(java.lang.String class_name, java.lang.String name, Type ret_type, Type[] arg_types, short kind, boolean use_interface)Create an invoke instruction.static LocalVariableInstructioncreateLoad(Type type, int index)NEWcreateNew(java.lang.String s)NEWcreateNew(ObjectType t)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 intstatic StackInstructioncreatePop(int size)InstructionListcreatePrintln(java.lang.String s)Create a call to the most popular System.out.println() method.PUTFIELDcreatePutField(java.lang.String class_name, java.lang.String name, Type t)PUTSTATICcreatePutStatic(java.lang.String class_name, java.lang.String name, Type t)static ReturnInstructioncreateReturn(Type type)Create typed returnstatic LocalVariableInstructioncreateStore(Type type, int index)static InstructioncreateThis()Create reference to `this'ClassGengetClassGen()ConstantPoolGengetConstantPool()voidsetClassGen(ClassGen c)voidsetConstantPool(ConstantPoolGen c)
-
-
-
Field Detail
-
cg
@Deprecated protected ClassGen cg
Deprecated.(since 6.0) will be made private; do not access directly, use getter/setter
-
cp
@Deprecated protected ConstantPoolGen cp
Deprecated.(since 6.0) will be made private; do not access directly, use getter/setter
-
-
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
-
createInvoke
public InvokeInstruction createInvoke(java.lang.String class_name, java.lang.String name, Type ret_type, Type[] arg_types, short kind)
Create an invoke instruction. (Except for invokedynamic.)- Parameters:
class_name- name of the called classname- name of the called methodret_type- return type of methodarg_types- argument types of methodkind- how to invoke, i.e., INVOKEINTERFACE, INVOKESTATIC, INVOKEVIRTUAL, or INVOKESPECIAL- See Also:
Const
-
createInvoke
public InvokeInstruction createInvoke(java.lang.String class_name, java.lang.String name, Type ret_type, Type[] arg_types, short kind, boolean use_interface)
Create an invoke instruction. (Except for invokedynamic.)- Parameters:
class_name- name of the called classname- name of the called methodret_type- return type of methodarg_types- argument types of methodkind- how to invoke: INVOKEINTERFACE, INVOKESTATIC, INVOKEVIRTUAL, or INVOKESPECIALuse_interface- force use of InterfaceMethodref- Returns:
- A new InvokeInstruction.
- Since:
- 6.5.0
-
createPrintln
public InstructionList createPrintln(java.lang.String s)
Create a call to the most popular System.out.println() method.- Parameters:
s- the string to print
-
createConstant
public Instruction createConstant(java.lang.Object value)
Uses PUSH to push a constant value onto the stack.- Parameters:
value- must be of type Number, Boolean, Character or String
-
createAppend
public Instruction createAppend(Type type)
-
createFieldAccess
public FieldInstruction createFieldAccess(java.lang.String class_name, java.lang.String name, Type type, short kind)
Create a field instruction.- Parameters:
class_name- name of the accessed classname- name of the referenced fieldtype- type of fieldkind- how to access, i.e., GETFIELD, PUTFIELD, GETSTATIC, PUTSTATIC- See Also:
Const
-
createThis
public static Instruction createThis()
Create reference to `this'
-
createReturn
public static ReturnInstruction createReturn(Type type)
Create typed return
-
createBinaryOperation
public static ArithmeticInstruction createBinaryOperation(java.lang.String op, Type type)
Create binary operation for simple basic types, such as int and float.- Parameters:
op- operation, such as "+", "*", "<<", etc.
-
createPop
public static StackInstruction createPop(int size)
- Parameters:
size- size of operand, either 1 (int, e.g.) or 2 (double)
-
createDup
public static StackInstruction createDup(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)
-
createDup_1
public static StackInstruction createDup_1(int size)
- Parameters:
size- size of operand, either 1 (int, e.g.) or 2 (double)
-
createStore
public static LocalVariableInstruction createStore(Type type, int index)
- Parameters:
index- index of local variable
-
createLoad
public static LocalVariableInstruction createLoad(Type type, int index)
- Parameters:
index- index of local variable
-
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()
-
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.
-
createGetField
public GETFIELD createGetField(java.lang.String class_name, java.lang.String name, Type t)
-
createGetStatic
public GETSTATIC createGetStatic(java.lang.String class_name, java.lang.String name, Type t)
-
createPutField
public PUTFIELD createPutField(java.lang.String class_name, java.lang.String name, Type t)
-
createPutStatic
public PUTSTATIC createPutStatic(java.lang.String class_name, java.lang.String name, Type t)
-
createCheckCast
public CHECKCAST createCheckCast(ReferenceType t)
-
createInstanceOf
public INSTANCEOF createInstanceOf(ReferenceType t)
-
createNew
public NEW createNew(ObjectType t)
-
createNew
public NEW createNew(java.lang.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
-
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.
-
setClassGen
public void setClassGen(ClassGen c)
-
getClassGen
public ClassGen getClassGen()
-
setConstantPool
public void setConstantPool(ConstantPoolGen c)
-
getConstantPool
public ConstantPoolGen getConstantPool()
-
-