Package serp.bytecode

# Class SwitchInstruction

java.lang.Object
serp.bytecode.Instruction
serp.bytecode.JumpInstruction
serp.bytecode.SwitchInstruction

All Implemented Interfaces:
  `BCEntity`, `InstructionPtr`, `VisitAcceptor`

public abstract class SwitchInstruction extends JumpInstruction
Contains functionality common to the different switch types (TableSwitch and LookupSwitch).

Author:
  Eric Lindauer

## Constructor Summary

Constructors
Constructor
Description
`SwitchInstruction(Code owner, int opcode)`

## Method Summary

Modifier and Type
Method
Description
`SwitchInstruction`
`addCase(int match, Instruction target)`
Add a case to this switch.
`abstract SwitchInstruction`
`addMatch(int match)`
`SwitchInstruction`
`addTarget(Instruction target)`
Add a target to this switch.
`int`
`countTargets()`
`int`
`getDefaultOffset()`
Synonymous with `JumpInstruction.getOffset()`.
`Instruction`
`getDefaultTarget()`
Synonymous with `JumpInstruction.getTarget()`.
`abstract int[]`
`getMatches()`
`int[]`
`getOffsets()`
Returns the current byte offsets for the different switch cases in this Instruction.
`int`
`getStackChange()`
Return the number of stack positions this instruction pushes or pops during its execution.
`Instruction[]`
`getTargets()`
Return the targets for this switch, or empty array if not set.
`void`
`replaceTarget(Instruction oldTarget, Instruction newTarget)`
Replace the given old, likely invalid, target with a new target.
`SwitchInstruction`
`setCases(int[] matches, Instruction[] targets)`
Set the match-jumppt pairs for this switch.
`SwitchInstruction`
`setDefaultOffset(int offset)`
Synonymous with `JumpInstruction.setOffset(int)`.
`SwitchInstruction`
`setDefaultTarget(Instruction ins)`
Synonymous with `JumpInstruction.setTarget(serp.bytecode.Instruction)`.
`SwitchInstruction`
`setMatches(int[] matches)`
`void`
`setOffsets(int[] offsets)`
Sets the offsets for the instructions representing the different switch statement cases.
`SwitchInstruction`
`setTargets(Instruction[] targets)`
Set the jump points for this switch.
`void`
`updateTargets()`
Use the byte indexes read from the class file to calculate and set references to the target instruction(s) for this ptr.

### Methods inherited from class serp.bytecode.JumpInstruction

`acceptVisit, equalsInstruction, getOffset, getTarget, setOffset, setTarget`

### Methods inherited from class serp.bytecode.Instruction

`getByteIndex, getClassLoader, getCode, getLineNumber, getLogicalStackChange, getName, getOpcode, getPool, getProject, isValid`

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

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

### Methods inherited from interface serp.bytecode.InstructionPtr

`getCode`

## Constructor Details

### SwitchInstruction

public SwitchInstruction(Code owner, int opcode)

## Method Details

### getOffsets

public int[] getOffsets()
Returns the current byte offsets for the different switch cases in this Instruction.

### setOffsets

public void setOffsets(int[] offsets)
Sets the offsets for the instructions representing the different switch statement cases. WARNING: these offsets will not be changed in the event that the code is modified following this call. It is typically a good idea to follow this call with a call to updateTargets as soon as the instructions at the given offsets are valid, at which point the Instructions themselves will be used as the targets and the offsets will be updated as expected.

### countTargets

public int countTargets()

### getDefaultTarget

public Instruction getDefaultTarget()
Synonymous with `JumpInstruction.getTarget()`.

### getDefaultOffset

public int getDefaultOffset()
Synonymous with `JumpInstruction.getOffset()`.

### setDefaultOffset

public SwitchInstruction setDefaultOffset(int offset)
Synonymous with `JumpInstruction.setOffset(int)`.

### setDefaultTarget

public SwitchInstruction setDefaultTarget(Instruction ins)
Synonymous with `JumpInstruction.setTarget(serp.bytecode.Instruction)`.

### getTargets

public Instruction[] getTargets()
Return the targets for this switch, or empty array if not set.

### setTargets

public SwitchInstruction setTargets(Instruction[] targets)
Set the jump points for this switch.

Returns:
  this instruction, for method chaining

### addTarget

public SwitchInstruction addTarget(Instruction target)
Add a target to this switch.

Returns:
  this instruction, for method chaining

### getStackChange

public int getStackChange()
Description copied from class: `Instruction`
Return the number of stack positions this instruction pushes or pops during its execution.

Overrides:
  `getStackChange` in class `Instruction`
Returns:
  0 if the stack is not affected by this instruction, a positive number if it pushes onto the stack, and a negative number if it pops from the stack

### updateTargets

public void updateTargets()
Description copied from interface: `InstructionPtr`
Use the byte indexes read from the class file to calculate and set references to the target instruction(s) for this ptr. This method will be called after the byte code has been read in for the first time and before it is written after modification.

Specified by:
  `updateTargets` in interface `InstructionPtr`
Overrides:
  `updateTargets` in class `JumpInstruction`

### replaceTarget

public void replaceTarget(Instruction oldTarget, Instruction newTarget)
Description copied from interface: `InstructionPtr`
Replace the given old, likely invalid, target with a new target. The new target Instruction is guaranteed to be in the same code block as this InstructionPtr.

Specified by:
  `replaceTarget` in interface `InstructionPtr`
Overrides:
  `replaceTarget` in class `JumpInstruction`

### setCases

public SwitchInstruction setCases(int[] matches, Instruction[] targets)
Set the match-jumppt pairs for this switch.

Returns:
  this instruction, for method chaining

### setMatches

public SwitchInstruction setMatches(int[] matches)

### addCase

public SwitchInstruction addCase(int match, Instruction target)
Add a case to this switch.

Returns:
  this instruction, for method chaining

### addMatch

public abstract SwitchInstruction addMatch(int match)

### getMatches

public abstract int[] getMatches()