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

FieldGen.java

00001 package de.fub.bytecode.generic;
00002 
00003 import de.fub.bytecode.Constants;
00004 import de.fub.bytecode.classfile.*;
00005 import java.util.Vector;
00006 
00007 /** 
00008  * Template class for building up a field.  The only reasonable thing
00009  * one can do is a constant value attribute to a field which must of
00010  * course be compatible with to the declared type.
00011  *
00012  * @version $Id: FieldGen.java,v 1.1.1.1 2002/01/24 03:44:03 pserver Exp $
00013  * @author  <A HREF="http://www.inf.fu-berlin.de/~dahm">M. Dahm</A>
00014  * @see Field
00015  */
00016 public class FieldGen extends AccessFlags implements Constants {
00017   private Type            type;
00018   private String          name;
00019   private ConstantPoolGen cp;
00020   private int             index = -1;
00021   private Vector          attributes;
00022 
00023   /**
00024    * Declare a field. If it is a static field (access_flags & ACC_STATIC != 0) it
00025    * may have an initial value associated with it as defined by setInitValue().
00026    *
00027    * @param access_flags access qualifiers
00028    * @param type  field type
00029    * @param name field name
00030    * @param cp constant pool
00031    */
00032   public FieldGen(int access_flags, Type type, String name, ConstantPoolGen cp) {
00033     this.access_flags = access_flags;
00034     this.type         = type;
00035     this.name         = name;
00036     this.cp           = cp;
00037   }  
00038   public FieldGen(Field field, ConstantPoolGen cp) {
00039     this(field.getAccessFlags(), Type.getType(field.getSignature()), field.getName(), cp);
00040   }  
00041   public void addAttribute(Attribute attr) {
00042     if(attributes == null)
00043       attributes = new Vector();
00044 
00045     attributes.addElement(attr);
00046   }  
00047   public Attribute[] getAttributes() {
00048     if(attributes != null) {
00049       Attribute[] attrs = new Attribute[attributes.size()];
00050       attributes.copyInto(attrs);
00051       return attrs;
00052     } else
00053       return null;
00054   }  
00055   public ConstantPoolGen getConstantPool()         { return cp; }  
00056   /**
00057    * Get method object.
00058    */
00059   public Field getField() {
00060     String      signature       = type.getSignature();
00061     int         name_index      = cp.addUtf8(name);
00062     int         signature_index = cp.addUtf8(signature);
00063 
00064     if(index > 0)
00065       if(isStatic())
00066     addAttribute(new ConstantValue(cp.addUtf8("ConstantValue"),
00067                        2, index, cp.getConstantPool()));
00068       else
00069     throw new ClassGenException("Only static fields may have an initial value!");
00070 
00071     return new Field(access_flags, name_index, signature_index, getAttributes(),
00072              cp.getConstantPool());
00073   }  
00074   public String getName()            { return name; }  
00075   public String getSignature() { return type.getSignature(); }  
00076   public Type   getType()            { return type; }  
00077   public void setConstantPool(ConstantPoolGen cp) { this.cp = cp; }  
00078   public void setInitValue(byte b)     { if(b != 0)      index = cp.addInteger(b); }  
00079   public void setInitValue(char c)     { if(c != 0)      index = cp.addInteger(c); }  
00080   public void setInitValue(double d)   { if(d != 0.0)    index = cp.addDouble(d); }  
00081   public void setInitValue(float f)    { if(f != 0.0)    index = cp.addFloat(f); }  
00082   public void setInitValue(int i)      { if(i != 0)      index = cp.addInteger(i); }  
00083   public void setInitValue(long l)     { if(l != 0L)     index = cp.addLong(l); }  
00084   /**
00085    * Set (optional) initial value of field, otherwise it will be set to null/0/false
00086    * by the JVM automatically.
00087    */
00088   public void setInitValue(String str) { if(str != null) index = cp.addString(str); }  
00089   public void setInitValue(short s)    { if(s != 0)      index = cp.addInteger(s); }  
00090   public void setInitValue(boolean b)  { if(b)           index = cp.addInteger(1); }  
00091   public void   setName(String name) { this.name = name; }  
00092   public void   setType(Type type)   { this.type = type; }  
00093 }

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