当没有传递参数类型并有方法重载时,需优先获取无参数的方法名

This commit is contained in:
thinkgem
2024-06-20 17:56:25 +08:00
parent b50e6005da
commit 66b4932d9d

View File

@@ -47,6 +47,9 @@ 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 = methodNames.length; i < n; i++)
if (methodNames[i].equals(methodName) && parameterTypes[i].length == 0) 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 non-private method: " + methodName);