+
+ public Field field(String name, Type type) { return new Field(name, type); }
+ public Method method(String name, Type returnType, Type[] argTypes) { return new Method(name, returnType, argTypes); }
+ public Method method(String name, String typeDescriptor) {
+ // FEATURE: This parser is ugly but it works (and shouldn't be a problem) might want to clean it up though
+ String s = typeDescriptor;
+ if(!s.startsWith("(")) throw new IllegalArgumentException("invalid method type descriptor");
+ int p = s.indexOf(')');
+ if(p == -1) throw new IllegalArgumentException("invalid method type descriptor");
+ String argsDesc = s.substring(1,p);
+ String retDesc = s.substring(p+1);
+ Type[] argsBuf = new Type[argsDesc.length()];
+ int i;
+ for(i=0,p=0;argsDesc.length() > 0;i++,p=0) {
+ while(p < argsDesc.length() && argsDesc.charAt(p) == '[') p++;
+ if(p == argsDesc.length()) throw new IllegalArgumentException("invalid method type descriptor");
+ if(argsDesc.charAt(p) == 'L') {
+ p = argsDesc.indexOf(';');
+ if(p == -1) throw new IllegalArgumentException("invalid method type descriptor");
+ }
+ argsBuf[i] = Type.instance(argsDesc.substring(0,p+1));
+ argsDesc = argsDesc.substring(p+1);
+ }
+ Type args[] = new Type[i];
+ System.arraycopy(argsBuf,0,args,0,i);
+ return method(name, Type.instance(retDesc), args);
+ }
+
+ 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 String toString() { return debugToString(); }
+ public abstract String debugToString();
+ }
+
+ public class Field extends Member {
+ public final Type type;
+ private Field(String name, Type t) { super(name); this.type = t; }
+ public String getTypeDescriptor() { return type.getDescriptor(); }
+ public Type getType() { return type; }
+ public String debugToString() { return getDeclaringClass()+"."+name+"["+type+"]"; }