added Type.Array
authoradam <adam@megacz.com>
Fri, 3 Jun 2005 00:46:57 +0000 (00:46 +0000)
committeradam <adam@megacz.com>
Fri, 3 Jun 2005 00:46:57 +0000 (00:46 +0000)
darcs-hash:20050603004657-5007d-07c488d1ef0342168596f74114a9b9df827eec48.gz

src/org/ibex/classgen/CPGen.java
src/org/ibex/classgen/ClassGen.java
src/org/ibex/classgen/FieldRef.java
src/org/ibex/classgen/MethodRef.java
src/org/ibex/classgen/Type.java

index 25f22be..01ef840 100644 (file)
@@ -88,7 +88,7 @@ class CPGen {
         private String fixme() { throw new Error("fixme"); }
         Object key() throws ClassGen.ClassReadExn {
             switch(tag) {
         private String fixme() { throw new Error("fixme"); }
         Object key() throws ClassGen.ClassReadExn {
             switch(tag) {
-                case 7: return new Type.Object(((Utf8Ent)e0).s);
+                case 7: return Type.fromDescriptor(((Utf8Ent)e0).s);
                 case 8: return (((Utf8Ent)e1).s);
                 case 9: {
                     NameAndTypeKey nt = (NameAndTypeKey) e2.key();
                 case 8: return (((Utf8Ent)e1).s);
                 case 9: {
                     NameAndTypeKey nt = (NameAndTypeKey) e2.key();
@@ -156,7 +156,7 @@ class CPGen {
         return ent.n;
     }
 
         return ent.n;
     }
 
-    public final Type.Object getType(int index) { return new Type.Object(((Utf8Ent)getByIndex(index)).s); }
+    public final Type.Object getType(int index) { return Type.fromDescriptor(((Utf8Ent)getByIndex(index)).s).asObject(); }
 
     public final Ent getByIndex(int index) {
         if(state < STABLE) throw new IllegalStateException("constant pool is not stable");
 
     public final Ent getByIndex(int index) {
         if(state < STABLE) throw new IllegalStateException("constant pool is not stable");
index c4b7e77..1300c3a 100644 (file)
@@ -19,7 +19,7 @@ public class ClassGen implements CGConst {
     
     /** @see #ClassGen(Type.Object,Type.Object,int) */
     public ClassGen(String name, String superName, int flags) {
     
     /** @see #ClassGen(Type.Object,Type.Object,int) */
     public ClassGen(String name, String superName, int flags) {
-        this(new Type.Object(name),new Type.Object(superName),flags);
+        this(Type.fromDescriptor(name).asObject(), Type.fromDescriptor(superName).asObject(), flags);
     }
 
     /** @see #ClassGen(Type.Object,Type.Object,int,Type.Object[]) */
     }
 
     /** @see #ClassGen(Type.Object,Type.Object,int,Type.Object[]) */
index 10b1b1e..2d6bc0e 100644 (file)
@@ -13,5 +13,5 @@ public class FieldRef extends ClassGen.FieldOrMethodRef {
     /** Equivalent to FieldRef(new Type.Object(s),...)
         @see #FieldRef(Type.Object,String,Type,)
     */
     /** Equivalent to FieldRef(new Type.Object(s),...)
         @see #FieldRef(Type.Object,String,Type,)
     */
-    public FieldRef(String s, String name, Type t) { this(new Type.Object(s),name,t); }
+    public FieldRef(String s, String name, Type t) { this(Type.fromDescriptor(s).asObject(), name, t); }
 }
 }
index 75d58f3..52ccf2f 100644 (file)
@@ -17,7 +17,7 @@ public class MethodRef extends ClassGen.FieldOrMethodRef {
         @see #MethodRef(Type.Object,String,Type,Type[])
     */
     public MethodRef(String s, String name, Type ret, Type[] args) {
         @see #MethodRef(Type.Object,String,Type,Type[])
     */
     public MethodRef(String s, String name, Type ret, Type[] args) {
-        this(new Type.Object(s),name,ret,args);
+        this(Type.fromDescriptor(s).asObject(), name, ret, args);
     }
     MethodRef(MethodRef i) { super(i); }
     
     }
     MethodRef(MethodRef i) { super(i); }
     
index ec5a30a..e301414 100644 (file)
@@ -25,7 +25,7 @@ public class Type {
     
     final String descriptor;
     
     
     final String descriptor;
     
-    Type(String descriptor) { this.descriptor = descriptor; }
+    protected Type(String descriptor) { this.descriptor = descriptor; }
     
     public static Type fromDescriptor(String descriptor) {
         if(descriptor.equals("V")) return VOID;
     
     public static Type fromDescriptor(String descriptor) {
         if(descriptor.equals("V")) return VOID;
@@ -37,6 +37,8 @@ public class Type {
         if(descriptor.equals("B")) return BYTE;
         if(descriptor.equals("C")) return CHAR;
         if(descriptor.equals("S")) return SHORT;
         if(descriptor.equals("B")) return BYTE;
         if(descriptor.equals("C")) return CHAR;
         if(descriptor.equals("S")) return SHORT;
+        if(descriptor.endsWith("[")) return new Type.Array(fromDescriptor(descriptor.substring(0, descriptor.indexOf('['))),
+                                                           descriptor.length() - descriptor.indexOf('['));
         if(Type.Object.validDescriptorString(descriptor)) return new Type.Object(descriptor);
         return null;
     }
         if(Type.Object.validDescriptorString(descriptor)) return new Type.Object(descriptor);
         return null;
     }
@@ -56,20 +58,22 @@ public class Type {
         @param dim Number if dimensions
         @return A one dimensional array of the base type
     */
         @param dim Number if dimensions
         @return A one dimensional array of the base type
     */
-    public static Type arrayType(Type base, int dim) {
-        StringBuffer sb = new StringBuffer(base.descriptor.length() + dim);
-        for(int i=0;i<dim;i++) sb.append("[");
-        sb.append(base.descriptor);
-        return new Type(sb.toString());
-    }
-    
-    /** Class representing Objec types (any non-primitive type) */
+    public static Type arrayType(Type base, int dim) { return new Type.Array(base, dim); }
+
+    public Type.Object asObject() { throw new RuntimeException("attempted to use "+this+" as a Type.Object, which it is not"); }
+    public Type.Array asArray() { throw new RuntimeException("attempted to use "+this+" as a Type.Array, which it is not"); }
+    public boolean isObject() { return false; }
+    public boolean isArray() { return false; }
+
+    /** Class representing Object types (any non-primitive type) */
     public static class Object extends Type {
         /** Create an Type.Object instance for the specified string. <i>s</i> can be a string in the form
             "java.lang.String", "java/lang/String", or "Ljava/lang/String;".
             @param s The type */
     public static class Object extends Type {
         /** Create an Type.Object instance for the specified string. <i>s</i> can be a string in the form
             "java.lang.String", "java/lang/String", or "Ljava/lang/String;".
             @param s The type */
-        public Object(String s) { super(_initHelper(s)); }
-        
+        protected Object(String s) { super(_initHelper(s)); }
+        public Type.Object asObject() { return this; }
+        public boolean isObject() { return true; }
+
         private static String _initHelper(String s) {
             if(!s.startsWith("L") || !s.endsWith(";")) s = "L" + s.replace('.','/') + ";";
             if(!validDescriptorString(s)) throw new IllegalArgumentException("invalid descriptor string");
         private static String _initHelper(String s) {
             if(!s.startsWith("L") || !s.endsWith(";")) s = "L" + s.replace('.','/') + ";";
             if(!validDescriptorString(s)) throw new IllegalArgumentException("invalid descriptor string");
@@ -89,4 +93,17 @@ public class Type {
             return s.startsWith("L") && s.endsWith(";");
         }
     }    
             return s.startsWith("L") && s.endsWith(";");
         }
     }    
+
+    public static class Array extends Object {
+        protected Array(Type t, int dim) {  super(arrayify(t,dim)); }
+        public Type.Array asArray() { return this; }
+        public boolean isArray() { return true; }
+        private static String arrayify(Type t, int dim) {
+            StringBuffer sb = new StringBuffer(t.descriptor.length() + dim);
+            for(int i=0;i<dim;i++) sb.append("[");
+            sb.append(t.descriptor);
+            return sb.toString();
+        }
+    }
+
 }
 }