refactored functionality out of FieldGen into Type.Class.Field
[org.ibex.classgen.git] / src / org / ibex / classgen / ClassFile.java
index f70ed48..32bda60 100644 (file)
@@ -4,7 +4,7 @@ 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;
@@ -13,7 +13,7 @@ public class ClassFile implements CGConst {
     final int flags;
     
     private final Vector fields = new Vector();
-    private final Vector methods = new Vector();
+    public final Vector methods = new Vector();
     
     private final AttrGen attributes;
 
@@ -34,6 +34,7 @@ public class ClassFile implements CGConst {
     }
 
     public Type.Class getType() { return thisType; }
+    public int getFlags() { return flags; }
     
     String debugToString() { return debugToString(new StringBuffer(4096)).toString(); }
     StringBuffer debugToString(StringBuffer sb) {
@@ -63,9 +64,10 @@ 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) {
+        thisType.super();
+        this.thisType = thisType;
         if((flags & ~(PUBLIC|FINAL|SUPER|INTERFACE|ABSTRACT)) != 0)
             throw new IllegalArgumentException("invalid flags");
-        this.thisType = thisType;
         this.superType = superType;
         this.interfaces = interfaces;
         this.flags = flags;
@@ -86,7 +88,7 @@ public class ClassFile implements CGConst {
         @see CGConst
     */
     public final MethodGen addMethod(String name, Type ret, Type[] args, int flags) {
-        MethodGen mg = new MethodGen(this.getType(), name, ret, args, flags, (this.flags & INTERFACE) != 0);
+        MethodGen mg = new MethodGen(getType().method(name, ret, args), flags);
         methods.addElement(mg);
         return mg;
     }
@@ -102,7 +104,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;
     }
@@ -199,23 +201,33 @@ public class ClassFile implements CGConst {
         }
     }
 
-    ClassFile(DataInput i) throws IOException {
-        int magic = i.readInt();
+    public ClassFile(DataInput i) throws IOException { this(i, false); }
+    public ClassFile(DataInput i, boolean ssa) throws IOException {
+        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();
         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();
+        this.minor = minor;
+        this.major = major;
+        this.flags = flags;
         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.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, i, cp));
         int numMethods = i.readShort();
-        for(int j=0; j<numMethods; j++) methods.addElement(new MethodGen(this.getType(), i, cp, (this.flags & INTERFACE) != 0));
+        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);
         
         // FEATURE: Support these