FileDocCategorySizeDatePackage
Instruction.javaAPI DocJava SE 5 API9356Fri Aug 26 14:55:22 BST 2005com.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.1.1 2001/10/29 20:00:18 jvanzyl Exp $
author
M. Dahm

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

 // Opcode number

                   
   
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 intgetLength()

return
length (in bytes) of instruction

 return length; 
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;
  
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 Constants.OPCODE_NAMES[opcode] + "[" + opcode + "](" + length + ")";
    else
      return Constants.OPCODE_NAMES[opcode];
  
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);