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();
}
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 accessClassName = className + "FieldAccess";
if (accessClassName.startsWith("java.")) accessClassName = "reflectasm." + accessClassName;
@ -215,24 +219,22 @@ public abstract class FieldAccess {
}
try {
FieldAccess access = (FieldAccess)accessClass.newInstance();
access.fields = fields.toArray(new Field[fields.size()]);
access.fieldNames = fieldNames;
return access;
} catch (Exception 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 Object get (Object object, int fieldIndex);
public int getIndex (String fieldName) {
for (int i = 0, n = fields.length; i < n; i++) {
Field field = fields[i];
if (field.getName().equals(fieldName)) return i;
}
for (int i = 0, n = fieldNames.length; i < n; i++)
if (fieldNames[i].equals(fieldName)) return i;
throw new IllegalArgumentException("Unable to find public field: " + fieldName);
}
@ -244,7 +246,7 @@ public abstract class FieldAccess {
return get(object, getIndex(fieldName));
}
public Field[] getFields () {
return fields;
public String[] getFieldNames () {
return fieldNames;
}
}

View File

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

View File

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

View File

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