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];
33 * A "descriptor" is the classfile-mangled text representation of a type (see JLS section 4.3)
34 * guarantee: there will only be one instance of Type for a given descriptor ==> equals() and == are interchangeable
36 public static Type fromDescriptor(String d) {
37 Type ret = (Type)instances.get(d);
38 if (ret != null) return ret;
39 if (d.startsWith("[")) return new Type.Array(Type.fromDescriptor(d.substring(1)));
40 return new Type.Class(d);
43 public final String toString() { return super.toString(); }
44 public abstract String debugToString();
46 public final String getDescriptor() { return descriptor; }
48 public Type.Array makeArray() { return (Type.Array)Type.fromDescriptor("["+descriptor); }
49 public Type.Array makeArray(int i) { return i==0 ? (Type.Array)this : makeArray().makeArray(i-1); }
51 public Type.Ref asRef() { throw new RuntimeException("attempted to use "+this+" as a Type.Ref, which it is not"); }
52 public Type.Class asClass() { throw new RuntimeException("attempted to use "+this+" as a Type.Class, which it is not"); }
53 public Type.Array asArray() { throw new RuntimeException("attempted to use "+this+" as a Type.Array, which it is not"); }
54 public boolean isPrimitive() { return false; }
55 public boolean isRef() { return false; }
56 public boolean isClass() { return false; }
57 public boolean isArray() { return false; }
59 // Protected/Private //////////////////////////////////////////////////////////////////////////////
61 protected final String descriptor;
63 protected Type(String descriptor) {
64 this.descriptor = descriptor;
65 instances.put(descriptor, this);
68 public static class Primitive extends Type {
69 private String humanReadable;
70 Primitive(String descriptor, String humanReadable) {
72 this.humanReadable = humanReadable;
74 public String debugToString() { return humanReadable; }
75 public boolean isPrimitive() { return true; }
78 public abstract static class Ref extends Type {
79 protected Ref(String descriptor) { super(descriptor); }
80 public abstract String debugToString();
81 public Type.Ref asRef() { return this; }
82 public boolean isRef() { return true; }
85 public static class Array extends Type.Ref {
86 public final Type base;
87 protected Array(Type t) { super("[" + t.getDescriptor()); base = t; }
88 public Type.Array asArray() { return this; }
89 public boolean isArray() { return true; }
90 public String debugToString() { return base.debugToString() + "[]"; }
91 public Type getElementType() { return Type.fromDescriptor(getDescriptor().substring(0, getDescriptor().length()-1)); }
94 public static class Class extends Type.Ref {
95 protected Class(String s) { super(_initHelper(s)); }
96 public Type.Class asClass() { return this; }
97 public boolean isClass() { return true; }
98 public static Type.Class instance(String className) {
99 return (Type.Class)Type.fromDescriptor("L"+className.replace('.', '/')+";"); }
100 public boolean extendsOrImplements(Type.Class c, Context cx) {
101 if (this==c) return true;
102 if (this==OBJECT) return false;
103 ClassFile cf = cx.resolve(getName());
105 System.err.println("warning: could not resolve class " + getName());
108 if (cf.superType == c) return true;
109 for(int i=0; i<cf.interfaces.length; i++) if (cf.interfaces[i].extendsOrImplements(c,cx)) return true;
110 if (cf.superType == null) return false;
111 return cf.superType.extendsOrImplements(c, cx);
113 String internalForm() { return descriptor.substring(1, descriptor.length()-1); }
114 public String debugToString() { return internalForm().replace('/','.'); }
115 public String getName() { return internalForm().replace('/','.'); }
116 public String getShortName() {
117 int p = descriptor.lastIndexOf('/');
118 return p == -1 ? descriptor.substring(1,descriptor.length()-1) : descriptor.substring(p+1,descriptor.length()-1);
120 private static String _initHelper(String s) {
121 if (!s.startsWith("L") || !s.endsWith(";")) throw new Error("invalid");
124 String[] components() {
125 StringTokenizer st = new StringTokenizer(descriptor.substring(1, descriptor.length()-1), "/");
126 String[] a = new String[st.countTokens()];
127 for(int i=0;st.hasMoreTokens();i++) a[i] = st.nextToken();
131 public Type.Class.Body getBody(Context cx) { return cx.resolve(this.getName()); }
132 public abstract class Body extends HasAttributes {
133 public abstract Type.Class.Method.Body[] methods();
134 public abstract Type.Class.Field.Body addField(Type.Class.Field field, int flags);
135 public abstract Type.Class.Field.Body[] fields();
136 public Body(int flags, ClassFile.AttrGen attrs) {
138 if ((flags & ~(PUBLIC|FINAL|SUPER|INTERFACE|ABSTRACT)) != 0)
139 throw new IllegalArgumentException("invalid flags: " + Integer.toString(flags,16));
143 public Field field(String name, Type type) { return new Field(name, type); }
144 public Field field(String name, String descriptor) { return field(name,Type.fromDescriptor(descriptor)); }
146 public Method method(String name, Type returnType, Type[] argTypes) { return new Method(name, returnType, argTypes); }
148 /** see JVM Spec section 2.10.2 */
149 public Method method(String name, String descriptor) {
150 // FEATURE: This parser is ugly but it works (and shouldn't be a problem) might want to clean it up though
151 String s = descriptor;
152 if(!s.startsWith("(")) throw new IllegalArgumentException("invalid method type descriptor");
153 int p = s.indexOf(')');
154 if(p == -1) throw new IllegalArgumentException("invalid method type descriptor");
155 String argsDesc = s.substring(1,p);
156 String retDesc = s.substring(p+1);
157 Type[] argsBuf = new Type[argsDesc.length()];
159 for(i=0,p=0;argsDesc.length() > 0;i++,p=0) {
160 while(p < argsDesc.length() && argsDesc.charAt(p) == '[') p++;
161 if(p == argsDesc.length()) throw new IllegalArgumentException("invalid method type descriptor");
162 if(argsDesc.charAt(p) == 'L') {
163 p = argsDesc.indexOf(';');
164 if(p == -1) throw new IllegalArgumentException("invalid method type descriptor");
166 argsBuf[i] = Type.fromDescriptor(argsDesc.substring(0,p+1));
167 argsDesc = argsDesc.substring(p+1);
169 Type args[] = new Type[i];
170 System.arraycopy(argsBuf,0,args,0,i);
171 return method(name, Type.fromDescriptor(retDesc), args);
174 public abstract class Member {
175 public final String name;
176 private Member(String name) { this.name = name; }
177 public Type.Class getDeclaringClass() { return Type.Class.this; }
178 public String getName() { return name; }
179 public abstract String getTypeDescriptor();
180 public abstract String debugToString();
183 public class Field extends Member {
184 public final Type type;
185 private Field(String name, Type t) { super(name); this.type = t; }
186 public String getTypeDescriptor() { return type.getDescriptor(); }
187 public Type getType() { return type; }
188 public String debugToString() { return getDeclaringClass().debugToString()+"."+name+"["+type.debugToString()+"]"; }
189 public class Body extends HasAttributes {
190 public Field getField() { return Field.this; }
191 public Body(int flags, ClassFile.AttrGen attrs) {
193 if ((flags & ~VALID_FIELD_FLAGS) != 0) throw new IllegalArgumentException("invalid flags");
198 public class Method extends Member {
199 final Type[] argTypes;
200 public final Type returnType;
201 public Type getReturnType() { return returnType; }
202 public int getNumArgs() { return argTypes.length; }
203 public Type getArgType(int i) { return argTypes[i]; }
204 public Type[] getArgTypes() {
205 Type[] ret = new Type[argTypes.length];
206 System.arraycopy(argTypes, 0, ret, 0, ret.length);
209 public boolean isConstructor() { return getName().equals("<init>"); }
210 public boolean isClassInitializer() { return getName().equals("<clinit>"); }
211 public String debugToString() {
212 StringBuffer sb = new StringBuffer();
213 if (name.equals("<clinit>")) sb.append("static ");
215 if (name.equals("<init>"))
216 sb.append(Class.this.getShortName());
218 sb.append(returnType.debugToString()).append(".").append(name);
220 for(int i=0; i<argTypes.length; i++)
221 sb.append((i==0?"":", ")+argTypes[i].debugToString());
224 return sb.toString();
226 private Method(String name, Type returnType, Type[] argTypes) {
228 this.argTypes = argTypes;
229 this.returnType = returnType;
231 //public Method.Body getBody(Context cx) { }
232 public String getTypeDescriptor() {
233 StringBuffer sb = new StringBuffer(argTypes.length*4);
235 for(int i=0;i<argTypes.length;i++) sb.append(argTypes[i].getDescriptor());
237 sb.append(returnType.getDescriptor());
238 return sb.toString();
240 public abstract class Body extends HasAttributes {
241 public abstract java.util.Hashtable getThrownExceptions();
242 public abstract void debugBodyToString(StringBuffer sb);
243 public Method getMethod() { return Method.this; }
244 public Body(int flags, ClassFile.AttrGen attrs) {
246 if ((flags & ~VALID_METHOD_FLAGS) != 0) throw new IllegalArgumentException("invalid flags");
248 public void debugToString(StringBuffer sb, String constructorName) {
249 int flags = getFlags();
250 sb.append(" ").append(ClassFile.flagsToString(flags,false));
251 sb.append(Method.this.debugToString());
252 java.util.Hashtable thrownExceptions = getThrownExceptions();
253 if (thrownExceptions.size() > 0) {
255 for(java.util.Enumeration e = thrownExceptions.keys();e.hasMoreElements();)
256 sb.append(" ").append(((Type.Class)e.nextElement()).debugToString()).append(",");
257 sb.setLength(sb.length()-1);
260 if ((flags & (NATIVE|ABSTRACT))==0) {
262 debugBodyToString(sb);
272 // FEATURE: This probably isn't the best place for these
273 static String methodTypeDescriptor(Type[] argTypes, Type returnType) {
274 StringBuffer sb = new StringBuffer(argTypes.length*4);
276 for(int i=0;i<argTypes.length;i++) sb.append(argTypes[i].getDescriptor());
278 sb.append(returnType.getDescriptor());
279 return sb.toString();