edu.rit.classfile
Class SwitchInstruction

java.lang.Object
  extended by edu.rit.classfile.Instruction
      extended by edu.rit.classfile.SwitchInstruction

public abstract class SwitchInstruction
extends Instruction

Class SwitchInstruction is the abstract superclass for all switch instructions. To add a switch instruction to a subroutine (method, constructor, or class initializer):

  1. Call the Op.LOOKUPSWITCH() or Op.TABLESWITCH() method to manufacture a switch instruction of the desired kind, specifying the target branch location for the default case.

  2. Do not add the switch instruction to the subroutine yet!

  3. Add each (case value, target branch location) pair to the switch instruction by calling the addCase() method.

  4. After all the cases have been specified, add the switch instruction to the subroutine.


Method Summary
 void addCase(int theCaseValue, Location theTarget)
          Add a (case value, target branch location) pair to this switch instruction.
 byte[] getByteCodes()
          Returns a copy of this switch instruction's bytecodes.
 int getLength()
          Returns this switch instruction's length, i.e., number of bytes.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

addCase

public void addCase(int theCaseValue,
                    Location theTarget)
Add a (case value, target branch location) pair to this switch instruction. If the case value had been previously added, the new target branch location replaces the old target branch location.

Parameters:
theCaseValue - Case value.
theTarget - Target branch location.
Throws:
NullPointerException - (unchecked exception) Thrown if theTarget is null.
IllegalStateException - (unchecked exception) Thrown if no further cases can be added because this switch instruction has already been added to a subroutine.

getLength

public int getLength()
Returns this switch instruction's length, i.e., number of bytes. The proper length cannot be determined until this switch instruction is added to a subroutine. Thus, if this switch instruction has not been added to a subroutine, 0 is returned.

Specified by:
getLength in class Instruction

getByteCodes

public byte[] getByteCodes()
                    throws ByteCodeException
Returns a copy of this switch instruction's bytecodes. (Altering the return value will not affect this switch instruction's bytecodes.)

Specified by:
getByteCodes in class Instruction
Throws:
ByteCodeException - Thrown if there was a problem generating this switch instruction's bytecodes. The exception's detail message describes the problem.


Copyright © 2001-2006 by Alan Kaminsky. All rights reserved. Send comments to ark­@­cs.rit.edu.