Class BytecodeStream

  • All Implemented Interfaces:
    BytecodeConstants

    public class BytecodeStream
    extends Object
    implements BytecodeConstants
    Provides minimal abstraction layer to a stream of bytecodes from the code attribute of a method.
    • Method Detail

      • method

        public final IMethod method()
        Returns the method that this bytecode stream is from
      • declaringClass

        public final IClass declaringClass()
        Returns the declaring class that this bytecode stream is from
      • length

        public final int length()
        Returns the length of the bytecode stream Returns 0 if the method doesn't have any bytecodes (i.e. is abstract or native)
      • index

        public final int index()
        Returns the current bytecode index
      • reset

        public final void reset()
        Resets the stream to the beginning
      • reset

        public final void reset​(int index)
        Resets the stream to a given position Use with caution
        Parameters:
        index - the position to reset the stream to
      • hasMoreBytecodes

        public final boolean hasMoreBytecodes()
        Does the stream have more bytecodes in it?
      • peekNextOpcode

        public final int peekNextOpcode()
        Returns the opcode of the next instruction in the sequence without advancing to it
        Returns:
        the opcode of the next instruction
        See Also:
        nextInstruction()
      • nextInstruction

        public final int nextInstruction()
        Sets up the next instruction in the sequence
        Returns:
        the opcode of the next instruction
        See Also:
        peekNextOpcode()
      • getOpcode

        public final int getOpcode()
        Returns the opcode of the current instruction in the sequence Note: if skipInstruction has been called, but nextInstruction has not, this method will return the opcode of the skipped instruction!
        Returns:
        the opcode of the current instruction
        See Also:
        nextInstruction(), isWide()
      • isWide

        public final boolean isWide()
        Are we currently processing a wide instruction?
        Returns:
        true if current instruction is wide
        See Also:
        nextInstruction(), getOpcode()
      • skipInstruction

        public final void skipInstruction​(int opc,
                                          boolean w)
        Skips the current instruction (without using the opcode field) A slightly optimized version of skipInstruction()
        Parameters:
        opc - current opcode
        w - whether current instruction follows wide
        See Also:
        skipInstruction()
      • getByteValue

        public final int getByteValue()
        Returns a signed byte value Used for bipush
        Returns:
        signed byte value
      • getShortValue

        public final int getShortValue()
        Returns a signed short value Used for sipush
        Returns:
        signed short value
      • getLocalNumber

        public final int getLocalNumber()
        Returns the number of the local (as an unsigned byte) Used for iload, lload, fload, dload, aload, istore, lstore, fstore, dstore, astore, iinc, ret
        Returns:
        local number
        See Also:
        getWideLocalNumber()
      • getWideLocalNumber

        public final int getWideLocalNumber()
        Returns the wide number of the local (as an unsigned short) Used for iload, lload, fload, dload, aload, istore, lstore, fstore, dstore, astore, iinc prefixed by wide
        Returns:
        wide local number
        See Also:
        getLocalNumber()
      • getIncrement

        public final int getIncrement()
        Returns an increment value (as a signed byte) Used for iinc
        Returns:
        increment
        See Also:
        getWideIncrement()
      • getWideIncrement

        public final int getWideIncrement()
        Returns an increment value (as a signed short) Used for iinc prefixed by wide
        Returns:
        wide increment
        See Also:
        getIncrement()
      • getBranchOffset

        public final int getBranchOffset()
        Returns the offset of the branch (as a signed short) Used for if, ificmp, ifacmp, goto, jsr
        Returns:
        branch offset
        See Also:
        getWideBranchOffset()
      • getWideBranchOffset

        public final int getWideBranchOffset()
        Returns the wide offset of the branch (as a signed int) Used for goto_w, jsr_w
        Returns:
        wide branch offset
        See Also:
        getBranchOffset()
      • alignSwitch

        public final void alignSwitch()
        Skips the padding of a switch instruction Used for tableswitch, lookupswitch
      • getDefaultSwitchOffset

        public final int getDefaultSwitchOffset()
        Returns the default offset of the switch (as a signed int) Used for tableswitch, lookupswitch
        Returns:
        default switch offset
      • getLowSwitchValue

        public final int getLowSwitchValue()
        Returns the lowest value of the tableswitch (as a signed int) Used for tableswitch
        Returns:
        lowest switch value
        See Also:
        getHighSwitchValue()
      • getHighSwitchValue

        public final int getHighSwitchValue()
        Returns the highest value of the tableswitch (as a signed int) Used for tableswitch
        Returns:
        highest switch value
        See Also:
        getLowSwitchValue()
      • skipTableSwitchOffsets

        public final void skipTableSwitchOffsets​(int num)
        Skips the offsets of a tableswitch instruction Used for tableswitch
        Parameters:
        num - the number of offsets to skip
        See Also:
        getTableSwitchOffset(int)
      • getTableSwitchOffset

        public final int getTableSwitchOffset​(int num)
        Returns the numbered offset of the tableswitch (as a signed int) Used for tableswitch The "cursor" has to be positioned at the start of the offset table NOTE: Will NOT advance cursor
        Parameters:
        num - the number of the offset to retrieve
        Returns:
        switch offset
      • computeTableSwitchOffset

        public final int computeTableSwitchOffset​(int value,
                                                  int low,
                                                  int high)
        Returns the offset for a given value of the tableswitch (as a signed int) or 0 if the value is out of range. Used for tableswitch The "cursor" has to be positioned at the start of the offset table NOTE: Will NOT advance cursor
        Parameters:
        value - the value to retrieve offset for
        low - the lowest value of the tableswitch
        high - the highest value of the tableswitch
        Returns:
        switch offset
      • getSwitchLength

        public final int getSwitchLength()
        Returns the number of match-offset pairs in the lookupswitch (as a signed int) Used for lookupswitch
        Returns:
        number of switch pairs
      • skipLookupSwitchPairs

        public final void skipLookupSwitchPairs​(int num)
        Skips the match-offset pairs of a lookupswitch instruction Used for lookupswitch
        Parameters:
        num - the number of match-offset pairs to skip
        See Also:
        getLookupSwitchValue(int), getLookupSwitchOffset(int)
      • getLookupSwitchOffset

        public final int getLookupSwitchOffset​(int num)
        Returns the numbered offset of the lookupswitch (as a signed int) Used for lookupswitch The "cursor" has to be positioned at the start of the pair table NOTE: Will NOT advance cursor
        Parameters:
        num - the number of the offset to retrieve
        Returns:
        switch offset
        See Also:
        getLookupSwitchValue(int)
      • getLookupSwitchValue

        public final int getLookupSwitchValue​(int num)
        Returns the numbered value of the lookupswitch (as a signed int) Used for lookupswitch The "cursor" has to be positioned at the start of the pair table NOTE: Will NOT advance cursor
        Parameters:
        num - the number of the value to retrieve
        Returns:
        switch value
        See Also:
        getLookupSwitchOffset(int)
      • computeLookupSwitchOffset

        public final int computeLookupSwitchOffset​(int value,
                                                   int num)
        Returns the offset for a given value of the lookupswitch (as a signed int) or 0 if the value is not in the table. Used for lookupswitch The "cursor" has to be positioned at the start of the offset table NOTE: Will NOT advance cursor WARNING: Uses LINEAR search. Whoever has time on their hands can re-implement this as a binary search.
        Parameters:
        value - the value to retrieve offset for
        num - the number of match-offset pairs in the lookupswitch
        Returns:
        switch offset
      • alignInvokeInterface

        public final void alignInvokeInterface()
        Skips the extra stuff after an invokeinterface instruction Used for invokeinterface
      • getArrayElementType

        public final int getArrayElementType()
        Returns the element type (primitive) of the array (as an unsigned byte) Used for newarray
        Returns:
        array element type
      • getArrayDimension

        public final int getArrayDimension()
        Returns the dimension of the array (as an unsigned byte) Used for multianewarray
        Returns:
        array dimension
      • getWideOpcode

        public final int getWideOpcode()
        Returns the opcode of the wide instruction Used for wide Can be one of iload, lload, fload, dload, aload, istore, lstore, fstore, dstore, astore, iinc
        Returns:
        the opcode of the wide instruction
      • getConstantIndex

        public final int getConstantIndex()
        Returns the constant pool index of a constant (as an unsigned byte) Used for ldc
        Returns:
        constant index
        See Also:
        getWideConstantIndex()
      • getWideConstantIndex

        public final int getWideConstantIndex()
        Returns the wide constant pool index of a constant (as an unsigned short) Used for ldc_w, ldc2_w
        Returns:
        wide constant index
        See Also:
        getConstantIndex()