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

Field.java

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

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