brians changes to FieldGen
authoradam <adam@megacz.com>
Mon, 27 Jun 2005 08:11:56 +0000 (08:11 +0000)
committeradam <adam@megacz.com>
Mon, 27 Jun 2005 08:11:56 +0000 (08:11 +0000)
darcs-hash:20050627081156-5007d-9f6348a2b3e823e75757534b177c2a39666613f9.gz

src/org/ibex/classgen/FieldGen.java

index ef4edcb..b65cb09 100644 (file)
@@ -5,62 +5,58 @@ import java.io.*;
 /** Class representing a field in a generated classfile
     @see ClassFile#addField */
 public class FieldGen implements CGConst {
 /** Class representing a field in a generated classfile
     @see ClassFile#addField */
 public class FieldGen implements CGConst {
-    private final ConstantPool cp;
     private final String name;
     private final Type type;
     private final int flags;
     private final ClassFile.AttrGen attrs;
     private final String name;
     private final Type type;
     private final int flags;
     private final ClassFile.AttrGen attrs;
-    
-    private Object constantValue;
 
 
-    public String toString() { StringBuffer sb = new StringBuffer(); toString(sb); return sb.toString(); }
-    public void   toString(StringBuffer sb) {
-        sb.append(ClassFile.flagsToString(flags));
-        sb.append(type);
+    StringBuffer debugToString(StringBuffer sb) {
+        sb.append(ClassFile.flagsToString(flags,false));
+        sb.append(type.debugToString());
         sb.append(" ");
         sb.append(name);
         sb.append(" ");
         sb.append(name);
+        if(attrs.contains("ConstantValue"))
+            sb.append(" = \"").append(attrs.get("ConstantValue")).append("\"");
         sb.append(";");
         sb.append(";");
-        // FIXME: attrs
+        return sb;
     }
     
     }
     
-    FieldGen(ConstantPool cp, DataInput in) throws IOException {
-        this.cp = cp;
+    FieldGen(DataInput in, ConstantPool cp) throws IOException {
         flags = in.readShort();
         flags = in.readShort();
-        name = cp.getUtf8ByIndex(in.readShort());
-        type = cp.getType(in.readShort());
-        attrs = new ClassFile.AttrGen(cp, in);
+        if((flags & ~(ACC_PUBLIC|ACC_PRIVATE|ACC_PROTECTED|ACC_VOLATILE|ACC_TRANSIENT|ACC_STATIC|ACC_FINAL)) != 0)
+            throw new ClassFile.ClassReadExn("invalid flags");        
+        name = cp.getUtf8KeyByIndex(in.readShort());
+        type = Type.instance(cp.getUtf8KeyByIndex(in.readShort()));
+        attrs = new ClassFile.AttrGen(in,cp);
     }
 
     FieldGen(ClassFile owner, String name, Type type, int flags) {
         if((flags & ~(ACC_PUBLIC|ACC_PRIVATE|ACC_PROTECTED|ACC_VOLATILE|ACC_TRANSIENT|ACC_STATIC|ACC_FINAL)) != 0)
             throw new IllegalArgumentException("invalid flags");
     }
 
     FieldGen(ClassFile owner, String name, Type type, int flags) {
         if((flags & ~(ACC_PUBLIC|ACC_PRIVATE|ACC_PROTECTED|ACC_VOLATILE|ACC_TRANSIENT|ACC_STATIC|ACC_FINAL)) != 0)
             throw new IllegalArgumentException("invalid flags");
-        this.cp = owner.cp;
         this.name = name;
         this.type = type;
         this.flags = flags;
         this.name = name;
         this.type = type;
         this.flags = flags;
-        this.attrs = new ClassFile.AttrGen(cp);
-        
-        cp.addUtf8(name);
-        cp.addUtf8(type.getDescriptor());
-    }
+        this.attrs = new ClassFile.AttrGen();        
+   }
     
     /** Sets the ContantValue attribute for this field. 
         @param val The value to set this field to. Must be an Integer, Long, Float, Double, or String */
     public void setConstantValue(Object val) {
         if((flags & ACC_STATIC) == 0) throw new IllegalStateException("field does not have the ACC_STATIC bit set");
     
     /** Sets the ContantValue attribute for this field. 
         @param val The value to set this field to. Must be an Integer, Long, Float, Double, or String */
     public void setConstantValue(Object val) {
         if((flags & ACC_STATIC) == 0) throw new IllegalStateException("field does not have the ACC_STATIC bit set");
-        constantValue = val;
+        attrs.put("ConstantValue",val);
     }
     
     }
     
-    void finish() {
-        if(constantValue != null && !attrs.contains("ConstantValue"))
-            attrs.add("ConstantValue", cp.add(constantValue));
+    void finish(ConstantPool cp) {
+        cp.addUtf8(name);
+        cp.addUtf8(type.getDescriptor());
+        
+        attrs.finish(cp);
     }
     
     }
     
-    void dump(DataOutput o) throws IOException {
+    void dump(DataOutput o, ConstantPool cp) throws IOException {
         o.writeShort(flags);
         o.writeShort(cp.getUtf8Index(name));
         o.writeShort(cp.getUtf8Index(type.getDescriptor()));
         o.writeShort(flags);
         o.writeShort(cp.getUtf8Index(name));
         o.writeShort(cp.getUtf8Index(type.getDescriptor()));
-        o.writeShort(attrs.size());
-        attrs.dump(o);
+        attrs.dump(o,cp);
     }
 }
     }
 }