Class 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 Detail

      • 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_generator

        protected org.apache.bcel.generic.MethodGen m_generator
        Generator for constructing method.
      • m_method

        protected org.apache.bcel.classfile.Method m_method
        Actual generated method information.
      • m_item

        protected ClassItem m_item
        Method class item information.
      • 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 Detail

      • MethodBuilder

        protected MethodBuilder​(String name,
                                org.apache.bcel.generic.Type ret,
                                org.apache.bcel.generic.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 Detail

      • getName

        public String getName()
        Get name of method being constructed.
        Specified by:
        getName in class BindingMethod
        Returns:
        name of method being constructed
      • 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 org.apache.bcel.classfile.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 org.apache.bcel.generic.InstructionHandle getFirstInstruction()
        Get first instruction in method.
        Returns:
        handle for first instruction in method
      • getLastInstruction

        protected org.apache.bcel.generic.InstructionHandle getLastInstruction()
        Get last instruction in method.
        Returns:
        handle for last instruction in method
      • setTarget

        protected final void setTarget​(org.apache.bcel.generic.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
      • 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
      • 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 org.apache.bcel.generic.LocalVariableGen createLocal​(String name,
                                                                       org.apache.bcel.generic.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
        Returns:
        Generated local variable
      • addLocal

        public int addLocal​(String name,
                            org.apache.bcel.generic.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​(org.apache.bcel.generic.Type type)
        Append typed return.
        Parameters:
        type - returned type (may be Type.VOID)
      • appendReturn

        public void appendReturn​(String type)
        Append typed return.
        Parameters:
        type - returned type (may be 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.
      • 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 org.apache.bcel.generic.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
        Returns:
        Instruction handle
      • 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
      • 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