Class/Object

org.opalj.br

Code

Related Docs: object Code | package br

Permalink

final class Code extends Attribute with CommonAttributes with InstructionsContainer

Representation of a method's code attribute, that is, representation of a method's implementation.

Linear Supertypes
Ordering
  1. Alphabetic
  2. By inheritance
Inherited
  1. Code
  2. InstructionsContainer
  3. CommonAttributes
  4. Attribute
  5. AnyRef
  6. Any
  1. Hide All
  2. Show all
Visibility
  1. Public
  2. All

Value Members

  1. final def !=(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  4. def alwaysResultsInException(pc: PC, joinInstructions: BitSet, anInvocation: (PC) ⇒ Boolean, aThrow: (PC) ⇒ Boolean): Boolean

    Permalink

    Tests if the sequence of instructions that starts with the given pc always ends with an ATHROW instruction or a method call that always throws an exception.

    Tests if the sequence of instructions that starts with the given pc always ends with an ATHROW instruction or a method call that always throws an exception. The call sequence furthermore has to contain no complex logic. Here, complex means that evaluating the instruction may result in multiple control flows. If the sequence contains complex logic, false will be returned.

    One use case of this method is to, e.g., check if the code of the default case of a switch instruction always throws some error (e.g., an UnknownError or AssertionError).

    switch(...) {
     case X : ....
     default :
         throw new AssertionError();
    }

    This is a typical idiom used in Java programs and which may be relevant for certain analyses to detect.

    pc

    The program counter of an instruction that strictly dominates all succeeding instructions up until the next join instruction (as determined by #joinInstructions. This is naturally the case for the very first instruction of each method and each exception handler unless these instructions are joinInstructions; in this case the false is returned.

    anInvocation

    When the analysis finds a method call, it calls this method to let the caller decide whether the called method is an (indirect) way of always throwing an exception. If true is returned the analysis terminates and returns true; otherwise the analysis continues.

    aThrow

    If all (non-exception) paths will always end in one specific ATHROW instruction then this function is called (callback) to let the caller decide if the "expected" exception is thrown. This analysis will return with the result of this call.

    returns

    true if the bytecode sequence starting with the instruction with the given pc always ends with an org.opalj.br.instructions.ATHROW instruction. false in all other cases (i.e., the sequence does not end with an athrow instruction or the control flow is more complex.)

    Annotations
    @inline()
    Note

    If complex control flows should also be considered it is possible to compute a methods org.opalj.br.cfg.CFG and use that one.

  5. final def asInstanceOf[T0]: T0

    Permalink
    Definition Classes
    Any
  6. def associateWithIndex(): Seq[(PC, Instruction)]

    Permalink

    Returns a new sequence that pairs the program counter of an instruction with the instruction.

  7. val attributes: Attributes

    Permalink
    Definition Classes
    CodeCommonAttributes
  8. def belongsToSubroutine(): Array[Int]

    Permalink

    Calculates for each instruction to which subroutine the respective instruction belongs to – if any.

    Calculates for each instruction to which subroutine the respective instruction belongs to – if any. This information is required to, e.g., identify the subroutine contexts that need to be reset in case of an exception in a subroutine.

    returns

    Basically a map that maps the pc of each instruction to the id of the subroutine. For each instruction (with a specific pc) the pc of the first instruction of the subroutine it belongs to is returned. The pc 0 identifies the instruction as belonging to the core method. The pc -1 identifies the instruction as dead by compilation.

    Note

    Calling this method only makes sense for Java bytecode that actually contains org.opalj.br.instructions.JSR and org.opalj.br.instructions.RET instructions.

  9. def clone(): AnyRef

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  10. def collect[B](f: PartialFunction[Instruction, B]): Seq[(PC, B)]

    Permalink

    Collects all instructions for which the given function is defined.

    Collects all instructions for which the given function is defined.

    Usage scenario

    Use this function if you want to search for and collect specific instructions and when you do not immediately require the program counter/index of the instruction in the instruction array to make the decision whether you want to collect the instruction.

    Examples

    Example usage to collect the declaring class of all get field accesses where the field name is "last".

    collect({
     case GETFIELD(declaringClass, "last", _) ⇒ declaringClass
    })

    Example usage to collect all instances of a "DUP" instruction.

    code.collect({ case dup @ DUP ⇒ dup })
    returns

    The result of applying the function f to all instructions for which f is defined combined with the index (program counter) of the instruction in the code array.

  11. def collectFirstWithIndex[B](f: PartialFunction[(PC, Instruction), B]): Option[B]

    Permalink

    Applies the given function to the first instruction for which the given function is defined.

  12. def collectInstructions[B](f: PartialFunction[Instruction, B]): Seq[B]

    Permalink

    Collects all instructions for which the given function is defined.

    Collects all instructions for which the given function is defined. The order in which the instructions are collected is reversed when compared to the order in the instructions array.

  13. def collectPair[B](f: PartialFunction[(Instruction, Instruction), B]): List[(PC, B)]

    Permalink

    Finds a pair of consecutive instructions that are matched by the given partial function.

    Finds a pair of consecutive instructions that are matched by the given partial function.

    Example Usage

    (pc, _) ← body.findPair {
         case (
             INVOKESPECIAL(receiver1, _, SingleArgumentMethodDescriptor((paramType: BaseType, _))),
             INVOKEVIRTUAL(receiver2, name, NoArgumentMethodDescriptor(returnType: BaseType))
         ) if (...) ⇒ (...)
         } yield ...
  14. def collectWithIndex[B](f: PartialFunction[(PC, Instruction), B]): Seq[B]

    Permalink

    Applies the given function f to all instruction objects for which the function is defined.

    Applies the given function f to all instruction objects for which the function is defined. The function is passed a tuple consisting of the current program counter/index in the code array and the corresponding instruction.

    Example

    Example usage to collect the program counters (indexes) of all instructions that are the target of a conditional branch instruction:

    code.collectWithIndex({
     case (pc, cbi: ConditionalBranchInstruction) ⇒
         Seq(cbi.indexOfNextInstruction(pc, code), pc + cbi.branchoffset)
     }) // .flatten should equal (Seq(...))
  15. final def eq(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  16. def equals(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  17. val exceptionHandlers: ExceptionHandlers

    Permalink
  18. def exceptionHandlersFor(pc: PC): List[ExceptionHandler]

    Permalink

    Returns a view of all potential exception handlers (if any) for the instruction with the given program counter (pc).

    Returns a view of all potential exception handlers (if any) for the instruction with the given program counter (pc). Finally handlers (catchType == None) are not returned but will stop the evaluation (as all further exception handlers have no further meaning w.r.t. the runtime)! In case of identical caught exceptions only the first of them will be returned. No further checks (w.r.t. the typehierarchy) are done.

    pc

    The program counter of an instruction of this Code array.

  19. final def exists(p: (PC, Instruction) ⇒ Boolean): Boolean

    Permalink

    Iterates over all instructions until an instruction is found that matches the given predicate.

    Iterates over all instructions until an instruction is found that matches the given predicate.

    Annotations
    @inline()
  20. def finalize(): Unit

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  21. def find(f: (Instruction) ⇒ Boolean): Option[PC]

    Permalink

    Tests if an instruction matches the given filter.

    Tests if an instruction matches the given filter. If so, the index of the first matching instruction is returned.

  22. def findSequence[B](windowSize: Int)(f: PartialFunction[Seq[Instruction], B]): List[(PC, B)]

    Permalink

    Finds a sequence of instructions that are matched by the given partial function.

    Finds a sequence of instructions that are matched by the given partial function.

    returns

    List of pairs where the first element is the pc of the first instruction of a matched sequence and the second value is the result of the evaluation of the partial function.

    Note

    If possible, use one of the more specialized methods, such as, collectPair. The pure iteration overhead caused by this method is roughly 10-20 times higher than this one.

  23. def firstLineNumber: Option[Int]

    Permalink

    Returns the smallest line number (if any).

    Returns the smallest line number (if any).

    Note

    The line number associated with the first instruction (pc === 0) is not necessarily the smallest one.

    public void foo(int i) {
        super.foo( // The call has the smallest line number.
            i+=1; // THIS IS THE FIRST OPERATION...
        )
    }
  24. final def foreach(f: (PC, Instruction) ⇒ Unit): Unit

    Permalink

    Iterates over all instructions and calls the given function f for every instruction.

    Iterates over all instructions and calls the given function f for every instruction.

    Annotations
    @inline()
  25. final def getClass(): Class[_]

    Permalink
    Definition Classes
    AnyRef → Any
  26. def handlerInstructionsFor(pc: PC): PCs

    Permalink

    The set of pcs of those instructions that may handle an exception if the evaluation of the instruction with the given pc throws an exception.

    The set of pcs of those instructions that may handle an exception if the evaluation of the instruction with the given pc throws an exception.

    In case of multiple finally handlers only the first one will be returned and no further exception handlers will be returned. In case of identical caught exceptions only the first of them will be returned. No further checks (w.r.t. the typehierarchy) are done.

  27. def handlersFor(pc: PC, justExceptions: Boolean = false): List[ExceptionHandler]

    Permalink

    Returns a view of all handlers (exception and finally handlers) for the instruction with the given program counter (pc) that may catch an exception.

    Returns a view of all handlers (exception and finally handlers) for the instruction with the given program counter (pc) that may catch an exception.

    In case of multiple exception handlers that are identical (in particular in case of the finally handlers) only the first one is returned as that one is the one that will be used by the JVM at runtime. In case of identical caught exceptions only the first of them will be returned. No further checks (w.r.t. the typehierarchy) are done.

    pc

    The program counter of an instruction of this Code array.

  28. def hashCode(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  29. def haveSameLineNumber(firstPC: PC, secondPC: PC): Option[Boolean]

    Permalink

    Returns Some(true) if both pcs have the same line number.

    Returns Some(true) if both pcs have the same line number. If line number information is not available None is returned.

  30. val instructions: Array[Instruction]

    Permalink

    The instructions of this Code array/Code block.

    The instructions of this Code array/Code block. Since the code array is not completely filled (it contains null values) the preferred way to iterate over all instructions is to use for-comprehensions and pattern matching or to use one of the predefined methods foreach, collect, collectPair, collectWithIndex, etc.. The instructions array must not be mutated!

  31. def instructionsCount: Int

    Permalink

    Counts the number of instructions.

    Counts the number of instructions. This operation has complexity O(n).

    The number of instructions is always smaller or equal to the size of the code array.

    Note

    The result is not cached and recalculated on-demand.

  32. def instructionsOption: Option[Array[Instruction]]

    Permalink
    Definition Classes
    CodeInstructionsContainer
  33. final def isInstanceOf[T0]: Boolean

    Permalink
    Definition Classes
    Any
  34. def isModifiedByWide(pc: PC): Boolean

    Permalink

    True if the instruction with the given program counter is modified by wide.

    True if the instruction with the given program counter is modified by wide.

    pc

    A valid index in the code array.

    Annotations
    @inline()
  35. def joinInstructions: BitSet

    Permalink

    Returns the set of all program counters where two or more control flow paths joins.

    Returns the set of all program counters where two or more control flow paths joins.

    Example

    	0: iload_1
    	1: ifgt	6
    	2: iconst_1
    	5: goto 10
    	6: ...
    	9: iload_1
     10:return // <= JOIN INSTRUCTION: the predecessors are the instructions 5 and 9.

    In case of exceptions handlers the sound over approximation is made that all exception handlers may be reached on multiple paths.

  36. def kindId: Int

    Permalink

    This attribute's kind id.

    This attribute's kind id.

    Definition Classes
    CodeAttribute
  37. def lineNumber(pc: PC): Option[Int]

    Permalink

    Returns the line number associated with the instruction with the given pc if it is available.

    Returns the line number associated with the instruction with the given pc if it is available.

    pc

    Index of the instruction for which we want to get the line number.

    returns

    Some line number or None if no line-number information is available.

  38. def lineNumberTable: Option[LineNumberTable]

    Permalink

    Returns the line number table - if any.

    Returns the line number table - if any.

    Note

    Depending on the configuration of the reader for ClassFiles this attribute may not be reified.

    ,

    A code attribute is allowed to have multiple line number tables. However, all tables are merged into one by OPAL at class loading time.

  39. def localVariable(pc: PC, index: Int): Option[LocalVariable]

    Permalink

    Returns the local variable stored at the given local variable index that is live at the given instruction (pc).

  40. def localVariableTable: Option[LocalVariables]

    Permalink

    Collects all local variable tables.

    Collects all local variable tables.

    Note

    Depending on the configuration of the reader for ClassFiles this attribute may not be reified.

    ,

    A code attribute is allowed to have multiple local variable tables. However, all tables are merged into one by OPAL at class loading time.

  41. def localVariableTypeTable: Seq[LocalVariableTypes]

    Permalink

    Collects all local variable type tables.

    Collects all local variable type tables.

    Note

    Depending on the configuration of the reader for ClassFiles this attribute may not be reified.

  42. def localVariablesAt(pc: PC): Map[Int, LocalVariable]

    Permalink

    Returns the set of local variables defined at the given pc.

    Returns the set of local variables defined at the given pc.

    returns

    A mapping of the index to the name of the local variable. The map is empty if no debug information is available.

  43. def matchPair(f: (Instruction, Instruction) ⇒ Boolean): List[PC]

    Permalink

    Matches pairs of two consecutive instructions.

    Matches pairs of two consecutive instructions. For each matched pair, the program counter of the first instruction is returned.

    Example Usage

    for {
     classFile ← project.view.map(_._1).par
     method @ MethodWithBody(body) ← classFile.methods
     pc ← body.matchPair({
         case (
             INVOKESPECIAL(receiver1, _, TheArgument(parameterType: BaseType)),
             INVOKEVIRTUAL(receiver2, name, NoArgumentMethodDescriptor(returnType: BaseType))
         ) ⇒ { (receiver1 eq receiver2) && (returnType ne parameterType) }
         case _ ⇒ false
         })
     } yield (classFile, method, pc)
  44. def matchTriple(matchMaxTriples: Int = Int.MaxValue, f: (Instruction, Instruction, Instruction) ⇒ Boolean): List[PC]

    Permalink

    Finds a sequence of 3 consecutive instructions for which the given function returns true, and returns the PC of the first instruction in each found sequence.

    Finds a sequence of 3 consecutive instructions for which the given function returns true, and returns the PC of the first instruction in each found sequence.

    matchMaxTriples

    Is the maximum number of triples that is passed to f. E.g., if matchMaxTriples is "1" only the first three instructions are passed to f.

  45. def matchTriple(f: (Instruction, Instruction, Instruction) ⇒ Boolean): List[PC]

    Permalink

    Finds all sequences of three consecutive instructions that are matched by f.

  46. val maxLocals: Int

    Permalink

    The number of registers/local variables needed to execute the method.

    The number of registers/local variables needed to execute the method. As in case of maxStack this number is expected to be the minimum, but this is not guaranteed.

  47. val maxStack: Int

    Permalink

    The maximum size of the stack during the execution of the method.

    The maximum size of the stack during the execution of the method. This value is determined by the compiler and is not necessarily the minimum. However, in the vast majority of cases it is the minimum.

  48. final def ne(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  49. def nextNonGotoInstruction(pc: PC): PC

    Permalink

    Returns the next instruction that will be returned at runtime that is not a org.opalj.br.instructions.GotoInstruction.

    Returns the next instruction that will be returned at runtime that is not a org.opalj.br.instructions.GotoInstruction. If the given instruction is not a org.opalj.br.instructions.GotoInstruction, the given instruction is returned.

    Annotations
    @tailrec()
  50. final def notify(): Unit

    Permalink
    Definition Classes
    AnyRef
  51. final def notifyAll(): Unit

    Permalink
    Definition Classes
    AnyRef
  52. final def pcOfNextInstruction(currentPC: PC): PC

    Permalink

    Returns the program counter of the next instruction after the instruction with the given counter (currentPC).

    Returns the program counter of the next instruction after the instruction with the given counter (currentPC).

    currentPC

    The program counter of an instruction. If currentPC is the program counter of the last instruction of the code block then the returned program counter will be equivalent to the length of the Code/Instructions array.

    Annotations
    @inline()
  53. final def pcOfPreviousInstruction(currentPC: PC): PC

    Permalink

    Returns the program counter of the previous instruction in the code array.

    Returns the program counter of the previous instruction in the code array. currentPC must be the program counter of an instruction.

    This function is only defined if currentPC is larger than 0; i.e., if there is a previous instruction! If currentPC is larger than instructions.size the behavior is undefined.

    Annotations
    @inline()
  54. def programCounters: Iterator[PC]

    Permalink

    Returns an iterator to iterate over the program counters of the instructions of this Code block.

    Returns an iterator to iterate over the program counters of the instructions of this Code block.

    See also

    See the method foreach for an alternative.

  55. def runtimeInvisibleTypeAnnotations: TypeAnnotations

    Permalink
    Definition Classes
    CommonAttributes
  56. def runtimeVisibleType: Seq[LocalVariableTypes]

    Permalink

    Collects all local variable type tables.

    Collects all local variable type tables.

    Note

    Depending on the configuration of the reader for ClassFiles this attribute may not be reified.

  57. def runtimeVisibleTypeAnnotations: TypeAnnotations

    Permalink
    Definition Classes
    CommonAttributes
  58. def slidingCollect[B](windowSize: Int)(f: PartialFunction[(PC, Seq[Instruction]), B]): Seq[B]

    Permalink

    Slides over the code array and tries to apply the given function to each sequence of instructions consisting of windowSize elements.

    Slides over the code array and tries to apply the given function to each sequence of instructions consisting of windowSize elements.

    Scenario

    If you want to search for specific patterns of bytecode instructions. Some "bug patterns" are directly related to specific bytecode sequences and these patterns can easily be identified using this method.

    Example

    Search for sequences of the bytecode instructions PUTFIELD and ALOAD_O in the method's body and return the list of program counters of the start of the identified sequences.

    code.slidingCollect(2)({
     case (pc, Seq(PUTFIELD(_, _, _), ALOAD_0)) ⇒ (pc)
    }) should be(Seq(...))
    windowSize

    The size of the sequence of instructions that is passed to the partial function. It must be larger than 0. **Do not use this method with windowSize "1"**; it is more efficient to use the collect or collectWithIndex methods instead.

    returns

    The list of results of applying the function f for each matching sequence.

    Note

    If possible, use one of the more specialized methods, such as, collectPair. The pure iteration overhead caused by this method is roughly 10-20 times higher than this one.

  59. def stackMapTable: Option[StackMapFrames]

    Permalink

    The JVM specification mandates that a Code attribute has at most one StackMapTable attribute.

    The JVM specification mandates that a Code attribute has at most one StackMapTable attribute.

    Note

    Depending on the configuration of the reader for ClassFiles this attribute may not be reified.

  60. final def synchronized[T0](arg0: ⇒ T0): T0

    Permalink
    Definition Classes
    AnyRef
  61. def toString(): String

    Permalink

    A complete representation of this code attribute (including instructions, attributes, etc.).

    A complete representation of this code attribute (including instructions, attributes, etc.).

    Definition Classes
    Code → AnyRef → Any
  62. def typeAnnotations: TypeAnnotations

    Permalink

    The list of all type annotations.

    The list of all type annotations. In general, if a specific type annotation is searched for, the method runtimeVisibleTypeAnnotations or runtimeInvisibleTypeAnnotations should be used.

    Definition Classes
    CommonAttributes
  63. final def wait(): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  64. final def wait(arg0: Long, arg1: Int): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  65. final def wait(arg0: Long): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )

Inherited from InstructionsContainer

Inherited from CommonAttributes

Inherited from Attribute

Inherited from AnyRef

Inherited from Any

Ungrouped