Package org.jacoco.core.internal.analysis

## Class Instruction

- java.lang.Object
- - org.jacoco.core.internal.analysis.Instruction

    public class Instruction
    extends java.lang.Object

Execution status of a single bytecode instruction internally used for coverage analysis. The execution status is recorded separately for each outgoing branch. Each instruction has at least one branch, for example in case of a simple sequence of instructions (by convention branch 0). Instances of this class are used in two steps:

### Step 1: Building the CFG

For each bytecode instruction of a method a `Instruction` instance is created. In correspondence with the CFG these instances are linked with each other with the `addBranch()` methods. The executions status is either directly derived from a probe which has been inserted in the execution flow (`addBranch(boolean, int)`) or indirectly propagated along the CFG edges (`addBranch(Instruction, int)`).

### Step 2: Querying the Coverage Status

After all instructions have been created and linked each instruction knows its execution status and can be queried with:

- `getLine()`
- `getInstructionCounter()`
- `getBranchCounter()`

For the purpose of filtering instructions can be combined to new instructions. Note that these methods create new `Instruction` instances and do not modify the existing ones.

- `merge(Instruction)`
- `replaceBranches(Collection)`

### Constructor Summary

Constructors
`Instruction​(int line)` New instruction at the given line.

### Method Summary

All Methods Instance Methods Concrete Methods
`void` `addBranch​(boolean executed, int branch)` Adds a branch to this instruction which execution status is directly derived from a probe.
`void` `addBranch​(Instruction target, int branch)` Adds a branch to this instruction which execution status is indirectly derived from the execution status of the target instruction.
`ICounter` `getBranchCounter()` Returns the branch coverage counter of this instruction.
`ICounter` `getInstructionCounter()` Returns the instruction coverage counter of this instruction.
`int` `getLine()` Returns the source line this instruction belongs to.
`Instruction` `merge​(Instruction other)` Merges information about covered branches of this instruction with another instruction.
`Instruction` `replaceBranches​(java.util.Collection<Instruction> newBranches)` Creates a copy of this instruction where all outgoing branches are replaced with the given instructions.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Constructor Detail

#### Instruction

    public Instruction​(int line)

New instruction at the given line.

Parameters:
  `line` - source line this instruction belongs to

### Method Detail

#### addBranch

    public void addBranch​(Instruction target,
                          int branch)

Adds a branch to this instruction which execution status is indirectly derived from the execution status of the target instruction. In case the branch is covered the status is propagated also to the predecessors of this instruction. Note: This method is not idempotent and must be called exactly once for every branch.

Parameters:
  `target` - target instruction of this branch
  `branch` - branch identifier unique for this instruction

#### addBranch

    public void addBranch​(boolean executed,
                          int branch)

Adds a branch to this instruction which execution status is directly derived from a probe. In case the branch is covered the status is propagated also to the predecessors of this instruction. Note: This method is not idempotent and must be called exactly once for every branch.

Parameters:
  `executed` - whether the corresponding probe has been executed
  `branch` - branch identifier unique for this instruction

#### getLine

    public int getLine()

Returns the source line this instruction belongs to.

Returns:
  corresponding source line

#### merge

    public Instruction merge​(Instruction other)

Merges information about covered branches of this instruction with another instruction.

Parameters:
  `other` - instruction to merge with
Returns:
  new instance with merged branches

#### replaceBranches

    public Instruction replaceBranches​(java.util.Collection<Instruction> newBranches)

Creates a copy of this instruction where all outgoing branches are replaced with the given instructions. The coverage status of the new instruction is derived from the status of the given instructions.

Parameters:
  `newBranches` - new branches to consider
Returns:
  new instance with replaced branches

#### getInstructionCounter

    public ICounter getInstructionCounter()

Returns the instruction coverage counter of this instruction. It is always 1 instruction which is covered or not.

Returns:
  the instruction coverage counter

#### getBranchCounter

    public ICounter getBranchCounter()

Returns the branch coverage counter of this instruction. Only instructions with at least 2 outgoing edges report branches.

Returns:
  the branch coverage counter