fixed HIDEOUS bug in ConstantPool -- was circumventing Type-instance caching (evil...
[org.ibex.classgen.git] / src / org / ibex / classgen / Type.java
index 14747db..aacbfe4 100644 (file)
@@ -29,11 +29,14 @@ public abstract class Type implements CGConst {
     /** A zero element Type[] array (can be passed as the "args" param when a method takes no arguments */
     public static final Type[] NO_ARGS = new Type[0];
     
-    /** guarantee: there will only be one instance of Type for a given descriptor ==> equals() and == are interchangeable */
-    public static Type instance(String d) {
+    /** 
+     *  A "descriptor" is the classfile-mangled text representation of a type (see JLS section 4.3)
+     *  guarantee: there will only be one instance of Type for a given descriptor ==> equals() and == are interchangeable
+     */
+    public static Type fromDescriptor(String d) {
         Type ret = (Type)instances.get(d);
         if (ret != null) return ret;
-        if (d.startsWith("[")) return new Type.Array(instance(d.substring(1)));
+        if (d.startsWith("[")) return new Type.Array(Type.fromDescriptor(d.substring(1)));
         return new Type.Class(d);
     }
 
@@ -42,7 +45,7 @@ public abstract class Type implements CGConst {
     
     public final String  getDescriptor() { return descriptor; }
 
-    public Type.Array  makeArray() { return (Type.Array)instance("["+descriptor); }
+    public Type.Array  makeArray() { return (Type.Array)Type.fromDescriptor("["+descriptor); }
     public Type.Array  makeArray(int i) { return i==0 ? (Type.Array)this : makeArray().makeArray(i-1); }
 
     public Type.Ref    asRef()       { throw new RuntimeException("attempted to use "+this+" as a Type.Ref, which it is not"); }
@@ -85,7 +88,7 @@ public abstract class Type implements CGConst {
         public Type.Array asArray() { return this; }
         public boolean isArray() { return true; }
         public String debugToString() { return base.debugToString() + "[]"; }
-        public Type getElementType() { return Type.instance(getDescriptor().substring(0, getDescriptor().length()-1)); }
+        public Type getElementType() { return Type.fromDescriptor(getDescriptor().substring(0, getDescriptor().length()-1)); }
     }
 
     public static class Class extends Type.Ref {
@@ -93,16 +96,29 @@ public abstract class Type implements CGConst {
         public Type.Class asClass() { return this; }
         public boolean isClass() { return true; }
         public static Type.Class instance(String className) {
-            return (Type.Class)Type.instance("L"+className.replace('.', '/')+";"); }
-        //public boolean extendsOrImplements(Type.Class c, Context cx) { }
+            return (Type.Class)Type.fromDescriptor("L"+className.replace('.', '/')+";"); }
+        public boolean extendsOrImplements(Type.Class c, Context cx) {
+            if (this==c) return true;
+            if (this==OBJECT) return false;
+            ClassFile cf = cx.resolve(getName());
+            if (cf==null) {
+                System.err.println("warning: could not resolve class " + getName());
+                return false;
+            }
+            if (cf.superType == c) return true;
+            for(int i=0; i<cf.interfaces.length; i++) if (cf.interfaces[i].extendsOrImplements(c,cx)) return true;
+            if (cf.superType == null) return false;
+            return cf.superType.extendsOrImplements(c, cx);
+        }
         String internalForm() { return descriptor.substring(1, descriptor.length()-1); }
         public String debugToString() { return internalForm().replace('/','.'); }
+        public String getName() { return internalForm().replace('/','.'); }
         public String getShortName() {
             int p = descriptor.lastIndexOf('/');
             return p == -1 ? descriptor.substring(1,descriptor.length()-1) : descriptor.substring(p+1,descriptor.length()-1);
         }
         private static String _initHelper(String s) {
-            if (!s.startsWith("L") || !s.endsWith(";")) s = "L" + s.replace('.', '/') + ";";
+            if (!s.startsWith("L") || !s.endsWith(";")) throw new Error("invalid");
             return s;
         }
         String[] components() {
@@ -112,10 +128,19 @@ public abstract class Type implements CGConst {
             return a;
         }
 
-        public Field field(String name, Type type) { return new Field(name, type); }
-        public abstract class Body extends HasFlags {
+        public abstract class Body extends HasAttributes {
+            public abstract Type.Class.Method.Body[] methods();
+            public abstract Type.Class.Field.Body addField(Type.Class.Field field, int flags);
+            public abstract Type.Class.Field.Body[] fields();
+            public Body(int flags, ClassFile.AttrGen attrs) {
+                super(flags, attrs);
+                if ((flags & ~(PUBLIC|FINAL|SUPER|INTERFACE|ABSTRACT)) != 0)
+                    throw new IllegalArgumentException("invalid flags: " + Integer.toString(flags,16));
+            }
         }
 
+        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 leftCrap, String rightCrap) { return method(leftCrap+rightCrap); }
 
@@ -138,12 +163,12 @@ public abstract class Type implements CGConst {
                     p = argsDesc.indexOf(';');
                     if(p == -1) throw new IllegalArgumentException("invalid method type descriptor");
                 }
-                argsBuf[i] = Type.instance(argsDesc.substring(0,p+1));
+                argsBuf[i] = Type.fromDescriptor(argsDesc.substring(0,p+1));
                 argsDesc = argsDesc.substring(p+1);
             }
             Type args[] = new Type[i];
             System.arraycopy(argsBuf,0,args,0,i);
-            return method(name, Type.instance(retDesc), args);
+            return method(name, Type.fromDescriptor(retDesc), args);
         }
 
         public abstract class Member {
@@ -160,12 +185,14 @@ public abstract class Type implements CGConst {
             private Field(String name, Type t) { super(name); this.type = t; }
             public String getTypeDescriptor() { return type.getDescriptor(); }
             public Type getType() { return type; }
-            public class Body extends HasFlags {
-                public final int flags;
-                public Body(int flags) { this.flags = flags; }
-                public int getFlags() { return flags; }
-            }
             public String debugToString() { return getDeclaringClass().debugToString()+"."+name+"["+type.debugToString()+"]"; }
+            public class Body extends HasAttributes {
+                public Field getField() { return Field.this; }
+                public Body(int flags, ClassFile.AttrGen attrs) {
+                    super(flags, attrs);
+                    if ((flags & ~VALID_FIELD_FLAGS) != 0) throw new IllegalArgumentException("invalid flags");
+                }
+            }
         }
 
         public class Method extends Member {
@@ -210,9 +237,14 @@ public abstract class Type implements CGConst {
                 sb.append(returnType.getDescriptor());
                 return sb.toString();
             }
-            public abstract class Body extends HasFlags {
+            public abstract class Body extends HasAttributes {
                 public abstract java.util.Hashtable getThrownExceptions();
                 public abstract void debugBodyToString(StringBuffer sb);
+                public Method getMethod() { return Method.this; }
+                public Body(int flags, ClassFile.AttrGen attrs) {
+                    super(flags, attrs);
+                    if ((flags & ~VALID_METHOD_FLAGS) != 0) throw new IllegalArgumentException("invalid flags");
+                }
                 public void debugToString(StringBuffer sb, String constructorName) {
                     int flags = getFlags();
                     sb.append("  ").append(ClassFile.flagsToString(flags,false));