Package org.jibx.binding.classes
Class MethodBuilder
java.lang.Object
org.jibx.binding.classes.BindingMethod
org.jibx.binding.classes.MethodBuilder
- Direct Known Subclasses:
ExceptionMethodBuilder
Method builder. Organizes and tracks the creation of a method, providing
convenience methods for common operations. This is customized for the needs
of JiBX, with some predetermined settings as appropriate. It supplies hash
code and equality checking based on the method signature and actual byte
code of the generated method, ignoring the method name.
- Author:
- Dennis M. Sosnoski
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Stringstatic final Stringstatic final Stringprotected ArrayListExceptions needing to be handled in method (lazy create,nullif not used).protected MethodGenGenerator for constructing method.protected intAccumulated hash code from adding instructions.protected InstructionBuilderBuilder for class instructions.private InstructionListList of instructions in method definition.protected ClassItemMethod class item information.private ArrayListValue types associated with local variable slots.protected MethodActual generated method information.private StringStackList of types currently on stack.protected BranchWrapper[]Branch to be aimed at next appended instruction.protected HashMapMap for initialized properties (lazy create,nullif not used).protected static ArrayListTable of argument name lists (generated as needed).static final int -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedMethodBuilder(String name, Type ret, Type[] args, ClassFile cf, int access) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddException(String name) Add exception to those needing handling.intAdd local variable to method.Add constructed method to class.voidaddMethodExceptions(ClassItem method) Add exceptions thrown by called method to those needing handling.private voidappend(CompoundInstruction ins) Append compound instruction to method.private voidappend(Instruction ins) Append instruction to method.voidAppend an AASTORE to the instruction list.voidAppend an ACONST_NULL to the instruction list.voidappendALOAD(String type) Append appropriate array load to the instruction list.voidAppend an ARRAYLENGTH to the instruction list.voidappendASTORE(String type) Append the appropriate array store to the instruction list.voidappendCall(ClassItem item) Create invoke instruction for static, member, or interface method and append to method.voidappendCallInit(String name, String signature) Create invoke initializer instruction from signature and append to method.voidappendCallInterface(String method, String signature) Create invoke interface method instruction from signature and append to method.voidappendCallStatic(String method, String signature) Create invoke static method instruction from signature and append to method.voidappendCallVirtual(String method, String signature) Create invoke virtual method instruction from signature and append to method.voidappendCreateArray(String type) Append instruction to create instance of array.voidAppend check cast instruction from object (if needed).voidappendCreateCast(String from, String to) Append check cast instruction (if needed).voidappendCreateNew(String name) Append instruction to create instance of class.voidAppend an DCMPG to the instruction list.voidAppend a DUP to the instruction list.voidAppend a DUP_X1 to the instruction list.voidAppend a DUP2 to the instruction list.voidAppend an FCMPG to the instruction list.voidCreate get instruction and append to method.voidappendGetField(ClassItem item) Create getfield instruction and append to method.voidappendGetStatic(ClassItem item) Create getstatic instruction and append to method.voidAppend an IASTORE to the instruction list.voidAppend an ICONST_0 to the instruction list.voidAppend an ICONST_1 to the instruction list.appendIF_ICMPNE(Object src) Append IF_ICMPNE branch instruction to method.appendIFEQ(Object src) Append IFEQ branch instruction to method.appendIFGE(Object src) Append IFGE branch instruction to method.appendIFLT(Object src) Append IFLT branch instruction to method.appendIFNE(Object src) Append IFNE branch instruction to method.appendIFNONNULL(Object src) Append IFNONNULL branch instruction to method.appendIFNULL(Object src) Append IFNULL branch instruction to method.voidappendIncrementLocal(int inc, int slot) Append instruction to increment local integer variable.voidAppend instanceof check instruction.voidAppend an ISUB to the instruction list.voidAppend an IXOR to the instruction list.voidAppend an LCMP to the instruction list.voidappendLoadConstant(int value) Create load constant instruction and append to method.voidappendLoadConstant(Object value) Create load constant instruction and append to method.voidappendLoadConstant(String value) Create load constant instruction and append to method.voidappendLoadLocal(int slot) Append instruction to load local variable.voidAppend a POP to the instruction list.voidAppend a POP2 to the instruction list.voidCreate put instruction and append to method.voidappendPutField(ClassItem item) Create putfield instruction and append to method.voidappendPutStatic(ClassItem item) Create putstatic instruction and append to method.voidAppend simple return.voidappendReturn(Type type) Append typed return.voidappendStoreLocal(int slot) Append instruction to store local variable.voidAppend a SWAP to the instruction list.voidAppend instructions to exchange a single-word value on the top of the stack with the double-word value below it on the stack.Append an ACONST_NULL to the instruction list as a branch target.appendTargetCreateNew(String name) Append instruction to create instance of class as a branch target.private BranchTargetappendTargetInstruction(CompoundInstruction inst) Append a compound instruction to the list as a branch target.private BranchTargetappendTargetInstruction(Instruction inst) Append an instruction to the list as a branch target.appendTargetLoadConstant(int value) Append a load constant instruction as a branch target.appendTargetLoadConstant(String value) Append a load constant instruction as a branch target.Append a NOP to the instruction list as a branch target.voidAppend exception throw.Append unconditional branch instruction to method.voidcodeComplete(boolean suffix) Complete method construction.protected LocalVariableGencreateLocal(String name, Type type) Add local variable to method.private StringGenerate description of current stack state.intGet access flags.protected InstructionHandleGet first instruction in method.getItem()Get the method item.getKeyValue(Object key) Get local variable for object.protected InstructionHandleGet last instruction in method.MethodGet the actual method.getName()Get name of method being constructed.Get signature.protected abstract voidProcess accumulated exceptions.inthashCode()Get hash code.protected voidinitStackState(String[] types) Initialize stack state to array of value types.voidinitStackState(BranchWrapper branch) Initialize stack state to match branch source.voidinitStackState(BranchWrapper branch, int pop) Initialize stack state to partially match branch source.protected InstructionHandleInternal append instruction to create instance of class.booleanCheck if top item on stack is a long value.voidsetAccessFlags(int flags) Set access flags.setKeyValue(Object key, Object value) Add keyed value to method definition.protected final voidsetTarget(InstructionHandle inst) Target branches if pending.voidtargetNext(BranchWrapper branch) Set branch target as next instruction added to method.voidtargetNext(BranchWrapper[] branches) Set branch targets as next instruction added to method.private StringVerify the top value in the stack state resulting from the current instruction list is an array.private voidverifyArray(String type) Verify the top value in the stack state resulting from the current instruction list is an array of the specified type.private voidverifyCallStack(String[] types) Verify the top values in the stack state resulting from the current instruction list.private voidverifyCallStack(String clas, String[] types) Verify the top values in the stack state resulting from the current instruction list.private voidverifyCompatible(String type, String need) Verify that a pair of value types represent compatible types.private voidverifyStack(String t1) Verify the top value in the stack state resulting from the current instruction list.private voidverifyStack(String t1, String t2) Verify the top two values in the stack state resulting from the current instruction list.private voidverifyStackDepth(int count) Verify that at least the specified number of items are present on the stack.private voidVerify that the top value in the stack state resulting from the current instruction list is an object reference.Methods inherited from class org.jibx.binding.classes.BindingMethod
computeMethodHash, equals, getClassFile, makeAccessible
-
Field Details
-
FRAMEWORK_EXCEPTION_CLASS
- See Also:
-
EXCEPTION_CONSTRUCTOR_SIGNATURE1
- See Also:
-
EXCEPTION_CONSTRUCTOR_SIGNATURE2
- See Also:
-
SYNTHETIC_ACCESS_FLAG
public static final int SYNTHETIC_ACCESS_FLAG- See Also:
-
s_argNameLists
Table of argument name lists (generated as needed). -
m_instructionBuilder
Builder for class instructions. -
m_instructionList
private InstructionList m_instructionListList of instructions in method definition. -
m_stackState
List of types currently on stack. -
m_generator
protected MethodGen m_generatorGenerator for constructing method. -
m_method
protected Method m_methodActual generated method information. -
m_item
Method class item information. -
m_localTypes
Value types associated with local variable slots. -
m_exceptions
Exceptions needing to be handled in method (lazy create,nullif not used). -
m_hashCode
protected int m_hashCodeAccumulated hash code from adding instructions. -
m_targetBranches
Branch to be aimed at next appended instruction. -
m_valueMap
Map for initialized properties (lazy create,nullif not used).
-
-
Constructor Details
-
MethodBuilder
Constructor. This sets up for constructing a method with public access.- Parameters:
name- method name to be builtret- method return typeargs- types of argumentscf- owning class file informationaccess- flags for method access
-
-
Method Details
-
getName
Get name of method being constructed.- Specified by:
getNamein classBindingMethod- Returns:
- name of method being constructed
-
getSignature
Get signature.- Specified by:
getSignaturein classBindingMethod- Returns:
- signature for method
-
getAccessFlags
public int getAccessFlags()Get access flags.- Specified by:
getAccessFlagsin classBindingMethod- Returns:
- flags for access type of method
-
setAccessFlags
public void setAccessFlags(int flags) Set access flags.- Specified by:
setAccessFlagsin classBindingMethod- Parameters:
flags- access type to be set
-
getMethod
public Method getMethod()Get the actual method. This can only be called once code generation is completed (after thecodeComplete(boolean)method is called).- Specified by:
getMethodin classBindingMethod- Returns:
- constructed method information
-
setKeyValue
Add keyed value to method definition.- Parameters:
key- retrieval keyvalue- keyed value- Returns:
- prior value for key
-
getKeyValue
Get local variable for object.- Parameters:
key- object key for local variable- Returns:
- local variable
-
addException
Add exception to those needing handling.- Parameters:
name- fully qualified name of exception class
-
addMethodExceptions
Add exceptions thrown by called method to those needing handling.- Parameters:
method- information for method to be handled
-
getFirstInstruction
protected InstructionHandle getFirstInstruction()Get first instruction in method.- Returns:
- handle for first instruction in method
-
getLastInstruction
protected InstructionHandle getLastInstruction()Get last instruction in method.- Returns:
- handle for last instruction in method
-
setTarget
protected final void setTarget(InstructionHandle inst) Target branches if pending. This implements setting the target of branch instructions supplied using thetargetNext(org.jibx.binding.classes.BranchWrapper)method.- Parameters:
inst- handle for appended instruction
-
describeStack
Generate description of current stack state.- Returns:
- stack state description
-
verifyCompatible
Verify that a pair of value types represent compatible types. This checks for equal types or downcast object types.- Parameters:
type- actual known type of valueneed- type needed
-
verifyStackDepth
private void verifyStackDepth(int count) Verify that at least the specified number of items are present on the stack.- Parameters:
count- minimum number of items required
-
verifyStack
Verify the top value in the stack state resulting from the current instruction list.- Parameters:
t1- expected type for top item on stack
-
verifyArray
Verify the top value in the stack state resulting from the current instruction list is an array.- Returns:
- array item type
-
verifyArray
Verify the top value in the stack state resulting from the current instruction list is an array of the specified type.- Parameters:
type- array item type
-
verifyStack
Verify the top two values in the stack state resulting from the current instruction list.- Parameters:
t1- expected type for first item on stackt2- expected type for second item on stack
-
verifyCallStack
Verify the top values in the stack state resulting from the current instruction list. This form checks only the actual call parameters.- Parameters:
types- expected parameter types on stack
-
verifyCallStack
Verify the top values in the stack state resulting from the current instruction list. This form checks both the object being called and the actual call parameters.- Parameters:
clas- name of method classtypes- expected parameter types on stack
-
verifyStackObject
private void verifyStackObject()Verify that the top value in the stack state resulting from the current instruction list is an object reference. -
appendIFEQ
Append IFEQ branch instruction to method.- Parameters:
src- object responsible for generating branch- Returns:
- wrapper for appended conditional branch
-
appendIFGE
Append IFGE branch instruction to method.- Parameters:
src- object responsible for generating branch- Returns:
- wrapper for appended conditional branch
-
appendIFLT
Append IFLT branch instruction to method.- Parameters:
src- object responsible for generating branch- Returns:
- wrapper for appended conditional branch
-
appendIFNE
Append IFNE branch instruction to method.- Parameters:
src- object responsible for generating branch- Returns:
- wrapper for appended conditional branch
-
appendIFNONNULL
Append IFNONNULL branch instruction to method.- Parameters:
src- object responsible for generating branch- Returns:
- wrapper for appended conditional branch
-
appendIFNULL
Append IFNULL branch instruction to method.- Parameters:
src- object responsible for generating branch- Returns:
- wrapper for appended conditional branch
-
appendIF_ICMPNE
Append IF_ICMPNE branch instruction to method.- Parameters:
src- object responsible for generating branch- Returns:
- wrapper for appended conditional branch
-
appendUnconditionalBranch
Append unconditional branch instruction to method.- Parameters:
src- object responsible for generating branch- Returns:
- wrapper for appended unconditional branch
-
append
private void append(CompoundInstruction ins) Append compound instruction to method.- Parameters:
ins- instruction to be appended
-
append
private void append(Instruction ins) Append instruction to method.- Parameters:
ins- instruction to be appended
-
appendLoadConstant
public void appendLoadConstant(int value) Create load constant instruction and append to method. Builds the most appropriate type of instruction for the value.- Parameters:
value- constant value to be loaded
-
appendLoadConstant
Create load constant instruction and append to method. Loads aStringreference from the constant pool.- Parameters:
value- constant value to be loaded
-
appendLoadConstant
Create load constant instruction and append to method. Loads an unwrapped primitive value from the constant pool.- Parameters:
value- constant value to be loaded
-
appendGetField
Create getfield instruction and append to method. Uses the target field information to generate the instruction.- Parameters:
item- information for field to be gotton
-
appendGetStatic
Create getstatic instruction and append to method. Uses the target field information to generate the instruction.- Parameters:
item- information for field to be set
-
appendGet
Create get instruction and append to method. This generates either a getstatic or a getfield instruction, as appropriate.- Parameters:
item- information for field to be gotten
-
appendPutField
Create putfield instruction and append to method. Uses the target field information to generate the instruction.- Parameters:
item- information for field to be set
-
appendPutStatic
Create putstatic instruction and append to method. Uses the target field information to generate the instruction.- Parameters:
item- information for field to be set
-
appendPut
Create put instruction and append to method. This generates either a putstatic or a putfield instruction, as appropriate.- Parameters:
item- information for field to be gotten
-
appendCall
Create invoke instruction for static, member, or interface method and append to method. Uses the target method information to generate the correct instruction.- Parameters:
item- information for method to be called
-
appendCallStatic
Create invoke static method instruction from signature and append to method.- Parameters:
method- fully qualified class and method namesignature- method signature in standard form
-
appendCallVirtual
Create invoke virtual method instruction from signature and append to method.- Parameters:
method- fully qualified class and method namesignature- method signature in standard form
-
appendCallInterface
Create invoke interface method instruction from signature and append to method.- Parameters:
method- fully qualified interface and method namesignature- method signature in standard form
-
appendCreateNew
Append instruction to create instance of class.- Parameters:
name- fully qualified class name
-
appendCallInit
Create invoke initializer instruction from signature and append to method.- Parameters:
name- fully qualified class namesignature- method signature in standard form
-
appendCreateArray
Append instruction to create instance of array.- Parameters:
type- fully qualified type name of array elements
-
appendCreateCast
Append check cast instruction (if needed).- Parameters:
from- fully qualified name of current typeto- fully qualified name of desired type
-
appendCreateCast
Append check cast instruction from object (if needed).- Parameters:
to- fully qualified name of desired type
-
appendInstanceOf
Append instanceof check instruction.- Parameters:
to- fully qualified name of type to check
-
createLocal
Add local variable to method. The current code in the method must have the initial value for the variable on the stack. The scope of the variable is defined from the last instruction to the end of the method unless otherwise modified.- Parameters:
name- local variable name (may benullto use default)type- variable type
-
addLocal
Add local variable to method. The current code in the method must have the initial value for the variable on the stack. The scope of the variable is defined from the preceding instruction to the end of the method.- Parameters:
name- local variable nametype- variable type- Returns:
- local variable slot number
-
appendLoadLocal
public void appendLoadLocal(int slot) Append instruction to load local variable.- Parameters:
slot- local variable slot to load
-
appendStoreLocal
public void appendStoreLocal(int slot) Append instruction to store local variable.- Parameters:
slot- local variable slot to store
-
appendIncrementLocal
public void appendIncrementLocal(int inc, int slot) Append instruction to increment local integer variable.- Parameters:
inc- amount of incrmentslot- local variable slot to load
-
appendReturn
public void appendReturn()Append simple return. -
appendReturn
public void appendReturn(Type type) Append typed return.- Parameters:
type- returned type (may beType.VOID)
-
appendThrow
public void appendThrow()Append exception throw. -
appendALOAD
Append appropriate array load to the instruction list.- Parameters:
type- array item type expected
-
appendAASTORE
public void appendAASTORE()Append an AASTORE to the instruction list. Doesn't actually check the types, just the count of items present. -
appendASTORE
Append the appropriate array store to the instruction list.- Parameters:
type- array item type expected
-
appendACONST_NULL
public void appendACONST_NULL()Append an ACONST_NULL to the instruction list. -
appendARRAYLENGTH
public void appendARRAYLENGTH()Append an ARRAYLENGTH to the instruction list. -
appendDCMPG
public void appendDCMPG()Append an DCMPG to the instruction list. -
appendDUP
public void appendDUP()Append a DUP to the instruction list. -
appendDUP2
public void appendDUP2()Append a DUP2 to the instruction list. -
appendDUP_X1
public void appendDUP_X1()Append a DUP_X1 to the instruction list. -
appendFCMPG
public void appendFCMPG()Append an FCMPG to the instruction list. -
appendIASTORE
public void appendIASTORE()Append an IASTORE to the instruction list. Doesn't actually check the types, just the count of items present. -
appendICONST_0
public void appendICONST_0()Append an ICONST_0 to the instruction list. -
appendICONST_1
public void appendICONST_1()Append an ICONST_1 to the instruction list. -
appendISUB
public void appendISUB()Append an ISUB to the instruction list. -
appendIXOR
public void appendIXOR()Append an IXOR to the instruction list. -
appendLCMP
public void appendLCMP()Append an LCMP to the instruction list. -
appendPOP
public void appendPOP()Append a POP to the instruction list. -
appendPOP2
public void appendPOP2()Append a POP2 to the instruction list. -
appendSWAP
public void appendSWAP()Append a SWAP to the instruction list. -
appendSWAP1For2
public void appendSWAP1For2()Append instructions to exchange a single-word value on the top of the stack with the double-word value below it on the stack. -
appendTargetInstruction
Append a compound instruction to the list as a branch target.- Parameters:
inst- compound instruction to be appended as branch target- Returns:
- branch target information
-
appendTargetInstruction
Append an instruction to the list as a branch target.- Parameters:
inst- instruction to be appended as branch target- Returns:
- branch target information
-
appendTargetNOP
Append a NOP to the instruction list as a branch target.- Returns:
- branch target information
-
appendTargetACONST_NULL
Append an ACONST_NULL to the instruction list as a branch target.- Returns:
- branch target information
-
appendTargetLoadConstant
Append a load constant instruction as a branch target. Builds the most appropriate type of instruction for the value.- Parameters:
value- constant value to be loaded- Returns:
- branch target information
-
appendTargetLoadConstant
Append a load constant instruction as a branch target. Loads aStringreference from the constant pool.- Parameters:
value- constant value to be loaded- Returns:
- branch target information
-
appendTargetCreateNew
Append instruction to create instance of class as a branch target.- Parameters:
name- fully qualified class name- Returns:
- branch target information
-
internalAppendCreateNew
Internal append instruction to create instance of class. This is used by subclasses when they need access to the actual instruction handle.- Parameters:
name- fully qualified class name
-
isStackTopLong
public boolean isStackTopLong()Check if top item on stack is a long value.- Returns:
trueif long value,falseif not
-
initStackState
Initialize stack state to match branch source. This can be used to set the expected stack state following an unconditional transfer of control instruction. The principle here is that the code to be generated must be reached by a branch, so the stack state must match that of the branch source.- Parameters:
branch- wrapper for branch to be for stack initialization
-
initStackState
Initialize stack state to partially match branch source. This can be used to set the expected stack state following an unconditional transfer of control instruction. The specified number of items are removed from the branch stack, with the assumption that code to add these items will be appended before the branch join point is reached.- Parameters:
branch- wrapper for branch to be for stack initializationpop- number of items to be removed from branch source stack state
-
initStackState
Initialize stack state to array of value types. This can be used to set the expected stack state following an unconditional transfer of control instruction.- Parameters:
types- array of type names on stack
-
targetNext
Set branch target as next instruction added to method. This effectively sets up a state trigger for the next append operation. The appended instruction is set as the target for the branch. This requires that instructions are only appended using the methods supplied in this class.- Parameters:
branch- wrapper for branch to be aimed at next instruction (may benull, in which case nothing is done)
-
targetNext
Set branch targets as next instruction added to method. This effectively sets up a state trigger for the next append operation. The appended instruction is set as the target for all the branches. This requires that instructions are only appended using the methods supplied in this class.- Parameters:
branches- wrappers for branches to be aimed at next instruction (may benull, in which case nothing is done)
-
handleExceptions
protected abstract void handleExceptions()Process accumulated exceptions. Each subclass must implement this method to perform the appropriate handling of the checked exceptions that may be thrown in the constructed method. -
codeComplete
public void codeComplete(boolean suffix) Complete method construction. Finalizes the instruction list and generates the byte code for the constructed method, then computes the hash code based on the byte code. If requested, an appropriate suffix is tacked on the end of the supplied name in order to make sure that it will not be duplicated (even in a superclass or subclass).- Parameters:
suffix- add suffix to make method name unique
-
getItem
Get the method item.- Specified by:
getItemin classBindingMethod- Returns:
- method item information
-
hashCode
public int hashCode()Get hash code. This is based only on the byte code in the method, and is only valid after thecodeComplete(boolean)method is called.- Specified by:
hashCodein classBindingMethod- Returns:
- hash code based on code sequence
-
addMethod
Add constructed method to class. Makes the method callable, generating the method information.- Returns:
- added method information
-