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 String
static final String
static final String
protected ArrayList
Exceptions needing to be handled in method (lazy create,null
if not used).protected MethodGen
Generator for constructing method.protected int
Accumulated hash code from adding instructions.protected InstructionBuilder
Builder for class instructions.private InstructionList
List of instructions in method definition.protected ClassItem
Method class item information.private ArrayList
Value types associated with local variable slots.protected Method
Actual generated method information.private StringStack
List of types currently on stack.protected BranchWrapper[]
Branch to be aimed at next appended instruction.protected HashMap
Map for initialized properties (lazy create,null
if not used).protected static ArrayList
Table of argument name lists (generated as needed).static final int
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
MethodBuilder
(String name, Type ret, Type[] args, ClassFile cf, int access) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addException
(String name) Add exception to those needing handling.int
Add local variable to method.Add constructed method to class.void
addMethodExceptions
(ClassItem method) Add exceptions thrown by called method to those needing handling.private void
append
(CompoundInstruction ins) Append compound instruction to method.private void
append
(Instruction ins) Append instruction to method.void
Append an AASTORE to the instruction list.void
Append an ACONST_NULL to the instruction list.void
appendALOAD
(String type) Append appropriate array load to the instruction list.void
Append an ARRAYLENGTH to the instruction list.void
appendASTORE
(String type) Append the appropriate array store to the instruction list.void
appendCall
(ClassItem item) Create invoke instruction for static, member, or interface method and append to method.void
appendCallInit
(String name, String signature) Create invoke initializer instruction from signature and append to method.void
appendCallInterface
(String method, String signature) Create invoke interface method instruction from signature and append to method.void
appendCallStatic
(String method, String signature) Create invoke static method instruction from signature and append to method.void
appendCallVirtual
(String method, String signature) Create invoke virtual method instruction from signature and append to method.void
appendCreateArray
(String type) Append instruction to create instance of array.void
Append check cast instruction from object (if needed).void
appendCreateCast
(String from, String to) Append check cast instruction (if needed).void
appendCreateNew
(String name) Append instruction to create instance of class.void
Append an DCMPG to the instruction list.void
Append a DUP to the instruction list.void
Append a DUP_X1 to the instruction list.void
Append a DUP2 to the instruction list.void
Append an FCMPG to the instruction list.void
Create get instruction and append to method.void
appendGetField
(ClassItem item) Create getfield instruction and append to method.void
appendGetStatic
(ClassItem item) Create getstatic instruction and append to method.void
Append an IASTORE to the instruction list.void
Append an ICONST_0 to the instruction list.void
Append 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.void
appendIncrementLocal
(int inc, int slot) Append instruction to increment local integer variable.void
Append instanceof check instruction.void
Append an ISUB to the instruction list.void
Append an IXOR to the instruction list.void
Append an LCMP to the instruction list.void
appendLoadConstant
(int value) Create load constant instruction and append to method.void
appendLoadConstant
(Object value) Create load constant instruction and append to method.void
appendLoadConstant
(String value) Create load constant instruction and append to method.void
appendLoadLocal
(int slot) Append instruction to load local variable.void
Append a POP to the instruction list.void
Append a POP2 to the instruction list.void
Create put instruction and append to method.void
appendPutField
(ClassItem item) Create putfield instruction and append to method.void
appendPutStatic
(ClassItem item) Create putstatic instruction and append to method.void
Append simple return.void
appendReturn
(Type type) Append typed return.void
appendStoreLocal
(int slot) Append instruction to store local variable.void
Append a SWAP to the instruction list.void
Append 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 BranchTarget
appendTargetInstruction
(CompoundInstruction inst) Append a compound instruction to the list as a branch target.private BranchTarget
appendTargetInstruction
(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.void
Append exception throw.Append unconditional branch instruction to method.void
codeComplete
(boolean suffix) Complete method construction.protected LocalVariableGen
createLocal
(String name, Type type) Add local variable to method.private String
Generate description of current stack state.int
Get access flags.protected InstructionHandle
Get first instruction in method.getItem()
Get the method item.getKeyValue
(Object key) Get local variable for object.protected InstructionHandle
Get last instruction in method.Method
Get the actual method.getName()
Get name of method being constructed.Get signature.protected abstract void
Process accumulated exceptions.int
hashCode()
Get hash code.protected void
initStackState
(String[] types) Initialize stack state to array of value types.void
initStackState
(BranchWrapper branch) Initialize stack state to match branch source.void
initStackState
(BranchWrapper branch, int pop) Initialize stack state to partially match branch source.protected InstructionHandle
Internal append instruction to create instance of class.boolean
Check if top item on stack is a long value.void
setAccessFlags
(int flags) Set access flags.setKeyValue
(Object key, Object value) Add keyed value to method definition.protected final void
setTarget
(InstructionHandle inst) Target branches if pending.void
targetNext
(BranchWrapper branch) Set branch target as next instruction added to method.void
targetNext
(BranchWrapper[] branches) Set branch targets as next instruction added to method.private String
Verify the top value in the stack state resulting from the current instruction list is an array.private void
verifyArray
(String type) Verify the top value in the stack state resulting from the current instruction list is an array of the specified type.private void
verifyCallStack
(String[] types) Verify the top values in the stack state resulting from the current instruction list.private void
verifyCallStack
(String clas, String[] types) Verify the top values in the stack state resulting from the current instruction list.private void
verifyCompatible
(String type, String need) Verify that a pair of value types represent compatible types.private void
verifyStack
(String t1) Verify the top value in the stack state resulting from the current instruction list.private void
verifyStack
(String t1, String t2) Verify the top two values in the stack state resulting from the current instruction list.private void
verifyStackDepth
(int count) Verify that at least the specified number of items are present on the stack.private void
Verify 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,null
if 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,null
if 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:
getName
in classBindingMethod
- Returns:
- name of method being constructed
-
getSignature
Get signature.- Specified by:
getSignature
in classBindingMethod
- Returns:
- signature for method
-
getAccessFlags
public int getAccessFlags()Get access flags.- Specified by:
getAccessFlags
in classBindingMethod
- Returns:
- flags for access type of method
-
setAccessFlags
public void setAccessFlags(int flags) Set access flags.- Specified by:
setAccessFlags
in 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:
getMethod
in 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 aString
reference 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 benull
to 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 aString
reference 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:
true
if long value,false
if 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:
getItem
in 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:
hashCode
in 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
-