resolve compile errors
[org.ibex.classgen.git] / src / org / ibex / classgen / ClassFile.java
index dd2247c..46f5e3e 100644 (file)
@@ -4,57 +4,67 @@ 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;
+    final Type.Class superType;
+    final Type.Class[] interfaces;
     private final short minor;
     private final short major;
-    final int flags;
     
     private final Vector fields = new Vector();
     private final Vector methods = new Vector();
-    
-    private final AttrGen attributes;
 
+    public Type.Class.Method.Body[] methods() {
+        Type.Class.Method.Body[] ret = new Type.Class.Method.Body[methods.size()];
+        methods.copyInto(ret);
+        return ret;
+    }
+
+    public Type.Class.Field.Body[] fields() {
+        Type.Class.Field.Body[] ret = new Type.Class.Field.Body[fields.size()];
+        fields.copyInto(ret);
+        return ret;
+    }
+    
     static String flagsToString(int flags, boolean isClass) {
         StringBuffer sb = new StringBuffer(32);
-        if ((flags & ACC_PUBLIC) != 0)       sb.append("public ");
-        if ((flags & ACC_PRIVATE) != 0)      sb.append("private ");
-        if ((flags & ACC_PROTECTED) != 0)    sb.append("protected ");
-        if ((flags & ACC_STATIC) != 0)       sb.append("static ");
-        if ((flags & ACC_FINAL) != 0)        sb.append("final ");
-        if ((flags & ACC_ABSTRACT) != 0)     sb.append("abstract ");
-        if (!isClass && (flags & ACC_SYNCHRONIZED) != 0) sb.append("synchronized ");
-        if (!isClass && (flags & ACC_NATIVE) != 0)       sb.append("native ");
-        if (!isClass && (flags & ACC_STRICT) != 0)       sb.append("strictfp ");
-        if (!isClass && (flags & ACC_VOLATILE) != 0)     sb.append("volatile ");
-        if (!isClass && (flags & ACC_TRANSIENT) != 0)    sb.append("transient ");
+        if ((flags & PUBLIC) != 0)       sb.append("public ");
+        if ((flags & PRIVATE) != 0)      sb.append("private ");
+        if ((flags & PROTECTED) != 0)    sb.append("protected ");
+        if ((flags & STATIC) != 0)       sb.append("static ");
+        if ((flags & FINAL) != 0)        sb.append("final ");
+        if ((flags & ABSTRACT) != 0)     sb.append("abstract ");
+        if (!isClass && (flags & SYNCHRONIZED) != 0) sb.append("synchronized ");
+        if (!isClass && (flags & NATIVE) != 0)       sb.append("native ");
+        if (!isClass && (flags & STRICT) != 0)       sb.append("strictfp ");
+        if (!isClass && (flags & VOLATILE) != 0)     sb.append("volatile ");
+        if (!isClass && (flags & TRANSIENT) != 0)    sb.append("transient ");
         return sb.toString();
     }
 
     public Type.Class getType() { return thisType; }
+    public int getFlags() { return flags; }
     
