Class MethodBuilder

java.lang.Object
org.jibx.binding.classes.BindingMethod
org.jibx.binding.classes.MethodBuilder
Direct Known Subclasses:
ExceptionMethodBuilder

public abstract class MethodBuilder extends BindingMethod
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 Details

    • FRAMEWORK_EXCEPTION_CLASS

      public static final String FRAMEWORK_EXCEPTION_CLASS
      See Also:
    • EXCEPTION_CONSTRUCTOR_SIGNATURE1

      public static final String EXCEPTION_CONSTRUCTOR_SIGNATURE1
      See Also:
    • EXCEPTION_CONSTRUCTOR_SIGNATURE2

      public static final String EXCEPTION_CONSTRUCTOR_SIGNATURE2
      See Also:
    • SYNTHETIC_ACCESS_FLAG

      public static final int SYNTHETIC_ACCESS_FLAG
      See Also:
    • s_argNameLists

      protected static ArrayList s_argNameLists
      Table of argument name lists (generated as needed).
    • m_instructionBuilder

      protected InstructionBuilder m_instructionBuilder
      Builder for class instructions.
    • m_instructionList

      private InstructionList m_instructionList
      List of instructions in method definition.
    • m_stackState

      private StringStack m_stackState
      List of types currently on stack.
    • m_generator

      protected MethodGen m_generator
      Generator for constructing method.
    • m_method

      protected Method m_method
      Actual generated method information.
    • m_item

      protected ClassItem m_item
      Method class item information.
    • m_localTypes

      private ArrayList m_localTypes
      Value types associated with local variable slots.
    • m_exceptions

      protected ArrayList m_exceptions
      Exceptions needing to be handled in method (lazy create, null if not used).
    • m_hashCode

      protected int m_hashCode
      Accumulated hash code from adding instructions.
    • m_targetBranches

      protected BranchWrapper[] m_targetBranches
      Branch to be aimed at next appended instruction.
    • m_valueMap

      protected HashMap m_valueMap
      Map for initialized properties (lazy create, null if not used).
  • Constructor Details

    • MethodBuilder

      protected MethodBuilder(String name, Type ret, Type[] args, ClassFile cf, int access)
      Constructor. This sets up for constructing a method with public access.
      Parameters:
      name - method name to be built
      ret - method return type
      args - types of arguments
      cf - owning class file information
      access - flags for method access
  • Method Details

    • getName

      public String getName()
      Get name of method being constructed.
      Specified by:
      getName in class BindingMethod
      Returns:
      name of method being constructed
    • getSignature

      public String getSignature()
      Get signature.
      Specified by:
      getSignature in class BindingMethod
      Returns:
      signature for method
    • getAccessFlags

      public int getAccessFlags()
      Get access flags.
      Specified by:
      getAccessFlags in class BindingMethod
      Returns:
      flags for access type of method
    • setAccessFlags

      public void setAccessFlags(int flags)
      Set access flags.
      Specified by:
      setAccessFlags in class BindingMethod
      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 the codeComplete(boolean) method is called).
      Specified by:
      getMethod in class BindingMethod
      Returns:
      constructed method information
    • setKeyValue

      public Object setKeyValue(Object key, Object value)
      Add keyed value to method definition.
      Parameters:
      key - retrieval key
      value - keyed value
      Returns:
      prior value for key
    • getKeyValue

      public Object getKeyValue(Object key)
      Get local variable for object.
      Parameters:
      key - object key for local variable
      Returns:
      local variable
    • addException

      public void addException(String name)
      Add exception to those needing handling.
      Parameters:
      name - fully qualified name of exception class
    • addMethodExceptions

      public void addMethodExceptions(ClassItem method)
      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 the targetNext(org.jibx.binding.classes.BranchWrapper) method.
      Parameters:
      inst - handle for appended instruction
    • describeStack

      private String describeStack()
      Generate description of current stack state.
      Returns:
      stack state description
    • verifyCompatible

      private void verifyCompatible(String type, String need)
      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 value
      need - 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

      private void verifyStack(String t1)
      Verify the top value in the stack state resulting from the current instruction list.
      Parameters:
      t1 - expected type for top item on stack
    • verifyArray

      private String verifyArray()
      Verify the top value in the stack state resulting from the current instruction list is an array.
      Returns:
      array item type
    • verifyArray

      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.
      Parameters:
      type - array item type
    • verifyStack

      private void verifyStack(String t1, String t2)
      Verify the top two values in the stack state resulting from the current instruction list.
      Parameters:
      t1 - expected type for first item on stack
      t2 - expected type for second item on stack
    • verifyCallStack

      private void verifyCallStack(String[] types)
      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

      private void verifyCallStack(String clas, String[] types)
      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 class
      types - 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

      public BranchWrapper appendIFEQ(Object src)
      Append IFEQ branch instruction to method.
      Parameters:
      src - object responsible for generating branch
      Returns:
      wrapper for appended conditional branch
    • appendIFGE

      public BranchWrapper appendIFGE(Object src)
      Append IFGE branch instruction to method.
      Parameters:
      src - object responsible for generating branch
      Returns:
      wrapper for appended conditional branch
    • appendIFLT

      public BranchWrapper appendIFLT(Object src)
      Append IFLT branch instruction to method.
      Parameters:
      src - object responsible for generating branch
      Returns:
      wrapper for appended conditional branch
    • appendIFNE

      public BranchWrapper appendIFNE(Object src)
      Append IFNE branch instruction to method.
      Parameters:
      src - object responsible for generating branch
      Returns:
      wrapper for appended conditional branch
    • appendIFNONNULL

      public BranchWrapper appendIFNONNULL(Object src)
      Append IFNONNULL branch instruction to method.
      Parameters:
      src - object responsible for generating branch
      Returns:
      wrapper for appended conditional branch
    • appendIFNULL

      public BranchWrapper appendIFNULL(Object src)
      Append IFNULL branch instruction to method.
      Parameters:
      src - object responsible for generating branch
      Returns:
      wrapper for appended conditional branch
    • appendIF_ICMPNE

      public BranchWrapper appendIF_ICMPNE(Object src)
      Append IF_ICMPNE branch instruction to method.
      Parameters:
      src - object responsible for generating branch
      Returns:
      wrapper for appended conditional branch
    • appendUnconditionalBranch

      public BranchWrapper appendUnconditionalBranch(Object src)
      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

      public void appendLoadConstant(String value)
      Create load constant instruction and append to method. Loads a String reference from the constant pool.
      Parameters:
      value - constant value to be loaded
    • appendLoadConstant

      public void appendLoadConstant(Object value)
      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

      public void appendGetField(ClassItem item)
      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

      public void appendGetStatic(ClassItem item)
      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

      public void appendGet(ClassItem item)
      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

      public void appendPutField(ClassItem item)
      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

      public void appendPutStatic(ClassItem item)
      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

      public void appendPut(ClassItem item)
      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

      public void appendCall(ClassItem item)
      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

      public void appendCallStatic(String method, String signature)
      Create invoke static method instruction from signature and append to method.
      Parameters:
      method - fully qualified class and method name
      signature - method signature in standard form
    • appendCallVirtual

      public void appendCallVirtual(String method, String signature)
      Create invoke virtual method instruction from signature and append to method.
      Parameters:
      method - fully qualified class and method name
      signature - method signature in standard form
    • appendCallInterface

      public void appendCallInterface(String method, String signature)
      Create invoke interface method instruction from signature and append to method.
      Parameters:
      method - fully qualified interface and method name
      signature - method signature in standard form
    • appendCreateNew

      public void appendCreateNew(String name)
      Append instruction to create instance of class.
      Parameters:
      name - fully qualified class name
    • appendCallInit

      public void appendCallInit(String name, String signature)
      Create invoke initializer instruction from signature and append to method.
      Parameters:
      name - fully qualified class name
      signature - method signature in standard form
    • appendCreateArray

      public void appendCreateArray(String type)
      Append instruction to create instance of array.
      Parameters:
      type - fully qualified type name of array elements
    • appendCreateCast

      public void appendCreateCast(String from, String to)
      Append check cast instruction (if needed).
      Parameters:
      from - fully qualified name of current type
      to - fully qualified name of desired type
    • appendCreateCast

      public void appendCreateCast(String to)
      Append check cast instruction from object (if needed).
      Parameters:
      to - fully qualified name of desired type
    • appendInstanceOf

      public void appendInstanceOf(String to)
      Append instanceof check instruction.
      Parameters:
      to - fully qualified name of type to check
    • createLocal

      protected LocalVariableGen createLocal(String name, Type type)
      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 be null to use default)
      type - variable type
    • addLocal

      public int addLocal(String name, Type type)
      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 name
      type - 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 incrment
      slot - 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 be Type.VOID)
    • appendThrow

      public void appendThrow()
      Append exception throw.
    • appendALOAD

      public void appendALOAD(String type)
      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

      public void appendASTORE(String type)
      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

      private BranchTarget appendTargetInstruction(CompoundInstruction inst)
      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

      private BranchTarget appendTargetInstruction(Instruction inst)
      Append an instruction to the list as a branch target.
      Parameters:
      inst - instruction to be appended as branch target
      Returns:
      branch target information
    • appendTargetNOP

      public BranchTarget appendTargetNOP()
      Append a NOP to the instruction list as a branch target.
      Returns:
      branch target information
    • appendTargetACONST_NULL

      public BranchTarget appendTargetACONST_NULL()
      Append an ACONST_NULL to the instruction list as a branch target.
      Returns:
      branch target information
    • appendTargetLoadConstant

      public BranchTarget appendTargetLoadConstant(int value)
      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

      public BranchTarget appendTargetLoadConstant(String value)
      Append a load constant instruction as a branch target. Loads a String reference from the constant pool.
      Parameters:
      value - constant value to be loaded
      Returns:
      branch target information
    • appendTargetCreateNew

      public BranchTarget appendTargetCreateNew(String name)
      Append instruction to create instance of class as a branch target.
      Parameters:
      name - fully qualified class name
      Returns:
      branch target information
    • internalAppendCreateNew

      protected InstructionHandle internalAppendCreateNew(String name)
      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

      public void initStackState(BranchWrapper branch)
      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

      public void initStackState(BranchWrapper branch, int pop)
      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 initialization
      pop - number of items to be removed from branch source stack state
    • initStackState

      protected void initStackState(String[] types)
      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

      public void targetNext(BranchWrapper branch)
      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 be null, in which case nothing is done)
    • targetNext

      public void targetNext(BranchWrapper[] branches)
      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 be null, 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

      public ClassItem getItem()
      Get the method item.
      Specified by:
      getItem in class BindingMethod
      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 the codeComplete(boolean) method is called.
      Specified by:
      hashCode in class BindingMethod
      Returns:
      hash code based on code sequence
    • addMethod

      public ClassItem addMethod()
      Add constructed method to class. Makes the method callable, generating the method information.
      Returns:
      added method information