TechTrader Bytecode Toolkit

com.techtrader.modules.tools.bytecode
Class Code

java.lang.Object
  |
  +--com.techtrader.modules.tools.bytecode.BCEntity
        |
        +--com.techtrader.modules.tools.bytecode.Attribute
              |
              +--com.techtrader.modules.tools.bytecode.Code

public class Code
extends Attribute
implements Constants

Representation of a code block of a class; a Code instance can only be obtained from a BCMethod. The methods of this class mimic those of the same name in the ListIterator class. Note that the size and index information of the Code block will change as opcodes are added.

Code blocks are usually obtained from a BCMethod, but can also be constructed via the default Constructor. Blocks created this way can be used to provide template instructions to the various search/replace methods in this class.

The Code class contains methods named after each JVM instruction, each of which adds the matching instruction to the code block at the current iterator position. There are also many pseudo-instruction methods that do not have a corresponding JVM opcode, but are provided for convenience when the exact opcode is difficult to determine at compile time. Unlike the other opcode methods, these convenience methods have javadoc comments so that they are easy to pick out; they should be skimmed to get an idea of the functionality that each provides. Also note that many Instructions are able to 'morph' their opcode on the fly as the arguments to the instrcution change. Thus the developer can initially call, for example, the aload_1 opcode, but later change the type to load to 'int', and the opcode will automatically morph to iload_1.

Author:
Abe White

Fields inherited from class com.techtrader.modules.tools.bytecode.Attribute
_nameIndex, _owner
 
Fields inherited from interface com.techtrader.modules.tools.bytecode.Constants
AALOAD, AASTORE, ACCESS_ABSTRACT, ACCESS_FINAL, ACCESS_INTERFACE, ACCESS_NATIVE, ACCESS_PRIVATE, ACCESS_PROTECTED, ACCESS_PUBLIC, ACCESS_STATIC, ACCESS_STRICT, ACCESS_SUPER, ACCESS_SYNCHRONIZED, ACCESS_TRANSIENT, ACCESS_VOLATILE, ACONST_NULL, ALOAD, ALOAD_0, ALOAD_1, ALOAD_2, ALOAD_3, ANEWARRAY, ARETURN, ARRAY_BOOLEAN, ARRAY_BYTE, ARRAY_CHAR, ARRAY_DOUBLE, ARRAY_FLOAT, ARRAY_INT, ARRAY_LONG, ARRAY_SHORT, ARRAYLENGTH, ASTORE, ASTORE_0, ASTORE_1, ASTORE_2, ASTORE_3, ATHROW, ATTR_CODE, ATTR_CONST, ATTR_DEPRECATED, ATTR_EXCEPTIONS, ATTR_INNERCLASS, ATTR_LINENUMBERS, ATTR_LOCALS, ATTR_SOURCE, ATTR_SYNTHETIC, ATTR_UNKNOWN, BALOAD, BASTORE, BIPUSH, CALOAD, CASTORE, CHECKCAST, D2F, D2I, D2L, DADD, DALOAD, DASTORE, DCMPG, DCMPL, DCONST_0, DCONST_1, DDIV, DLOAD, DLOAD_0, DLOAD_1, DLOAD_2, DLOAD_3, DMUL, DNEG, DREM, DRETURN, DSTORE, DSTORE_0, DSTORE_1, DSTORE_2, DSTORE_3, DSUB, DUP, DUP_X1, DUP_X2, DUP2, DUP2_X1, DUP2_X2, F2D, F2I, F2L, FADD, FALOAD, FASTORE, FCMPG, FCMPL, FCONST_0, FCONST_1, FCONST_2, FDIV, FLOAD, FLOAD_0, FLOAD_1, FLOAD_2, FLOAD_3, FMUL, FNEG, FREM, FRETURN, FSTORE, FSTORE_0, FSTORE_1, FSTORE_2, FSTORE_3, FSUB, GETFIELD, GETSTATIC, GOTO, GOTO_W, I2B, I2C, I2D, I2F, I2L, I2S, IADD, IALOAD, IAND, IASTORE, ICONST_0, ICONST_1, ICONST_2, ICONST_3, ICONST_4, ICONST_5, ICONST_M1, IDIV, IF_ACMPEQ, IF_ACMPNE, IF_EQ, IF_GE, IF_GT, IF_ICMPEQ, IF_ICMPGE, IF_ICMPGT, IF_ICMPLE, IF_ICMPLT, IF_ICMPNE, IF_LE, IF_LT, IF_NE, IF_NONNULL, IF_NULL, IINC, ILOAD, ILOAD_0, ILOAD_1, ILOAD_2, ILOAD_3, IMUL, INEG, INSTANCEOF, INVOKEINTERFACE, INVOKESPECIAL, INVOKESTATIC, INVOKEVIRTUAL, IOR, IREM, IRETURN, ISHL, ISHR, ISTORE, ISTORE_0, ISTORE_1, ISTORE_2, ISTORE_3, ISUB, IUSHR, IXOR, JSR, JSR_W, L2D, L2F, L2I, LADD, LALOAD, LAND, LASTORE, LCMP, LCONST_0, LCONST_1, LDC, LDC_W, LDC2_W, LDIV, LLOAD, LLOAD_0, LLOAD_1, LLOAD_2, LLOAD_3, LMUL, LNEG, LOOKUPSWITCH, LOR, LREM, LRETURN, LSHL, LSHR, LSTORE, LSTORE_0, LSTORE_1, LSTORE_2, LSTORE_3, LSUB, LUSHR, LXOR, MATH_ADD, MATH_AND, MATH_DIV, MATH_MUL, MATH_NEG, MATH_OR, MATH_REM, MATH_SHL, MATH_SHR, MATH_SUB, MATH_USHR, MATH_XOR, MONITORENTER, MONITOREXIT, MULTIANEWARRAY, NEW, NEWARRAY, NOP, OPCODE_NAMES, POP, POP2, PUTFIELD, PUTSTATIC, RET, RETURN, SALOAD, SASTORE, SIPUSH, SWAP, TABLESWITCH, VALID_MAGIC, WIDE
 
