last ditch efford to separate names and type descriptors
[org.ibex.classgen.git] / src / org / ibex / classgen / Type.java
index 6e20f67..491e3bb 100644 (file)
@@ -97,15 +97,28 @@ public abstract class Type implements CGConst {
         public boolean isClass() { return true; }
         public static Type.Class instance(String className) {
             return (Type.Class)Type.fromDescriptor("L"+className.replace('.', '/')+";"); }
-        //public boolean extendsOrImplements(Type.Class c, Context cx) { }
+        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() {
@@ -115,7 +128,11 @@ public abstract class Type implements CGConst {
             return a;
         }
 
+        public Type.Class.Body getBody(Context cx) { return cx.resolve(this.getName()); }
         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)
@@ -124,15 +141,14 @@ public abstract class Type implements CGConst {
         }
 
         public Field field(String name, Type type) { return new Field(name, type); }
+        public Field field(String name, String descriptor) { return field(name,Type.fromDescriptor(descriptor)); }
 
         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); }
 
         /** see JVM Spec section 2.10.2 */
-        public Method method(String signature) {
+        public Method method(String name, String descriptor) {
             // FEATURE: This parser is ugly but it works (and shouldn't be a problem) might want to clean it up though
-            String name = signature.substring(0, signature.indexOf('('));
-            String s = signature.substring(signature.indexOf('('));
+            String s = descriptor;
             if(!s.startsWith("(")) throw new IllegalArgumentException("invalid method type descriptor");
             int p = s.indexOf(')');
             if(p == -1) throw new IllegalArgumentException("invalid method type descriptor");
@@ -224,6 +240,7 @@ public abstract class Type implements CGConst {
             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");