1
1
/*
2
- * Copyright 2002-2010 the original author or authors.
2
+ * Copyright 2002-2022 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -328,7 +328,23 @@ private Callback[] getCallbacks(Class rootClass) throws Exception {
328
328
}
329
329
330
330
/**
331
- * Wrap a return of this if necessary to be the proxy
331
+ * Invoke the given method with a CGLIB MethodProxy if possible, falling back
332
+ * to a plain reflection invocation in case of a fast-class generation failure.
333
+ */
334
+ private static Object invokeMethod (Object target , Method method , Object [] args , MethodProxy methodProxy )
335
+ throws Throwable {
336
+ try {
337
+ return methodProxy .invoke (target , args );
338
+ }
339
+ catch (CodeGenerationException ex ) {
340
+ logger .warn ("Unable to fast-class invoke method [" + method + "] on target [" + target + "]. Falling back to reflection." );
341
+ return AopUtils .invokeJoinpointUsingReflection (target , method , args );
342
+ }
343
+ }
344
+
345
+ /**
346
+ * Process a return value. Wraps a return of {@code this} if necessary to be the
347
+ * {@code proxy} and also verifies that {@code null} is not returned as a primitive.
332
348
*/
333
349
private static Object massageReturnTypeIfNecessary (Object proxy , Object target , Method method , Object retVal ) {
334
350
// Massage return value if necessary
@@ -378,7 +394,7 @@ public StaticUnadvisedInterceptor(Object target) {
378
394
}
379
395
380
396
public Object intercept (Object proxy , Method method , Object [] args , MethodProxy methodProxy ) throws Throwable {
381
- Object retVal = methodProxy . invoke (this .target , args );
397
+ Object retVal = invokeMethod (this .target , method , args , methodProxy );
382
398
return massageReturnTypeIfNecessary (proxy , this .target , method , retVal );
383
399
}
384
400
}
@@ -400,7 +416,7 @@ public Object intercept(Object proxy, Method method, Object[] args, MethodProxy
400
416
Object oldProxy = null ;
401
417
try {
402
418
oldProxy = AopContext .setCurrentProxy (proxy );
403
- Object retVal = methodProxy . invoke (this .target , args );
419
+ Object retVal = invokeMethod (this .target , method , args , methodProxy );
404
420
return massageReturnTypeIfNecessary (proxy , this .target , method , retVal );
405
421
}
406
422
finally {
@@ -426,7 +442,7 @@ public DynamicUnadvisedInterceptor(TargetSource targetSource) {
426
442
public Object intercept (Object proxy , Method method , Object [] args , MethodProxy methodProxy ) throws Throwable {
427
443
Object target = this .targetSource .getTarget ();
428
444
try {
429
- Object retVal = methodProxy . invoke (target , args );
445
+ Object retVal = invokeMethod (target , method , args , methodProxy );
430
446
return massageReturnTypeIfNecessary (proxy , target , method , retVal );
431
447
}
432
448
finally {
@@ -452,7 +468,7 @@ public Object intercept(Object proxy, Method method, Object[] args, MethodProxy
452
468
Object target = this .targetSource .getTarget ();
453
469
try {
454
470
oldProxy = AopContext .setCurrentProxy (proxy );
455
- Object retVal = methodProxy . invoke (target , args );
471
+ Object retVal = invokeMethod (target , method , args , methodProxy );
456
472
return massageReturnTypeIfNecessary (proxy , target , method , retVal );
457
473
}
458
474
finally {
@@ -615,7 +631,8 @@ public Object intercept(Object proxy, Method method, Object[] args, MethodProxy
615
631
// Note that the final invoker must be an InvokerInterceptor, so we know
616
632
// it does nothing but a reflective operation on the target, and no hot
617
633
// swapping or fancy proxying.
618
- retVal = methodProxy .invoke (target , args );
634
+ Object [] argsToUse = AopProxyUtils .adaptArgumentsIfNecessary (method , args );
635
+ retVal = invokeMethod (target , method , argsToUse , methodProxy );
619
636
}
620
637
else {
621
638
// We need to create a method invocation...
0 commit comments