X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fibex%2Fclassgen%2FType.java;h=21b2f03a06a5c73f80c10b092aa6ba6edcdfb7e1;hb=325a99a87fc2c459b2e143832402793fac2ab916;hp=bc6160c932037f5a912dce43314965804c6bcb3d;hpb=313d0d7b61cbae37ba11f48b752aebd52be68b5c;p=org.ibex.classgen.git diff --git a/src/org/ibex/classgen/Type.java b/src/org/ibex/classgen/Type.java index bc6160c..21b2f03 100644 --- a/src/org/ibex/classgen/Type.java +++ b/src/org/ibex/classgen/Type.java @@ -30,10 +30,10 @@ public class Type { 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 fromDescriptor(String d) { + public static Type instance(String d) { Type ret = (Type)instances.get(d); if (ret != null) return ret; - if (d.endsWith("[")) return new Type.Array(fromDescriptor(d.substring(d.length()-1))); + if (d.startsWith("[")) return new Type.Array(instance(d.substring(1))); return new Type.Class(d); } @@ -42,7 +42,7 @@ public class Type { public int hashCode() { return descriptor.hashCode(); } public boolean equals(java.lang.Object o) { return this==o; } - public Type.Array makeArray() { return (Type.Array)fromDescriptor(descriptor+"["); } + public Type.Array makeArray() { return (Type.Array)instance("["+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"); } @@ -69,6 +69,13 @@ public class Type { public boolean isRef() { return true; } } + 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 getDescriptor().lastIndexOf('['); } + } + public static class Class extends Type.Ref { protected Class(String s) { super(_initHelper(s), _initHelper2(s)); } public Type.Class asClass() { return this; } @@ -89,13 +96,48 @@ public class Type { for(int i=0;st.hasMoreTokens();i++) a[i] = st.nextToken(); return a; } - } - 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('['); } - } + public Method method(String name, Type returnType, Type[] argTypes) { return new Method(name, returnType, argTypes); } + public Field field(String name, Type type) { return new Field(name, type); } + + public abstract class Member { + public final String name; + private Member(String name) { this.name = name; } + public Type.Class getDeclaringClass() { return Type.Class.this; } + public abstract String getDescriptor(); + public boolean equals(Object o_) { + if(!(o_ instanceof Member)) return false; + Member o = (Member) o_; + return o.getDeclaringClass().equals(getDeclaringClass()) && + o.name.equals(name) && + o.getDescriptor().equals(getDescriptor()); + } + public int hashCode() { return getDeclaringClass().hashCode() ^ name.hashCode() ^ getDescriptor().hashCode(); } + } + + public class Field extends Member { + public final Type type; + private Field(String name, Type t) { super(name); this.type = t; } + public String getDescriptor() { return name; } + } + + public class Method extends Member { + final Type[] argTypes; + final Type returnType; + private Method(String name, Type returnType, Type[] argTypes) { + super(name); + this.argTypes = argTypes; + this.returnType = returnType; + } + public String getDescriptor() { + StringBuffer sb = new StringBuffer(argTypes.length*4); + sb.append("("); + for(int i=0;i