Constructor Summary
  Code()
          The public constructor is for creating template code modules that can be used to produce Instructions to be used in matching for various search() and replace() methods.
protected Code(int nameIndex, BCEntity owner)
           
 
Method Summary
 ArrayLoadInstruction aaload()
           
 ArrayStoreInstruction aastore()
           
 void acceptVisit(BCVisitor visit)
          Accept a visit from a BCVisitor, calling the appropriate methods to notify the visitor that it has entered this entity, and to provide it with the proper callbacks for each sub-entity owned by this object.
 ConstantInstruction aconst_null()
           
 MathInstruction add()
          This is a convenience method to invoke the proper math instruction if the type is not known at compile time.
 ExceptionHandler addExceptionHandler()
          Add an exception handler to this code block.
 ExceptionHandler addExceptionHandler(Instruction tryStart, Instruction tryEnd, Instruction handlerStart, Class catchType)
          Add an exception handler to this code block.
 ExceptionHandler addExceptionHandler(Instruction tryStart, Instruction tryEnd, Instruction handlerStart, String catchType)
          Add an exception handler to this code block.
 void afterLast()
          Set the position of the instruction iterator to after the last opcode.
 LoadInstruction aload_0()
           
 LoadInstruction aload_1()
           
 LoadInstruction aload_2()
           
 LoadInstruction aload_3()
           
 LoadInstruction aload()
           
 MathInstruction and()
          This is a convenience method to invoke the proper math instruction if the type is not known at compile time.
 ClassInstruction anewarray()
           
 ReturnInstruction areturn()
           
 Instruction arraylength()
           
 ArrayLoadInstruction arrayload()
          This is a convenience method to invoke the proper array load instruction if the type is not known at compile time.
 ArrayStoreInstruction arraystore()
          This is a convenience method to invoke the proper array store instruction if the type is not known at compile time.
 StoreInstruction astore_0()
           
 StoreInstruction astore_1()
           
 StoreInstruction astore_2()
           
 StoreInstruction astore_3()
           
 StoreInstruction astore()
           
 Instruction athrow()
           
 ArrayLoadInstruction baload()
           
 ArrayStoreInstruction bastore()
           
 void before(Instruction ins)
          Position the iterator just before the given instruction.
 void before(int index)
          Place the iterator before the given list index.
 void beforeFirst()
          Reset the position of the instruction iterator to the first opcode.
 ConstantInstruction bipush()
           
 void calculateMaxLocals()
          Ask the code to figure out the number of locals it needs based on the instructions used and the parameters of the method this code block is a part of.
 void calculateMaxStack()
          Ask the code to figure out the maximum stack depth it needs the instructions used.
 ArrayLoadInstruction caload()
           
 ArrayStoreInstruction castore()
           
 ClassInstruction checkcast()
           
 ClassConstantInstruction classconstant()
          Loads a Class constant onto the stack.
 void clearExceptionHandlers()
          Clear all exception handlers.
 CmpInstruction cmp()
          Convenience method to use when the types being compared are not known at compile time.
 ConstantInstruction constant()
          Load some constant onto the stack.
 ConvertInstruction convert()
          This is a convenience method to invoke the proper conversion instruction if the types being converted are not known at compile time.
