refactored tons of functionality into Class.Body and HasAttributes
[org.ibex.classgen.git] / src / org / ibex / classgen / ClassFile.java
index dd8b424..70c447b 100644 (file)
@@ -4,19 +4,16 @@ import java.util.*;
 import java.io.*;
 
 /** Class generation object representing the whole classfile */
-public class ClassFile implements CGConst {
+public class ClassFile extends Type.Class.Body {
     private final Type.Class thisType;
     private final Type.Class superType;
     private final Type.Class[] interfaces;
     private final short minor;
     private final short major;
-    final int flags;
     
     private final Vector fields = new Vector();
     public final Vector methods = new Vector();
     
-    private final AttrGen attributes;
-
     static String flagsToString(int flags, boolean isClass) {
         StringBuffer sb = new StringBuffer(32);
         if ((flags & PUBLIC) != 0)       sb.append("public ");
@@ -46,7 +43,7 @@ public class ClassFile implements CGConst {
         for(int i=0; i<interfaces.length; i++) sb.append((i==0?" ":", ")+interfaces[i].debugToString());
         sb.append(" {");
         sb.append(" // v"+major+"."+minor);
-        ConstantPool.Utf8Key sourceFile = (ConstantPool.Utf8Key) attributes.get("SourceFile");
+        ConstantPool.Utf8Key sourceFile = (ConstantPool.Utf8Key) attrs.get("SourceFile");
         if (sourceFile != null) sb.append(" from " + sourceFile.s);
         sb.append("\n");
         for(int i=0; i<fields.size(); i++) {
@@ -64,15 +61,12 @@ public class ClassFile implements CGConst {
 
     public ClassFile(Type.Class thisType, Type.Class superType, int flags) { this(thisType, superType, flags, null); }
     public ClassFile(Type.Class thisType, Type.Class superType, int flags, Type.Class[] interfaces) {
-        if((flags & ~(PUBLIC|FINAL|SUPER|INTERFACE|ABSTRACT)) != 0)
-            throw new IllegalArgumentException("invalid flags");
+        thisType.super(flags, new AttrGen());
         this.thisType = thisType;
         this.superType = superType;
         this.interfaces = interfaces;
-        this.flags = flags;
         this.minor = 3;
         this.major = 45;        
-        this.attributes = new AttrGen();
     }
     
     /** Adds a new method to this class 
@@ -103,7 +97,7 @@ public class ClassFile implements CGConst {
         @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;
     }
@@ -111,7 +105,7 @@ public class ClassFile implements CGConst {
     /** Sets the source value of the SourceFile attribute of this class 
         @param sourceFile The string to be uses as the SourceFile of this class
     */
-    public void setSourceFile(String sourceFile) { attributes.put("SourceFile", new ConstantPool.Utf8Key(sourceFile)); }
+    public void setSourceFile(String sourceFile) { attrs.put("SourceFile", new ConstantPool.Utf8Key(sourceFile)); }
     
     /** Writes the classfile data to the file specifed
         @see ClassFile#dump(OutputStream)
@@ -156,7 +150,7 @@ public class ClassFile implements CGConst {
         if(interfaces != null) for(int i=0;i<interfaces.length;i++) cp.add(interfaces[i]);
         for(int i=0;i<methods.size();i++) ((MethodGen)methods.elementAt(i)).finish(cp);
         for(int i=0;i<fields.size();i++) ((FieldGen)fields.elementAt(i)).finish(cp);
-        attributes.finish(cp);
+        attrs.finish(cp);
         
         cp.optimize();
         cp.seal();
@@ -180,7 +174,7 @@ public class ClassFile implements CGConst {
         o.writeShort(methods.size()); // methods_count
         for(int i=0;i<methods.size();i++) ((MethodGen)methods.elementAt(i)).dump(o,cp); // methods
         
-        attributes.dump(o,cp); // attributes        
+        attrs.dump(o,cp); // attributes        
     }
     
     public static ClassFile read(String s) throws IOException { return read(new File(s)); }
@@ -202,30 +196,34 @@ public class ClassFile implements CGConst {
 
     public ClassFile(DataInput i) throws IOException { this(i, false); }
     public ClassFile(DataInput i, boolean ssa) throws IOException {
-        int magic = i.readInt();
+        this(i.readInt(), i.readShort(), i.readShort(), new ConstantPool(i), i.readShort(), i, ssa);
+    }
+    private ClassFile(int magic, short minor, short major, ConstantPool cp,
+                      short flags, DataInput i, boolean ssa) throws IOException {
+        this(magic, minor, major, cp, flags, (Type.Class)cp.getKeyByIndex(i.readShort()), i, ssa);
+    }
+    private ClassFile(int magic, short minor, short major, ConstantPool cp, short flags,
+                      Type.Class thisType, DataInput i, boolean ssa) throws IOException {
+        thisType.super(flags, null);
         if (magic != 0xcafebabe) throw new ClassReadExn("invalid magic: " + Long.toString(0xffffffffL & magic, 16));
-        minor = i.readShort();
-        major = i.readShort();
-        ConstantPool cp = new ConstantPool(i);
-        flags = i.readShort();
-        if((flags & ~(PUBLIC|FINAL|SUPER|INTERFACE|ABSTRACT)) != 0)
-            throw new ClassReadExn("invalid flags: " + Integer.toString(flags,16));
-        thisType = (Type.Class) cp.getKeyByIndex(i.readShort());
+        this.minor = minor;
+        this.major = major;
+        this.thisType = thisType;
         superType = (Type.Class) cp.getKeyByIndex(i.readShort());
         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.getType(), i, cp));
         int numMethods = i.readShort();
         for(int j=0; j<numMethods; j++) methods.addElement(ssa 
                                                            ? new JSSA(this.getType(), i, cp) 
                                                            : new MethodGen(this.getType(), i, cp));
-        attributes = new AttrGen(i, cp);
+        readAttributes(i, cp);
         
         // FEATURE: Support these
         // NOTE: Until we can support them properly we HAVE to delete them,
         //       they'll be incorrect after we rewrite the constant pool, etc
-        attributes.remove("InnerClasses");
+        attrs.remove("InnerClasses");
     }
     
     /** Thrown when class generation fails for a reason not under the control of the user