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

FieldOrMethod.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 fields and methods.
00008  *
00009  * @version $Id: FieldOrMethod.java,v 1.1.1.1 2002/01/24 03:41:37 pserver Exp $
00010  * @author  <A HREF="http://www.inf.fu-berlin.de/~dahm">M. Dahm</A>
00011  */
00012 public abstract class FieldOrMethod extends AccessFlags implements Cloneable {
00013   protected int          name_index;      // Points to field name in constant pool 
00014   protected int          signature_index; // Points to encoded signature
00015   protected int          attributes_count;// No. of attributes
00016   protected Attribute[]  attributes;      // Collection of attributes
00017   protected ConstantPool constant_pool;
00018 
00019   FieldOrMethod() {}  
00020   /**
00021    * @param access_flags Access rights of method
00022    * @param name_index Points to field name in constant pool
00023    * @param signature_index Points to encoded signature
00024    * @param attributes Collection of attributes
00025    * @param constant_pool Array of constants
00026    */
00027   protected FieldOrMethod(int access_flags, int name_index, int signature_index,
00028               Attribute[] attributes, ConstantPool constant_pool)
00029   {
00030     this.access_flags    = access_flags;
00031     this.name_index      = name_index;
00032     this.signature_index = signature_index;
00033     this.constant_pool   = constant_pool;
00034 
00035     setAttributes(attributes);
00036   }  
00037   /**
00038    * Initialize from another object. Note that both objects use the same
00039    * references (shallow copy). Use clone() for a physical copy.
00040    */
00041   protected FieldOrMethod(FieldOrMethod c) {
00042     this(c.getAccessFlags(), c.getNameIndex(), c.getSignatureIndex(),
00043      c.getAttributes(), c.getConstantPool());
00044   }  
00045   /**
00046    * Construct object from file stream.
00047    * @param file Input stream
00048    * @throw IOException
00049    * @throw ClassFormatError
00050    */
00051   protected FieldOrMethod(DataInputStream file, ConstantPool constant_pool)
00052     throws IOException, ClassFormatError
00053   {
00054     this(file.readUnsignedShort(), file.readUnsignedShort(),
00055      file.readUnsignedShort(), null, constant_pool);
00056 
00057     attributes_count = file.readUnsignedShort();
00058     attributes       = new Attribute[attributes_count];
00059     for(int i=0; i < attributes_count; i++)
00060       attributes[i] = Attribute.readAttribute(file, constant_pool);
00061   }  
00062   /**
00063    * @return deep copy of this field
00064    */
00065   protected FieldOrMethod copy_(ConstantPool constant_pool) {
00066     FieldOrMethod c = null;
00067 
00068     try {
00069       c = (FieldOrMethod)clone();
00070     } catch(CloneNotSupportedException e) {}
00071 
00072     c.constant_pool    = constant_pool;
00073     c.attributes       = new Attribute[attributes_count];
00074 
00075     for(int i=0; i < attributes_count; i++)
00076       c.attributes[i] = attributes[i].copy(constant_pool);
00077 
00078     return c;
00079   }  
00080   /**
00081    * Dump object to file stream on binary format.
00082    *
00083    * @param file Output file stream
00084    * @throw IOException
00085    */ 
00086   public final void dump(DataOutputStream file) throws IOException
00087   {
00088     file.writeShort(access_flags);
00089     file.writeShort(name_index);
00090     file.writeShort(signature_index);
00091     file.writeShort(attributes_count);
00092 
00093     for(int i=0; i < attributes_count; i++)
00094       attributes[i].dump(file);
00095   }  
00096   /**
00097    * @return Collection of object attributes.
00098    */   
00099   public final Attribute[] getAttributes() { return attributes; }  
00100   /**
00101    * @return Constant pool used by this object.
00102    */   
00103   public final ConstantPool getConstantPool() { return constant_pool; }  
00104   /**
00105    * @return Name of object, i.e., method name or field name
00106    */   
00107   public final String getName() {
00108     ConstantUtf8  c;
00109     c = (ConstantUtf8)constant_pool.getConstant(name_index, 
00110                         Constants.CONSTANT_Utf8);
00111     return c.getBytes();
00112   }  
00113   /**
00114    * @return Index in constant pool of object's name.
00115    */   
00116   public final int getNameIndex() { return name_index; }  
00117   /**
00118    * @return String representation of object's type signature (java style)
00119    */   
00120   public final String getSignature() {
00121     ConstantUtf8  c;
00122     c = (ConstantUtf8)constant_pool.getConstant(signature_index,
00123                         Constants.CONSTANT_Utf8);
00124     return c.getBytes();
00125   }  
00126   /**
00127    * @return Index in constant pool of field signature.
00128    */   
00129   public final int getSignatureIndex() { return signature_index; }  
00130   /**
00131    * @param attributes Collection of object attributes.
00132    */
00133   public final void setAttributes(Attribute[] attributes) {
00134     this.attributes  = attributes;
00135     attributes_count = (attributes == null)? 0 : attributes.length;
00136   }  
00137   /**
00138    * @param constant_pool Constant pool to be used for this object.
00139    */   
00140   public final void setConstantPool(ConstantPool constant_pool) {
00141     this.constant_pool = constant_pool;
00142   }  
00143   /**
00144    * @param name_index Index in constant pool of object's name.
00145    */
00146   public final void setNameIndex(int name_index) {
00147     this.name_index = name_index;
00148   }  
00149   /**
00150    * @param signature_index Index in constant pool of field signature.
00151    */
00152   public final void setSignatureIndex(int signature_index) {
00153     this.signature_index = signature_index;
00154   }  
00155 }

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