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:44:06 pserver Exp $
00017  * @author  <A HREF="http://www.inf.fu-berlin.de/~dahm">M. Dahm</A>
00018  */
00019 public abstract class CPInstruction extends Instruction implements Constants {
00020   protected int index; // index to constant pool
00021 
00022   /**
00023    * Empty constructor needed for the Class.newInstance() statement in
00024    * Instruction.readInstruction(). Not to be used otherwise.
00025    */
00026   CPInstruction() {}  
00027   /**
00028    * @param index to constant pool
00029    */
00030   protected CPInstruction(short tag, int index) {
00031     super(tag, (short)3);
00032     setIndex(index);
00033   }  
00034   /**
00035    * Dump instruction as byte code to stream out.
00036    * @param out Output stream
00037    */
00038   public void dump(DataOutputStream out) throws IOException {
00039     out.writeByte(tag);
00040     out.writeShort(index);
00041   }  
00042   /**
00043    * @return index in constant pool referred by this instruction.
00044    */
00045   public final int getIndex() { return index; }  
00046   /** @return type related with this instruction.
00047    */
00048   public Type getType(ConstantPoolGen cpg) {
00049     ConstantPool cp   = cpg.getConstantPool();
00050     String       name = cp.getConstantString(index, CONSTANT_Class);
00051 
00052     if(!name.startsWith("["))
00053       name = "L" + name + ";";
00054 
00055     return Type.getType(name);
00056   }  
00057   /**
00058    * Read needed data (i.e. index) from file.
00059    */
00060   protected void initFromFile(ByteSequence bytes, boolean wide)
00061        throws IOException
00062   {
00063     setIndex(bytes.readUnsignedShort());
00064     length = 3;
00065   }  
00066   /**
00067    * Set the index to constant pool.
00068    */
00069   public void setIndex(int index) { 
00070     if(index < 0)
00071       throw new ClassGenException("Negative index value: " + index);
00072 
00073     this.index = index;
00074   }  
00075   /**
00076    * @return mnemonic for instruction with symbolic references resolved
00077    */
00078   public String toString(ConstantPool cp) {
00079     Constant c   = cp.getConstant(index);
00080     String   str = cp.constantToString(c);
00081 
00082     if(c instanceof ConstantClass)
00083       str = str.replace('.', '/');
00084 
00085     return OPCODE_NAMES[tag] + " " + str;
00086   }  
00087   /**
00088    * Long output format:
00089    *
00090    * &lt;name of opcode&gt; "["&lt;opcode number&gt;"]" 
00091    * "("&lt;length of instruction&gt;")" "&lt;"&lt; constant pool index&gt;"&gt;"
00092    *
00093    * @param verbose long/short format switch
00094    * @return mnemonic for instruction
00095    */
00096   public String toString(boolean verbose) {
00097     return super.toString(verbose) + " " + index;
00098   }  
00099 }

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