protected  void copy(Attribute attr)
          Copies the instructions of the given code block to this one; used to import methods from other classes or copy methods within a class.
 ConvertInstruction d2f()
           
 ConvertInstruction d2i()
           
 ConvertInstruction d2l()
           
 MathInstruction dadd()
           
 ArrayLoadInstruction daload()
           
 ArrayStoreInstruction dastore()
           
 CmpInstruction dcmpg()
           
 CmpInstruction dcmpl()
           
 ConstantInstruction dconst_0()
           
 ConstantInstruction dconst_1()
           
 MathInstruction ddiv()
           
 MathInstruction div()
          This is a convenience method to invoke the proper math instruction if the type is not known at compile time.
 LoadInstruction dload_0()
           
 LoadInstruction dload_1()
           
 LoadInstruction dload_2()
           
 LoadInstruction dload_3()
           
 LoadInstruction dload()
           
 MathInstruction dmul()
           
 MathInstruction dneg()
           
 MathInstruction drem()
           
 ReturnInstruction dreturn()
           
 StoreInstruction dstore_0()
           
 StoreInstruction dstore_1()
           
 StoreInstruction dstore_2()
           
 StoreInstruction dstore_3()
           
 StoreInstruction dstore()
           
 MathInstruction dsub()
           
 StackInstruction dup_x1()
           
 StackInstruction dup_x2()
           
 StackInstruction dup()
           
 StackInstruction dup2_x1()
           
 StackInstruction dup2_x2()
           
 StackInstruction dup2()
           
 ConvertInstruction f2d()
           
 ConvertInstruction f2i()
           
 ConvertInstruction f2l()
           
 MathInstruction fadd()
           
 ArrayLoadInstruction faload()
           
 ArrayStoreInstruction fastore()
           
 CmpInstruction fcmpg()
           
 CmpInstruction fcmpl()
           
 ConstantInstruction fconst_0()
           
 ConstantInstruction fconst_1()
           
 ConstantInstruction fconst_2()
           
 MathInstruction fdiv()
           
 LoadInstruction fload_0()
           
 LoadInstruction fload_1()
           
 LoadInstruction fload_2()
           
 LoadInstruction fload_3()
           
 LoadInstruction fload()
           
 MathInstruction fmul()
           
 MathInstruction fneg()
           
 MathInstruction frem()
           
 ReturnInstruction freturn()
           
 StoreInstruction fstore_0()
           
 StoreInstruction fstore_1()
           
 StoreInstruction fstore_2()
           
 StoreInstruction fstore_3()
           
 StoreInstruction fstore()
           
 MathInstruction fsub()
           
 byte[] getCode()
          Get the code for this method as a byte array.
 ExceptionHandler getExceptionHandler(Class catchType)
          Get the exception handler that catches the given exception type; if multiple handlers catch the given type, which is returned is undefined.
 ExceptionHandler getExceptionHandler(String catchType)
          Get the exception handler that catches the given exception type; if multiple handlers catch the given type, which is returned is undefined.
 ExceptionHandler[] getExceptionHandlers()
          Get the exception handlers active in this code block, or an empty array if none.
 ExceptionHandler[] getExceptionHandlers(Class catchType)
          Get all exception handlers that catch the given exception type.
 ExceptionHandler[] getExceptionHandlers(String catchType)
          Get all exception handlers that catch the given exception type.
 GetFieldInstruction getfield()
           
 Instruction[] getInstructions()
          Get all the Instructions of this method.
 int getLength()
          Return the length of the .class representation of this attribute, in bytes.
 int getLocalsIndex(int paramIndex)
          Get the local variable index for the paramIndex'th parameter to the method.
 int getMaxLocals()
          Get the maximum number of local variables (including params) in this method.
 int getMaxStack()
          Get the maximum stack depth for this code block.
 int getNextLocalsIndex()
          Get the next next available local variable index.
 GetFieldInstruction getstatic()
           
 JumpInstruction go2_w()
           
 JumpInstruction go2()
           
 boolean hasNext()
          Return true if a subsequent call to next() will return an instruction.
 boolean hasPrevious()
          Return true if a subsequent call to previous() will return an instruction.
 ConvertInstruction i2b()
           
 ConvertInstruction i2c()
           
 ConvertInstruction i2d()
           
 ConvertInstruction i2f()
           
 ConvertInstruction i2l()
           
 ConvertInstruction i2s()
           
 MathInstruction iadd()
           
 ArrayLoadInstruction iaload()
           
 MathInstruction iand()
           
 ArrayStoreInstruction iastore()
           
 ConstantInstruction iconst_0()
           
 ConstantInstruction iconst_1()
           
 ConstantInstruction iconst_2()
           
 ConstantInstruction iconst_3()
           
 ConstantInstruction iconst_4()
           
 ConstantInstruction iconst_5()
           
 ConstantInstruction iconst_m1()
           
 MathInstruction idiv()
           
 JumpInstruction if_acmpeq()
           
 JumpInstruction if_acmpne()
           
 JumpInstruction if_eq()
           
 JumpInstruction if_ge()
           
 JumpInstruction if_gt()
           
 JumpInstruction if_icmpeq()
           
 JumpInstruction if_icmpge()
           
 JumpInstruction if_icmpgt()
           
 JumpInstruction if_icmple()
           
 JumpInstruction if_icmplt()
           
 JumpInstruction if_icmpne()
           
 JumpInstruction if_le()
           
 JumpInstruction if_lt()
           
 JumpInstruction if_ne()
           
 JumpInstruction if_nonnull()
           
 JumpInstruction if_null()
           
 IIncInstruction iinc()
           
 LoadInstruction iload_0()
           
 LoadInstruction iload_1()
           
 LoadInstruction iload_2()
           
 LoadInstruction iload_3()
           
 LoadInstruction iload()
           
 MathInstruction imul()
           
 MathInstruction ineg()
           
 ClassInstruction instanceofins()
           
 MethodInstruction invokeinterface()
           
 MethodInstruction invokespecial()
           
 MethodInstruction invokestatic()
           
 MethodInstruction invokevirtual()
           
 MathInstruction ior()
           
 MathInstruction irem()
           
 ReturnInstruction ireturn()
           
 MathInstruction ishl()
           
 MathInstruction ishr()
           
 StoreInstruction istore_0()
           
 StoreInstruction istore_1()
           
 StoreInstruction istore_2()
           
 StoreInstruction istore_3()
           
 StoreInstruction istore()
           
 MathInstruction isub()
           
 MathInstruction iushr()
           
 MathInstruction ixor()
           
 JumpInstruction jsr_w()
           
 JumpInstruction jsr()
           
 ConvertInstruction l2d()
           
 ConvertInstruction l2f()
           
 ConvertInstruction l2i()
           
 MathInstruction ladd()
           
 ArrayLoadInstruction laload()
           
 MathInstruction land()
           
 ArrayStoreInstruction lastore()
           
 CmpInstruction lcmp()
           
 ConstantInstruction lconst_0()
           
 ConstantInstruction lconst_1()
           
 ConstantInstruction ldc_w()
           
 ConstantInstruction ldc()
           
 ConstantInstruction ldc2_w()
           
 MathInstruction ldiv()
           
 LoadInstruction lload_0()
           
 LoadInstruction lload_1()
           
 LoadInstruction lload_2()
           
 LoadInstruction lload_3()
           
 LoadInstruction lload()
           
 MathInstruction lmul()
           
 MathInstruction lneg()
           
 LoadInstruction load()
          This is a convenience method to load a local variable onto the stack, if the type and index to load is not known at compile time.
 LookupSwitchInstruction lookupswitch()
           
 MathInstruction lor()
           
 MathInstruction lrem()
           
 ReturnInstruction lreturn()
           
 MathInstruction lshl()
           
 MathInstruction lshr()
           
 StoreInstruction lstore_0()
           
 StoreInstruction lstore_1()
           
 StoreInstruction lstore_2()
           
 StoreInstruction lstore_3()
           
 StoreInstruction lstore()
           
 MathInstruction lsub()
           
 MathInstruction lushr()
           
 MathInstruction lxor()
           
 MathInstruction math()
          This is a convenience method to invoke the proper math instruction if neither the type nor operation are known at compile time.
 MonitorEnterInstruction monitorenter()
           
 MonitorExitInstruction monitorexit()
           
 MathInstruction mul()
          This is a convenience method to invoke the proper math instruction if the type is not known at compile time.
 MultiANewArrayInstruction multianewarray()
           
 MathInstruction neg()
          This is a convenience method to invoke the proper math instruction if the type is not known at compile time.
 NewArrayInstruction newarray()
           
 ClassInstruction newins()
           
 Instruction next()
          Return the next instruction.
 int nextIndex()
          Return the index of the next instruction, or size() if at end.
 Instruction nop()
           
 MathInstruction or()
          This is a convenience method to invoke the proper math instruction if the type is not known at compile time.
 StackInstruction pop()
           
 StackInstruction pop2()
           
 Instruction previous()
          Return the previous instruction.
 int previousIndex()
          Return the index of the previous instruction, or -1 if at beginning.
 PutFieldInstruction putfield()
           
 PutFieldInstruction putstatic()
           
