+ public Field field(String name, Type type) { return new Field(name, type); }
+ public Field field(String name, String descriptor) { return field(name,Type.fromDescriptor(descriptor)); }
+
+ public Method method(String name, Type returnType, Type[] argTypes) { return new Method(name, returnType, argTypes); }
+
+ /** see JVM Spec section 2.10.2 */
+ public Method method(String name, String descriptor) {
+ // FEATURE: This parser is ugly but it works (and shouldn't be a problem) might want to clean it up though
+ String s = descriptor;
+ 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.fromDescriptor(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.fromDescriptor(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 String getName() { return name; }
+ public abstract String getTypeDescriptor();
+ public abstract String toString();
+ public abstract int hashCode();
+ public abstract boolean equals(Object o);
+ }
+
+ 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 toString() { return getDeclaringClass().toString()+"."+name+"["+type.toString()+"]"; }
+ public class Body extends HasAttributes {
+ public Field getField() { return Field.this; }
+ public Body(int flags, ClassFile.AttrGen attrs) {
+ super(flags, attrs);
+ if ((flags & ~VALID_FIELD_FLAGS) != 0) throw new IllegalArgumentException("invalid flags");
+ }
+ }
+ public int hashCode() {
+ return type.hashCode() ^ name.hashCode() ^ getDeclaringClass().hashCode();
+ }
+ public boolean equals(Object o_) {
+ if(o_ == this) return true;
+ if(!(o_ instanceof Field)) return false;
+ Field o = (Field) o_;
+ return o.getDeclaringClass() == getDeclaringClass() && o.type == type && o.name.equals(name);
+ }
+ }
+
+ public class Method extends Member {
+ final Type[] argTypes;
+ public final Type returnType;
+ public Type getReturnType() { return returnType; }
+ public int getNumArgs() { return argTypes.length; }
+ public Type getArgType(int i) { return argTypes[i]; }
+ public Type[] getArgTypes() {
+ Type[] ret = new Type[argTypes.length];
+ System.arraycopy(argTypes, 0, ret, 0, ret.length);
+ return ret;
+ }
+ public boolean isConstructor() { return getName().equals("<init>"); }
+ public boolean isClassInitializer() { return getName().equals("<clinit>"); }
+
+ public String toString() {
+ StringBuffer sb = new StringBuffer();
+ if (name.equals("<clinit>")) sb.append("static ");
+ else {
+ if (name.equals("<init>"))
+ sb.append(Class.this.getShortName());
+ else
+ sb.append(returnType.toString()).append(" ").append(name);
+ sb.append("(");
+ for(int i=0; i<argTypes.length; i++)
+ sb.append((i==0?"":", ")+argTypes[i].toString());
+ sb.append(") ");
+ }
+ return sb.toString();
+ }
+ private Method(String name, Type returnType, Type[] argTypes) {
+ super(name);
+ this.argTypes = argTypes;
+ this.returnType = returnType;
+ }
+ //public Method.Body getBody(Context cx) { }
+ public String getTypeDescriptor() {
+ StringBuffer sb = new StringBuffer(argTypes.length*4);
+ sb.append("(");
+ for(int i=0;i<argTypes.length;i++) sb.append(argTypes[i].getDescriptor());
+ sb.append(")");
+ sb.append(returnType.getDescriptor());
+ return sb.toString();
+ }
+ public abstract class Body extends HasAttributes {
+ public abstract java.util.Hashtable getThrownExceptions();
+ public abstract void debugBodyToString(StringBuffer sb);
+ public Method getMethod() { return Method.this; }
+ public Body(int flags, ClassFile.AttrGen attrs) {
+ super(flags, attrs);
+ if ((flags & ~VALID_METHOD_FLAGS) != 0) throw new IllegalArgumentException("invalid flags");
+ }
+ public boolean isConcrete() { return !isAbstract() && !isNative() /*FIXME: !inAnInterface*/; }
+ public void toString(StringBuffer sb, String constructorName) {
+ int flags = getFlags();
+ sb.append(" ").append(ClassFile.flagsToString(flags,false));
+ sb.append(Method.this.toString());
+ java.util.Hashtable thrownExceptions = getThrownExceptions();
+ if (thrownExceptions.size() > 0) {
+ sb.append("throws");
+ for(java.util.Enumeration e = thrownExceptions.keys();e.hasMoreElements();)
+ sb.append(" ").append(((Type.Class)e.nextElement()).toString()).append(",");
+ sb.setLength(sb.length()-1);
+ sb.append(" ");
+ }
+ if ((flags & (NATIVE|ABSTRACT))==0) {
+ sb.append("{\n");
+ debugBodyToString(sb);
+ sb.append(" }\n");
+ } else {
+ sb.append(";");
+ }
+ }
+ }
+ public int hashCode() {
+ int h = returnType.hashCode() ^ name.hashCode() ^ getDeclaringClass().hashCode();
+ for(int i=0;i<argTypes.length;i++) h ^= argTypes[i].hashCode();
+ return h;
+ }
+ public boolean equals(Object o_) {
+ if(o_ == this) return true;
+ if(!(o_ instanceof Method)) return false;
+ Method o = (Method) o_;
+ if(!(o.getDeclaringClass() == getDeclaringClass() && o.returnType == returnType && o.name.equals(name))) return false;
+ if(o.argTypes.length != argTypes.length) return false;
+ for(int i=0;i<argTypes.length;i++)
+ if(o.argTypes[i] != argTypes[i]) return false;
+ return true;
+ }