Skip to content

Commit 7326e35

Browse files
committed
Optimization: sort the matched methods only instead of all
1 parent 2c34d1d commit 7326e35

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

java/src/main/java/org/astonbitecode/j4rs/api/invocation/JsonInvocationImpl.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,6 @@ Method findMethodInHierarchy(Class clazz, String methodName, Class[] argTypes) t
261261
.filter(m -> m.getName().equals(methodName))
262262
// Match the params number
263263
.filter(m -> m.getGenericParameterTypes().length == argTypes.length)
264-
// Sort the methods to prefer methods with specific parameter types over methods with generic types
265-
.sorted((m1, m2) -> Long.compare(getGenericTypeCount(m1), getGenericTypeCount(m2)))
266264
// Match the actual parameters
267265
.filter(m -> {
268266
// Each element of the matchedParams list shows whether a parameter is matched
@@ -317,8 +315,14 @@ Method findMethodInHierarchy(Class clazz, String methodName, Class[] argTypes) t
317315
}
318316
return matchedParams.stream().allMatch(Boolean::booleanValue);
319317
}).collect(Collectors.toList());
320-
if (!found.isEmpty()) {
318+
if (found.size() == 1) {
321319
return found.get(0);
320+
} else if(found.size() > 1) {
321+
return found.stream()
322+
// Sort the methods to prefer methods with specific parameter types over methods with generic types
323+
.sorted((m1, m2) -> Long.compare(getGenericTypeCount(m1), getGenericTypeCount(m2)))
324+
.findFirst()
325+
.get();
322326
} else {
323327
Class<?> superclass = clazz.getSuperclass();
324328
if (superclass == null) {
441 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)