refactored a lot of functionality up from MethodGen into Type.Class.Method
authoradam <adam@megacz.com>
Mon, 27 Jun 2005 08:30:01 +0000 (08:30 +0000)
committeradam <adam@megacz.com>
Mon, 27 Jun 2005 08:30:01 +0000 (08:30 +0000)
darcs-hash:20050627083001-5007d-98b135f8f7eeddb1bc30e4af05bfb8d58d8708be.gz

src/org/ibex/classgen/CGConst.java
src/org/ibex/classgen/ClassFile.java
src/org/ibex/classgen/MethodGen.java
src/org/ibex/classgen/Type.java

index d789292..9d8d6d4 100644 (file)
@@ -20,11 +20,12 @@ public interface CGConst {
     public static final int SYNCHRONIZED = 0x0020;
     public static final int NATIVE       = 0x0100;
     public static final int STRICT       = 0x0800;
     public static final int SYNCHRONIZED = 0x0020;
     public static final int NATIVE       = 0x0100;
     public static final int STRICT       = 0x0800;
+    public static final int VALID_METHOD_FLAGS = PUBLIC|PRIVATE|PROTECTED|STATIC|FINAL|SYNCHRONIZED|NATIVE|ABSTRACT|STRICT;
 
     // Field only
     public static final int VOLATILE  = 0x0040;
     public static final int TRANSIENT = 0x0080;
 
     // Field only
     public static final int VOLATILE  = 0x0040;
     public static final int TRANSIENT = 0x0080;
-    
+
     
     // Constant Pool Stuff
     public static final int CONSTANT_UTF8 = 1;
     
     // Constant Pool Stuff
     public static final int CONSTANT_UTF8 = 1;
index 8cb2853..f70ed48 100644 (file)
@@ -86,7 +86,7 @@ public class ClassFile implements CGConst {
         @see CGConst
     */
     public final MethodGen addMethod(String name, Type ret, Type[] args, int flags) {
         @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 & INTERFACE) != 0);
+        MethodGen mg = new MethodGen(this.getType(), name, ret, args, flags, (this.flags & INTERFACE) != 0);
         methods.addElement(mg);
         return mg;
     }
         methods.addElement(mg);
         return mg;
     }
@@ -215,7 +215,7 @@ public class ClassFile implements CGConst {
         int numFields = i.readShort();
         for(int j=0; j<numFields; j++) fields.addElement(new FieldGen(i, cp));
         int numMethods = i.readShort();
         int numFields = i.readShort();
         for(int j=0; j<numFields; j++) fields.addElement(new FieldGen(i, cp));
         int numMethods = i.readShort();
-        for(int j=0; j<numMethods; j++) methods.addElement(new MethodGen(this, i, cp, (this.flags & INTERFACE) != 0));
+        for(int j=0; j<numMethods; j++) methods.addElement(new MethodGen(this.getType(), i, cp, (this.flags & INTERFACE) != 0));
         attributes = new AttrGen(i, cp);
         
         // FEATURE: Support these
         attributes = new AttrGen(i, cp);
         
         // FEATURE: Support these
index e534c65..3e8571d 100644 (file)
@@ -10,10 +10,7 @@ public class MethodGen implements CGConst {
     
     private static final int NO_CODE = -1;
 
     
     private static final int NO_CODE = -1;
 
-    private final ClassFile cf;
-    private final String name;
-    private final Type ret;
-    private final Type[] args;
+    private final Type.Class.Method method;
     private final int flags;
     private final ClassFile.AttrGen attrs;
     private final ClassFile.AttrGen codeAttrs;
     private final int flags;
     private final ClassFile.AttrGen attrs;
     private final ClassFile.AttrGen codeAttrs;
@@ -78,24 +75,13 @@ public class MethodGen implements CGConst {
         };
         
         sb.append("  ").append(ClassFile.flagsToString(flags,false));
         };
         
         sb.append("  ").append(ClassFile.flagsToString(flags,false));
-
-        if (name.equals("<clinit>")) sb.append("static ");
-        else {
-            if (name.equals("<init>"))
-                sb.append(constructorName);
-            else
-                sb.append(ret.debugToString()).append(" ").append(name);
-            sb.append("(");
-            for(int i=0; i<args.length; i++)
-                sb.append((i==0?"":", ")+args[i].debugToString());
-            sb.append(") ");
-            if(thrownExceptions.size() > 0) {
-                sb.append("throws");
-                for(Enumeration e = thrownExceptions.keys();e.hasMoreElements();)
-                    sb.append(" ").append(((Type.Class)e.nextElement()).debugToString()).append(",");
-                sb.setLength(sb.length()-1);
-                sb.append(" ");
-            }
+        sb.append(method.debugToString());
+        if(thrownExceptions.size() > 0) {
+            sb.append("throws");
+            for(Enumeration e = thrownExceptions.keys();e.hasMoreElements();)
+                sb.append(" ").append(((Type.Class)e.nextElement()).debugToString()).append(",");
+            sb.setLength(sb.length()-1);
+            sb.append(" ");
         }
         if((flags & (NATIVE|ABSTRACT))==0) {
             sb.append("{\n");
         }
         if((flags & (NATIVE|ABSTRACT))==0) {
             sb.append("{\n");
@@ -118,17 +104,11 @@ public class MethodGen implements CGConst {
         }
     }
 
         }
     }
 
