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, for example, 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 Modifier and Type Class Description private static classInstructionFactory.MethodObject-
Nested classes/interfaces inherited from interface org.apache.bcel.generic.InstructionConstants
InstructionConstants.Clinit
-
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.StringAPPENDprivate static InstructionFactory.MethodObject[]APPEND_METHOD_OBJECTSprotected 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/setterprivate static java.lang.StringFQCN_STRING_BUFFERprivate static java.lang.String[]SHORT_NAMESThese must agree with the order of Constants.T_CHAR through T_LONG.-
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 object.InstructionFactory(ClassGen cg, ConstantPoolGen cp)Constructs an InstructionFactory.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)Creates an append instruction for the given type.static ArrayInstructioncreateArrayLoad(Type type)Creates an array load instruction for the given type.static ArrayInstructioncreateArrayStore(Type type)Creates an array store instruction for the given type.private static ArithmeticInstructioncreateBinaryDoubleOp(char op)private static ArithmeticInstructioncreateBinaryFloatOp(char op)private static ArithmeticInstructioncreateBinaryIntOp(char first, java.lang.String op)private static ArithmeticInstructioncreateBinaryLongOp(char first, java.lang.String op)Create an invokedynamic instruction.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 srcType, Type destType)Create conversion operation for two stack operands, this may be an I2C, instruction, for example, if the operands are basic types and CHECKCAST if they are reference types.CHECKCASTcreateCheckCast(ReferenceType t)Creates a CHECKCAST instruction.InstructioncreateConstant(java.lang.Object value)Uses PUSH to push a constant value onto the stack.static StackInstructioncreateDup(int size)Creates a DUP instruction.static StackInstructioncreateDup_1(int size)Creates a DUP_X1 instruction.static StackInstructioncreateDup_2(int size)Creates a DUP_X2 instruction.FieldInstructioncreateFieldAccess(java.lang.String className, java.lang.String name, Type type, short kind)Create a field instruction.GETFIELDcreateGetField(java.lang.String className, java.lang.String name, Type t)Creates a GETFIELD instruction.GETSTATICcreateGetStatic(java.lang.String className, java.lang.String name, Type t)Creates a GETSTATIC instruction.INSTANCEOFcreateInstanceOf(ReferenceType t)Creates an INSTANCEOF instruction.InvokeInstructioncreateInvoke(java.lang.String className, java.lang.String name, Type retType, Type[] argTypes, short kind)Create an invoke instruction.InvokeInstructioncreateInvoke(java.lang.String className, java.lang.String name, Type retType, Type[] argTypes, short kind, boolean useInterface)Create an invoke instruction.private InvokeInstructioncreateInvoke(InstructionFactory.MethodObject m, short kind)static LocalVariableInstructioncreateLoad(Type type, int index)Creates a load instruction for the given type.NEWcreateNew(java.lang.String s)Creates a NEW instruction.NEWcreateNew(ObjectType t)Creates a NEW instruction.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)Creates a pop instruction.InstructionListcreatePrintln(java.lang.String s)Create a call to the most popular System.out.println() method.PUTFIELDcreatePutField(java.lang.String className, java.lang.String name, Type t)Creates a PUTFIELD instruction.PUTSTATICcreatePutStatic(java.lang.String className, java.lang.String name, Type t)Creates a PUTSTATIC instruction.static ReturnInstructioncreateReturn(Type type)Create typed return.static LocalVariableInstructioncreateStore(Type type, int index)Creates a store instruction.static InstructioncreateThis()Create reference to 'this'.ClassGengetClassGen()Gets the class generator.ConstantPoolGengetConstantPool()Gets the constant pool generator.private static booleanisString(Type type)voidsetClassGen(ClassGen c)Sets the class generator.voidsetConstantPool(ConstantPoolGen c)Sets the constant pool generator.
-
-
-
Field Detail
-
APPEND
private static final java.lang.String APPEND
- See Also:
- Constant Field Values
-
FQCN_STRING_BUFFER
private static final java.lang.String FQCN_STRING_BUFFER
- See Also:
- Constant Field Values
-
SHORT_NAMES
private static final java.lang.String[] SHORT_NAMES
These must agree with the order of Constants.T_CHAR through T_LONG.
-
APPEND_METHOD_OBJECTS
private static final InstructionFactory.MethodObject[] APPEND_METHOD_OBJECTS
-
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)
Initialize with ClassGen object.- Parameters:
cg- the class generator.
-
InstructionFactory
public InstructionFactory(ClassGen cg, ConstantPoolGen cp)
Constructs an InstructionFactory.- Parameters:
cg- the class generator.cp- the constant pool generator.
-
InstructionFactory
public InstructionFactory(ConstantPoolGen cp)
Initialize just with ConstantPoolGen object.- Parameters:
cp- the constant pool generator.
-
-
Method Detail
-
createArrayLoad
public static ArrayInstruction createArrayLoad(Type type)
Creates an array load instruction for the given type.- Parameters:
type- type of elements of array, that is, array.getElementType().- Returns:
- the array load instruction.
-
createArrayStore
public static ArrayInstruction createArrayStore(Type type)
Creates an array store instruction for the given type.- Parameters:
type- type of elements of array, that is, array.getElementType().- Returns:
- the array store instruction.
-
createBinaryDoubleOp
private static ArithmeticInstruction createBinaryDoubleOp(char op)
-
createBinaryFloatOp
private static ArithmeticInstruction createBinaryFloatOp(char op)
-
createBinaryIntOp
private static ArithmeticInstruction createBinaryIntOp(char first, java.lang.String op)
-
createBinaryLongOp
private static ArithmeticInstruction createBinaryLongOp(char first, java.lang.String op)
Create an invokedynamic instruction.- Parameters:
bootstrap_index- index into the bootstrap_methods array.name- name of the called method.ret_type- return type of method.argTypes- argument types of method.- See Also:
Const
-
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.type- the operand type.- Returns:
- the binary arithmetic instruction.
-
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.- Parameters:
opcode- the branch opcode.target- the target instruction handle.- Returns:
- the branch instruction.
-
createDup
public static StackInstruction createDup(int size)
Creates a DUP instruction.- Parameters:
size- size of operand, either 1 (int, for example) or 2 (double).- Returns:
- the DUP instruction.
-
createDup_1
public static StackInstruction createDup_1(int size)
Creates a DUP_X1 instruction.- Parameters:
size- size of operand, either 1 (int, for example) or 2 (double).- Returns:
- the DUP instruction.
-
createDup_2
public static StackInstruction createDup_2(int size)
Creates a DUP_X2 instruction.- Parameters:
size- size of operand, either 1 (int, for example) or 2 (double).- Returns:
- the DUP instruction.
-
createLoad
public static LocalVariableInstruction createLoad(Type type, int index)
Creates a load instruction for the given type.- Parameters:
type- the variable type.index- index of local variable.- Returns:
- the load instruction.
-
createNull
public static Instruction createNull(Type type)
Create "null" value for reference types, 0 for basic types like int.- Parameters:
type- the type.- Returns:
- the null or zero instruction.
-
createPop
public static StackInstruction createPop(int size)
Creates a pop instruction.- Parameters:
size- size of operand, either 1 (int, for example) or 2 (double).- Returns:
- the pop instruction.
-
createReturn
public static ReturnInstruction createReturn(Type type)
Create typed return.- Parameters:
type- the return type.- Returns:
- the return instruction.
-
createStore
public static LocalVariableInstruction createStore(Type type, int index)
Creates a store instruction.- Parameters:
type- the variable type.index- index of local variable.- Returns:
- the store instruction.
-
createThis
public static Instruction createThis()
Create reference to 'this'.- Returns:
- reference to 'this'.
-
isString
private static boolean isString(Type type)
-
createAppend
public Instruction createAppend(Type type)
Creates an append instruction for the given type.- Parameters:
type- the type to append.- Returns:
- the append instruction.
-
createCast
public Instruction createCast(Type srcType, Type destType)
Create conversion operation for two stack operands, this may be an I2C, instruction, for example, if the operands are basic types and CHECKCAST if they are reference types.- Parameters:
srcType- the source type.destType- the destination type.- Returns:
- the conversion instruction.
-
createCheckCast
public CHECKCAST createCheckCast(ReferenceType t)
Creates a CHECKCAST instruction.- Parameters:
t- the reference type.- Returns:
- the CHECKCAST instruction.
-
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.- Returns:
- the instruction.
-
createFieldAccess
public FieldInstruction createFieldAccess(java.lang.String className, java.lang.String name, Type type, short kind)
Create a field instruction.- Parameters:
className- name of the accessed class.name- name of the referenced field.type- type of field.kind- how to access, that is, GETFIELD, PUTFIELD, GETSTATIC, PUTSTATIC.- See Also:
Const
-
createGetField
public GETFIELD createGetField(java.lang.String className, java.lang.String name, Type t)
Creates a GETFIELD instruction.- Parameters:
className- the class name.name- the field name.t- the field type.- Returns:
- the GETFIELD instruction.
-
createGetStatic
public GETSTATIC createGetStatic(java.lang.String className, java.lang.String name, Type t)
Creates a GETSTATIC instruction.- Parameters:
className- the class name.name- the field name.t- the field type.- Returns:
- the GETSTATIC instruction.
-
createInstanceOf
public INSTANCEOF createInstanceOf(ReferenceType t)
Creates an INSTANCEOF instruction.- Parameters:
t- the reference type.- Returns:
- the INSTANCEOF instruction.
-
createInvoke
private InvokeInstruction createInvoke(InstructionFactory.MethodObject m, short kind)
-
createInvoke
public InvokeInstruction createInvoke(java.lang.String className, java.lang.String name, Type retType, Type[] argTypes, short kind)
Create an invoke instruction. (Except for invokedynamic.)- Parameters:
className- name of the called class.name- name of the called method.retType- return type of method.argTypes- argument types of method.kind- how to invoke, that is, INVOKEINTERFACE, INVOKESTATIC, INVOKEVIRTUAL, or INVOKESPECIAL.- Returns:
- the invoke instruction.
- See Also:
Const
-
createInvoke
public InvokeInstruction createInvoke(java.lang.String className, java.lang.String name, Type retType, Type[] argTypes, short kind, boolean useInterface)
Create an invoke instruction. (Except for invokedynamic.)- Parameters:
className- name of the called class.name- name of the called method.retType- return type of method.argTypes- argument types of method.kind- how to invoke: INVOKEINTERFACE, INVOKESTATIC, INVOKEVIRTUAL, or INVOKESPECIAL.useInterface- force use of InterfaceMethodref.- Returns:
- A new InvokeInstruction.
- Since:
- 6.5.0
-
createNew
public NEW createNew(ObjectType t)
Creates a NEW instruction.- Parameters:
t- the object type.- Returns:
- the NEW instruction.
-
createNew
public NEW createNew(java.lang.String s)
Creates a NEW instruction.- Parameters:
s- the class name.- Returns:
- the NEW instruction.
-
createNewArray
public Instruction createNewArray(Type t, short dim)
Create new array of given size and type.- Parameters:
t- the array element type.dim- the array dimensions.- Returns:
- an instruction that creates the corresponding array at runtime, for example is an AllocationInstruction.
-
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.- Returns:
- the instruction list.
-
createPutField
public PUTFIELD createPutField(java.lang.String className, java.lang.String name, Type t)
Creates a PUTFIELD instruction.- Parameters:
className- the class name.name- the field name.t- the field type.- Returns:
- the PUTFIELD instruction.
-
createPutStatic
public PUTSTATIC createPutStatic(java.lang.String className, java.lang.String name, Type t)
Creates a PUTSTATIC instruction.- Parameters:
className- the class name.name- the field name.t- the field type.- Returns:
- the PUTSTATIC instruction.
-
getClassGen
public ClassGen getClassGen()
Gets the class generator.- Returns:
- the class generator.
-
getConstantPool
public ConstantPoolGen getConstantPool()
Gets the constant pool generator.- Returns:
- the constant pool generator.
-
setClassGen
public void setClassGen(ClassGen c)
Sets the class generator.- Parameters:
c- the class generator.
-
setConstantPool
public void setConstantPool(ConstantPoolGen c)
Sets the constant pool generator.- Parameters:
c- the constant pool generator.
-
-