|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
public interface InstructionBuilder
Simplifies the generation of method instructions for a particular method (or constructor), allowing bytecode to be created with a friendlier API that focuses on Java type names (names as they would appear in Java source) rather than JVM descriptors or internal names. In some limited cases, types may be specified as Java Class instances as well. In addition, there is good support for primitive type boxing and unboxing.
Most methods return the same instance of InstructionBuilder, allowing for a "fluid" API. More complex functionality, such as try/catch blocks, is more like a DSL (domain specific language), and is based on callbacks. This looks better in Groovy and will be more reasonable once JDK 1.8 closures are available; in the meantime, it means some deeply nested inner classes, but helps ensure that correct bytecode is generated and helps to limit the amount of bookkeeping is necessary on the part of code using InstructionBuilder.
Method Summary | |
---|---|
InstructionBuilder |
arrayLength()
Expects the top object on the stack to be an array. |
InstructionBuilder |
boxPrimitive(String typeName)
If the type name is a primitive type, adds code to box the type into the equivalent wrapper type, using static methods on the wrapper type. |
InstructionBuilder |
castOrUnbox(String typeName)
Casts the object on top of the stack to the indicated type. |
InstructionBuilder |
checkcast(Class clazz)
|
InstructionBuilder |
checkcast(String className)
Adds a check that the object on top of the stack is assignable to the indicated class. |
InstructionBuilder |
compareSpecial(String typeName)
Special comparison logic for primitive float, double and long. |
InstructionBuilder |
doWhile(Condition condition,
WhileCallback callback)
Implements a simple loop based on a condition. |
InstructionBuilder |
dupe()
Duplicates the top object on the stack. |
InstructionBuilder |
dupe(int depth)
Duplicates the top object on the stack, placing the result at some depth. |
InstructionBuilder |
dupeWide()
Duplicates a wide value (a primitive long or double). |
InstructionBuilder |
getField(PlasticField field)
Loads an instance or static field onto the stack. |
InstructionBuilder |
getField(String className,
String fieldName,
Class fieldType)
Loads a field onto the stack. |
InstructionBuilder |
getField(String className,
String fieldName,
String typeName)
Loads an instance field onto the stack. |
InstructionBuilder |
getStaticField(String className,
String fieldName,
Class fieldType)
Gets a static field; does not consume a value from the stack, but pushes the fields' value onto the stack. |
InstructionBuilder |
getStaticField(String className,
String fieldName,
String typeName)
Gets a static field; does not consume a value from the stack, but pushes the fields' value onto the stack. |
InstructionBuilder |
increment(LocalVariable variable)
Increments a local integer variable. |
InstructionBuilder |
invoke(Class clazz,
Class returnType,
String methodName,
Class... argumentTypes)
Automatically invokes an interface or virtual method. |
InstructionBuilder |
invoke(Method method)
Automatically invokes an interface or virtual method. |
InstructionBuilder |
invokeConstructor(Class clazz,
Class... argumentTypes)
|
InstructionBuilder |
invokeConstructor(String className,
String... argumentTypes)
Invokes a constructor on a class. |
InstructionBuilder |
invokeInterface(String interfaceName,
String returnType,
String methodName,
String... argumentTypes)
Invokes a standard virtual method. |
InstructionBuilder |
invokeSpecial(String containingClassName,
MethodDescription description)
Invokes an instance method of a base class, or a private method of a class, using the target object and parameters already on the stack. |
InstructionBuilder |
invokeStatic(Class clazz,
Class returnType,
String methodName,
Class... argumentTypes)
Invokes a static method of a class. |
InstructionBuilder |
invokeVirtual(PlasticMethod method)
|
InstructionBuilder |
invokeVirtual(String className,
String returnType,
String methodName,
String... argumentTypes)
Invokes a standard virtual method. |
InstructionBuilder |
iterateArray(InstructionBuilderCallback callback)
Expects an array to be the top value on the stack. |
InstructionBuilder |
loadArgument(int index)
Loads an argument onto the stack, using the opcode appropriate to the argument's type. |
InstructionBuilder |
loadArguments()
Loads all arguments for the current method onto the stack; this is used when invoking a method that takes the exact same parameters (often, a super-class implementation). |
InstructionBuilder |
loadArrayElement()
Loads a value from an array object. |
InstructionBuilder |
loadArrayElement(int index,
String elementType)
Loads a value from an array object, which must be the top element of the stack. |
InstructionBuilder |
loadConstant(Object constant)
Loads a constant value |
InstructionBuilder |
loadNull()
Loads the null constant onto the stack. |
InstructionBuilder |
loadThis()
Loads this onto the stack. |
InstructionBuilder |
loadTypeConstant(Class type)
Loads a Java type (a Class instance) as a constant. |
InstructionBuilder |
loadTypeConstant(String typeName)
Loads a Java type (a Class instance) as a constant. |
InstructionBuilder |
loadVariable(LocalVariable variable)
Loads a value from a local variable and pushes it onto the stack. |
InstructionBuilder |
newInstance(Class clazz)
A convenience version of newInstance(String) used when the class is known
at build time. |
InstructionBuilder |
newInstance(String className)
Creates a new, uninitialized instance of the indicated class. |
InstructionBuilder |
pop()
Discards the top value on the stack. |
InstructionBuilder |
popWide()
Pops a wide value (a primitive long or double). |
InstructionBuilder |
putField(String className,
String fieldName,
Class fieldType)
|
InstructionBuilder |
putField(String className,
String fieldName,
String typeName)
Expects the stack to contain the instance to update, and the value to store into the field. |
InstructionBuilder |
putStaticField(String className,
String fieldName,
Class fieldType)
Sets a static field; the new field value should be on top of the stack. |
InstructionBuilder |
putStaticField(String className,
String fieldName,
String typeName)
Sets a static field; the new field value should be on top of the stack. |
InstructionBuilder |
returnDefaultValue()
Returns the default value for the method, which may be null, or a specific primitive value. |
InstructionBuilder |
returnResult()
Returns the top value on the stack. |
InstructionBuilder |
startSwitch(int min,
int max,
SwitchCallback callback)
Starts a switch statement. |
InstructionBuilder |
startTryCatch(TryCatchCallback tryCatchCallback)
Defines the start of a block that can have exception handlers and finally blocks applied. |
InstructionBuilder |
startVariable(String type,
LocalVariableCallback callback)
Starts a block where the given name is active. |
InstructionBuilder |
storeVariable(LocalVariable variable)
Stores the value on top of the stack to a local variable (previously defined by startVariable(String, LocalVariableCallback) . |
InstructionBuilder |
swap()
Swaps the top element of the stack with the next element down. |
InstructionBuilder |
throwException()
Throws the exception on the top of the stack. |
InstructionBuilder |
throwException(Class<? extends Throwable> exceptionType,
String message)
|
InstructionBuilder |
throwException(String className,
String message)
Throws an exception with a fixed message. |
InstructionBuilder |
unboxPrimitive(String typeName)
Unboxes a wrapper type to a primitive type if typeName is a primitive type name (the value on the stack should be the corresponding wrapper type instance). |
InstructionBuilder |
when(Condition condition,
InstructionBuilderCallback ifTrue)
Simplified version of when(Condition, WhenCallback) that
simply executes the callback code when the condition is true and does nothing
if the condition is false (the more general case). |
InstructionBuilder |
when(Condition condition,
WhenCallback callback)
Executes conditional code based on a Condition . |
Method Detail |
---|
@Opcodes(value="ACONST_NULL, LCONST_0, FCONST_0, DCONST_0, ICONST_0, RETURN, ARETURN, IRETURN, FRETURN, LRETURN, DRETURN") InstructionBuilder returnDefaultValue()
@Opcodes(value="ALOAD") InstructionBuilder loadThis()
@Opcodes(value="ACONST_NULL") InstructionBuilder loadNull()
@Opcodes(value="ALOAD, ILOAD, LLOAD, FLOAD, DLOAD") InstructionBuilder loadArgument(int index)
index
- to argument (0 is the first argument, not this)@Opcodes(value="ALOAD, ILOAD, LLOAD, FLOAD, DLOAD") InstructionBuilder loadArguments()
loadThis()
(or
some other way of identifying the target method) should precede this call.
@Opcodes(value="INVOKESPECIAL") InstructionBuilder invokeSpecial(String containingClassName, MethodDescription description)
containingClassName
- class name containing the methoddescription
- describes the method name, parameters and return type@Opcodes(value="INVOKEVIRTUAL") InstructionBuilder invokeVirtual(String className, String returnType, String methodName, String... argumentTypes)
@Opcodes(value="INVOKEVIRTUAL") InstructionBuilder invokeVirtual(PlasticMethod method)
@Opcodes(value="INVOKEINTERFACE") InstructionBuilder invokeInterface(String interfaceName, String returnType, String methodName, String... argumentTypes)
@Opcodes(value="INVOKEVIRTUAL, INVOKEINTERFACE") InstructionBuilder invoke(Class clazz, Class returnType, String methodName, Class... argumentTypes)
invokeConstructor(Class, Class...)
for constructors and invokeSpecial(String, MethodDescription)
for private methods.
InstructionBuilder invoke(Method method)
invokeConstructor(Class, Class...)
for constructors and invokeSpecial(String, MethodDescription)
for private methods.
@Opcodes(value="INVOKESTATIC") InstructionBuilder invokeStatic(Class clazz, Class returnType, String methodName, Class... argumentTypes)
@Opcodes(value="ARETURN, IRETURN, LRETURN, FRETURN, DRETURN") InstructionBuilder returnResult()
@Opcodes(value="INVOKESTATIC") InstructionBuilder boxPrimitive(String typeName)
@Opcodes(value="INVOKEVIRTUAL") InstructionBuilder unboxPrimitive(String typeName)
typeName
- possibly primitive type name@Opcodes(value="GETFIELD") InstructionBuilder getField(String className, String fieldName, String typeName)
loadThis()
).
className
- name of class containing the fieldfieldName
- name of the fieldtypeName
- type of field@Opcodes(value="GETFIELD") InstructionBuilder getField(PlasticField field)
loadThis()
).
field
- identifies name, type and container of field to load@Opcodes(value="GETFIELD") InstructionBuilder getField(String className, String fieldName, Class fieldType)
className
- name of class containing the fieldfieldName
- name of the fieldfieldType
- type of field@Opcodes(value="GETSTATIC") InstructionBuilder getStaticField(String className, String fieldName, Class fieldType)
className
- name of class containing the fieldfieldName
- name of the fieldfieldType
- type of field@Opcodes(value="GETSTATIC") InstructionBuilder getStaticField(String className, String fieldName, String typeName)
className
- name of class containing the fieldfieldName
- name of the fieldtypeName
- type of field@Opcodes(value="PUTSTATIC") InstructionBuilder putStaticField(String className, String fieldName, Class fieldType)
className
- name of class containing the fieldfieldName
- name of the fieldfieldType
- type of field@Opcodes(value="PUTSTATIC") InstructionBuilder putStaticField(String className, String fieldName, String typeName)
className
- name of class containing the fieldfieldName
- name of the fieldtypeName
- type of field@Opcodes(value="PUTFIELD") InstructionBuilder putField(String className, String fieldName, String typeName)
@Opcodes(value="PUTFIELD") InstructionBuilder putField(String className, String fieldName, Class fieldType)
@Opcodes(value="LDC, AALOAD") InstructionBuilder loadArrayElement(int index, String elementType)
index
- constant index into the arrayelementType
- the type name of the elements of the array
Note: currently only reference types (objects and arrays) are supported, not
primitives@Opcodes(value="AALOAD") InstructionBuilder loadArrayElement()
@Opcodes(value="CHECKCAST") InstructionBuilder checkcast(String className)
className
- class to cast to@Opcodes(value="CHECKCAST") InstructionBuilder checkcast(Class clazz)
InstructionBuilder startTryCatch(TryCatchCallback tryCatchCallback)
tryCatchCallback
- allows generation of try, catch, and finally clauses@Opcodes(value="NEW") InstructionBuilder newInstance(String className)
className
- of class to instantiate@Opcodes(value="NEW") InstructionBuilder newInstance(Class clazz)
newInstance(String)
used when the class is known
at build time.
clazz
- to instantiate@Opcodes(value="INVOKESPECIAL") InstructionBuilder invokeConstructor(String className, String... argumentTypes)
className
- the class containing the constructorargumentTypes
- java type names for each argument of the constructor@Opcodes(value="INVOKESPECIAL") InstructionBuilder invokeConstructor(Class clazz, Class... argumentTypes)
@Opcodes(value="DUP, DUP_X1, DUP_X2") InstructionBuilder dupe(int depth)
depth
- 0 (DUP), 1 (DUP_X1) or 2 (DUP_X2)@Opcodes(value="DUP2") InstructionBuilder dupeWide()
@Opcodes(value="POP2") InstructionBuilder popWide()
@Opcodes(value="DUP") InstructionBuilder dupe()
when(Condition, WhenCallback)
.
dupe(int)
InstructionBuilder pop()
@Opcodes(value="SWAP") InstructionBuilder swap()
@Opcodes(value="LDC, ICONST_*, LCONST_*, FCONST_*, DCONST_*, ACONST_NULL") InstructionBuilder loadConstant(Object constant)
constant
- Integer, Float, Double, Long, String or null@Opcodes(value="LDC") InstructionBuilder loadTypeConstant(String typeName)
typeName
- Java class name@Opcodes(value="LDC") InstructionBuilder loadTypeConstant(Class type)
type
- Java type to load as a constant@Opcodes(value="CHECKCAST, INVOKEVIRTUAL") InstructionBuilder castOrUnbox(String typeName)
typeName
- to cast or unbox to@Opcodes(value="NEW, DUP, LDC, INVOKESPECIAL, ATHROW") InstructionBuilder throwException(String className, String message)
className
- name of exception class to instantiatemessage
- message (passed as first and only parameter to constructor)@Opcodes(value="NEW, DUP, LDC, INVOKESPECIAL, ATHROW") InstructionBuilder throwException(Class<? extends Throwable> exceptionType, String message)
@Opcodes(value="ATHROW") InstructionBuilder throwException()
@Opcodes(value="TABLESWITCH") InstructionBuilder startSwitch(int min, int max, SwitchCallback callback)
min
- the minimum value to match againstmax
- the maximum value to match againstInstructionBuilder startVariable(String type, LocalVariableCallback callback)
type
- type of local variablecallback
- generates code used when variable is in effect@Opcodes(value="ASTORE, ISTORE, LSTORE, FSTORE, DSTORE") InstructionBuilder storeVariable(LocalVariable variable)
startVariable(String, LocalVariableCallback)
.
@Opcodes(value="ALOAD, ILOAD, LLOAD, FLOAD, DLOAD") InstructionBuilder loadVariable(LocalVariable variable)
startVariable(String, LocalVariableCallback)
.
@Opcodes(value="IFEQ, etc., GOTO") InstructionBuilder when(Condition condition, WhenCallback callback)
Condition
. The testing opcodes all pop
the value off the stack, so this is usually preceded by dupe(0)
.
condition
- defines true and false casescallback
- provides code for true and false blocks
@Opcodes(value="IFEQ, etc., GOTO") InstructionBuilder when(Condition condition, InstructionBuilderCallback ifTrue)
when(Condition, WhenCallback)
that
simply executes the callback code when the condition is true and does nothing
if the condition is false (the more general case).
The testing opcodes all pop the value off the stack, so this is usually preceded by dupe(0)
.
condition
- to evaluateifTrue
- generates code for when condition is true
@Opcodes(value="IFEQ, etc., GOTO") InstructionBuilder doWhile(Condition condition, WhileCallback callback)
WhileCallback.buildBody(InstructionBuilder)
is executed, and then a GOTO back to the test code.
condition
- callback
-
@Opcodes(value="IINC, ARRAYLENGTH, IFEQ, etc., GOTO") InstructionBuilder iterateArray(InstructionBuilderCallback callback)
callback
- to invoke. The element will be the top value on the stack. The callback is responsible
for removing it from the stack.
@Opcodes(value="IINC") InstructionBuilder increment(LocalVariable variable)
@Opcodes(value="ARRAYLENGTH") InstructionBuilder arrayLength()
@Opcodes(value="LCMP, FCMPL, DCMPL") InstructionBuilder compareSpecial(String typeName)
typeName
-
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |