FileDocCategorySizeDatePackage
Select.javaAPI DocJava SE 6 API8319Tue Jun 10 00:22:22 BST 2008com.sun.org.apache.bcel.internal.generic

Select

public abstract class Select extends BranchInstruction implements StackProducer, VariableLengthInstruction
Select - Abstract super class for LOOKUPSWITCH and TABLESWITCH instructions.
version
$Id: Select.java,v 1.1.2.1 2005/07/31 23:45:26 jeffsuttor Exp $
author
M. Dahm
see
LOOKUPSWITCH
see
TABLESWITCH
see
InstructionList

Fields Summary
protected int[]
match
protected int[]
indices
protected InstructionHandle[]
targets
protected int
fixed_length
protected int
match_length
protected int
padding
Constructors Summary
Select()
Empty constructor needed for the Class.newInstance() statement in Instruction.readInstruction(). Not to be used otherwise.

  // number of pad bytes for alignment
  
                   
   
Select(short opcode, int[] match, InstructionHandle[] targets, InstructionHandle target)
(Match, target) pairs for switch. `Match' and `targets' must have the same length of course.

param
match array of matching values
param
targets instruction targets
param
target default instruction target

    super(opcode, target);

    this.targets = targets;
    for(int i=0; i < targets.length; i++)
      notifyTarget(null, targets[i], this);

    this.match = match;

    if((match_length = match.length) != targets.length)
      throw new ClassGenException("Match and target array have not the same length");

    indices = new int[match_length];
  
Methods Summary
public booleancontainsTarget(com.sun.org.apache.bcel.internal.generic.InstructionHandle ih)

return
true, if ih is target of this instruction

    if(target == ih)
      return true;

    for(int i=0; i < targets.length; i++)
      if(targets[i] == ih)
	return true;

    return false;
  
voiddispose()
Inform targets that they're not targeted anymore.

    super.dispose();

    for(int i=0; i < targets.length; i++)
      targets[i].removeTargeter(this);
  
public voiddump(java.io.DataOutputStream out)
Dump instruction as byte code to stream out.

param
out Output stream

    out.writeByte(opcode);

    for(int i=0; i < padding; i++) // Padding bytes
      out.writeByte(0);

    index = getTargetOffset();     // Write default target offset
    out.writeInt(index);
  
public int[]getIndices()

return
array of match target offsets

 return indices; 
public int[]getMatchs()

return
array of match indices

 return match; 
public com.sun.org.apache.bcel.internal.generic.InstructionHandle[]getTargets()

return
array of match targets

 return targets; 
protected voidinitFromFile(com.sun.org.apache.bcel.internal.util.ByteSequence bytes, boolean wide)
Read needed data (e.g. index) from file.

    padding = (4 - (bytes.getIndex() % 4)) % 4; // Compute number of pad bytes

    for(int i=0; i < padding; i++) {
      bytes.readByte();
    }
    
    // Default branch target common for both cases (TABLESWITCH, LOOKUPSWITCH)
    index = bytes.readInt();
  
public voidsetTarget(int i, com.sun.org.apache.bcel.internal.generic.InstructionHandle target)
Set branch target for `i'th case

    notifyTarget(targets[i], target, this);
    targets[i] = target;
  
public java.lang.StringtoString(boolean verbose)

return
mnemonic for instruction

    StringBuffer buf = new StringBuffer(super.toString(verbose));

    if(verbose) {
      for(int i=0; i < match_length; i++) {
	String s = "null";
	
	if(targets[i] != null)
	  s = targets[i].getInstruction().toString();
	
	buf.append("(" + match[i] + ", " + s + " = {" + indices[i] + "})");
      }
    }
    else
      buf.append(" ...");
    
    return buf.toString();
  
protected intupdatePosition(int offset, int max_offset)
Since this is a variable length instruction, it may shift the following instructions which then need to update their position. Called by InstructionList.setPositions when setting the position for every instruction. In the presence of variable length instructions `setPositions' performs multiple passes over the instruction list to calculate the correct (byte) positions and offsets by calling this function.

param
offset additional offset caused by preceding (variable length) instructions
param
max_offset the maximum offset that may be caused by these instructions
return
additional offset caused by possible change of this instruction's length

    position += offset; // Additional offset caused by preceding SWITCHs, GOTOs, etc.

    short old_length = length;

    /* Alignment on 4-byte-boundary, + 1, because of tag byte.
     */
    padding = (4 - ((position + 1) % 4)) % 4;
    length  = (short)(fixed_length + padding); // Update length

    return length - old_length;
  
public voidupdateTarget(com.sun.org.apache.bcel.internal.generic.InstructionHandle old_ih, com.sun.org.apache.bcel.internal.generic.InstructionHandle new_ih)

param
old_ih old target
param
new_ih new target

    boolean targeted = false;

    if(target == old_ih) {
      targeted = true;
      setTarget(new_ih);
    }

    for(int i=0; i < targets.length; i++) {
      if(targets[i] == old_ih) {
	targeted = true;
	setTarget(i, new_ih);
      }
    }
    
    if(!targeted)
      throw new ClassGenException("Not targeting " + old_ih);