protected  void readData(DataInput in, int length)
          Should be overridden by subclasses to read their internal data from the given stream, up to length bytes, excluding the name index.
 MathInstruction rem()
          This is a convenience method to invoke the proper math instruction if the type is not known at compile time.
 void remove()
          Remove the current instruction.
 boolean removeExceptionHandler(Class catchType)
          Remove all exception handlers that catch the given type.
 boolean removeExceptionHandler(ExceptionHandler handler)
          Remove an exception handler from this code block.
 boolean removeExceptionHandler(String catchType)
          Remove all exception handlers that catch the given type.
 int replaceAll(Instruction[] templates, Instruction[] with)
          Equivalent to looping over each given template/replacement pair and calling replaceAll(Instruction, Instruction) for each.
 int replaceAll(Instruction template, Instruction with)
          Replaces all the Instructions in this code block that match the given template with the given Instruction.
 Instruction replaceNext(Instruction with)
          Replaces the next Instruction with the given one.
 Instruction replacePrevious(Instruction with)
          Replaces the previous Instruction with the given one.
 RetInstruction ret()
           
 ReturnInstruction returnins()
          This is a convenience method to invoke the proper return instruction if the type is not known at compile time.
 ArrayLoadInstruction saload()
           
 ArrayStoreInstruction sastore()
           
 boolean searchBackward(Instruction template)
          Find the closest previous Instruction from the current iterator position that matches the given one, according to the equals() methods of the Instruction types.
 boolean searchForward(Instruction template)
          Find the next Instruction from the current iterator position that matches the given one, according to the equals() methods of the Instruction types.
 void setCode(byte[] code)
          Set the code for this method as a byte array.
 void setMaxLocals(int max)
          Set the maximum number of local variables (including params) in this method.
 void setMaxStack(int max)
          Set the maximum stack depth for this code block.
 MathInstruction shl()
          This is a convenience method to invoke the proper math instruction if the type is not known at compile time.
 MathInstruction shr()
          This is a convenience method to invoke the proper math instruction if the type is not known at compile time.
 ConstantInstruction sipush()
           
 int size()
          Return the number of instructions in the method.
 StoreInstruction store()
          This is a convenience method to store a stack value into a local variable if the type and index to store is not known at compile time.
 MathInstruction sub()
          This is a convenience method to invoke the proper math instruction if the type is not known at compile time.
 StackInstruction swap()
           
 TableSwitchInstruction tableswitch()
           
 MathInstruction ushr()
          This is a convenience method to invoke the proper math instruction if the type is not known at compile time.
 ReturnInstruction vreturn()
           
 WideInstruction wide()
           
