1 package org.ibex.classgen;
3 import java.util.StringTokenizer;
4 import java.util.Hashtable;
6 public abstract class Type implements CGConst {
8 private static Hashtable instances = new Hashtable(); // this has to appear at the top of the file
10 // Public API //////////////////////////////////////////////////////////////////////////////
12 public static final Type VOID = new Primitive("V", "void");
13 public static final Type INT = new Primitive("I", "int");
14 public static final Type LONG = new Primitive("J", "long");
15 public static final Type BOOLEAN = new Primitive("Z", "boolean");
16 public static final Type DOUBLE = new Primitive("D", "double");
17 public static final Type FLOAT = new Primitive("F", "float");
18 public static final Type BYTE = new Primitive("B", "byte");
19 public static final Type CHAR = new Primitive("C", "char");
20 public static final Type SHORT = new Primitive("S", "short");
22 public static final Type.Class OBJECT = Type.Class.instance("java.lang.Object");
23 public static final Type.Class STRING = Type.Class.instance("java.lang.String");
24 public static final Type.Class STRINGBUFFER = Type.Class.instance("java.lang.StringBuffer");
25 public static final Type.Class INTEGER_OBJECT = Type.Class.instance("java.lang.Integer");
26 public static final Type.Class DOUBLE_OBJECT = Type.Class.instance("java.lang.Double");
27 public static final Type.Class FLOAT_OBJECT = Type.Class.instance("java.lang.Float");
29 /** A zero element Type[] array (can be passed as the "args" param when a method takes no arguments */
30 public static final Type[] NO_ARGS = new Type[0];
32 /** guarantee: there will only be one instance of Type for a given descriptor ==> equals() and == are interchangeable */
33 public static Type instance(String d) {
34 Type ret = (Type)instances.get(d);
35 if (ret != null) return ret;
36 if (d.startsWith("[")) return new Type.Array(instance(d.substring(1)));
37 return new Type.Class(d);
40 public final String toString() { return super.toString(); }
41 public abstract String debugToString();
43 public final String getDescriptor() { return descriptor; }
45 public Type.Array makeArray() { return (Type.Array)instance("["+descriptor); }
46 public Type.Array makeArray(int i) { return i==0 ? (Type.Array)this : makeArray().makeArray(i-1); }
48 public Type.Ref asRef() { throw new RuntimeException("attempted to use "+this+" as a Type.Ref, which it is not"); }
49 public Type.Class asClass() { throw new RuntimeException("attempted to use "+this+" as a Type.Class, which it is not"); }
50 public Type.Array asArray() { throw new RuntimeException("attempted to use "+this+" as a Type.Array, which it is not"); }
51 public boolean isPrimitive() { return false; }
52 public boolean isRef() { return false; }
53 public boolean isClass() { return false; }
54 public boolean isArray() { return false; }
56 // Protected/Private //////////////////////////////////////////////////////////////////////////////
58 protected final String descriptor;
60 protected Type(String descriptor) {
61 this.descriptor = descriptor;
62 instances.put(descriptor, this);
65 public static class Primitive extends Type {
66 private String humanReadable;
67 Primitive(String descriptor, String humanReadable) {
69 this.humanReadable = humanReadable;
71 public String debugToString() { return humanReadable; }
72 public boolean isPrimitive() { return true; }
75 public abstract static class Ref extends Type {
76 protected Ref(String descriptor) { super(descriptor); }
77 public abstract String debugToString();
78 public Type.Ref asRef() { return this; }
79 public boolean isRef() { return true; }
82 public static class Array extends Type.Ref {
83 public final Type base;
84 protected Array(Type t) { super("[" + t.getDescriptor()); base = t; }
85 public Type.Array asArray() { return this; }
86 public boolean isArray() { return true; }
87 public String debugToString() { return base.debugToString() + "[]"; }
88 public Type getElementType() { return Type.instance(getDescriptor().substring(0, getDescriptor().length()-1)); }
91 public static class Class extends Type.Ref {
92 protected Class(String s) { super(_initHelper(s)); }
93 public Type.Class asClass() { return this; }
94 public boolean isClass() { return true; }
95 public static Type.Class instance(String className) {
96 return (Type.Class)Type.instance("L"+className.replace('.', '/')+";"); }
97 //public boolean extendsOrImplements(Type.Class c, Context cx) { }
98 String internalForm() { return descriptor.substring(1, descriptor.length()-1); }
99 public String debugToString() { return internalForm().replace('/','.'); }
100 public String getShortName() {
101 int p = descriptor.lastIndexOf('/');
102 return p == -1 ? descriptor.substring(1,descriptor.length()-1) : descriptor.substring(p+1,descriptor.length()-1);
104 private static String _initHelper(String s) {
105 if (!s.startsWith("L") || !s.endsWith(";")) s = "L" + s.replace('.', '/') + ";";
108 String[] components() {
109 StringTokenizer st = new StringTokenizer(descriptor.substring(1, descriptor.length()-1), "/");
110 String[] a = new String[st.countTokens()];
111 for(int i=0;st.hasMoreTokens();i++) a[i] = st.nextToken();
115 public Field field(String name, Type type) { return new Field(name, type); }
116 public abstract class Body extends HasFlags {
119 public Method method(String name, Type returnType, Type[] argTypes) { return new Method(name, returnType, argTypes); }
120 public Method method(String leftCrap, String rightCrap) { return method(leftCrap+rightCrap); }
122 /** see JVM Spec section 2.10.2 */
123 public Method method(String signature) {
124 // FEATURE: This parser is ugly but it works (and shouldn't be a problem) might want to clean it up though
125 String name = signature.substring(0, signature.indexOf('('));
126 String s = signature.substring(signature.indexOf('('));
127 if(!s.startsWith("(")) throw new IllegalArgumentException("invalid method type descriptor");
128 int p = s.indexOf(')');
129 if(p == -1) throw new IllegalArgumentException("invalid method type descriptor");
130 String argsDesc = s.substring(1,p);
131 String retDesc = s.substring(p+1);
132 Type[] argsBuf = new Type[argsDesc.length()];
134 for(i=0,p=0;argsDesc.length() > 0;i++,p=0) {
135 while(p < argsDesc.length() && argsDesc.charAt(p) == '[') p++;
136 if(p == argsDesc.length()) throw new IllegalArgumentException("invalid method type descriptor");
137 if(argsDesc.charAt(p) == 'L') {
138 p = argsDesc.indexOf(';');
139 if(p == -1) throw new IllegalArgumentException("invalid method type descriptor");
141 argsBuf[i] = Type.instance(argsDesc.substring(0,p+1));
142 argsDesc = argsDesc.substring(p+1);
144 Type args[] = new Type[i];
145 System.arraycopy(argsBuf,0,args,0,i);
146 return method(name, Type.instance(retDesc), args);
149 public abstract class Member {
150 public final String name;
151 private Member(String name) { this.name = name; }
152 public Type.Class getDeclaringClass() { return Type.Class.this; }
153 public String getName() { return name; }
154 public abstract String getTypeDescriptor();
155 public abstract String debugToString();
158 public class Field extends Member {
159 public final Type type;
160 private Field(String name, Type t) { super(name); this.type = t; }
161 public String getTypeDescriptor() { return type.getDescriptor(); }
162 public Type getType() { return type; }
163 public class Body extends HasFlags {
164 public final int flags;
165 public Body(int flags) { this.flags = flags; }
166 public int getFlags() { return flags; }
168 public String debugToString() { return getDeclaringClass().debugToString()+"."+name+"["+type.debugToString()+"]"; }
171 public class Method extends Member {
172 final Type[] argTypes;
173 public final Type returnType;
174 public Type getReturnType() { return returnType; }
175 public int getNumArgs() { return argTypes.length; }
176 public Type getArgType(int i) { return argTypes[i]; }
177 public Type[] getArgTypes() {
178 Type[] ret = new Type[argTypes.length];
179 System.arraycopy(argTypes, 0, ret, 0, ret.length);
182 public boolean isConstructor() { return getName().equals("<init>"); }
183 public boolean isClassInitializer() { return getName().equals("<clinit>"); }
184 public String debugToString() {
185 StringBuffer sb = new StringBuffer();
186 if (name.equals("<clinit>")) sb.append("static ");
188 if (name.equals("<init>"))
189 sb.append(Class.this.getShortName());
191 sb.append(returnType.debugToString()).append(".").append(name);
193 for(int i=0; i<argTypes.length; i++)
194 sb.append((i==0?"":", ")+argTypes[i].debugToString());
197 return sb.toString();
199 private Method(String name, Type returnType, Type[] argTypes) {
201 this.argTypes = argTypes;
202 this.returnType = returnType;
204 //public Method.Body getBody(Context cx) { }
205 public String getTypeDescriptor() {
206 StringBuffer sb = new StringBuffer(argTypes.length*4);
208 for(int i=0;i<argTypes.length;i++) sb.append(argTypes[i].getDescriptor());
210 sb.append(returnType.getDescriptor());
211 return sb.toString();
213 public abstract class Body extends HasFlags {
214 public abstract java.util.Hashtable getThrownExceptions();
215 public abstract void debugBodyToString(StringBuffer sb);
216 public void debugToString(StringBuffer sb, String constructorName) {
217 int flags = getFlags();
218 sb.append(" ").append(ClassFile.flagsToString(flags,false));
219 sb.append(Method.this.debugToString());
220 java.util.Hashtable thrownExceptions = getThrownExceptions();
221 if (thrownExceptions.size() > 0) {
223 for(java.util.Enumeration e = thrownExceptions.keys();e.hasMoreElements();)
224 sb.append(" ").append(((Type.Class)e.nextElement()).debugToString()).append(",");
225 sb.setLength(sb.length()-1);
228 if ((flags & (NATIVE|ABSTRACT))==0) {
230 debugBodyToString(sb);
240 // FEATURE: This probably isn't the best place for these
241 static String methodTypeDescriptor(Type[] argTypes, Type returnType) {
242 StringBuffer sb = new StringBuffer(argTypes.length*4);
244 for(int i=0;i<argTypes.length;i++) sb.append(argTypes[i].getDescriptor());
246 sb.append(returnType.getDescriptor());
247 return sb.toString();