got it to compile
[org.ibex.classgen.git] / src / org / ibex / classgen / ClassGen.java
index d579b99..c4b7e77 100644 (file)
@@ -7,10 +7,10 @@ import java.io.*;
 public class ClassGen implements CGConst {
     private final Type.Object thisType;
     private final Type.Object superType;
+    private final Type.Object[] interfaces;
     final int flags;
     
     private String sourceFile; 
-    private final Vector interfaces = new Vector();
     private final Vector fields = new Vector();
     private final Vector methods = new Vector();
     
@@ -21,17 +21,23 @@ public class ClassGen implements CGConst {
     public ClassGen(String name, String superName, int flags) {
         this(new Type.Object(name),new Type.Object(superName),flags);
     }
+
+    /** @see #ClassGen(Type.Object,Type.Object,int,Type.Object[]) */
+    public ClassGen(Type.Object thisType,Type.Object superType, int flags) {
+        this(thisType,superType,flags,null);
+    }
     
     /** Creates a new ClassGen object 
         @param thisType The type of the class to generate
-        @param superType The superclas of the generated class (commonly Type.OBJECT) 
+        @param superType The superclass of the generated class (commonly Type.OBJECT) 
         @param flags The access flags for this class (ACC_PUBLIC, ACC_FINAL, ACC_SUPER, ACC_INTERFACE, and ACC_ABSTRACT)
     */
-    public ClassGen(Type.Object thisType,Type.Object superType, int flags) {
+    public ClassGen(Type.Object thisType,Type.Object superType, int flags, Type.Object[] interfaces) {
         if((flags & ~(ACC_PUBLIC|ACC_FINAL|ACC_SUPER|ACC_INTERFACE|ACC_ABSTRACT)) != 0)
             throw new IllegalArgumentException("invalid flags");
         this.thisType = thisType;
         this.superType = superType;
+        this.interfaces = interfaces;
         this.flags = flags;
         
         cp = new CPGen();
@@ -95,7 +101,9 @@ public class ClassGen implements CGConst {
             }
             f = new File(f,a[i] + ".class");
         }
-        dump(new FileOutputStream(f));
+        OutputStream os = new FileOutputStream(f);
+        dump(os);
+        os.close();
     }
    
     /** Writes the classfile data to the outputstream specified
@@ -111,13 +119,14 @@ public class ClassGen implements CGConst {
     }
     
     private void _dump(DataOutput o) throws IOException {
+        cp.optimize();
+        cp.stable();
+        
         cp.add(thisType);
         cp.add(superType);
-        for(int i=0;i<interfaces.size();i++) cp.add((Type.Object)interfaces.elementAt(i));
+        if(interfaces != null) for(int i=0;i<interfaces.length;i++) cp.add(interfaces[i]);
         if(sourceFile != null && !attributes.contains("SourceFile")) attributes.add("SourceFile",cp.addUtf8(sourceFile));
-        
-        cp.stable();
-        
+                
         for(int i=0;i<methods.size();i++) ((MethodGen)methods.elementAt(i)).finish();
         for(int i=0;i<fields.size();i++) ((FieldGen)fields.elementAt(i)).finish();
         
@@ -127,15 +136,14 @@ public class ClassGen implements CGConst {
         o.writeShort(3); // minor_version
         o.writeShort(45); // major_version
         
-        o.writeShort(cp.size()); // constant_pool_count
         cp.dump(o); // constant_pool
         
         o.writeShort(flags);
         o.writeShort(cp.getIndex(thisType)); // this_class
         o.writeShort(cp.getIndex(superType)); // super_class
         
-        o.writeShort(interfaces.size()); // interfaces_count
-        for(int i=0;i<interfaces.size();i++) o.writeShort(cp.getIndex(interfaces.elementAt(i))); // interfaces
+        o.writeShort(interfaces==null ? 0 : interfaces.length); // interfaces_count
+        if(interfaces != null) for(int i=0;i<interfaces.length;i++) o.writeShort(cp.getIndex(interfaces[i])); // interfaces
         
         o.writeShort(fields.size()); // fields_count
         for(int i=0;i<fields.size();i++) ((FieldGen)fields.elementAt(i)).dump(o); // fields
@@ -147,12 +155,45 @@ public class ClassGen implements CGConst {
         attributes.dump(o); // attributes        
     }
     
+    public ClassGen read(File f) throws ClassReadExn, IOException {
+        InputStream is = new FileInputStream(f);
+        ClassGen ret = read(is);
+        is.close();
+        return ret;
+    }
+    
+    public ClassGen read(InputStream is) throws ClassReadExn, IOException {
+        return new ClassGen(new DataInputStream(new BufferedInputStream(is)));
+    }
+
+    ClassGen(DataInput i) throws ClassReadExn, IOException {
+        if(i.readInt() != 0xcadebabe) throw new ClassReadExn("invalid magic");
+        short minor = (short)i.readInt();
+        if(minor != 3) throw new ClassReadExn("invalid minor version: " + minor);
+        if(i.readInt() != 45) throw new ClassReadExn("invalid major version");
+        cp = new CPGen(i);
+        flags = (short)i.readShort();
+        thisType = cp.getType(i.readShort());
+        superType = cp.getType(i.readShort());
+        interfaces = new Type.Object[i.readShort()];
+        for(int j=0; j<interfaces.length; j++) interfaces[j] = cp.getType(i.readShort());
+        int numFields = i.readShort();
+        for(int j=0; j<numFields; j++) fields.add(new FieldGen(i));
+        int numMethods = i.readShort();
+        for(int j=0; j<numMethods; j++) methods.add(new MethodGen(i));
+        attributes = new AttrGen(i);
+    }
+    
     /** Thrown when class generation fails for a reason not under the control of the user
         (IllegalStateExceptions are thrown in those cases */
     public static class Exn extends RuntimeException {
         public Exn(String s) { super(s); }
     }
     
+    public static class ClassReadExn extends IOException {
+        public ClassReadExn(String s) { super(s); }
+    }
+    
     /** A class representing a field or method reference. This is used as an argument to the INVOKE*, GET*, and PUT* bytecodes
         @see MethodRef
         @see FieldRef
@@ -178,6 +219,9 @@ public class ClassGen implements CGConst {
         private final CPGen cp;
         private final Hashtable ht = new Hashtable();
         
+        public AttrGen(DataInput in) {
+            throw new Error("Brian is superlame");
+        }
         public AttrGen(CPGen cp) {
             this.cp = cp;
         }
@@ -202,7 +246,7 @@ public class ClassGen implements CGConst {
                     o.write(buf);
                 } else if(val instanceof CPGen.Ent) {
                     o.writeInt(2);
-                    o.writeShort(((CPGen.Ent)val).getIndex());
+                    o.writeShort(cp.getIndex((CPGen.Ent)val));
                 } else {
                     throw new Error("should never happen");
                 }