-    MethodGen(ClassFile cf, DataInput in, ConstantPool cp, boolean ownerInterface) throws IOException {
-        this.cf = cf;
-        flags = in.readShort();
-        if((flags & ~(PUBLIC|PRIVATE|PROTECTED|STATIC|FINAL|SYNCHRONIZED|NATIVE|ABSTRACT|STRICT)) != 0)
-            throw new ClassFile.ClassReadExn("invalid flags");
-        this.name = cp.getUtf8KeyByIndex(in.readShort());
-        //System.err.println("Processing " + name + "...");
-        Type.Class.Method m = cf.getType().method(name+cp.getUtf8KeyByIndex(in.readShort()));
-        this.ret = m.returnType;
-        this.args = new Type[m.getNumArgs()];
-        for(int i=0; i<args.length; i++) args[i] = m.getArgType(i);
+    MethodGen(Type.Class c, DataInput in, ConstantPool cp, boolean ownerInterface) throws IOException {
+        this.flags = in.readShort();
+        if ((flags & ~VALID_METHOD_FLAGS) != 0) throw new ClassFile.ClassReadExn("invalid flags");
+        String name = cp.getUtf8KeyByIndex(in.readShort());
+        this.method = c.method(name+cp.getUtf8KeyByIndex(in.readShort()));
         this.attrs = new ClassFile.AttrGen(in,cp);
         
         if((flags & (NATIVE|ABSTRACT))==0)  {
         this.attrs = new ClassFile.AttrGen(in,cp);
         
         if((flags & (NATIVE|ABSTRACT))==0)  {
@@ -282,7 +262,8 @@ public class MethodGen implements CGConst {
             }
             add(op,arg);
         }
             }
             add(op,arg);
         }
-        if(pc != codeLen) throw new ClassFile.ClassReadExn("didn't read enough code (" + pc + "/" + codeLen + " in " + name + ")");
+        if(pc != codeLen)
+            throw new ClassFile.ClassReadExn("didn't read enough code (" + pc + "/" + codeLen + " in " + method.name + ")");
         for(int i=0;i<size();i++) {
             switch(op[i]) {
                 case TABLESWITCH:
         for(int i=0;i<size();i++) {
             switch(op[i]) {
                 case TABLESWITCH:
@@ -313,13 +294,9 @@ public class MethodGen implements CGConst {
         return map;
     }
 
         return map;
     }
 
-    MethodGen(ClassFile cf, String name, Type ret, Type[] args, int flags, boolean ownerInterface) {
-        if((flags & ~(PUBLIC|PRIVATE|PROTECTED|STATIC|FINAL|SYNCHRONIZED|NATIVE|ABSTRACT|STRICT)) != 0)
-            throw new IllegalArgumentException("invalid flags");
-        this.cf = cf;
-        this.name = name;
-        this.ret = ret;
-        this.args = args;
+    MethodGen(Type.Class c, String name, Type ret, Type[] args, int flags, boolean ownerInterface) {
+        if ((flags & ~VALID_METHOD_FLAGS) != 0) throw new IllegalArgumentException("invalid flags");
+        this.method = c.method(name, ret, args);
         this.flags = flags;
         
         attrs = new ClassFile.AttrGen();
         this.flags = flags;
         
         attrs = new ClassFile.AttrGen();
@@ -623,8 +600,8 @@ public class MethodGen implements CGConst {
         @exception Exn if the byteocode could not be generated for any other reason (constant pool full, etc)
     */
     void finish(ConstantPool cp) {
         @exception Exn if the byteocode could not be generated for any other reason (constant pool full, etc)
     */
     void finish(ConstantPool cp) {
-        cp.addUtf8(name);
-        cp.addUtf8(Type.methodTypeDescriptor(args,ret));
+        cp.addUtf8(method.name);
+        cp.addUtf8(method.getDescriptor());
         
         for(Enumeration e = thrownExceptions.keys();e.hasMoreElements();)
             cp.add(e.nextElement());
         
         for(Enumeration e = thrownExceptions.keys();e.hasMoreElements();)
             cp.add(e.nextElement());
@@ -957,8 +934,8 @@ public class MethodGen implements CGConst {
         generateExceptions(cp);
         
         o.writeShort(flags);
         generateExceptions(cp);
         
         o.writeShort(flags);
-        o.writeShort(cp.getUtf8Index(name));
-        o.writeShort(cp.getUtf8Index(Type.methodTypeDescriptor(args,ret)));
+        o.writeShort(cp.getUtf8Index(method.name));
+        o.writeShort(cp.getUtf8Index(method.getDescriptor()));
         attrs.dump(o,cp);
     }
     
         attrs.dump(o,cp);
     }
     
index af3da6b..95d9394 100644 (file)
@@ -152,6 +152,21 @@ public class Type {
             public final Type   returnType;
             public Type getArgType(int i) { return argTypes[i]; }
             public int  getNumArgs()      { return argTypes.length; }
             public final Type   returnType;
             public Type getArgType(int i) { return argTypes[i]; }
             public int  getNumArgs()      { return argTypes.length; }
+            public String debugToString() {
+                StringBuffer sb = new StringBuffer();
+                if (name.equals("<clinit>")) sb.append("static ");
+                else {
+                    if (name.equals("<init>"))
+                        sb.append(Class.this.getShortName());
+                    else
+                        sb.append(returnType).append(" ").append(name);
+                    sb.append("(");
+                    for(int i=0; i<argTypes.length; i++)
+                        sb.append((i==0?"":", ")+argTypes[i].debugToString());
+                    sb.append(") ");
+                }
+                return sb.toString();
+            }
             private Method(String name, Type returnType, Type[] argTypes) {
                 super(name);
                 this.argTypes = argTypes;
             private Method(String name, Type returnType, Type[] argTypes) {
                 super(name);
                 this.argTypes = argTypes;