protected  void writeData(DataOutput out, int length)
          Should be overridden by subclasses to write their internal data to the given stream, up to length bytes, excluding the name index.
 MathInstruction xor()
          This is a convenience method to invoke the proper math instruction if the type is not known at compile time.
 
Methods inherited from class com.techtrader.modules.tools.bytecode.Attribute
createAttribute, getName, getNameIndex, getOwner, getPool, invalidate
 
Methods inherited from class com.techtrader.modules.tools.bytecode.BCEntity
addAttribute, clearAttributes, getAttribute, getAttributes, getAttributes, importAttribute, importAttributes, readAttributes, removeAttribute, removeAttribute, visitAttributes, writeAttributes
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Code

protected Code(int nameIndex,
               BCEntity owner)

Code

public Code()
The public constructor is for creating template code modules that can be used to produce Instructions to be used in matching for various search() and replace() methods.
Method Detail

getMaxStack

public int getMaxStack()
Get the maximum stack depth for this code block.

setMaxStack

public void setMaxStack(int max)
Set the maximum stack depth for this code block.

getMaxLocals

public int getMaxLocals()
Get the maximum number of local variables (including params) in this method.

setMaxLocals

public void setMaxLocals(int max)
Set the maximum number of local variables (including params) in this method.

getLocalsIndex

public int getLocalsIndex(int paramIndex)
Get the local variable index for the paramIndex'th parameter to the method. These numbers are different because a) non-static methods use the 0th local variable for the 'this' ptr, and b) double and long values occupy two spots in the local variable array.

getNextLocalsIndex

public int getNextLocalsIndex()
Get the next next available local variable index.

calculateMaxLocals

public void calculateMaxLocals()
Ask the code to figure out the number of locals it needs based on the instructions used and the parameters of the method this code block is a part of.

calculateMaxStack

public void calculateMaxStack()
Ask the code to figure out the maximum stack depth it needs the instructions used.

getExceptionHandlers

public ExceptionHandler[] getExceptionHandlers()
Get the exception handlers active in this code block, or an empty array if none.

getExceptionHandler

public ExceptionHandler getExceptionHandler(Class catchType)
Get the exception handler that catches the given exception type; if multiple handlers catch the given type, which is returned is undefined.

getExceptionHandler

public ExceptionHandler getExceptionHandler(String catchType)
Get the exception handler that catches the given exception type; if multiple handlers catch the given type, which is returned is undefined.

getExceptionHandlers

public ExceptionHandler[] getExceptionHandlers(Class catchType)
Get all exception handlers that catch the given exception type.

getExceptionHandlers

public ExceptionHandler[] getExceptionHandlers(String catchType)
Get all exception handlers that catch the given exception type.

addExceptionHandler

public ExceptionHandler addExceptionHandler()
Add an exception handler to this code block.

addExceptionHandler

public ExceptionHandler addExceptionHandler(Instruction tryStart,
                                            Instruction tryEnd,
                                            Instruction handlerStart,
                                            Class catchType)
Add an exception handler to this code block.
Parameters:
tryStart - the first instruction of the try {} block
tryEnd - the last instruction of the try {} block
handlerStart - the first instruction of the catch {} block
catchType - the type of Exception being caught

addExceptionHandler

public ExceptionHandler addExceptionHandler(Instruction tryStart,
                                            Instruction tryEnd,
                                            Instruction handlerStart,
                                            String catchType)
Add an exception handler to this code block.
Parameters:
tryStart - the first instruction of the try {} block
tryEnd - the last instruction of the try {} block
handlerStart - the first instruction of the catch {} block
catchType - the type of Exception being caught

clearExceptionHandlers

public void clearExceptionHandlers()
Clear all exception handlers.

removeExceptionHandler

public boolean removeExceptionHandler(Class catchType)
Remove all exception handlers that catch the given type.

removeExceptionHandler

public boolean removeExceptionHandler(String catchType)
Remove all exception handlers that catch the given type.

removeExceptionHandler

public boolean removeExceptionHandler(ExceptionHandler handler)
Remove an exception handler from this code block.

size

public int size()
Return the number of instructions in the method.

beforeFirst

public void beforeFirst()
Reset the position of the instruction iterator to the first opcode.

afterLast

public void afterLast()
Set the position of the instruction iterator to after the last opcode.

before

public void before(Instruction ins)
Position the iterator just before the given instruction. The instruction must belong to this method.

hasNext

public boolean hasNext()
Return true if a subsequent call to next() will return an instruction.

hasPrevious

public boolean hasPrevious()
Return true if a subsequent call to previous() will return an instruction.

next

public Instruction next()
Return the next instruction.

nextIndex

public int nextIndex()
Return the index of the next instruction, or size() if at end.

previous

public Instruction previous()
Return the previous instruction.

previousIndex

public int previousIndex()
Return the index of the previous instruction, or -1 if at beginning.

before

public void before(int index)
Place the iterator before the given list index.

