Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

com.techtrader.modules.tools.bytecode
Class Code  view Code download Code.java

java.lang.Object
  extended bycom.techtrader.modules.tools.bytecode.BCEntity
      extended bycom.techtrader.modules.tools.bytecode.Attribute
          extended bycom.techtrader.modules.tools.bytecode.Code
All Implemented Interfaces:
Constants, com.techtrader.modules.tools.bytecode.visitor.VisitAcceptor

public class Code
extends Attribute
implements Constants

Representation of a code block of a class. The methods of this class mimic those of the same name in the java.util.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 opcode 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 instruction 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.


Field Summary
private  java.util.List _handlers
           
private  java.util.ListIterator _li
           
private  int _maxLocals
           
private  int _maxStack
           
private  java.util.List _opcodes
           
 
Fields inherited from class com.techtrader.modules.tools.bytecode.Attribute
_nameIndex, _owner
 
Fields inherited from class com.techtrader.modules.tools.bytecode.BCEntity
 
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(com.techtrader.modules.tools.bytecode.visitor.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, java.lang.Class catchType)
          Add an exception handler to this code block.
 ExceptionHandler addExceptionHandler(Instruction tryStart, Instruction tryEnd, Instruction handlerStart, java.lang.String catchType)
          Add an exception handler to this code block.
private  Instruction addInstruction(Instruction ins)
           
private  Instruction addInstruction(int opcode)
           
 void after(Instruction ins)
          Position the iterator just after the given instruction.
 void after(int index)
          Place the iterator after the given list index.
 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(java.lang.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(java.lang.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(java.lang.Class catchType)
          Get all exception handlers that catch the given exception type.
 ExceptionHandler[] getExceptionHandlers(java.lang.String catchType)
          Get all exception handlers that catch the given exception type.
 GetFieldInstruction getfield()
           
private  Instruction getInstruction(int opcode)
           
 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()