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

Constant.java

00001 package de.fub.bytecode.classfile;
00002 
00003 import  de.fub.bytecode.Constants;
00004 import  java.io.*;
00005 
00006 /**
00007  * Abstract superclass for classes to represent the different constant types
00008  * in the constant pool of a class file. The classes keep closely to
00009  * the JVM specification.
00010  *
00011  * @version $Id: Constant.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     ConstantClass
00014  * @see     ConstantFieldref
00015  * @see     ConstantMethodref
00016  * @see     ConstantInterfaceMethodref
00017  * @see     ConstantString
00018  * @see     ConstantInteger
00019  * @see     ConstantFloat
00020  * @see     ConstantLong
00021  * @see     ConstantDouble
00022  * @see     ConstantNameAndType
00023  * @see     ConstantUtf8
00024  */
00025 public abstract class Constant implements Cloneable {
00026   /* In fact this tag is redundant since we can distinguish different
00027    * `Constant' objects by their type, i.e., via `instanceof'. In some
00028    * places we will use the tag for switch()es anyway.
00029    *
00030    * First, we want match the specification as closely as possible. Second we
00031    * need the tag as an index to select the corresponding class name from the 
00032    * `CONSTANT_NAMES' array.
00033    */
00034   protected byte tag;
00035 
00036   Constant(byte tag) { this.tag = tag; }  
00037   /**
00038    * Called by objects that are traversing the nodes of the tree implicitely
00039    * defined by the contents of a Java class. I.e., the hierarchy of methods,
00040    * fields, attributes, etc. spawns a tree of objects.
00041    *
00042    * @param v Visitor object
00043    */
00044   public abstract void accept(Visitor v);  
00045   public Object clone() throws CloneNotSupportedException {
00046     return super.clone();
00047   }  
00048   /**
00049    * @return deep copy of this constant
00050    */
00051   public Constant copy() {
00052     try {
00053       return (Constant)super.clone();
00054     } catch(CloneNotSupportedException e) {}
00055 
00056     return null;
00057   }  
00058   public abstract void dump(DataOutputStream file) throws IOException;  
00059   /**
00060    * @return Tag of constant, i.e., its type. No setTag() method to avoid
00061    * confusion.
00062    */
00063   public final byte getTag() { return tag; }  
00064   /**
00065    * Read one constant from the given file, the type depends on a tag byte.
00066    *
00067    * @param file Input stream
00068    * @return Constant object
00069    */
00070   static final Constant readConstant(DataInputStream file)
00071     throws IOException, ClassFormatError
00072   {
00073     byte b = file.readByte(); // Read tag byte
00074 
00075     switch(b) {
00076     case Constants.CONSTANT_Class:              return new ConstantClass(file);
00077     case Constants.CONSTANT_Fieldref:           return new ConstantFieldref(file);
00078     case Constants.CONSTANT_Methodref:          return new ConstantMethodref(file);
00079     case Constants.CONSTANT_InterfaceMethodref: return new 
00080                     ConstantInterfaceMethodref(file);
00081     case Constants.CONSTANT_String:             return new ConstantString(file);
00082     case Constants.CONSTANT_Integer:            return new ConstantInteger(file);
00083     case Constants.CONSTANT_Float:              return new ConstantFloat(file);
00084     case Constants.CONSTANT_Long:               return new ConstantLong(file);
00085     case Constants.CONSTANT_Double:             return new ConstantDouble(file);
00086     case Constants.CONSTANT_NameAndType:        return new ConstantNameAndType(file);
00087     case Constants.CONSTANT_Utf8:               return new ConstantUtf8(file);
00088     default:                          
00089       throw new ClassFormatError("Invalid byte tag in constant pool: " + b);
00090     }
00091   }  
00092   /**
00093    * @return String representation.
00094    */  
00095   public String toString() {
00096     return Constants.CONSTANT_NAMES[tag] + "[" + tag + "]";
00097   }  
00098 }

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