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

Attribute.java

00001 package de.fub.bytecode.classfile;
00002 
00003 import  de.fub.bytecode.Constants;
00004 import  java.io.*;
00005 
00006 /**
00007  * Abstract super class for <em>Attribute</em> objects. Currently the
00008  * <em>ConstantValue</em>, <em>SourceFile</em>, <em>Code</em>,
00009  * <em>Exceptiontable</em>, <em>LineNumberTable</em>, 
00010  * <em>LocalVariableTable</em>, <em>InnerClasses</em> and
00011  * <em>Synthetic</em> attributes are supported. The
00012  * <em>Unknown</em> attribute stands for non-standard-attributes.
00013  *
00014  * @version $Id: Attribute.java,v 1.1.1.1 2002/01/24 03:44:00 pserver Exp $
00015  * @author  <A HREF="http://www.inf.fu-berlin.de/~dahm">M. Dahm</A>
00016  * @see     ConstantValue
00017  * @see     SourceFile
00018  * @see     Code
00019  * @see     Unknown
00020  * @see     ExceptionTable
00021  * @see     LineNumberTable
00022  * @see     LocalVariableTable 
00023  * @see     InnerClasses
00024  * @see     Synthetic
00025  * @see     Deprecated
00026 */
00027 public abstract class Attribute implements Constants, Cloneable
00028 {
00029   protected int          name_index; // Points to attribute name in constant pool
00030   protected int          length;     // Content length of attribute field
00031   protected byte         tag;        // Tag to distiguish subclasses
00032   protected ConstantPool constant_pool;
00033 
00034   Attribute(byte tag, int name_index, int length, ConstantPool constant_pool) {
00035     this.tag           = tag;
00036     this.name_index    = name_index;
00037     this.length        = length;
00038     this.constant_pool = constant_pool;
00039   }  
00040   /**
00041    * Called by objects that are traversing the nodes of the tree implicitely
00042    * defined by the contents of a Java class. I.e., the hierarchy of methods,
00043    * fields, attributes, etc. spawns a tree of objects.
00044    *
00045    * @param v Visitor object
00046    */
00047   public abstract void accept(Visitor v);  
00048   /**
00049    * Use copy() if you want to have a deep copy(), i.e. with all references
00050    * copied correctly.
00051    *
00052    * @return shallow copy of this attribute
00053    */
00054   public Object clone() {
00055     Object o = null;
00056 
00057     try {
00058       o = super.clone();
00059     } catch(CloneNotSupportedException e) {
00060       e.printStackTrace(); // Never occurs
00061     }
00062 
00063     return o;
00064   }  
00065   /**
00066    * @return deep copy of this attribute
00067    */
00068   public abstract Attribute copy(ConstantPool constant_pool);  
00069   /**
00070    * Dump attribute to file stream in binary format.
00071    *
00072    * @param file Output file stream
00073    * @throw IOException
00074    */
00075   public void dump(DataOutputStream file) throws IOException
00076   {
00077     file.writeShort(name_index);
00078     file.writeInt(length);
00079   }  
00080   /**
00081    * @return Constant pool used by this object.
00082    * @see ConstantPool
00083    */   
00084   public final ConstantPool getConstantPool() { return constant_pool; }  
00085   /**
00086    * @return Length of attribute field in bytes.
00087    */  
00088   public final int   getLength()    { return length; }  
00089   /**
00090    * @return Name index in constant pool of attribute name.
00091    */  
00092   public final int getNameIndex() { return name_index; }  
00093   /**
00094    * @return Tag of attribute, i.e. its type. Value may not be altered, thus
00095    * there is no setTag() method.
00096    */
00097   public final byte  getTag()       { return tag; }  
00098   /* Class method reads one attribute from the input data stream.
00099    * This method must not be accessible from the outside.
00100    * It is called by the <A HREF="DE.fub.inf.JavaClass.Field.html">
00101    * Field</A> and <A HREF="DE.fub.inf.JavaClass.Method.html">
00102    * Method</A> constructor methods.
00103    *
00104    * @see    Field
00105    * @see    Method
00106    * @param  file Input stream
00107    * @param  constant_pool Array of constants
00108    * @return Attribute
00109    * @throw  IOException
00110    * @throw  ClassFormatError
00111    * @throw  InternalError
00112    */
00113   static final Attribute readAttribute(DataInputStream file,
00114                        ConstantPool constant_pool)
00115     throws IOException, ClassFormatError, InternalError
00116   {
00117     ConstantUtf8 c;
00118     String       name;
00119     int          name_index;
00120     int          length;
00121     byte         tag = ATTR_UNKNOWN; // Unknown attribute
00122 
00123     // Get class name from constant pool via `name_index' indirection
00124     name_index = (int)(file.readUnsignedShort());
00125     c          = (ConstantUtf8)constant_pool.getConstant(name_index, 
00126                              CONSTANT_Utf8);
00127     name       = c.getBytes();
00128 
00129     // Length of data in bytes
00130     length     = file.readInt();
00131 
00132     // Compare strings to find known attribute
00133     for(byte i=0; i < KNOWN_ATTRIBUTES; i++) {
00134       if(name.equals(ATTRIBUTE_NAMES[i])) {
00135     tag = i; // found!
00136     break;
00137       }
00138     }
00139 
00140     // Call proper constructor, depending on `tag'
00141     switch(tag) {
00142     case ATTR_UNKNOWN:
00143       return new Unknown(name_index, length, file, constant_pool);
00144 
00145     case ATTR_CONSTANT_VALUE:
00146       return new ConstantValue(name_index, length, file, constant_pool);
00147 
00148     case ATTR_SOURCE_FILE:
00149       return new SourceFile(name_index, length, file, constant_pool);
00150       
00151     case ATTR_CODE:
00152       return new Code(name_index, length, file, constant_pool);
00153       
00154     case ATTR_EXCEPTIONS:
00155       return new ExceptionTable(name_index, length, file, constant_pool);
00156       
00157     case ATTR_LINE_NUMBER_TABLE:
00158       return new LineNumberTable(name_index, length, file, constant_pool);
00159       
00160     case ATTR_LOCAL_VARIABLE_TABLE:
00161       return new LocalVariableTable(name_index, length, file, constant_pool);
00162 
00163     case ATTR_INNER_CLASSES:
00164       return new InnerClasses(name_index, length, file, constant_pool);
00165 
00166     case ATTR_SYNTHETIC:
00167       return new Synthetic(name_index, length, file, constant_pool);
00168 
00169     case ATTR_DEPRECATED:
00170       return new Deprecated(name_index, length, file, constant_pool);
00171 
00172     case ATTR_PMG:
00173       return new PMGClass(name_index, length, file, constant_pool);
00174 
00175     case ATTR_SIGNATURE:
00176       return new Signature(name_index, length, file, constant_pool);
00177 
00178     default: // Never reached
00179       throw new InternalError("Ooops! default case reached.");
00180     }
00181   }  
00182   /**
00183    * @param constant_pool Constant pool to be used for this object.
00184    * @see ConstantPool
00185    */   
00186   public final void setConstantPool(ConstantPool constant_pool) {
00187     this.constant_pool = constant_pool;
00188   }  
00189   /**
00190    * @param Attribute length in bytes.
00191    */
00192   public final void setLength(int length) {
00193     this.length = length;
00194   }  
00195   /**
00196    * @param name_index of attribute.
00197    */
00198   public final void setNameIndex(int name_index) {
00199     this.name_index = name_index;
00200   }  
00201   /**
00202    * @return attribute name.
00203    */ 
00204   public String toString() {
00205     return ATTRIBUTE_NAMES[tag];
00206   }  
00207 }

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