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</PRE>
00010  *
00011  * @version $Id: LDC.java,v 1.1.1.1 2002/01/24 03:41:41 pserver Exp $
00012  * @author  <A HREF="http://www.inf.fu-berlin.de/~dahm">M. Dahm</A>
00013  */
00014 public class LDC extends CPInstruction
00015   implements PushInstruction, ExceptionThrower, TypedInstruction {
00016   /**
00017    * Empty constructor needed for the Class.newInstance() statement in
00018    * Instruction.readInstruction(). Not to be used otherwise.
00019    */
00020   LDC() {}  
00021   public LDC(int index) {
00022     super(de.fub.bytecode.Constants.LDC_W, index);
00023     setSize();
00024   }  
00025   /**
00026    * Call corresponding visitor method(s). The order is:
00027    * Call visitor methods of implemented interfaces first, then
00028    * call methods according to the class hierarchy in descending order,
00029    * i.e., the most specific visitXXX() call comes last.
00030    *
00031    * @param v Visitor object
00032    */
00033   public void accept(Visitor v) {
00034     v.visitStackProducer(this);
00035     v.visitPushInstruction(this);
00036     v.visitExceptionThrower(this);
00037     v.visitTypedInstruction(this);
00038     v.visitCPInstruction(this);
00039     v.visitLDC(this);
00040   }  
00041   /**
00042    * Dump instruction as byte code to stream out.
00043    * @param out Output stream
00044    */
00045   public void dump(DataOutputStream out) throws IOException {
00046     out.writeByte(tag);
00047 
00048     if(length == 2)
00049       out.writeByte(index);
00050     else // Applies for LDC_W
00051       out.writeShort(index);
00052   }  
00053   public Class[] getExceptions() {
00054     return de.fub.bytecode.ExceptionConstants.EXCS_STRING_RESOLUTION;
00055   }  
00056   public Type getType(ConstantPoolGen cpg) {
00057     switch(cpg.getConstantPool().getConstant(index).getTag()) {
00058     case de.fub.bytecode.Constants.CONSTANT_String:  return Type.STRING;
00059     case de.fub.bytecode.Constants.CONSTANT_Float:   return Type.FLOAT;
00060     case de.fub.bytecode.Constants.CONSTANT_Integer: return Type.INT;
00061     default: // Never reached
00062       throw new RuntimeException("Unknown or invalid constant type at " + index);
00063     }
00064   }  
00065   public Object getValue(ConstantPoolGen cpg) {
00066     de.fub.bytecode.classfile.Constant c = cpg.getConstantPool().getConstant(index);
00067 
00068     switch(c.getTag()) {
00069       case de.fub.bytecode.Constants.CONSTANT_String:
00070     int i = ((de.fub.bytecode.classfile.ConstantString)c).getStringIndex();
00071     c = cpg.getConstantPool().getConstant(i);
00072     return ((de.fub.bytecode.classfile.ConstantUtf8)c).getBytes();
00073 
00074     case de.fub.bytecode.Constants.CONSTANT_Float:
00075     return new Float(((de.fub.bytecode.classfile.ConstantFloat)c).getBytes());
00076 
00077     case de.fub.bytecode.Constants.CONSTANT_Integer:
00078     return new Integer(((de.fub.bytecode.classfile.ConstantInteger)c).getBytes());
00079 
00080     default: // Never reached
00081       throw new RuntimeException("Unknown or invalid constant type at " + index);
00082       }
00083   }  
00084   /**
00085    * Read needed data (e.g. index) from file.
00086    */
00087   protected void initFromFile(ByteSequence bytes, boolean wide)
00088        throws IOException
00089   {
00090     length = 2;
00091     index  = bytes.readUnsignedByte();
00092   }  
00093   /**
00094    * Set the index to constant pool and adjust size.
00095    */
00096   public final void setIndex(int index) { 
00097     super.setIndex(index);
00098     setSize();
00099   }  
00100   // Adjust to proper size
00101   protected final void setSize() {
00102     if(index <= de.fub.bytecode.Constants.MAX_BYTE) { // Fits in one byte?
00103       tag    = de.fub.bytecode.Constants.LDC;
00104       length = 2;
00105     } else {
00106       tag    = de.fub.bytecode.Constants.LDC_W;
00107       length = 3;
00108     }
00109   }  
00110 }

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