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

Field.java

00001 package de.fub.bytecode.classfile;
00002 
00003 import java.io.*;
00004 
00005 /**
00006  * This class represents the field info structure, i.e. the representation 
00007  * for a variable in the class. See JVM specification for details.
00008  *
00009  * @version $Id: Field.java,v 1.1.1.1 2002/01/24 03:44:01 pserver Exp $
00010  * @author  <A HREF="http://www.inf.fu-berlin.de/~dahm">M. Dahm</A>
00011  */
00012 public final class Field extends FieldOrMethod {
00013   /**
00014    * @param access_flags Access rights of field
00015    * @param name_index Points to field name in constant pool
00016    * @param signature_index Points to encoded signature
00017    * @param attributes Collection of attributes
00018    * @param constant_pool Array of constants
00019    */
00020   public Field(int access_flags, int name_index, int signature_index,
00021            Attribute[] attributes, ConstantPool constant_pool)
00022   {
00023     super(access_flags, name_index, signature_index, attributes, constant_pool);
00024   }  
00025   /**
00026    * Initialize from another object. Note that both objects use the same
00027    * references (shallow copy). Use clone() for a physical copy.
00028    */
00029   public Field(Field c) {
00030     super(c);
00031   }  
00032   /**
00033    * Construct object from file stream.
00034    * @param file Input stream
00035    */
00036   Field(DataInputStream file, ConstantPool constant_pool)
00037        throws IOException, ClassFormatError
00038   {
00039     super(file, constant_pool);
00040   }  
00041   /**
00042    * Called by objects that are traversing the nodes of the tree implicitely
00043    * defined by the contents of a Java class. I.e., the hierarchy of methods,
00044    * fields, attributes, etc. spawns a tree of objects.
00045    *
00046    * @param v Visitor object
00047    */
00048   public void accept(Visitor v) {
00049     v.visitField(this);
00050   }  
00051   /**
00052    * @return deep copy of this field
00053    */
00054   public final Field copy(ConstantPool constant_pool) {
00055     return (Field)copy_(constant_pool);
00056   }  
00057   /**
00058    * @return constant value associated with this field (may be null)
00059    */
00060   public final ConstantValue getConstantValue() {
00061     for(int i=0; i < attributes_count; i++)
00062       if(attributes[i].getTag() == ATTR_CONSTANT_VALUE)
00063     return (ConstantValue)attributes[i];
00064 
00065     return null;
00066   }  
00067   /**
00068    * Return string representation close to declaration format,
00069    * `public static final short MAX = 100', e.g..
00070    *
00071    * @return String representation of field, including the signature.
00072    */
00073   public final String toString() {
00074     String name, signature, access; // Short cuts to constant pool
00075 
00076     // Get names from constant pool
00077     access    = Utility.accessToString(access_flags);
00078     access    = access.equals("")? "" : (access + " ");
00079     signature = Utility.signatureToString(getSignature());
00080     name      = getName();
00081 
00082     StringBuffer  buf = new StringBuffer(access + signature + " " + name);
00083     ConstantValue cv  = getConstantValue();
00084 
00085     if(cv != null)
00086       buf.append(" = " + cv);
00087 
00088     for(int i=0; i < attributes_count; i++) {
00089       Attribute a = attributes[i];
00090 
00091       if(!(a instanceof ConstantValue))
00092     buf.append(" <" + a.toString() + ">");
00093     }
00094 
00095     return buf.toString();
00096   }  
00097 }

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