added Type.Field.Body and Type.Class.Body
authoradam <adam@megacz.com>
Sun, 3 Jul 2005 02:43:23 +0000 (02:43 +0000)
committeradam <adam@megacz.com>
Sun, 3 Jul 2005 02:43:23 +0000 (02:43 +0000)
darcs-hash:20050703024323-5007d-4bd60b17ae5fb293f0c5f0c35886b764e4638964.gz

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

index dd8b424..f41a44f 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;
@@ -64,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;
@@ -202,15 +203,22 @@ public class ClassFile implements CGConst {
 
     public ClassFile(DataInput i) throws IOException { this(i, false); }
     public ClassFile(DataInput i, boolean ssa) throws IOException {
-        int magic = i.readInt();
+        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());
index 264bf59..067e611 100644 (file)
@@ -82,6 +82,8 @@ public class Type implements CGConst {
         public Type.Class asClass() { return this; }
         public boolean isClass() { return true; }
         public String getShortName() { return toString.substring(toString.lastIndexOf('.')+1); }
+        public String getName() { return toString(); }
+        //public boolean extendsOrImplements(Type.Class c, Context cx) { }
         String internalForm() { return descriptor.substring(1, descriptor.length()-1); }
         private static String _initHelper(String s) {
             if (!s.startsWith("L") || !s.endsWith(";")) s = "L" + s.replace('.', '/') + ";";
@@ -98,6 +100,9 @@ public class Type implements CGConst {
             return a;
         }
 
+        public abstract class Body extends HasFlags {
+        }
+
         public Field  field(String name, Type type) { return new Field(name, type); }
         public Method method(String name, Type returnType, Type[] argTypes) { return new Method(name, returnType, argTypes); }
         public Method method(String signature) {
@@ -141,14 +146,27 @@ public class Type implements CGConst {
             public String getDescriptor() { return type.getDescriptor(); }
             public Type getType() { return type; }
             public String debugToString() { return getDeclaringClass()+"."+name+"["+type+"]"; }
+            public class Body extends HasFlags {
+                public final int flags;
+                public Body(int flags) { this.flags = flags; }
+                public int getFlags() { return flags; }
+            }
         }
 
         public class Method extends Member {
             final Type[] argTypes;
             public final Type   returnType;
-            public Type getArgType(int i) { return argTypes[i]; }
-            public int  getNumArgs()      { return argTypes.length; }
             public Type getReturnType()   { return returnType; }
+            public int  getNumArgs()      { return argTypes.length; }
+            public Type getArgType(int i) { return argTypes[i]; }
+            public Type[] getArgTypes()   {
+                Type[] ret = new Type[argTypes.length];
+                System.arraycopy(argTypes, 0, ret, 0, ret.length);
+                return ret;
+            }
+
+            public boolean isConstructor() { return getName().equals("<init>"); }
+            public boolean isClassInitializer() { return getName().equals("<clinit>"); }
             public String debugToString() {
                 StringBuffer sb = new StringBuffer();
                 if (name.equals("<clinit>")) sb.append("static ");
@@ -169,6 +187,7 @@ public class Type implements CGConst {
                 this.argTypes = argTypes;
                 this.returnType = returnType;
             }
+            //public Method.Body getBody(Context cx) { }
             public String getDescriptor() {
                 StringBuffer sb = new StringBuffer(argTypes.length*4);
                 sb.append("(");
@@ -177,7 +196,7 @@ public class Type implements CGConst {
                 sb.append(returnType.getDescriptor());
                 return sb.toString();
             }
-            public abstract class Body implements HasFlags {
+            public abstract class Body extends HasFlags {
                 public abstract java.util.Hashtable getThrownExceptions();
                 public abstract void debugBodyToString(StringBuffer sb);
                 public void debugToString(StringBuffer sb, String constructorName) {