mirror of
https://github.com/EsotericSoftware/reflectasm.git
synced 2025-01-14 05:40:23 +08:00
This commit is contained in:
parent
53f49b98e4
commit
d5ce2af867
@ -4,6 +4,7 @@ package com.esotericsoftware.reflectasm;
|
||||
import static org.objectweb.asm.Opcodes.*;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.objectweb.asm.ClassWriter;
|
||||
import org.objectweb.asm.Label;
|
||||
@ -15,8 +16,7 @@ public abstract class MethodAccess {
|
||||
static private AccessClassLoader loader = new AccessClassLoader();
|
||||
|
||||
static public MethodAccess get (Class type) {
|
||||
Method[] methods = type.getDeclaredMethods();
|
||||
|
||||
Method[] methods = type.getMethods();
|
||||
String className = type.getName();
|
||||
String accessClassName = className + "MethodAccess";
|
||||
Class accessClass = null;
|
||||
@ -63,7 +63,7 @@ public abstract class MethodAccess {
|
||||
for (int i = 0, n = methods.length; i < n; i++) {
|
||||
mv.visitLabel(labels[i]);
|
||||
if (i == 0)
|
||||
mv.visitFrame(Opcodes.F_APPEND, 1, new Object[] {"com/esotericsoftware/reflectasm/TestObject"}, 0, null);
|
||||
mv.visitFrame(Opcodes.F_APPEND, 1, new Object[] {classNameInternal}, 0, null);
|
||||
else
|
||||
mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
|
||||
mv.visitVarInsn(ALOAD, 4);
|
||||
@ -72,7 +72,6 @@ public abstract class MethodAccess {
|
||||
buffer.append('(');
|
||||
|
||||
Method method = methods[i];
|
||||
System.out.println(method);
|
||||
Class[] paramTypes = method.getParameterTypes();
|
||||
maxArgCount = Math.max(maxArgCount, paramTypes.length);
|
||||
for (int paramIndex = 0; paramIndex < paramTypes.length; paramIndex++) {
|
||||
@ -193,10 +192,16 @@ public abstract class MethodAccess {
|
||||
|
||||
abstract public Object invoke (Object object, int methodIndex, Object... args);
|
||||
|
||||
/**
|
||||
* Invokes the first method with the specified name.
|
||||
*/
|
||||
public Object invoke (Object object, String methodName, Object... args) {
|
||||
return invoke(object, getIndex(methodName), args);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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];
|
||||
@ -204,4 +209,12 @@ public abstract class MethodAccess {
|
||||
}
|
||||
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;
|
||||
}
|
||||
throw new IllegalArgumentException("Unable to find public method: " + methodName + " " + Arrays.toString(parameterTypes));
|
||||
}
|
||||
}
|
||||
|
@ -4,10 +4,9 @@ package com.esotericsoftware.reflectasm;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class MethodAccessTest extends TestCase {
|
||||
public void testIndexInvoke () {
|
||||
public void testInvoke () {
|
||||
MethodAccess access = MethodAccess.get(SomeClass.class);
|
||||
SomeClass someObject = new SomeClass();
|
||||
int index;
|
||||
Object value;
|
||||
|
||||
value = access.invoke(someObject, "getName");
|
||||
@ -30,6 +29,10 @@ public class MethodAccessTest extends TestCase {
|
||||
|
||||
value = access.invoke(someObject, "methodWithManyArguments", 1, 2f, 3, 4.2f, null, null, null);
|
||||
assertEquals("test", value);
|
||||
|
||||
int index = access.getIndex("methodWithManyArguments", int.class, float.class, Integer.class, Float.class, SomeClass.class,
|
||||
SomeClass.class, SomeClass.class);
|
||||
assertEquals(access.getIndex("methodWithManyArguments"), index);
|
||||
}
|
||||
|
||||
static public class SomeClass {
|
||||
|
@ -8,7 +8,7 @@ import com.esotericsoftware.reflectasm.FieldAccess;
|
||||
|
||||
public class FieldAccessBenchmark extends Benchmark {
|
||||
public FieldAccessBenchmark () throws Exception {
|
||||
int count = 10000000;
|
||||
int count = 1000000;
|
||||
Object[] dontCompileMeAway = new Object[count];
|
||||
|
||||
FieldAccess access = FieldAccess.get(SomeClass.class);
|
||||
|
@ -8,7 +8,7 @@ import com.esotericsoftware.reflectasm.MethodAccess;
|
||||
|
||||
public class MethodAccessBenchmark extends Benchmark {
|
||||
public MethodAccessBenchmark () throws Exception {
|
||||
int count = 1000000;
|
||||
int count = 100000;
|
||||
Object[] dontCompileMeAway = new Object[count];
|
||||
|
||||
MethodAccess access = MethodAccess.get(SomeClass.class);
|
||||
|
Loading…
Reference in New Issue
Block a user