searchForward

public boolean searchForward(Instruction template)
Find the next Instruction from the current iterator position that matches the given one, according to the equals() methods of the Instruction types. This allows for matching based on template instructions, as the equals() methods of most Instructions return true if the information for the given Instruction has not been filled in. If a match is found, the iterator is placed after the matching Instruction. If no match is found, moves the iterator to afterLast().
Returns:
true if match found

searchBackward

public boolean searchBackward(Instruction template)
Find the closest previous Instruction from the current iterator position that matches the given one, according to the equals() methods of the Instruction types. This allows for matching based on template instructions, as the equals() methods of most Instructions returns true if the information for the given Instruction has not been filled in. If a match is found, the iterator is placed before the matching Instruction. If no match is found, moves the iterator to beforeFirst().
Returns:
true if match found

replaceNext

public Instruction replaceNext(Instruction with)
Replaces the next Instruction with the given one. It is an error to call this method if the iterator is afterLast(). After this method, the iterator will be after the newly added Instruction. This method will also make sure that all jump points that referenced the old opcode are updated correctly.
Returns:
the newly added Instruction

replacePrevious

public Instruction replacePrevious(Instruction with)
Replaces the previous Instruction with the given one. It is an error to call this method if the iterator is beforeFirst(). After this method, the iterator will be before the newly added Instruction. This method will also make sure that all jump points that referenced the old opcode are updated correctly.
Returns:
the newly added Instruction

replaceAll

public int replaceAll(Instruction template,
                      Instruction with)
Replaces all the Instructions in this code block that match the given template with the given Instruction. After this method, the iterator will be in its original position.
Returns:
the number of substitutions made

replaceAll

public int replaceAll(Instruction[] templates,
                      Instruction[] with)
Equivalent to looping over each given template/replacement pair and calling replaceAll(Instruction, Instruction) for each.

remove

public void remove()
Remove the current instruction.

nop

public Instruction nop()

constant

public ConstantInstruction constant()
Load some constant onto the stack. The ConstantInstruction type takes any constant and correctly translates it into the proper opcode, depending on the constant type and value. For example, if the constant value is set to 0L, the opcode will be set to lconst_0.

classconstant

public ClassConstantInstruction classconstant()
Loads a Class constant onto the stack. For primitive types, this translates into a getstatic() for the TYPE field of the primitive's wrapper type. For non-primitives, things get much more complex. Suffice it to say that the operation involves adding synthetic static fields and even methods to the class. Note that this instruction requires up to 3 stack positions to execute.

aconst_null

public ConstantInstruction aconst_null()

iconst_m1

public ConstantInstruction iconst_m1()

iconst_0

public ConstantInstruction iconst_0()

iconst_1

public ConstantInstruction iconst_1()

iconst_2

public ConstantInstruction iconst_2()

iconst_3

public ConstantInstruction iconst_3()

iconst_4

public ConstantInstruction iconst_4()

iconst_5

public ConstantInstruction iconst_5()

lconst_0

public ConstantInstruction lconst_0()

lconst_1

public ConstantInstruction lconst_1()

fconst_0

public ConstantInstruction fconst_0()

fconst_1

public ConstantInstruction fconst_1()

fconst_2

public ConstantInstruction fconst_2()

dconst_0

public ConstantInstruction dconst_0()

dconst_1

public ConstantInstruction dconst_1()

bipush

public ConstantInstruction bipush()

sipush

public ConstantInstruction sipush()

ldc

public ConstantInstruction ldc()

ldc_w

public ConstantInstruction ldc_w()

ldc2_w

public ConstantInstruction ldc2_w()

load

public LoadInstruction load()
This is a convenience method to load a local variable onto the stack, if the type and index to load is not known at compile time.

iload

public LoadInstruction iload()

iload_0

public LoadInstruction iload_0()

iload_1

public LoadInstruction iload_1()

iload_2

public LoadInstruction iload_2()

iload_3

public LoadInstruction iload_3()

lload

public LoadInstruction lload()

lload_0

public LoadInstruction lload_0()

lload_1

public LoadInstruction lload_1()

lload_2

public LoadInstruction lload_2()

lload_3

public LoadInstruction lload_3()

fload

public LoadInstruction fload()

fload_0

public LoadInstruction fload_0()

fload_1

public LoadInstruction fload_1()

fload_2

public LoadInstruction fload_2()

fload_3

public LoadInstruction fload_3()

dload

public LoadInstruction dload()

dload_0

public LoadInstruction dload_0()

dload_1

public LoadInstruction dload_1()

dload_2

public LoadInstruction dload_2()

dload_3

public LoadInstruction dload_3()

aload

public LoadInstruction aload()

aload_0

public LoadInstruction aload_0()

aload_1

public LoadInstruction aload_1()

aload_2

public LoadInstruction aload_2()

aload_3

public LoadInstruction aload_3()

store

public StoreInstruction store()
This is a convenience method to store a stack value into a local variable if the type and index to store is not known at compile time.

istore

public StoreInstruction istore()

istore_0

public StoreInstruction istore_0()

istore_1

public StoreInstruction istore_1()

istore_2

public StoreInstruction istore_2()

istore_3

public StoreInstruction istore_3()

lstore

