From 313d0d7b61cbae37ba11f48b752aebd52be68b5c Mon Sep 17 00:00:00 2001 From: adam Date: Fri, 3 Jun 2005 04:09:50 +0000 Subject: [PATCH] introduced Type.Ref as common superclass of Type.Class and Type.Array darcs-hash:20050603040950-5007d-5109d9dffdb00afb65ad6317fa96485a78901d1e.gz --- src/org/ibex/classgen/CPGen.java | 8 +++--- src/org/ibex/classgen/ClassGen.java | 34 ++++++++++++------------ src/org/ibex/classgen/FieldRef.java | 8 +++--- src/org/ibex/classgen/MethodGen.java | 4 +-- src/org/ibex/classgen/MethodRef.java | 10 +++---- src/org/ibex/classgen/Type.java | 48 ++++++++++++++++++++-------------- 6 files changed, 60 insertions(+), 52 deletions(-) diff --git a/src/org/ibex/classgen/CPGen.java b/src/org/ibex/classgen/CPGen.java index 3682fe9..8ac9e7f 100644 --- a/src/org/ibex/classgen/CPGen.java +++ b/src/org/ibex/classgen/CPGen.java @@ -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); diff --git a/src/org/ibex/classgen/ClassGen.java b/src/org/ibex/classgen/ClassGen.java index 7ddd161..e336723 100644 --- a/src/org/ibex/classgen/ClassGen.java +++ b/src/org/ibex/classgen/ClassGen.java @@ -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; jname of class c with the type t */ - 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); } } diff --git a/src/org/ibex/classgen/MethodGen.java b/src/org/ibex/classgen/MethodGen.java index 2d3a0fc..632fc79 100644 --- a/src/org/ibex/classgen/MethodGen.java +++ b/src/org/ibex/classgen/MethodGen.java @@ -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)); } diff --git a/src/org/ibex/classgen/MethodRef.java b/src/org/ibex/classgen/MethodRef.java index deec819..6460280 100644 --- a/src/org/ibex/classgen/MethodRef.java +++ b/src/org/ibex/classgen/MethodRef.java @@ -10,14 +10,14 @@ package org.ibex.classgen; public class MethodRef extends ClassGen.FieldOrMethodRef { /** Create a reference to method name of class c with the return type ret and the arguments args */ - 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); } } diff --git a/src/org/ibex/classgen/Type.java b/src/org/ibex/classgen/Type.java index 0a35c65..bc6160c 100644 --- a/src/org/ibex/classgen/Type.java +++ b/src/org/ibex/classgen/Type.java @@ -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()"); } } } -- 1.7.10.4