-    String debugToString() { return debugToString(new StringBuffer(4096)).toString(); }
-    StringBuffer debugToString(StringBuffer sb) {
+    public String toString() { return toString(new StringBuffer(4096)).toString(); }
+    StringBuffer toString(StringBuffer sb) {
         sb.append(flagsToString(flags,true));
-        sb.append((flags & ACC_INTERFACE) != 0 ? "interface " : "class ");
-        sb.append(thisType.debugToString());
-        if (superType != null) sb.append(" extends " + superType.debugToString());
+        sb.append((flags & INTERFACE) != 0 ? "interface " : "class ");
+        sb.append(thisType.toString());
+        if (superType != null) sb.append(" extends " + superType.toString());
         if (interfaces != null && interfaces.length > 0) sb.append(" implements");
-        for(int i=0; i<interfaces.length; i++) sb.append((i==0?" ":", ")+interfaces[i].debugToString());
+        for(int i=0; i<interfaces.length; i++) sb.append((i==0?" ":", ")+interfaces[i].toString());
         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++) {
             sb.append("  ");
-            ((FieldGen)fields.elementAt(i)).debugToString(sb);
+            ((FieldGen)fields.elementAt(i)).toString(sb);
             sb.append("\n");
         }
         for(int i=0; i<methods.size(); i++) {
-            ((MethodGen)methods.elementAt(i)).debugToString(sb,thisType.getShortName());
+            ((MethodGen)methods.elementAt(i)).toString(sb,thisType.getShortName());
             sb.append("\n");
         }
         sb.append("}");
@@ -63,15 +73,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 & ~(ACC_PUBLIC|ACC_FINAL|ACC_SUPER|ACC_INTERFACE|ACC_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 
@@ -79,14 +86,17 @@ public class ClassFile implements CGConst {
         @param ret The return type of the method
         @param args The arguments to the method
         @param flags The flags for the method
-                     (ACC_PUBLIC, ACC_PRIVATE, ACC_PROTECTED, ACC_STATIC, ACC_SYNCHRONIZED, ACC_NATIVE, ACC_ABSTRACT, ACC_STRICT)
+                     (PUBLIC, PRIVATE, PROTECTED, STATIC, SYNCHRONIZED, NATIVE, ABSTRACT, STRICT)
         @return A new MethodGen object for the method
         @exception IllegalArgumentException if illegal flags are specified
         @see MethodGen
         @see CGConst
     */
     public final MethodGen addMethod(String name, Type ret, Type[] args, int flags) {
-        MethodGen mg = new MethodGen(this, name, ret, args, flags, (this.flags & ACC_INTERFACE) != 0);
+        return addMethod(getType().method(name, ret, args),flags);
+    }
+    public final MethodGen addMethod(Type.Class.Method method,int flags) {
+        MethodGen mg = new MethodGen(method, flags);
         methods.addElement(mg);
         return mg;
     }
@@ -95,14 +105,17 @@ public class ClassFile implements CGConst {
         @param name The name of the filed (not the signature, just the name)
         @param type The type of the field
         @param flags The flags for the field
-        (ACC_PUBLIC, ACC_PRIVATE, ACC_PROTECTED, ACC_STATIC, ACC_FINAL, ACC_VOLATILE, ACC_TRANSIENT)
+        (PUBLIC, PRIVATE, PROTECTED, STATIC, FINAL, VOLATILE, TRANSIENT)
         @return A new FieldGen object for the method
         @exception IllegalArgumentException if illegal flags are specified
         @see FieldGen
         @see CGConst
         */  
     public final FieldGen addField(String name, Type type, int flags) {
-        FieldGen fg = new FieldGen(this, name, type, flags);
+        return addField(getType().field(name,type),flags);
+    }
+    public final FieldGen addField(Type.Class.Field field, int flags) {
+        FieldGen fg = new FieldGen(field , flags);
         fields.addElement(fg);
         return fg;
     }
@@ -110,7 +123,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)
@@ -155,7 +168,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();
@@ -179,7 +192,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)); }
@@ -199,29 +212,36 @@ 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(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 & ~(ACC_PUBLIC|ACC_FINAL|ACC_SUPER|ACC_INTERFACE|ACC_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(new MethodGen(this, i, cp, (this.flags & ACC_INTERFACE) != 0));
-        attributes = new AttrGen(i, cp);
+        for(int j=0; j<numMethods; j++) methods.addElement(ssa 
+                                                           ? new JSSA(this.getType(), i, cp) 
+                                                           : new MethodGen(this.getType(), 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
@@ -298,18 +318,18 @@ public class ClassFile implements CGConst {
         }
         else if (args.length==1) {
             if (args[0].endsWith(".class")) {
-                System.out.println(new ClassFile(new DataInputStream(new FileInputStream(args[0]))).debugToString());
+                System.out.println(new ClassFile(new DataInputStream(new FileInputStream(args[0]))).toString());
             } else {
                 InputStream is = Class.forName(args[0]).getClassLoader().getResourceAsStream(args[0].replace('.', '/')+".class");
-                System.out.println(new ClassFile(new DataInputStream(is)).debugToString());
+                System.out.println(new ClassFile(new DataInputStream(is)).toString());
             }
         } else {
             /*
             Type.Class me = new Type.Class("Test");
-            ClassFile cg = new ClassFile("Test", "java.lang.Object", ACC_PUBLIC|ACC_SUPER|ACC_FINAL);
-            FieldGen fg = cg.addField("foo", Type.INT, ACC_PUBLIC|ACC_STATIC);
+            ClassFile cg = new ClassFile("Test", "java.lang.Object", PUBLIC|SUPER|FINAL);
+            FieldGen fg = cg.addField("foo", Type.INT, PUBLIC|STATIC);
         
-            MethodGen mg = cg.addMethod("main", Type.VOID, new Type[]{Type.arrayType(Type.STRING)}, ACC_STATIC|ACC_PUBLIC);
+            MethodGen mg = cg.addMethod("main", Type.VOID, new Type[]{Type.arrayType(Type.STRING)}, STATIC|PUBLIC);
             mg.setMaxLocals(1);
             mg.addPushConst(0);
             //mg.add(ISTORE_0);