public StoreInstruction lstore()

lstore_0

public StoreInstruction lstore_0()

lstore_1

public StoreInstruction lstore_1()

lstore_2

public StoreInstruction lstore_2()

lstore_3

public StoreInstruction lstore_3()

fstore

public StoreInstruction fstore()

fstore_0

public StoreInstruction fstore_0()

fstore_1

public StoreInstruction fstore_1()

fstore_2

public StoreInstruction fstore_2()

fstore_3

public StoreInstruction fstore_3()

dstore

public StoreInstruction dstore()

dstore_0

public StoreInstruction dstore_0()

dstore_1

public StoreInstruction dstore_1()

dstore_2

public StoreInstruction dstore_2()

dstore_3

public StoreInstruction dstore_3()

astore

public StoreInstruction astore()

astore_0

public StoreInstruction astore_0()

astore_1

public StoreInstruction astore_1()

astore_2

public StoreInstruction astore_2()

astore_3

public StoreInstruction astore_3()

ret

public RetInstruction ret()

iinc

public IIncInstruction iinc()

wide

public WideInstruction wide()

arrayload

public ArrayLoadInstruction arrayload()
This is a convenience method to invoke the proper array load instruction if the type is not known at compile time.

iaload

public ArrayLoadInstruction iaload()

laload

public ArrayLoadInstruction laload()

faload

public ArrayLoadInstruction faload()

daload

public ArrayLoadInstruction daload()

aaload

public ArrayLoadInstruction aaload()

baload

public ArrayLoadInstruction baload()

caload

public ArrayLoadInstruction caload()

saload

public ArrayLoadInstruction saload()

arraystore

public ArrayStoreInstruction arraystore()
This is a convenience method to invoke the proper array store instruction if the type is not known at compile time.

iastore

public ArrayStoreInstruction iastore()

lastore

public ArrayStoreInstruction lastore()

fastore

public ArrayStoreInstruction fastore()

dastore

public ArrayStoreInstruction dastore()

aastore

public ArrayStoreInstruction aastore()

bastore

public ArrayStoreInstruction bastore()

castore

public ArrayStoreInstruction castore()

sastore

public ArrayStoreInstruction sastore()

pop

public StackInstruction pop()

pop2

public StackInstruction pop2()

dup

public StackInstruction dup()

dup_x1

public StackInstruction dup_x1()

dup_x2

public StackInstruction dup_x2()

dup2

public StackInstruction dup2()

dup2_x1

public StackInstruction dup2_x1()

dup2_x2

public StackInstruction dup2_x2()

swap

public StackInstruction swap()

math

public MathInstruction math()
This is a convenience method to invoke the proper math instruction if neither the type nor operation are known at compile time.

add

public MathInstruction add()
This is a convenience method to invoke the proper math instruction if the type is not known at compile time.

iadd

public MathInstruction iadd()

ladd

public MathInstruction ladd()

fadd

public MathInstruction fadd()

dadd

public MathInstruction dadd()

sub

public MathInstruction sub()
This is a convenience method to invoke the proper math instruction if the type is not known at compile time.

isub

public MathInstruction isub()

lsub

public MathInstruction lsub()

fsub

public MathInstruction fsub()

dsub

public MathInstruction dsub()

mul

public MathInstruction mul()
This is a convenience method to invoke the proper math instruction if the type is not known at compile time.

imul

public MathInstruction imul()

lmul

public MathInstruction lmul()

fmul

public MathInstruction fmul()

dmul

public MathInstruction dmul()

div

public MathInstruction div()
This is a convenience method to invoke the proper math instruction if the type is not known at compile time.

idiv

public MathInstruction idiv()

ldiv

public MathInstruction ldiv()

fdiv

public MathInstruction fdiv()

ddiv

public MathInstruction ddiv()

rem

public MathInstruction rem()
This is a convenience method to invoke the proper math instruction if the type is not known at compile time.

irem

public MathInstruction irem()

lrem

public MathInstruction lrem()

frem

public MathInstruction frem()

drem

public MathInstruction drem()

neg

public MathInstruction neg()
This is a convenience method to invoke the proper math instruction if the type is not known at compile time.

ineg

public MathInstruction ineg()

lneg

public MathInstruction lneg()

fneg

public MathInstruction fneg()

dneg

public MathInstruction dneg()

shl

public MathInstruction shl()
This is a convenience method to invoke the proper math instruction if the type is not known at compile time.

ishl

public MathInstruction ishl()

lshl

public MathInstruction lshl()

shr

public MathInstruction shr()
This is a convenience method to invoke the proper math instruction if the type is not known at compile time.

ishr

public MathInstruction ishr()

lshr

public MathInstruction lshr()

ushr

public MathInstruction ushr()
This is a convenience method to invoke the proper math instruction if the type is not known at compile time.

iushr

public MathInstruction iushr()

lushr

public MathInstruction lushr()

and

public MathInstruction and()
This is a convenience method to invoke the proper math instruction if the type is not known at compile time.

iand

public MathInstruction iand()

land

public MathInstruction land()

or

public MathInstruction or()
This is a convenience method to invoke the proper math instruction if the type is not known at compile time.

ior

public MathInstruction ior()

lor

public MathInstruction lor()

xor

