refactored functionality out of FieldGen into Type.Class.Field
authoradam <adam@megacz.com>
Sun, 3 Jul 2005 03:26:59 +0000 (03:26 +0000)
committeradam <adam@megacz.com>
Sun, 3 Jul 2005 03:26:59 +0000 (03:26 +0000)
darcs-hash:20050703032659-5007d-a306efe74ee0624a76e439ae04695295b48ffb01.gz

src/org/ibex/classgen/ClassFile.java
src/org/ibex/classgen/FieldGen.java
src/org/ibex/classgen/Type.java

index f41a44f..32bda60 100644 (file)
@@ -104,7 +104,7 @@ public class ClassFile extends Type.Class.Body {
         @see CGConst
         */  
     public final FieldGen addField(String name, Type type, int flags) {
-        FieldGen fg = new FieldGen(this, name, type, flags);
+        FieldGen fg = new FieldGen(getType().field(name, type), flags);
         fields.addElement(fg);
         return fg;
     }
@@ -223,7 +223,7 @@ public class ClassFile extends Type.Class.Body {
         interfaces = new Type.Class[i.readShort()];
         for(int j=0; j<interfaces.length; j++) interfaces[j] = (Type.Class) cp.getKeyByIndex(i.readShort());
         int numFields = i.readShort();
-        for(int j=0; j<numFields; j++) fields.addElement(new FieldGen(i, cp));
+        for(int j=0; j<numFields; j++) fields.addElement(new FieldGen(this, i, cp));
         int numMethods = i.readShort();
         for(int j=0; j<numMethods; j++) methods.addElement(ssa 
                                                            ? new JSSA(this.getType(), i, cp) 
index 1e7373d..7f5242c 100644 (file)
@@ -4,59 +4,51 @@ import java.io.*;
 
 /** Class representing a field in a generated classfile
     @see ClassFile#addField */
-public class FieldGen implements CGConst {
-    private final String name;
-    private final Type type;
-    private final int flags;
+public class FieldGen extends Type.Class.Field.Body {
     private final ClassFile.AttrGen attrs;
 
     StringBuffer debugToString(StringBuffer sb) {
-        sb.append(ClassFile.flagsToString(flags,false));
-        sb.append(type.debugToString());
+        sb.append(ClassFile.flagsToString(flags, false));
+        sb.append(getField().getType().debugToString());
         sb.append(" ");
-        sb.append(name);
+        sb.append(getField().getName());
         if(attrs.contains("ConstantValue"))
             sb.append(" = \"").append(attrs.get("ConstantValue")).append("\"");
         sb.append(";");
         return sb;
     }
     
-    FieldGen(DataInput in, ConstantPool cp) throws IOException {
-        flags = in.readShort();
-        if((flags & ~VALID_FIELD_FLAGS) != 0)
-            throw new ClassFile.ClassReadExn("invalid flags");        
-        name = cp.getUtf8KeyByIndex(in.readShort());
-        type = Type.fromDescriptor(cp.getUtf8KeyByIndex(in.readShort()));
-        attrs = new ClassFile.AttrGen(in,cp);
+    FieldGen(ClassFile cf, DataInput in, ConstantPool cp) throws IOException {
+        this(in.readShort(),
+             cf.getType().field(cp.getUtf8KeyByIndex(in.readShort()),
+                                Type.fromDescriptor(cp.getUtf8KeyByIndex(in.readShort()))));
     }
 
-    FieldGen(ClassFile owner, String name, Type type, int flags) {
-        if((flags & ~VALID_FIELD_FLAGS) != 0)
-            throw new IllegalArgumentException("invalid flags");
-        this.name = name;
-        this.type = type;
-        this.flags = flags;
-        this.attrs = new ClassFile.AttrGen();        
+    private FieldGen(int flags, Type.Class.Field field) { this(field, flags); }
+    FieldGen(Type.Class.Field field, int flags) { this(field, flags, new ClassFile.AttrGen()); }
+    FieldGen(Type.Class.Field field, int flags, ClassFile.AttrGen attrs) {
+        field.super(flags);
+        this.attrs = attrs;
    }
     
     /** 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 & STATIC) == 0) throw new IllegalStateException("field does not have the STATIC bit set");
+        if (!isStatic()) throw new IllegalStateException("field does not have the STATIC bit set");
         attrs.put("ConstantValue",val);
     }
     
     void finish(ConstantPool cp) {
-        cp.addUtf8(name);
-        cp.addUtf8(type.getDescriptor());
+        cp.addUtf8(getField().getName());
+        cp.addUtf8(getField().getType().getDescriptor());
         
         attrs.finish(cp);
     }
     
     void dump(DataOutput o, ConstantPool cp) throws IOException {
-        o.writeShort(flags);
-        o.writeShort(cp.getUtf8Index(name));
-        o.writeShort(cp.getUtf8Index(type.getDescriptor()));
+        o.writeShort(getFlags());
+        o.writeShort(cp.getUtf8Index(getField().getName()));
+        o.writeShort(cp.getUtf8Index(getField().getType().getDescriptor()));
         attrs.dump(o,cp);
     }
 }
index 405bdbb..08c346f 100644 (file)
@@ -163,12 +163,16 @@ public abstract class Type implements CGConst {
             private Field(String name, Type t) { super(name); this.type = t; }
             public String getTypeDescriptor() { return type.getDescriptor(); }
             public Type getType() { return type; }
+            public String debugToString() { return getDeclaringClass().debugToString()+"."+name+"["+type.debugToString()+"]"; }
             public class Body extends HasFlags {
                 public final int flags;
-                public Body(int flags) { this.flags = flags; }
+                public Body(int flags) {
+                    if ((flags & ~VALID_FIELD_FLAGS) != 0) throw new IllegalArgumentException("invalid flags");
+                    this.flags = flags;
+                }
                 public int getFlags() { return flags; }
+                public Field getField() { return Field.this; }
             }
-            public String debugToString() { return getDeclaringClass().debugToString()+"."+name+"["+type.debugToString()+"]"; }
         }
 
         public class Method extends Member {