Skip to content

Commit c754bfe

Browse files
committed
Fall back to getPubliclyAccessibleMethodIfPossible on IllegalAccessException
Closes gh-34028
1 parent 5aa1592 commit c754bfe

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

spring-core/src/main/java/org/springframework/util/MethodInvoker.java

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.util;
1818

19+
import java.lang.reflect.InaccessibleObjectException;
1920
import java.lang.reflect.InvocationTargetException;
2021
import java.lang.reflect.Method;
2122
import java.lang.reflect.Modifier;
@@ -178,7 +179,8 @@ public void prepare() throws ClassNotFoundException, NoSuchMethodException {
178179
Object[] arguments = getArguments();
179180
Class<?>[] argTypes = new Class<?>[arguments.length];
180181
for (int i = 0; i < arguments.length; ++i) {
181-
argTypes[i] = (arguments[i] != null ? arguments[i].getClass() : Object.class);
182+
Object argument = arguments[i];
183+
argTypes[i] = (argument != null ? argument.getClass() : Object.class);
182184
}
183185

184186
// Try to get the exact method first.
@@ -279,8 +281,20 @@ public Object invoke() throws InvocationTargetException, IllegalAccessException
279281
if (targetObject == null && !Modifier.isStatic(preparedMethod.getModifiers())) {
280282
throw new IllegalArgumentException("Target method must not be non-static without a target");
281283
}
282-
ReflectionUtils.makeAccessible(preparedMethod);
283-
return preparedMethod.invoke(targetObject, getArguments());
284+
try {
285+
ReflectionUtils.makeAccessible(preparedMethod);
286+
return preparedMethod.invoke(targetObject, getArguments());
287+
}
288+
catch (IllegalAccessException | InaccessibleObjectException ex) {
289+
if (targetObject != null) {
290+
Method fallbackMethod =
291+
ClassUtils.getPubliclyAccessibleMethodIfPossible(preparedMethod, targetObject.getClass());
292+
if (fallbackMethod != preparedMethod) {
293+
return fallbackMethod.invoke(targetObject, getArguments());
294+
}
295+
}
296+
throw ex;
297+
}
284298
}
285299

286300

@@ -307,12 +321,13 @@ public Object invoke() throws InvocationTargetException, IllegalAccessException
307321
public static int getTypeDifferenceWeight(Class<?>[] paramTypes, Object[] args) {
308322
int result = 0;
309323
for (int i = 0; i < paramTypes.length; i++) {
310-
if (!ClassUtils.isAssignableValue(paramTypes[i], args[i])) {
324+
Class<?> paramType = paramTypes[i];
325+
Object arg = args[i];
326+
if (!ClassUtils.isAssignableValue(paramType, arg)) {
311327
return Integer.MAX_VALUE;
312328
}
313-
if (args[i] != null) {
314-
Class<?> paramType = paramTypes[i];
315-
Class<?> superClass = args[i].getClass().getSuperclass();
329+
if (arg != null) {
330+
Class<?> superClass = arg.getClass().getSuperclass();
316331
while (superClass != null) {
317332
if (paramType.equals(superClass)) {
318333
result = result + 2;

0 commit comments

Comments
 (0)