introduced Type.Ref as common superclass of Type.Class and Type.Array
authoradam <adam@megacz.com>
Fri, 3 Jun 2005 04:09:50 +0000 (04:09 +0000)
committeradam <adam@megacz.com>
Fri, 3 Jun 2005 04:09:50 +0000 (04:09 +0000)
darcs-hash:20050603040950-5007d-5109d9dffdb00afb65ad6317fa96485a78901d1e.gz

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

index 3682fe9..8ac9e7f 100644 (file)
@@ -93,12 +93,12 @@ class CPGen {
                     NameAndTypeKey nt = (NameAndTypeKey) e2.key();
                     Type t = Type.fromDescriptor(nt.type);
                     if(t == null) throw new ClassGen.ClassReadExn("invalid type descriptor");
-                    return new FieldRef((Type.Object)e1.key(), nt.name, t);
+                    return new FieldRef((Type.Class)e1.key(), nt.name, t);
                 }
                 case 10: case 11: {
                     NameAndTypeKey nt = (NameAndTypeKey) e2.key();
                     if (e1.key() == null) throw new Error(e1.tag + " => " + e1.key());
-                    return new MethodRef((Type.Object)e1.key(), "methodname", Type.VOID, new Type[0]); // FIXME FIXME
+                    return new MethodRef((Type.Class)e1.key(), "methodname", Type.VOID, new Type[0]); // FIXME FIXME
                 }
                 case 12: {
                     return new NameAndTypeKey(((Utf8Ent)e1).s, ((Utf8Ent)e2).s); 
@@ -190,9 +190,9 @@ class CPGen {
             return ent;
         }
         
-        if(o instanceof Type.Object) {
+        if(o instanceof Type.Class) {
             CPRefEnt ce = new CPRefEnt(this, 7);
-            ce.e1 = addUtf8(((Type.Object)o).internalForm());
+            ce.e1 = addUtf8(((Type.Class)o).internalForm());
             ent = ce;
         } else if(o instanceof String) {
             CPRefEnt ce = new CPRefEnt(this, 8);
index 7ddd161..e336723 100644 (file)
@@ -5,9 +5,9 @@ import java.io.*;
 
 /** Class generation object representing the whole classfile */
 public class ClassGen implements CGConst {
-    private final Type.Object thisType;
-    private final Type.Object superType;
-    private final Type.Object[] interfaces;
+    private final Type.Class thisType;
+    private final Type.Class superType;
+    private final Type.Class[] interfaces;
     private short minor;
     private short major;
     final int flags;
@@ -60,13 +60,13 @@ public class ClassGen implements CGConst {
         sb.append("}");
     }
 
-    /** @see #ClassGen(Type.Object, Type.Object, int) */
+    /** @see #ClassGen(Type.Class, Type.Class, int) */
     public ClassGen(String name, String superName, int flags) {
-        this(Type.fromDescriptor(name).asObject(), Type.fromDescriptor(superName).asObject(), flags);
+        this(Type.fromDescriptor(name).asClass(), Type.fromDescriptor(superName).asClass(), flags);
     }
 
-    /** @see #ClassGen(Type.Object, Type.Object, int, Type.Object[]) */
-    public ClassGen(Type.Object thisType, Type.Object superType, int flags) {
+    /** @see #ClassGen(Type.Class, Type.Class, int, Type.Class[]) */
+    public ClassGen(Type.Class thisType, Type.Class superType, int flags) {
         this(thisType, superType, flags, null);
     }
     
@@ -75,7 +75,7 @@ public class ClassGen implements CGConst {
         @param superType The superclass of the generated class (commonly Type.OBJECT) 
         @param flags The access flags for this class (ACC_PUBLIC, ACC_FINAL, ACC_SUPER, ACC_INTERFACE, and ACC_ABSTRACT)
     */
-    public ClassGen(Type.Object thisType, Type.Object superType, int flags, Type.Object[] interfaces) {
+    public ClassGen(Type.Class thisType, Type.Class superType, int flags, Type.Class[] interfaces) {
         if((flags & ~(ACC_PUBLIC|ACC_FINAL|ACC_SUPER|ACC_INTERFACE|ACC_ABSTRACT)) != 0)
             throw new IllegalArgumentException("invalid flags");
         this.thisType = thisType;
@@ -220,10 +220,10 @@ public class ClassGen implements CGConst {
         //if (major != 45 && major != 46) throw new ClassReadExn("invalid major version");
         cp = new CPGen(i);
         flags = i.readShort();
-        thisType = (Type.Object)cp.getType(i.readShort());
-        superType = (Type.Object)cp.getType(i.readShort());
-        interfaces = new Type.Object[i.readShort()];
-        for(int j=0; j<interfaces.length; j++) interfaces[j] = (Type.Object)cp.getType(i.readShort());
+        thisType = (Type.Class)cp.getType(i.readShort());
+        superType = (Type.Class)cp.getType(i.readShort());
+        interfaces = new Type.Class[i.readShort()];
+        for(int j=0; j<interfaces.length; j++) interfaces[j] = (Type.Class)cp.getType(i.readShort());
         int numFields = i.readShort();
         for(int j=0; j<numFields; j++) fields.add(new FieldGen(cp, i));
         int numMethods = i.readShort();
@@ -249,11 +249,11 @@ public class ClassGen implements CGConst {
         @see FieldRef
     */
     public static abstract class FieldOrMethodRef {
-        Type.Object klass;
+        Type.Class klass;
         String name;
         String descriptor;
         
-        FieldOrMethodRef(Type.Object klass, String name, String descriptor) { this.klass = klass; this.name = name; this.descriptor = descriptor; }
+        FieldOrMethodRef(Type.Class klass, String name, String descriptor) { this.klass = klass; this.name = name; this.descriptor = descriptor; }
         FieldOrMethodRef(FieldOrMethodRef o) { this.klass = o.klass; this.name = o.name; this.descriptor = o.descriptor; }
         public boolean equals(Object o_) {
             if(!(o_ instanceof FieldOrMethodRef)) return false;
@@ -334,7 +334,7 @@ public class ClassGen implements CGConst {
             }
         } else {
             /*
-            Type.Object me = new Type.Object("Test");
+            Type.Class me = new Type.Class("Test");
             ClassGen cg = new ClassGen("Test", "java.lang.Object", ACC_PUBLIC|ACC_SUPER|ACC_FINAL);
             FieldGen fg = cg.addField("foo", Type.INT, ACC_PUBLIC|ACC_STATIC);
         
@@ -344,10 +344,10 @@ public class ClassGen implements CGConst {
             //mg.add(ISTORE_0);
             mg.add(PUTSTATIC, fieldRef(me, "foo", Type.INT));
             int top = mg.size();
-            mg.add(GETSTATIC, cg.fieldRef(new Type.Object("java.lang.System"), "out", new Type.Object("java.io.PrintStream")));
+            mg.add(GETSTATIC, cg.fieldRef(new Type.Class("java.lang.System"), "out", new Type.Class("java.io.PrintStream")));
             //mg.add(ILOAD_0);
             mg.add(GETSTATIC, cg.fieldRef(me, "foo", Type.INT));
-            mg.add(INVOKEVIRTUAL, cg.methodRef(new Type.Object("java.io.PrintStream"), "println",
+            mg.add(INVOKEVIRTUAL, cg.methodRef(new Type.Class("java.io.PrintStream"), "println",
                                                Type.VOID, new Type[]{Type.INT}));
             //mg.add(IINC, new int[]{0, 1});
             //mg.add(ILOAD_0);
index 9548a24..9a4b790 100644 (file)
@@ -9,9 +9,9 @@ GETFIELD, PUTFIELD, GETSTATIC, and PUTSTATCI bytecodes
 */
 public class FieldRef extends ClassGen.FieldOrMethodRef {
     /** Create a reference to field <i>name</i> of class <i>c</i> with the type <i>t</i>  */    
-    public FieldRef(Type.Object c, String name, Type t) { super(c, name, t.getDescriptor()); }
-    /** Equivalent to FieldRef(new Type.Object(s), ...)
-        @see #FieldRef(Type.Object, String, Type, )
+    public FieldRef(Type.Class c, String name, Type t) { super(c, name, t.getDescriptor()); }
+    /** Equivalent to FieldRef(new Type.Class(s), ...)
+        @see #FieldRef(Type.Class, String, Type, )
     */
-    public FieldRef(String s, String name, Type t) { this(Type.fromDescriptor(s).asObject(), name, t); }
+    public FieldRef(String s, String name, Type t) { this(Type.fromDescriptor(s).asClass(), name, t); }
 }
index 2d3a0fc..632fc79 100644 (file)
@@ -110,7 +110,7 @@ public class MethodGen implements CGConst {
         @param handler The instruction of the excepton handler
         @param type The type of exception that is to be handled (MUST inherit from Throwable)
     */
-    public final void addExceptionHandler(int start, int end, int handler, Type.Object type) {
+    public final void addExceptionHandler(int start, int end, int handler, Type.Class type) {
         exnTable.put(type, new ExnTableEnt(start, end, handler, cp.add(type)));
     }
     
@@ -118,7 +118,7 @@ public class MethodGen implements CGConst {
         NOTE: This isn't enforced by the JVM. This is for reference only. A method can throw exceptions not declared to be thrown 
         @param type The type of exception that can be thrown 
     */
-    public final void addThrow(Type.Object type) {
+    public final void addThrow(Type.Class type) {
         thrownExceptions.put(type, cp.add(type));
     }
     
index deec819..6460280 100644 (file)
@@ -10,14 +10,14 @@ package org.ibex.classgen;
 public class MethodRef extends ClassGen.FieldOrMethodRef {
     /** Create a reference to method <i>name</i> of class <i>c</i> with the return type <i>ret</i> and the
         arguments <i>args</i> */
-    public MethodRef(Type.Object c, String name, Type ret, Type[] args) {
+    public MethodRef(Type.Class c, String name, Type ret, Type[] args) {
         super(c, name, getDescriptor(ret, args));
     }
-    /** Equivalent to MethodRef(new Type.Object(s), ...)
-        @see #MethodRef(Type.Object, String, Type, Type[])
+    /** Equivalent to MethodRef(new Type.Class(s), ...)
+        @see #MethodRef(Type.Class, String, Type, Type[])
     */
     public MethodRef(String s, String name, Type ret, Type[] args) {
-        this(Type.fromDescriptor(s).asObject(), name, ret, args);
+        this(Type.fromDescriptor(s).asClass(), name, ret, args);
     }
     MethodRef(MethodRef i) { super(i); }
     
@@ -35,7 +35,7 @@ public class MethodRef extends ClassGen.FieldOrMethodRef {
         users don't need to be concerned with this though because MethodRef's are automatically converted
         to MethodRef.I's when they are applied to an INVOKEINTERFACE bytecode */
     public static class I extends MethodRef {
-        public I(Type.Object c, String name, Type ret, Type[] args) { super(c, name, ret, args); }
+        public I(Type.Class c, String name, Type ret, Type[] args) { super(c, name, ret, args); }
         public I(String s, String name, Type ret, Type[] args) { super(s, name, ret, args); }
         I(MethodRef m) { super(m); }
     }
index 0a35c65..bc6160c 100644 (file)
@@ -19,12 +19,12 @@ public class Type {
     public static final Type CHAR = new Type("C", "char");
     public static final Type SHORT = new Type("S", "short");
     
-    public static final Type.Object OBJECT = new Type.Object("java.lang.Object");
-    public static final Type.Object STRING = new Type.Object("java.lang.String");
-    public static final Type.Object STRINGBUFFER = new Type.Object("java.lang.StringBuffer");
-    public static final Type.Object INTEGER_OBJECT = new Type.Object("java.lang.Integer");
-    public static final Type.Object DOUBLE_OBJECT = new Type.Object("java.lang.Double");
-    public static final Type.Object FLOAT_OBJECT = new Type.Object("java.lang.Float");
+    public static final Type.Class OBJECT = new Type.Class("java.lang.Object");
+    public static final Type.Class STRING = new Type.Class("java.lang.String");
+    public static final Type.Class STRINGBUFFER = new Type.Class("java.lang.StringBuffer");
+    public static final Type.Class INTEGER_OBJECT = new Type.Class("java.lang.Integer");
+    public static final Type.Class DOUBLE_OBJECT = new Type.Class("java.lang.Double");
+    public static final Type.Class FLOAT_OBJECT = new Type.Class("java.lang.Float");
     
     /** 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];
@@ -34,7 +34,7 @@ public class Type {
         Type ret = (Type)instances.get(d);
         if (ret != null) return ret;
         if (d.endsWith("[")) return new Type.Array(fromDescriptor(d.substring(d.length()-1)));
-        return new Type.Object(d);
+        return new Type.Class(d);
     }
 
     public       String  toString() { return toString; }
@@ -42,11 +42,15 @@ public class Type {
     public       int     hashCode() { return descriptor.hashCode(); }
     public       boolean equals(java.lang.Object o) { return this==o; }
 
-    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 Type.Array  makeArray() { return new Type.Array(this); }
-    public boolean     isObject() { return false; }
-    public boolean     isArray() { return false; }
+    public Type.Array  makeArray() { return (Type.Array)fromDescriptor(descriptor+"["); }
+
+    public Type.Ref    asRef()       { throw new RuntimeException("attempted to use "+this+" as a Type.Ref, which it is not"); }
+    public Type.Class  asClass()     { throw new RuntimeException("attempted to use "+this+" as a Type.Class, 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     isPrimitive() { return !isRef(); }
+    public boolean     isRef()       { return false; }
+    public boolean     isClass()     { return false; }
+    public boolean     isArray()     { return false; }
 
     // Protected/Private //////////////////////////////////////////////////////////////////////////////
 
@@ -58,12 +62,17 @@ public class Type {
         instances.put(this.descriptor = descriptor, this);
     }
 
-    /** Class representing Object types (any non-primitive type) */
-    public static class Object extends Type {
-        protected Object(String s) { super(_initHelper(s), _initHelper2(s)); }
-        protected Object(String descriptor, String hr) { super(_initHelper(descriptor), _initHelper2(hr)); }
-        public Type.Object asObject() { return this; }
-        public boolean isObject() { return true; }
+    public static class Ref extends Type {
+        protected Ref(String descriptor) { super(descriptor); }
+        protected Ref(String descriptor, String humanReadable) { super(descriptor, humanReadable); }
+        public    Type.Ref asRef() { return this; }
+        public    boolean  isRef() { return true; }
+    }
+
+    public static class Class extends Type.Ref {
+        protected Class(String s) { super(_initHelper(s), _initHelper2(s)); }
+        public Type.Class asClass() { return this; }
+        public boolean isClass() { return true; }
         public String getShortName() { return toString.substring(toString.lastIndexOf('.')+1); }
         String internalForm() { return descriptor.substring(1, descriptor.length()-1); }
         private static String _initHelper(String s) {
@@ -82,12 +91,11 @@ public class Type {
         }
     }    
 
-    public static class Array extends Type.Object {
+    public static class Array extends Type.Ref {
         protected Array(Type t) { super(t.getDescriptor() + "[", t.toString() + "[]"); }
         public Type.Array asArray() { return this; }
         public boolean isArray() { return true; }
         public int dimension() { return descriptor.length() - descriptor.indexOf('['); }
-        String[] components() { throw new Error("Type.Array does not have components()"); }
     }
 
 }