Package org.apache.tapestry5.plastic
Interface InstructionBuilder
- All Known Implementing Classes:
InstructionBuilderImpl
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
Modifier and TypeMethodDescriptionExpects the top object on the stack to be an array.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.castOrUnbox
(String typeName) Casts the object on top of the stack to the indicated type.Adds a check that the object on top of the stack is assignable to the indicated class.compareSpecial
(String typeName) Special comparison logic for primitive float, double and long.doWhile
(Condition condition, WhileCallback callback) Implements a simple loop based on a condition.dupe()
Duplicates the top object on the stack.dupe
(int depth) Duplicates the top object on the stack, placing the result at some depth.dupeWide()
Duplicates a wide value (a primitive long or double).Loads a field onto the stack.Loads an instance field onto the stack.getField
(PlasticField field) Loads an instance or static field onto the stack.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.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.increment
(LocalVariable variable) Increments a local integer variable.instanceOf
(Class clazz) Adds a check that the object on top of the stack is assignable to the indicated class.instanceOf
(String className) Adds a check that the object on top of the stack is assignable to the indicated class.Automatically invokes an interface or virtual method.Automatically invokes an interface or virtual method.invokeConstructor
(Class clazz, Class... argumentTypes) invokeConstructor
(String className, String... argumentTypes) Invokes a constructor on a class.invokeInterface
(String interfaceName, String returnType, String methodName, String... argumentTypes) Invokes a standard virtual method.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.invokeStatic
(Class clazz, Class returnType, String methodName, Class... argumentTypes) Invokes a static method of a class.invokeVirtual
(String className, String returnType, String methodName, String... argumentTypes) Invokes a standard virtual method.invokeVirtual
(PlasticMethod method) iterateArray
(InstructionBuilderCallback callback) Expects an array to be the top value on the stack.loadArgument
(int index) Loads an argument onto the stack, using the opcode appropriate to the argument's type.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).Loads a value from an array object.loadArrayElement
(int index, String elementType) Loads a value from an array object, which must be the top element of the stack.loadConstant
(Object constant) Loads a constant valueloadNull()
Loads the null constant onto the stack.loadThis()
Loads this onto the stack.loadTypeConstant
(Class type) Loads a Java type (a Class instance) as a constant.loadTypeConstant
(String typeName) Loads a Java type (a Class instance) as a constant.loadVariable
(LocalVariable variable) Loads a value from a local variable and pushes it onto the stack.newInstance
(Class clazz) A convenience version ofnewInstance(String)
used when the class is known at build time.newInstance
(String className) Creates a new, uninitialized instance of the indicated class.pop()
Discards the top value on the stack.popWide()
Pops a wide value (a primitive long or double).Expects the stack to contain the instance to update, and the value to store into the field.putStaticField
(String className, String fieldName, Class fieldType) Sets a static field; the new field value should be on top of the stack.putStaticField
(String className, String fieldName, String typeName) Sets a static field; the new field value should be on top of the stack.Returns the default value for the method, which may be null, or a specific primitive value.Returns the top value on the stack.startSwitch
(int min, int max, SwitchCallback callback) Starts a switch statement.startTryCatch
(TryCatchCallback tryCatchCallback) Defines the start of a block that can have exception handlers and finally blocks applied.startVariable
(String type, LocalVariableCallback callback) Starts a block where the given name is active.storeVariable
(LocalVariable variable) Stores the value on top of the stack to a local variable (previously defined bystartVariable(String, LocalVariableCallback)
.swap()
Swaps the top element of the stack with the next element down.Throws the exception on the top of the stack.throwException
(Class<? extends Throwable> exceptionType, String message) throwException
(String className, String message) Throws an exception with a fixed message.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).when
(Condition condition, InstructionBuilderCallback ifTrue) Simplified version ofwhen(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).when
(Condition condition, WhenCallback callback) Executes conditional code based on aCondition
.
-
Method Details
-
returnDefaultValue
@Opcodes("ACONST_NULL, LCONST_0, FCONST_0, DCONST_0, ICONST_0, RETURN, ARETURN, IRETURN, FRETURN, LRETURN, DRETURN") InstructionBuilder returnDefaultValue()Returns the default value for the method, which may be null, or a specific primitive value. -
loadThis
Loads this onto the stack. -
loadNull
Loads the null constant onto the stack. -
loadArgument
Loads an argument onto the stack, using the opcode appropriate to the argument's type. In addition this automatically adjusts for arguments of primitive type long or double (which take up two local variable indexes, rather than one as for all other types)- Parameters:
index
- to argument (0 is the first argument, not this)
-
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). A call toloadThis()
(or some other way of identifying the target method) should precede this call. -
invokeSpecial
@Opcodes("INVOKESPECIAL") 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. Leaves the result on the stack (unless its a void method).- Parameters:
containingClassName
- class name containing the methoddescription
- describes the method name, parameters and return type
-
invokeVirtual
@Opcodes("INVOKEVIRTUAL") InstructionBuilder invokeVirtual(String className, String returnType, String methodName, String... argumentTypes) Invokes a standard virtual method. -
invokeVirtual
-
invokeInterface
@Opcodes("INVOKEINTERFACE") InstructionBuilder invokeInterface(String interfaceName, String returnType, String methodName, String... argumentTypes) Invokes a standard virtual method. -
invoke
@Opcodes("INVOKEVIRTUAL, INVOKEINTERFACE") InstructionBuilder invoke(Class clazz, Class returnType, String methodName, Class... argumentTypes) Automatically invokes an interface or virtual method. Remember to useinvokeConstructor(Class, Class...)
for constructors andinvokeSpecial(String, MethodDescription)
for private methods. -
invoke
Automatically invokes an interface or virtual method. Remember to useinvokeConstructor(Class, Class...)
for constructors andinvokeSpecial(String, MethodDescription)
for private methods. -
invokeStatic
@Opcodes("INVOKESTATIC") InstructionBuilder invokeStatic(Class clazz, Class returnType, String methodName, Class... argumentTypes) Invokes a static method of a class. -
returnResult
Returns the top value on the stack. For void methods, no value should be on the stack and the method will simply return. -
boxPrimitive
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. Does nothing if the type is not primitive, or type void. -
unboxPrimitive
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). Does nothing for non-primitive types.- Parameters:
typeName
- possibly primitive type name
-
getField
@Opcodes("GETFIELD") InstructionBuilder getField(String className, String fieldName, String typeName) Loads an instance field onto the stack. The object containing the field should already be loaded onto the stack (usually, vialoadThis()
).- Parameters:
className
- name of class containing the fieldfieldName
- name of the fieldtypeName
- type of field
-
getField
Loads an instance or static field onto the stack. The plastic class instance containing the field should already be loaded onto the stack (usually, vialoadThis()
).- Parameters:
field
- identifies name, type and container of field to load
-
getField
@Opcodes("GETFIELD") InstructionBuilder getField(String className, String fieldName, Class fieldType) Loads a field onto the stack. This version is used when the field type is known at build time, rather than discovered at runtime.- Parameters:
className
- name of class containing the fieldfieldName
- name of the fieldfieldType
- type of field
-
getStaticField
@Opcodes("GETSTATIC") 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.- Parameters:
className
- name of class containing the fieldfieldName
- name of the fieldfieldType
- type of field
-
getStaticField
@Opcodes("GETSTATIC") 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.- Parameters:
className
- name of class containing the fieldfieldName
- name of the fieldtypeName
- type of field
-
putStaticField
@Opcodes("PUTSTATIC") InstructionBuilder putStaticField(String className, String fieldName, Class fieldType) Sets a static field; the new field value should be on top of the stack.- Parameters:
className
- name of class containing the fieldfieldName
- name of the fieldfieldType
- type of field
-
putStaticField
@Opcodes("PUTSTATIC") InstructionBuilder putStaticField(String className, String fieldName, String typeName) Sets a static field; the new field value should be on top of the stack.- Parameters:
className
- name of class containing the fieldfieldName
- name of the fieldtypeName
- type of field
-
putField
@Opcodes("PUTFIELD") 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. -
putField
-
loadArrayElement
Loads a value from an array object, which must be the top element of the stack.- Parameters:
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
-
loadArrayElement
Loads a value from an array object. The stack should have the array at depth 1, and an array index on top. Only object arrays (not arrays of primitives) are supported. -
checkcast
Adds a check that the object on top of the stack is assignable to the indicated class.- Parameters:
className
- class to cast to
-
checkcast
-
instanceOf
Adds a check that the object on top of the stack is assignable to the indicated class.- Parameters:
className
- class to cast to- Since:
- 5.8.4
-
instanceOf
Adds a check that the object on top of the stack is assignable to the indicated class.- Since:
- 5.8.4
-
startTryCatch
Defines the start of a block that can have exception handlers and finally blocks applied. Continue using this InstructionBuilder to define code inside the block, then call methods on the InstructionBlock to define the end of the block and set up handlers.- Parameters:
tryCatchCallback
- allows generation of try, catch, and finally clauses
-
newInstance
Creates a new, uninitialized instance of the indicated class. This should be followed by code to call the new instance's constructor.- Parameters:
className
- of class to instantiate
-
newInstance
A convenience version ofnewInstance(String)
used when the class is known at build time.- Parameters:
clazz
- to instantiate
-
invokeConstructor
@Opcodes("INVOKESPECIAL") InstructionBuilder invokeConstructor(String className, String... argumentTypes) Invokes a constructor on a class. The instance should already be on the stack, followed by the right number and type of parameters. Note that a constructor acts like a void method, so you will often follow the sequence: newInstance(), dupe(0), invokeConstructor() so that a reference to the instance is left on the stack.F- Parameters:
className
- the class containing the constructorargumentTypes
- java type names for each argument of the constructor
-
invokeConstructor
-
dupe
Duplicates the top object on the stack, placing the result at some depth.- Parameters:
depth
- 0 (DUP), 1 (DUP_X1) or 2 (DUP_X2)
-
dupeWide
Duplicates a wide value (a primitive long or double). -
popWide
Pops a wide value (a primitive long or double). -
dupe
Duplicates the top object on the stack. Commonly used withwhen(Condition, WhenCallback)
.- See Also:
-
pop
Discards the top value on the stack. Assumes the value is a single word value: an object reference, or a small primitive) and not a double or long. -
swap
Swaps the top element of the stack with the next element down. Note that this can cause problems if the top element on the stack is a long or double. -
loadConstant
@Opcodes("LDC, ICONST_*, LCONST_*, FCONST_*, DCONST_*, ACONST_NULL") InstructionBuilder loadConstant(Object constant) Loads a constant value- Parameters:
constant
- Integer, Float, Double, Long, String or null
-
loadTypeConstant
Loads a Java type (a Class instance) as a constant. This assumes the type name is the name of class (or array) but not a primitive type.- Parameters:
typeName
- Java class name
-
loadTypeConstant
Loads a Java type (a Class instance) as a constant. This assumes the type name is the name of class (or array) but not a primitive type.- Parameters:
type
- Java type to load as a constant
-
castOrUnbox
Casts the object on top of the stack to the indicated type. For primitive types, casts to the wrapper type and invokes the appropriate unboxing static method call, leaving a primitive type value on the stack.- Parameters:
typeName
- to cast or unbox to
-
throwException
@Opcodes("NEW, DUP, LDC, INVOKESPECIAL, ATHROW") InstructionBuilder throwException(String className, String message) Throws an exception with a fixed message. Assumes the exception class includes a constructor that takes a single string.- Parameters:
className
- name of exception class to instantiatemessage
- message (passed as first and only parameter to constructor)
-
throwException
@Opcodes("NEW, DUP, LDC, INVOKESPECIAL, ATHROW") InstructionBuilder throwException(Class<? extends Throwable> exceptionType, String message) -
throwException
Throws the exception on the top of the stack. -
startSwitch
Starts a switch statement.- Parameters:
min
- the minimum value to match againstmax
- the maximum value to match against
-
startVariable
Starts a block where the given name is active.- Parameters:
type
- type of local variablecallback
- generates code used when variable is in effect
-
storeVariable
@Opcodes("ASTORE, ISTORE, LSTORE, FSTORE, DSTORE") InstructionBuilder storeVariable(LocalVariable variable) Stores the value on top of the stack to a local variable (previously defined bystartVariable(String, LocalVariableCallback)
. -
loadVariable
@Opcodes("ALOAD, ILOAD, LLOAD, FLOAD, DLOAD") InstructionBuilder loadVariable(LocalVariable variable) Loads a value from a local variable and pushes it onto the stack. The variable is defined bystartVariable(String, LocalVariableCallback)
. -
when
Executes conditional code based on aCondition
. The testing opcodes all pop the value off the stack, so this is usually preceded bydupe(0)
.- Parameters:
condition
- defines true and false casescallback
- provides code for true and false blocks- Returns:
- this builder
-
when
@Opcodes("IFEQ, etc., GOTO") InstructionBuilder when(Condition condition, InstructionBuilderCallback ifTrue) Simplified version ofwhen(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 bydupe(0)
.- Parameters:
condition
- to evaluateifTrue
- generates code for when condition is true- Returns:
- this builder
-
doWhile
@Opcodes("IFEQ, etc., GOTO") InstructionBuilder doWhile(Condition condition, WhileCallback callback) Implements a simple loop based on a condition. First the WhileCallback.buildTest(InstructionBuilder) code is executed, then the condition is evaluated (which will consume at least the top value on the stack). When the condition is false, the loop is exited. When the condition is true, the code defined byWhileCallback.buildBody(InstructionBuilder)
is executed, and then a GOTO back to the test code.- Parameters:
condition
-callback
-- Returns:
- this builder
-
iterateArray
@Opcodes("IINC, ARRAYLENGTH, IFEQ, etc., GOTO") InstructionBuilder iterateArray(InstructionBuilderCallback callback) Expects an array to be the top value on the stack. Iterates the array. The callback generates code that will have each successive value from the array as the top value on the stack. Creates a variable to store the loop index.- Parameters:
callback
- to invoke. The element will be the top value on the stack. The callback is responsible for removing it from the stack.- Returns:
- this builder
-
increment
Increments a local integer variable.- Returns:
- this builder
-
arrayLength
Expects the top object on the stack to be an array. Replaces it with the length of that array. -
compareSpecial
Special comparison logic for primitive float, double and long. Expect two matching wide values on the stack. Reduces the two wide values to a single int value: -1, 0, or 1 depending on whether the deeper value is less than, equal to, or greater than the top value on the stack.- Parameters:
typeName
-
-