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:41:37 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(Constants.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    * Escape all occurences of newline chars '\n', quotes \", etc.
00064    */
00065   private static final String convertString(String label) {
00066     char[]       ch  = label.toCharArray();
00067     StringBuffer buf = new StringBuffer();
00068 
00069     for(int i=0; i < ch.length; i++) {
00070       switch(ch[i]) {
00071       case '\n':
00072     buf.append("\\n"); break;
00073       case '\r':
00074     buf.append("\\r"); break;
00075       case '\"':
00076     buf.append("\\\""); break;
00077       case '\'':
00078     buf.append("\\'"); break;
00079       case '\\':
00080     buf.append("\\\\"); break;
00081       default:
00082     buf.append(ch[i]); break;
00083       }
00084     }
00085 
00086     return buf.toString();
00087   }  
00088   /**
00089    * @return deep copy of this attribute
00090    */
00091   public Attribute copy(ConstantPool constant_pool) {
00092     ConstantValue c = (ConstantValue)clone();
00093     c.constant_pool = constant_pool;
00094     return c;
00095   }  
00096   /**
00097    * Dump constant value attribute to file stream on binary format.
00098    *
00099    * @param file Output file stream
00100    * @throw IOException
00101    */ 
00102   public final void dump(DataOutputStream file) throws IOException
00103   {
00104     super.dump(file);
00105     file.writeShort(constantvalue_index);
00106   }  
00107   /**
00108    * @return Index in constant pool of constant value.
00109    */  
00110   public final int getConstantValueIndex() { return constantvalue_index; }  
00111   /**
00112    * @param constantvalue_index.
00113    */
00114   public final void setConstantValueIndex(int constantvalue_index) {
00115     this.constantvalue_index = constantvalue_index;
00116   }  
00117   /**
00118    * @return String representation of constant value.
00119    */ 
00120   public final String toString() throws InternalError
00121   {
00122     Constant c = constant_pool.getConstant(constantvalue_index);
00123     
00124     String   buf;
00125     int    i;
00126 
00127     // Print constant to string depending on its type
00128     switch(c.getTag()) {
00129     case Constants.CONSTANT_Long:    buf = "" + ((ConstantLong)c).getBytes();    break;
00130     case Constants.CONSTANT_Float:   buf = "" + ((ConstantFloat)c).getBytes();   break;
00131     case Constants.CONSTANT_Double:  buf = "" + ((ConstantDouble)c).getBytes();  break;
00132     case Constants.CONSTANT_Integer: buf = "" + ((ConstantInteger)c).getBytes(); break;
00133     case Constants.CONSTANT_String:  
00134       i   = ((ConstantString)c).getStringIndex();
00135       c   = constant_pool.getConstant(i, Constants.CONSTANT_Utf8);
00136       buf = "\"" + convertString(((ConstantUtf8)c).getBytes()) + "\"";
00137       break;
00138     default: throw new InternalError("Type of ConstValue invalid: " + c);
00139     }
00140 
00141     return buf;
00142   }  
00143 }

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