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

LDC.java

00001 package de.fub.bytecode.generic;
00002 
00003 import java.io.*;
00004 import de.fub.bytecode.util.ByteSequence;
00005 
00006 /** 
00007  * LDC - Push item from constant pool
00008  *
00009  * <PRE>Stack: ... -&gt; ..., item.word1, item.word2</PRE>
00010  *
00011  * @version $Id: LDC.java,v 1.1.1.1 2002/01/24 03:44:03 pserver Exp $
00012  * @author  <A HREF="http://www.inf.fu-berlin.de/~dahm">M. Dahm</A>
00013  */
00014 public class LDC extends CPInstruction implements PushInstruction, ExceptionThrower {
00015   /**
00016    * Empty constructor needed for the Class.newInstance() statement in
00017    * Instruction.readInstruction(). Not to be used otherwise.
00018    */
00019   LDC() {}  
00020   public LDC(int index) {
00021     super(LDC_W, index);
00022     setSize();
00023   }  
00024   /**
00025    * Dump instruction as byte code to stream out.
00026    * @param out Output stream
00027    */
00028   public void dump(DataOutputStream out) throws IOException {
00029     out.writeByte(tag);
00030 
00031     if(length == 2)
00032       out.writeByte(index);
00033     else // Applies for LDC_W
00034       out.writeShort(index);
00035   }  
00036   public Class[] getExceptions() { return EXCS_STRING_RESOLUTION; }  
00037   public Type getType(ConstantPoolGen cpg) {
00038     switch(cpg.getConstantPool().getConstant(index).getTag()) {
00039     case CONSTANT_String:  return Type.STRING;
00040     case CONSTANT_Float:   return Type.FLOAT;
00041     case CONSTANT_Integer: return Type.INT;
00042     default: // Never reached
00043       throw new RuntimeException("Unknown constant type " + tag);
00044     }
00045   }  
00046   /**
00047    * Read needed data (e.g. index) from file.
00048    */
00049   protected void initFromFile(ByteSequence bytes, boolean wide)
00050        throws IOException
00051   {
00052     length = 2;
00053     index  = bytes.readUnsignedByte();
00054   }  
00055   /**
00056    * Set the index to constant pool and adjust size.
00057    */
00058   public final void setIndex(int index) { 
00059     super.setIndex(index);
00060     setSize();
00061   }  
00062   // Adjust to proper size
00063   protected final void setSize() {
00064     if(index <= MAX_BYTE) { // Fits in one byte?
00065       tag    = LDC;
00066       length = 2;
00067     } else {
00068       tag    = LDC_W;
00069       length = 3;
00070     }
00071   }  
00072 }

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