Fixed issue 7.

This commit is contained in:
Nathan Sweet 2012-06-13 11:04:21 +00:00
parent 829461228b
commit 4ef6cb7b08
4 changed files with 42 additions and 30 deletions

View File

@ -31,6 +31,10 @@ public abstract class FieldAccess {
nextClass = nextClass.getSuperclass(); nextClass = nextClass.getSuperclass();
} }
String[] fieldNames = new String[fields.size()];
for (int i = 0, n = fieldNames.length; i < n; i++)
fieldNames[i] = fields.get(i).getName();
String className = type.getName(); String className = type.getName();
String accessClassName = className + "FieldAccess"; String accessClassName = className + "FieldAccess";
if (accessClassName.startsWith("java.")) accessClassName = "reflectasm." + accessClassName; if (accessClassName.startsWith("java.")) accessClassName = "reflectasm." + accessClassName;
@ -215,24 +219,22 @@ public abstract class FieldAccess {
} }
try { try {
FieldAccess access = (FieldAccess)accessClass.newInstance(); FieldAccess access = (FieldAccess)accessClass.newInstance();
access.fields = fields.toArray(new Field[fields.size()]); access.fieldNames = fieldNames;
return access; return access;
} catch (Exception ex) { } catch (Exception ex) {
throw new RuntimeException("Error constructing field access class: " + accessClassName, ex); throw new RuntimeException("Error constructing field access class: " + accessClassName, ex);
} }
} }
private Field[] fields; private String[] fieldNames;
abstract public void set (Object object, int fieldIndex, Object value); abstract public void set (Object object, int fieldIndex, Object value);
abstract public Object get (Object object, int fieldIndex); abstract public Object get (Object object, int fieldIndex);
public int getIndex (String fieldName) { public int getIndex (String fieldName) {
for (int i = 0, n = fields.length; i < n; i++) { for (int i = 0, n = fieldNames.length; i < n; i++)
Field field = fields[i]; if (fieldNames[i].equals(fieldName)) return i;
if (field.getName().equals(fieldName)) return i;
}
throw new IllegalArgumentException("Unable to find public field: " + fieldName); throw new IllegalArgumentException("Unable to find public field: " + fieldName);
} }
@ -244,7 +246,7 @@ public abstract class FieldAccess {
return get(object, getIndex(fieldName)); return get(object, getIndex(fieldName));
} }
public Field[] getFields () { public String[] getFieldNames () {
return fields; return fieldNames;
} }
} }

View File

@ -32,6 +32,14 @@ public abstract class MethodAccess {
nextClass = nextClass.getSuperclass(); nextClass = nextClass.getSuperclass();
} }
Class[][] parameterTypes = new Class[methods.size()][];
String[] methodNames = new String[methods.size()];
for (int i = 0, n = methodNames.length; i < n; i++) {
Method method = methods.get(i);
methodNames[i] = method.getName();
parameterTypes[i] = method.getParameterTypes();
}
String className = type.getName(); String className = type.getName();
String accessClassName = className + "MethodAccess"; String accessClassName = className + "MethodAccess";
if (accessClassName.startsWith("java.")) accessClassName = "reflectasm." + accessClassName; if (accessClassName.startsWith("java.")) accessClassName = "reflectasm." + accessClassName;
@ -196,14 +204,16 @@ public abstract class MethodAccess {
} }
try { try {
MethodAccess access = (MethodAccess)accessClass.newInstance(); MethodAccess access = (MethodAccess)accessClass.newInstance();
access.methods = methods.toArray(new Method[methods.size()]); access.methodNames = methodNames;
access.parameterTypes = parameterTypes;
return access; return access;
} catch (Exception ex) { } catch (Exception ex) {
throw new RuntimeException("Error constructing method access class: " + accessClassName, ex); throw new RuntimeException("Error constructing method access class: " + accessClassName, ex);
} }
} }
private Method[] methods; private String[] methodNames;
private Class[][] parameterTypes;
abstract public Object invoke (Object object, int methodIndex, Object... args); abstract public Object invoke (Object object, int methodIndex, Object... args);
@ -214,22 +224,22 @@ public abstract class MethodAccess {
/** Returns the index of the first method with the specified name. */ /** Returns the index of the first method with the specified name. */
public int getIndex (String methodName) { public int getIndex (String methodName) {
for (int i = 0, n = methods.length; i < n; i++) { for (int i = 0, n = methodNames.length; i < n; i++)
Method method = methods[i]; if (methodNames[i].equals(methodName)) return i;
if (method.getName().equals(methodName)) return i;
}
throw new IllegalArgumentException("Unable to find public method: " + methodName); throw new IllegalArgumentException("Unable to find public method: " + methodName);
} }
public int getIndex (String methodName, Class... parameterTypes) { public int getIndex (String methodName, Class... paramTypes) {
for (int i = 0, n = methods.length; i < n; i++) { for (int i = 0, n = methodNames.length; i < n; i++)
Method method = methods[i]; if (methodNames[i].equals(methodName) && Arrays.equals(paramTypes, parameterTypes[i])) return i;
if (method.getName().equals(methodName) && Arrays.equals(parameterTypes, method.getParameterTypes())) return i;
}
throw new IllegalArgumentException("Unable to find public method: " + methodName + " " + Arrays.toString(parameterTypes)); throw new IllegalArgumentException("Unable to find public method: " + methodName + " " + Arrays.toString(parameterTypes));
} }
public Method[] getMethods () { public String[] getMethodNames () {
return methods; return methodNames;
}
public Class[][] getParameterTypes () {
return parameterTypes;
} }
} }

