Main Page   Packages   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

CPInstruction.java

00001 package de.fub.bytecode.generic;
00002 
00003 import java.io.*;
00004 import de.fub.bytecode.util.ByteSequence;
00005 import de.fub.bytecode.Constants;
00006 import de.fub.bytecode.classfile.*;
00007 
00008 /** 
00009  * Abstract super class for instructions that use an index into the 
00010  * constant pool such as LDC, INVOKEVIRTUAL, etc.
00011  *
00012  * @see ConstantPoolGen
00013  * @see LDC
00014  * @see INVOKEVIRTUAL
00015  *
00016  * @version $Id: CPInstruction.java,v 1.1.1.1 2002/01/24 03:41:38 pserver Exp $
00017  * @author  <A HREF="http://www.inf.fu-berlin.de/~dahm">M. Dahm</A>
00018  */
00019 public abstract class CPInstruction extends Instruction
00020   implements TypedInstruction, IndexedInstruction
00021 {
00022   protected int index; // index to constant pool
00023 
00024   /**
00025    * Empty constructor needed for the Class.newInstance() statement in
00026    * Instruction.readInstruction(). Not to be used otherwise.
00027    */
00028   CPInstruction() {}  
00029   /**
00030    * @param index to constant pool
00031    */
00032   protected CPInstruction(short tag, int index) {
00033     super(tag, (short)3);
00034     setIndex(index);
00035   }  
00036   /**
00037    * Dump instruction as byte code to stream out.
00038    * @param out Output stream
00039    */
00040   public void dump(DataOutputStream out) throws IOException {
00041     out.writeByte(tag);
00042     out.writeShort(index);
00043   }  
00044   /**
00045    * @return index in constant pool referred by this instruction.
00046    */
00047   public final int getIndex() { return index; }  
00048   /** @return type related with this instruction.
00049    */
00050   public Type getType(ConstantPoolGen cpg) {
00051     ConstantPool cp   = cpg.getConstantPool();
00052     String       name = cp.getConstantString(index, de.fub.bytecode.Constants.CONSTANT_Class);
00053 
00054     if(!name.startsWith("["))
00055       name = "L" + name + ";";
00056 
00057     return Type.getType(name);
00058   }  
00059   /**
00060    * Read needed data (i.e., index) from file.
00061    * @param bytes input stream
00062    * @param wide wide prefix?
00063    */
00064   protected void initFromFile(ByteSequence bytes, boolean wide)
00065        throws IOException
00066   {
00067     setIndex(bytes.readUnsignedShort());
00068     length = 3;
00069   }  
00070   /**
00071    * Set the index to constant pool.
00072    * @param index in  constant pool.
00073    */
00074   public void setIndex(int index) { 
00075     if(index < 0)
00076       throw new ClassGenException("Negative index value: " + index);
00077 
00078     this.index = index;
00079   }  
00080   /**
00081    * @return mnemonic for instruction with symbolic references resolved
00082    */
00083   public String toString(ConstantPool cp) {
00084     Constant c   = cp.getConstant(index);
00085     String   str = cp.constantToString(c);
00086 
00087     if(c instanceof ConstantClass)
00088       str = str.replace('.', '/');
00089 
00090     return de.fub.bytecode.Constants.OPCODE_NAMES[tag] + " " + str;
00091   }  
00092   /**
00093    * Long output format:
00094    *
00095    * &lt;name of opcode&gt; "["&lt;opcode number&gt;"]" 
00096    * "("&lt;length of instruction&gt;")" "&lt;"&lt; constant pool index&gt;"&gt;"
00097    *
00098    * @param verbose long/short format switch
00099    * @return mnemonic for instruction
00100    */
00101   public String toString(boolean verbose) {
00102     return super.toString(verbose) + " " + index;
00103   }  
00104 }

Generated at Thu Feb 7 06:43:11 2002 for Bandera by doxygen1.2.10 written by Dimitri van Heesch, © 1997-2001