rename CPGen -> ConstantPool
[org.ibex.classgen.git] / src / org / ibex / classgen / ClassFile.java
index 6be6bfa..b926d85 100644 (file)
@@ -12,11 +12,10 @@ public class ClassFile implements CGConst {
     private short major;
     final int flags;
     
-    private String sourceFile; 
     private final Vector fields = new Vector();
     private final Vector methods = new Vector();
     
-    final CPGen cp;
+    final ConstantPool cp;
     private final AttrGen attributes;
 
     public static String flagsToString(int flags) {
@@ -34,7 +33,8 @@ public class ClassFile implements CGConst {
         if ((flags & ACC_TRANSIENT) != 0)    ret += "transient ";
         return ret;
     }
-  
+
+    public Type.Class getType() { return thisType; }
     public String toString() { StringBuffer sb = new StringBuffer(); toString(sb); return sb.toString(); }
     public void   toString(StringBuffer sb) {
         sb.append(flagsToString(flags));
@@ -45,6 +45,7 @@ public class ClassFile implements CGConst {
         for(int i=0; i<interfaces.length; i++) sb.append((i==0?" ":", ")+interfaces[i]);
         sb.append(" {");
         sb.append(" // [jcf v"+major+"."+minor+"]");
+        String sourceFile = (String)attributes.get("SourceFile");
         if (sourceFile != null) sb.append(" from " + sourceFile);
         sb.append("\n");
         for(int i=0; i<fields.size(); i++) {
@@ -60,21 +61,7 @@ public class ClassFile implements CGConst {
         sb.append("}");
     }
 
-    /** @see #ClassFile(Type.Class, Type.Class, int) */
-    public ClassFile(String name, String superName, int flags) {
-        this(Type.instance(name).asClass(), Type.instance(superName).asClass(), flags);
-    }
-
-    /** @see #ClassFile(Type.Class, Type.Class, int, Type.Class[]) */
-    public ClassFile(Type.Class thisType, Type.Class superType, int flags) {
-        this(thisType, superType, flags, null);
-    }
-    
-    /** Creates a new ClassFile object 
-        @param thisType The type of the class to generate
-        @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 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");
@@ -85,7 +72,7 @@ public class ClassFile implements CGConst {
         this.minor = 3;
         this.major = 45;
         
-        cp = new CPGen();
+        cp = new ConstantPool();
         attributes = new AttrGen(cp);
     }
     
@@ -125,7 +112,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) { this.sourceFile = sourceFile; }
+    public void setSourceFile(String sourceFile) { attributes.add("SourceFile", sourceFile); }
     
     /** Writes the classfile data to the file specifed
         @see ClassFile#dump(OutputStream)
@@ -170,7 +157,6 @@ public class ClassFile implements CGConst {
         cp.add(thisType);
         cp.add(superType);
         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));
                 
         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();
@@ -218,7 +204,7 @@ public class ClassFile implements CGConst {
         //if (minor != 3) throw new ClassReadExn("invalid minor version: " + minor);
         major = i.readShort();
         //if (major != 45 && major != 46) throw new ClassReadExn("invalid major version");
-        cp = new CPGen(i);
+        cp = new ConstantPool(i);
         flags = i.readShort();
         thisType = (Type.Class)cp.getType(i.readShort());
         superType = (Type.Class)cp.getType(i.readShort());
@@ -227,9 +213,8 @@ public class ClassFile implements CGConst {
         int numFields = i.readShort();
         for(int j=0; j<numFields; j++) fields.add(new FieldGen(cp, i));
         int numMethods = i.readShort();
-        for(int j=0; j<numMethods; j++) methods.add(new MethodGen(cp, i));
+        for(int j=0; j<numMethods; j++) methods.add(new MethodGen(cp, i, this));
         attributes = new AttrGen(cp, i);
-        sourceFile = (String)attributes.get("SourceFile");
     }
     
     /** Thrown when class generation fails for a reason not under the control of the user
@@ -243,17 +228,17 @@ public class ClassFile implements CGConst {
     }
     
     static class AttrGen {
-        private final CPGen cp;
+        private final ConstantPool cp;
         private final Hashtable ht = new Hashtable();
         
-        public AttrGen(CPGen cp) { this.cp = cp; }
-        public AttrGen(CPGen cp, DataInput in) throws IOException {
+        public AttrGen(ConstantPool cp) { this.cp = cp; }
+        public AttrGen(ConstantPool cp, DataInput in) throws IOException {
             this(cp);
             int size = in.readShort();
             for(int i=0; i<size; i++) {
                 String name = null;
                 int idx = in.readShort();
-                CPGen.Ent e = cp.getByIndex(idx);
+                ConstantPool.Ent e = cp.getByIndex(idx);
                 Object key = e.key();
                 if (key instanceof String) name = (String)key;
                 else name = ((Type)key).getDescriptor();
@@ -271,7 +256,7 @@ public class ClassFile implements CGConst {
 
         public Object get(String s) {
             Object ret = ht.get(s);
-            if (ret instanceof CPGen.Utf8Ent) return ((CPGen.Utf8Ent)ret).s;
+            if (ret instanceof ConstantPool.Utf8Ent) return ((ConstantPool.Utf8Ent)ret).s;
             return ret;
         }
         
@@ -293,9 +278,9 @@ public class ClassFile implements CGConst {
                     byte[] buf = (byte[]) val;
                     o.writeInt(buf.length);
                     o.write(buf);
-                } else if(val instanceof CPGen.Ent) {
+                } else if (val instanceof ConstantPool.Ent) {
                     o.writeInt(2);
-                    o.writeShort(cp.getIndex((CPGen.Ent)val));
+                    o.writeShort(cp.getIndex((ConstantPool.Ent)val));
                 } else {
                     throw new Error("should never happen");
                 }