View File

@ -43,25 +43,25 @@ public class FieldAccessTest extends TestCase {
access.getIndex("name"); access.getIndex("name");
fail(); fail();
} catch (IllegalArgumentException expected) { } catch (IllegalArgumentException expected) {
expected.printStackTrace(); // expected.printStackTrace();
} }
try { try {
access.get(new EmptyClass(), "meow"); access.get(new EmptyClass(), "meow");
fail(); fail();
} catch (IllegalArgumentException expected) { } catch (IllegalArgumentException expected) {
expected.printStackTrace(); // expected.printStackTrace();
} }
try { try {
access.get(new EmptyClass(), 0); access.get(new EmptyClass(), 0);
fail(); fail();
} catch (IllegalArgumentException expected) { } catch (IllegalArgumentException expected) {
expected.printStackTrace(); // expected.printStackTrace();
} }
try { try {
access.set(new EmptyClass(), "foo", "moo"); access.set(new EmptyClass(), "foo", "moo");
fail(); fail();
} catch (IllegalArgumentException expected) { } catch (IllegalArgumentException expected) {
expected.printStackTrace(); // expected.printStackTrace();
} }
} }

View File

@ -43,31 +43,31 @@ public class MethodAccessTest extends TestCase {
access.getIndex("name"); access.getIndex("name");
fail(); fail();
} catch (IllegalArgumentException expected) { } catch (IllegalArgumentException expected) {
expected.printStackTrace(); // expected.printStackTrace();
} }
try { try {
access.getIndex("name", String.class); access.getIndex("name", String.class);
fail(); fail();
} catch (IllegalArgumentException expected) { } catch (IllegalArgumentException expected) {
expected.printStackTrace(); // expected.printStackTrace();
} }
try { try {
access.invoke(new EmptyClass(), "meow", "moo"); access.invoke(new EmptyClass(), "meow", "moo");
fail(); fail();
} catch (IllegalArgumentException expected) { } catch (IllegalArgumentException expected) {
expected.printStackTrace(); // expected.printStackTrace();
} }
try { try {
access.invoke(new EmptyClass(), 0); access.invoke(new EmptyClass(), 0);
fail(); fail();
} catch (IllegalArgumentException expected) { } catch (IllegalArgumentException expected) {
expected.printStackTrace(); // expected.printStackTrace();
} }
try { try {
access.invoke(new EmptyClass(), 0, "moo"); access.invoke(new EmptyClass(), 0, "moo");
fail(); fail();
} catch (IllegalArgumentException expected) { } catch (IllegalArgumentException expected) {
expected.printStackTrace(); // expected.printStackTrace();
} }
} }