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

Instruction

public abstract class Instruction extends Object implements Serializable, Cloneable
Abstract super class for all Java byte codes.
version
$Id: Instruction.java,v 1.1.2.1 2005/07/31 23:44:37 jeffsuttor Exp $
author
M. Dahm

Fields Summary
protected short
length
protected short
opcode
private static InstructionComparator
cmp
Constructors Summary
Instruction()
Empty constructor needed for the Class.newInstance() statement in Instruction.readInstruction(). Not to be used otherwise.


                   
   
public Instruction(short opcode, short length)

    this.length = length;
    this.opcode = opcode;
  
Methods Summary
public abstract voidaccept(com.sun.org.apache.bcel.internal.generic.Visitor v)
Call corresponding visitor method(s). The order is: Call visitor methods of implemented interfaces first, then call methods according to the class hierarchy in descending order, i.e., the most specific visitXXX() call comes last.

param
v Visitor object

private static final java.lang.StringclassName(short opcode)

    String name = Constants.OPCODE_NAMES[opcode].toUpperCase();

    /* ICONST_0, etc. will be shortened to ICONST, etc., since ICONST_0 and the like
     * are not implemented (directly).
     */
    try {
      int  len = name.length();
      char ch1 = name.charAt(len - 2), ch2 = name.charAt(len - 1);

      if((ch1 == '_") && (ch2 >= '0")  && (ch2 <= '5"))
	name = name.substring(0, len - 2);
      
      if(name.equals("ICONST_M1")) // Special case
	name = "ICONST";
    } catch(StringIndexOutOfBoundsException e) { System.err.println(e); }

    return "com.sun.org.apache.bcel.internal.generic." + name;
  
public intconsumeStack(com.sun.org.apache.bcel.internal.generic.ConstantPoolGen cpg)
This method also gives right results for instructions whose effect on the stack depends on the constant pool entry they reference.

return
Number of words consumed from stack by this instruction, or Constants.UNPREDICTABLE, if this can not be computed statically

    return Constants.CONSUME_STACK[opcode];
  
public com.sun.org.apache.bcel.internal.generic.Instructioncopy()
Use with caution, since `BranchInstruction's have a `target' reference which is not copied correctly (only basic types are). This also applies for `Select' instructions with their multiple branch targets.

see
BranchInstruction
return
(shallow) copy of an instruction

    Instruction i = null;

    // "Constant" instruction, no need to duplicate
    if(InstructionConstants.INSTRUCTIONS[this.getOpcode()] != null)
      i = this;
    else {
      try {
	i = (Instruction)clone();
      } catch(CloneNotSupportedException e) {
	System.err.println(e);
      }
    }

    return i;
  
voiddispose()
Some instructions may be reused, so don't do anything by default.

public voiddump(java.io.DataOutputStream out)
Dump instruction as byte code to stream out.

param
out Output stream

    out.writeByte(opcode); // Common for all instructions
  
public booleanequals(java.lang.Object that)
Check for equality, delegated to comparator

return
true if that is an Instruction and has the same opcode

    return (that instanceof Instruction)?
      cmp.equals(this, (Instruction)that) : false;
  
public static com.sun.org.apache.bcel.internal.generic.InstructionComparatorgetComparator()
Get Comparator object used in the equals() method to determine equality of instructions.

return
currently used comparator for equals()

 return cmp; 
public intgetLength()

return
length (in bytes) of instruction

 return length; 
public java.lang.StringgetName()

return
name of instruction, i.e., opcode name

    return Constants.OPCODE_NAMES[opcode];
  
public shortgetOpcode()

return
this instructions opcode

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

param
bytes byte sequence to read from
param
wide "wide" instruction flag

public intproduceStack(com.sun.org.apache.bcel.internal.generic.ConstantPoolGen cpg)
This method also gives right results for instructions whose effect on the stack depends on the constant pool entry they reference.

return
Number of words produced onto stack by this instruction, or Constants.UNPREDICTABLE, if this can not be computed statically

    return Constants.PRODUCE_STACK[opcode];
  
public static final com.sun.org.apache.bcel.internal.generic.InstructionreadInstruction(com.sun.org.apache.bcel.internal.util.ByteSequence bytes)
Read an instruction from (byte code) input stream and return the appropiate object.

param
file file to read from
return
instruction object being read

    boolean     wide   = false;
    short       opcode = (short)bytes.readUnsignedByte();
    Instruction obj    = null;

    if(opcode == Constants.WIDE) { // Read next opcode after wide byte
      wide = true;
      opcode  = (short)bytes.readUnsignedByte();
    }

    if(InstructionConstants.INSTRUCTIONS[opcode] != null)
      return InstructionConstants.INSTRUCTIONS[opcode]; // Used predefined immutable object, if available

    /* Find appropiate class, instantiate an (empty) instruction object
     * and initialize it by hand.
     */
    Class clazz;

    try {
      clazz = Class.forName(className(opcode));
    } catch (ClassNotFoundException cnfe){
      // If a class by that name does not exist, the opcode is illegal.
      // Note that IMPDEP1, IMPDEP2, BREAKPOINT are also illegal in a sense.
      throw new ClassGenException("Illegal opcode detected.");
    }

    try {
      obj = (Instruction)clazz.newInstance();

      if(wide && !((obj instanceof LocalVariableInstruction) ||
		   (obj instanceof IINC) ||
		   (obj instanceof RET)))
	throw new Exception("Illegal opcode after wide: " + opcode);

      obj.setOpcode(opcode);
      obj.initFromFile(bytes, wide); // Do further initializations, if any
      // Byte code offset set in InstructionList
    } catch(Exception e) { throw new ClassGenException(e.toString()); }

    return obj;
  
public static voidsetComparator(com.sun.org.apache.bcel.internal.generic.InstructionComparator c)
Set comparator to be used for equals().

 cmp = c; 
private voidsetOpcode(short opcode)
Needed in readInstruction.

 this.opcode = opcode; 
public java.lang.StringtoString(boolean verbose)
Long output format: <name of opcode> "["<opcode number>"]" "("<length of instruction>")"

param
verbose long/short format switch
return
mnemonic for instruction

    if(verbose)
      return getName() + "[" + opcode + "](" + length + ")";
    else
      return getName();
  
public java.lang.StringtoString()

return
mnemonic for instruction in verbose format

    return toString(true);
  
public java.lang.StringtoString(com.sun.org.apache.bcel.internal.classfile.ConstantPool cp)

return
mnemonic for instruction with sumbolic references resolved

    return toString(false);