16
16
17
17
package org .springframework .util ;
18
18
19
+ import java .lang .reflect .InaccessibleObjectException ;
19
20
import java .lang .reflect .InvocationTargetException ;
20
21
import java .lang .reflect .Method ;
21
22
import java .lang .reflect .Modifier ;
@@ -178,7 +179,8 @@ public void prepare() throws ClassNotFoundException, NoSuchMethodException {
178
179
Object [] arguments = getArguments ();
179
180
Class <?>[] argTypes = new Class <?>[arguments .length ];
180
181
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 );
182
184
}
183
185
184
186
// Try to get the exact method first.
@@ -279,8 +281,20 @@ public Object invoke() throws InvocationTargetException, IllegalAccessException
279
281
if (targetObject == null && !Modifier .isStatic (preparedMethod .getModifiers ())) {
280
282
throw new IllegalArgumentException ("Target method must not be non-static without a target" );
281
283
}
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
+ }
284
298
}
285
299
286
300
@@ -307,12 +321,13 @@ public Object invoke() throws InvocationTargetException, IllegalAccessException
307
321
public static int getTypeDifferenceWeight (Class <?>[] paramTypes , Object [] args ) {
308
322
int result = 0 ;
309
323
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 )) {
311
327
return Integer .MAX_VALUE ;
312
328
}
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 ();
316
331
while (superClass != null ) {
317
332
if (paramType .equals (superClass )) {
318
333
result = result + 2 ;
0 commit comments