Skip to content

Commit

Permalink
Merge pull request #1 from ccqy66/master
Browse files Browse the repository at this point in the history
🐛 fix same method bug
  • Loading branch information
hellokaton authored May 2, 2022
2 parents 1320e02 + a01454a commit 44584ef
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/main/java/com/blade/reflectasm/MethodAccess.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public abstract class MethodAccess {
private String[] methodNames;
private Class[][] parameterTypes;
private Class[] returnTypes;
private Map<Method, Integer> methodIndexs;
private Map<String, Integer> methodNameIndexes;

abstract public Object invoke(Object object, int methodIndex, Object... args);
Expand All @@ -36,6 +37,13 @@ public Object invoke(Object object, String methodName, Class[] paramTypes, Objec
return invoke(object, getIndex(methodName, paramTypes), args);
}

/**
* Invokes the method with the specified method and the specified param types.
*/
public Object invoke(Object object,Method method,Class[] paramTypes,Object...args) {
return invoke(object,getIndex(method,paramTypes),args);
}

/**
* Invokes the first method with the specified name and the specified number of arguments.
*/
Expand All @@ -56,6 +64,18 @@ public int getIndex(String methodName) {
throw new IllegalArgumentException("Unable to find non-private method: " + methodName);
}

/**
* Returns the index of the method with the method.
*/
private int getIndex(Method method, Class[] paramTypes) {
Integer index = 0;
if ((index = methodIndexs.get(method)) != null) {
return index;
}
index = getIndex(method.getName(),paramTypes);
methodIndexs.put(method,index);
return index;
}
/**
* Returns the index of the first method with the specified name and param types.
*/
Expand Down Expand Up @@ -100,7 +120,6 @@ static public MethodAccess get(Class type, List<Method> methods) {
for (int i = 0; i < n; i++) {
Method method = methods.get(i);
methodNames[i] = method.getName();
methodNameIndexes.put(method.getName(), i);
parameterTypes[i] = method.getParameterTypes();
returnTypes[i] = method.getReturnType();
}
Expand Down Expand Up @@ -281,6 +300,7 @@ else if (Modifier.isStatic(methods.get(i).getModifiers()))
access.parameterTypes = parameterTypes;
access.returnTypes = returnTypes;
access.methodNameIndexes = methodNameIndexes;
access.methodIndexs = new HashMap<>(n);
return access;
} catch (Throwable t) {
throw new RuntimeException("Error constructing method access class: " + accessClassName, t);
Expand Down
40 changes: 40 additions & 0 deletions src/test/java/com/blade/reflectasm/SameMethodTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.blade.reflectasm;

import junit.framework.TestCase;

import java.lang.reflect.Method;

/**
* @author: chenchen_839@126.com
* @date: 2018/7/18
*/
public class SameMethodTest extends TestCase{

public void testSame() throws NoSuchMethodException {
MethodAccess access = MethodAccess.get(SameMethodTest.Demo.class);
Demo demo = new Demo();
Method method = demo.getClass().getMethod("register",String.class);
Object result = null;
long start = System.currentTimeMillis();
for (int i = 0;i<=100000000;i++) {
result = access.invoke(demo,"register",new Class[]{Integer.class},i);
}
long end = System.currentTimeMillis();
for (int i = 0;i<=100000000;i++) {
result = access.invoke(demo, method, new Class[]{Integer.class}, i);
}
long fina = System.currentTimeMillis();

System.out.println(end-start);
System.out.println(fina - end);
}

public class Demo{
public String register(String name) {
return name;
}
public String register(Integer name) {
return name.toString();
}
}
}

0 comments on commit 44584ef

Please sign in to comment.