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 getDescriptor() { return descriptor; }
45 public Type.Array makeArray() { return (Type.Array)Type.fromDescriptor("["+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 toString() { 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 toString();
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 toString() { return base.toString() + "[]"; }
88 public Type getElementType() { return Type.fromDescriptor(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.fromDescriptor("L"+className.replace('.', '/')+";"); }
97 public boolean extendsOrImplements(Type.Class c, Context cx) {
98 if (this==c) return true;
99 if (this==OBJECT) return false;
100 ClassFile cf = cx.resolve(getName());
102 System.err.println("warning: could not resolve class " + getName());
105 if (cf.superType == c) return true;
106 for(int i=0; i<cf.interfaces.length; i++) if (cf.interfaces[i].extendsOrImplements(c,cx)) return true;
107 if (cf.superType == null) return false;
108 return cf.superType.extendsOrImplements(c, cx);
110 String internalForm() { return descriptor.substring(1, descriptor.length()-1); }
111 public String toString() { return internalForm().replace('/','.'); }
112 public String getName() { return internalForm().replace('/','.'); }
113 public String getShortName() {
114 int p = descriptor.lastIndexOf('/');
115 return p == -1 ? descriptor.substring(1,descriptor.length()-1) : descriptor.substring(p+1,descriptor.length()-1);
117 private static String _initHelper(String s) {
118 if (!s.startsWith("L") || !s.endsWith(";")) throw new Error("invalid");
121 String[] components() {
122 StringTokenizer st = new StringTokenizer(descriptor.substring(1, descriptor.length()-1), "/");
123 String[] a = new String[st.countTokens()];
124 for(int i=0;st.hasMoreTokens();i++) a[i] = st.nextToken();
128 public Type.Class.Body getBody(Context cx) { return cx.resolve(this.getName()); }
129 public abstract class Body extends HasAttributes {
130 public abstract Type.Class.Method.Body[] methods();
131 public abstract Type.Class.Field.Body addField(Type.Class.Field field, int flags);
132 public abstract Type.Class.Field.Body[] fields();
133 public Body(int flags, ClassFile.AttrGen attrs) {
135 if ((flags & ~(PUBLIC|FINAL|SUPER|INTERFACE|ABSTRACT)) != 0)
136 throw new IllegalArgumentException("invalid flags: " + Integer.toString(flags,16));
140 public Field field(String name, Type type) { return new Field(name, type); }
141 public Field field(String name, String descriptor) { return field(name,Type.fromDescriptor(descriptor)); }
143 public Method method(String name, Type returnType, Type[] argTypes) { return new Method(name, returnType, argTypes); }
145 /** see JVM Spec section 2.10.2 */
146 public Method method(String name, String descriptor) {
147 // FEATURE: This parser is ugly but it works (and shouldn't be a problem) might want to clean it up though
148 String s = descriptor;
149 if(!s.startsWith("(")) throw new IllegalArgumentException("invalid method type descriptor");
150 int p = s.indexOf(')');
151 if(p == -1) throw new IllegalArgumentException("invalid method type descriptor");
152 String argsDesc = s.substring(1,p);
153 String retDesc = s.substring(p+1);
154 Type[] argsBuf = new Type[argsDesc.length()];
156 for(i=0,p=0;argsDesc.length() > 0;i++,p=0) {
157 while(p < argsDesc.length() && argsDesc.charAt(p) == '[') p++;
158 if(p == argsDesc.length()) throw new IllegalArgumentException("invalid method type descriptor");
159 if(argsDesc.charAt(p) == 'L') {
160 p = argsDesc.indexOf(';');
161 if(p == -1) throw new IllegalArgumentException("invalid method type descriptor");
163 argsBuf[i] = Type.fromDescriptor(argsDesc.substring(0,p+1));
164 argsDesc = argsDesc.substring(p+1);
166 Type args[] = new Type[i];
167 System.arraycopy(argsBuf,0,args,0,i);
168 return method(name, Type.fromDescriptor(retDesc), args);
171 public abstract class Member {
172 public final String name;
173 private Member(String name) { this.name = name; }
174 public Type.Class getDeclaringClass() { return Type.Class.this; }
175 public String getName() { return name; }
176 public abstract String getTypeDescriptor();
177 public abstract String toString();
180 public class Field extends Member {
181 public final Type type;
182 private Field(String name, Type t) { super(name); this.type = t; }
183 public String getTypeDescriptor() { return type.getDescriptor(); }
184 public Type getType() { return type; }
185 public String toString() { return getDeclaringClass().toString()+"."+name+"["+type.toString()+"]"; }
186 public class Body extends HasAttributes {
187 public Field getField() { return Field.this; }
188 public Body(int flags, ClassFile.AttrGen attrs) {
190 if ((flags & ~VALID_FIELD_FLAGS) != 0) throw new IllegalArgumentException("invalid flags");
195 public class Method extends Member {
196 final Type[] argTypes;
197 public final Type returnType;
198 public Type getReturnType() { return returnType; }
199 public int getNumArgs() { return argTypes.length; }
200 public Type getArgType(int i) { return argTypes[i]; }
201 public Type[] getArgTypes() {
202 Type[] ret = new Type[argTypes.length];
203 System.arraycopy(argTypes, 0, ret, 0, ret.length);
206 public boolean isConstructor() { return getName().equals("<init>"); }
207 public boolean isClassInitializer() { return getName().equals("<clinit>"); }
209 public String toString() {
210 StringBuffer sb = new StringBuffer();
211 if (name.equals("<clinit>")) sb.append("static ");
213 if (name.equals("<init>"))
214 sb.append(Class.this.getShortName());
216 sb.append(returnType.toString()).append(".").append(name);
218 for(int i=0; i<argTypes.length; i++)
219 sb.append((i==0?"":", ")+argTypes[i].toString());
222 return sb.toString();
224 private Method(String name, Type returnType, Type[] argTypes) {
226 this.argTypes = argTypes;
227 this.returnType = returnType;
229 //public Method.Body getBody(Context cx) { }
230 public String getTypeDescriptor() {
231 StringBuffer sb = new StringBuffer(argTypes.length*4);
233 for(int i=0;i<argTypes.length;i++) sb.append(argTypes[i].getDescriptor());
235 sb.append(returnType.getDescriptor());
236 return sb.toString();
238 public abstract class Body extends HasAttributes {
239 public abstract java.util.Hashtable getThrownExceptions();
240 public abstract void debugBodyToString(StringBuffer sb);
241 public Method getMethod() { return Method.this; }
242 public Body(int flags, ClassFile.AttrGen attrs) {
244 if ((flags & ~VALID_METHOD_FLAGS) != 0) throw new IllegalArgumentException("invalid flags");
246 public boolean isConcrete() { return !isAbstract() && !isNative() /*FIXME: !inAnInterface*/; }
247 public void toString(StringBuffer sb, String constructorName) {
248 int flags = getFlags();
249 sb.append(" ").append(ClassFile.flagsToString(flags,false));
250 sb.append(Method.this.toString());
251 java.util.Hashtable thrownExceptions = getThrownExceptions();
252 if (thrownExceptions.size() > 0) {
254 for(java.util.Enumeration e = thrownExceptions.keys();e.hasMoreElements();)
255 sb.append(" ").append(((Type.Class)e.nextElement()).toString()).append(",");
256 sb.setLength(sb.length()-1);
259 if ((flags & (NATIVE|ABSTRACT))==0) {
261 debugBodyToString(sb);
271 // FEATURE: This probably isn't the best place for these
272 static String methodTypeDescriptor(Type[] argTypes, Type returnType) {
273 StringBuffer sb = new StringBuffer(argTypes.length*4);
275 for(int i=0;i<argTypes.length;i++) sb.append(argTypes[i].getDescriptor());
277 sb.append(returnType.getDescriptor());
278 return sb.toString();