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

ConstantValue.java

00001 package de.fub.bytecode.classfile;
00002 
00003 import  de.fub.bytecode.Constants;
00004 import  java.io.*;
00005 
00006 /**
00007  * This class is derived from <em>Attribute</em> and represents a constant 
00008  * value, i.e. a default value for initializing a class field.
00009  * This class is instantiated by the <em>Attribute.readAttribute()</em> method.
00010  *
00011  * @version $Id: ConstantValue.java,v 1.1.1.1 2002/01/24 03:44:00 pserver Exp $
00012  * @author  <A HREF="http://www.inf.fu-berlin.de/~dahm">M. Dahm</A>
00013  * @see     Attribute
00014  */
00015 public final class ConstantValue extends Attribute {
00016   private int constantvalue_index;
00017 
00018   /**
00019    * @param name_index Name index in constant pool
00020    * @param length Content length in bytes
00021    * @param constantvalue_index Index in constant pool
00022    * @param constant_pool Array of constants
00023    */  
00024   public ConstantValue(int name_index, int length, 
00025                int constantvalue_index,
00026                ConstantPool constant_pool)
00027   {
00028     super(ATTR_CONSTANT_VALUE, name_index, length, constant_pool);
00029     this.constantvalue_index = constantvalue_index;
00030   }  
00031   /**
00032    * Construct object from file stream.
00033    * @param name_index Name index in constant pool
00034    * @param length Content length in bytes
00035    * @param file Input stream
00036    * @param constant_pool Array of constants
00037    * @throw IOException
00038    */
00039   ConstantValue(int name_index, int length, DataInputStream file,
00040         ConstantPool constant_pool) throws IOException
00041   {
00042     this(name_index, length, (int)file.readUnsignedShort(), constant_pool);
00043   }  
00044   /**
00045    * Initialize from another object. Note that both objects use the same
00046    * references (shallow copy). Use clone() for a physical copy.
00047    */
00048   public ConstantValue(ConstantValue c) {
00049     this(c.getNameIndex(), c.getLength(), c.getConstantValueIndex(),
00050      c.getConstantPool());
00051   }  
00052   /**
00053    * Called by objects that are traversing the nodes of the tree implicitely
00054    * defined by the contents of a Java class. I.e., the hierarchy of methods,
00055    * fields, attributes, etc. spawns a tree of objects.
00056    *
00057    * @param v Visitor object
00058    */
00059   public void accept(Visitor v) {
00060     v.visitConstantValue(this);
00061   }  
00062   /**
00063    * @return deep copy of this attribute
00064    */
00065   public Attribute copy(ConstantPool constant_pool) {
00066     ConstantValue c = (ConstantValue)clone();
00067     c.constant_pool = constant_pool;
00068     return c;
00069   }  
00070   /**
00071    * Dump constant value attribute to file stream on binary format.
00072    *
00073    * @param file Output file stream
00074    * @throw IOException
00075    */ 
00076   public final void dump(DataOutputStream file) throws IOException
00077   {
00078     super.dump(file);
00079     file.writeShort(constantvalue_index);
00080   }  
00081   /**
00082    * @return Index in constant pool of constant value.
00083    */  
00084   public final int getConstantValueIndex() { return constantvalue_index; }  
00085   /**
00086    * @param constantvalue_index.
00087    */
00088   public final void setConstantValueIndex(int constantvalue_index) {
00089     this.constantvalue_index = constantvalue_index;
00090   }  
00091   /**
00092    * @return String representation of constant value.
00093    */ 
00094   public final String toString() throws InternalError
00095   {
00096     Constant c = constant_pool.getConstant(constantvalue_index);
00097     
00098     String   buf;
00099     int    i;
00100 
00101     // Print constant to string depending on its type
00102     switch(c.getTag()) {
00103     case CONSTANT_Long:    buf = "" + ((ConstantLong)c).getBytes();    break;
00104     case CONSTANT_Float:   buf = "" + ((ConstantFloat)c).getBytes();   break;
00105     case CONSTANT_Double:  buf = "" + ((ConstantDouble)c).getBytes();  break;
00106     case CONSTANT_Integer: buf = "" + ((ConstantInteger)c).getBytes(); break;
00107     case CONSTANT_String:  
00108       i   = ((ConstantString)c).getStringIndex();
00109       c   = constant_pool.getConstant(i, CONSTANT_Utf8);
00110       buf = "\"" + ((ConstantUtf8)c).getBytes() + "\"";
00111       break;
00112     default: throw new InternalError("Type of ConstValue invalid: " + c);
00113     }
00114 
00115     return buf;
00116   }  
00117 }

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