public MathInstruction xor()
This is a convenience method to invoke the proper math instruction if the type is not known at compile time.

ixor

public MathInstruction ixor()

lxor

public MathInstruction lxor()

convert

public ConvertInstruction convert()
This is a convenience method to invoke the proper conversion instruction if the types being converted are not known at compile time.

i2l

public ConvertInstruction i2l()

i2f

public ConvertInstruction i2f()

i2d

public ConvertInstruction i2d()

i2b

public ConvertInstruction i2b()

i2c

public ConvertInstruction i2c()

i2s

public ConvertInstruction i2s()

l2i

public ConvertInstruction l2i()

l2f

public ConvertInstruction l2f()

l2d

public ConvertInstruction l2d()

f2i

public ConvertInstruction f2i()

f2l

public ConvertInstruction f2l()

f2d

public ConvertInstruction f2d()

d2i

public ConvertInstruction d2i()

d2l

public ConvertInstruction d2l()

d2f

public ConvertInstruction d2f()

cmp

public CmpInstruction cmp()
Convenience method to use when the types being compared are not known at compile time.

lcmp

public CmpInstruction lcmp()

fcmpl

public CmpInstruction fcmpl()

fcmpg

public CmpInstruction fcmpg()

dcmpl

public CmpInstruction dcmpl()

dcmpg

public CmpInstruction dcmpg()

if_eq

public JumpInstruction if_eq()

if_ne

public JumpInstruction if_ne()

if_lt

public JumpInstruction if_lt()

if_ge

public JumpInstruction if_ge()

if_gt

public JumpInstruction if_gt()

if_le

public JumpInstruction if_le()

if_icmpeq

public JumpInstruction if_icmpeq()

if_icmpne

public JumpInstruction if_icmpne()

if_icmplt

public JumpInstruction if_icmplt()

if_icmpge

public JumpInstruction if_icmpge()

if_icmpgt

public JumpInstruction if_icmpgt()

if_icmple

public JumpInstruction if_icmple()

if_acmpeq

public JumpInstruction if_acmpeq()

if_acmpne

public JumpInstruction if_acmpne()

if_null

public JumpInstruction if_null()

if_nonnull

public JumpInstruction if_nonnull()

go2

public JumpInstruction go2()

jsr

public JumpInstruction jsr()

go2_w

public JumpInstruction go2_w()

jsr_w

public JumpInstruction jsr_w()

tableswitch

public TableSwitchInstruction tableswitch()

lookupswitch

public LookupSwitchInstruction lookupswitch()

returnins

public ReturnInstruction returnins()
This is a convenience method to invoke the proper return instruction if the type is not known at compile time.

vreturn

public ReturnInstruction vreturn()

ireturn

public ReturnInstruction ireturn()

lreturn

public ReturnInstruction lreturn()

freturn

public ReturnInstruction freturn()

dreturn

public ReturnInstruction dreturn()

areturn

public ReturnInstruction areturn()

getfield

public GetFieldInstruction getfield()

getstatic

public GetFieldInstruction getstatic()

putfield

public PutFieldInstruction putfield()

putstatic

public PutFieldInstruction putstatic()

invokevirtual

public MethodInstruction invokevirtual()

invokespecial

public MethodInstruction invokespecial()

invokeinterface

public MethodInstruction invokeinterface()

invokestatic

public MethodInstruction invokestatic()

newins

public ClassInstruction newins()

anewarray

public ClassInstruction anewarray()

checkcast

public ClassInstruction checkcast()

instanceofins

public ClassInstruction instanceofins()

multianewarray

public MultiANewArrayInstruction multianewarray()

newarray

public NewArrayInstruction newarray()

arraylength

public Instruction arraylength()

athrow

public Instruction athrow()

monitorenter

public MonitorEnterInstruction monitorenter()

monitorexit

public MonitorExitInstruction monitorexit()

getInstructions

public Instruction[] getInstructions()
Get all the Instructions of this method.

getCode

public byte[] getCode()
               throws IOException
Get the code for this method as a byte array.

setCode

public void setCode(byte[] code)
             throws IOException
Set the code for this method as a byte array.

getLength

public int getLength()
Description copied from class: Attribute
Return the length of the .class representation of this attribute, in bytes.
Overrides:
getLength in class Attribute

copy

protected void copy(Attribute attr)
Copies the instructions of the given code block to this one; used to import methods from other classes or copy methods within a class.
Overrides:
copy in class Attribute

readData

protected void readData(DataInput in,
                        int length)
                 throws IOException
Description copied from class: Attribute
Should be overridden by subclasses to read their internal data from the given stream, up to length bytes, excluding the name index.
Overrides:
readData in class Attribute

writeData

protected void writeData(DataOutput out,
                         int length)
                  throws IOException
Description copied from class: Attribute
Should be overridden by subclasses to write their internal data to the given stream, up to length bytes, excluding the name index.
Overrides:
writeData in class Attribute

acceptVisit

public void acceptVisit(BCVisitor visit)
Description copied from interface: VisitAcceptor
Accept a visit from a BCVisitor, calling the appropriate methods to notify the visitor that it has entered this entity, and to provide it with the proper callbacks for each sub-entity owned by this object.

TechTrader Bytecode Toolkit