Skip to content

Commit a3be0dd

Browse files
jhoellerBenjamin Reed
authored and
Benjamin Reed
committed
Consistent fallback in case of fast-class generation failure
Closes spring-projectsgh-28138 (cherry picked from commit 7aed627)
1 parent 740d7c1 commit a3be0dd

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

spring-aop/src/main/java/org/springframework/aop/framework/CglibAopProxy.java

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -334,6 +334,21 @@ private Callback[] getCallbacks(Class<?> rootClass) throws Exception {
334334
return callbacks;
335335
}
336336

337+
/**
338+
* Invoke the given method with a CGLIB MethodProxy if possible, falling back
339+
* to a plain reflection invocation in case of a fast-class generation failure.
340+
*/
341+
private static Object invokeMethod(Object target, Method method, Object[] args, MethodProxy methodProxy)
342+
throws Throwable {
343+
try {
344+
return methodProxy.invoke(target, args);
345+
}
346+
catch (CodeGenerationException ex) {
347+
logger.warn("Unable to fast-class invoke method [" + method + "] on target [" + target + "]. Falling back to reflection.");
348+
return AopUtils.invokeJoinpointUsingReflection(target, method, args);
349+
}
350+
}
351+
337352
/**
338353
* Process a return value. Wraps a return of {@code this} if necessary to be the
339354
* {@code proxy} and also verifies that {@code null} is not returned as a primitive.
@@ -389,7 +404,7 @@ public StaticUnadvisedInterceptor(Object target) {
389404
}
390405

391406
public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
392-
Object retVal = methodProxy.invoke(this.target, args);
407+
Object retVal = invokeMethod(this.target, method, args, methodProxy);
393408
return processReturnType(proxy, this.target, method, retVal);
394409
}
395410
}
@@ -411,7 +426,7 @@ public Object intercept(Object proxy, Method method, Object[] args, MethodProxy
411426
Object oldProxy = null;
412427
try {
413428
oldProxy = AopContext.setCurrentProxy(proxy);
414-
Object retVal = methodProxy.invoke(this.target, args);
429+
Object retVal = invokeMethod(this.target, method, args, methodProxy);
415430
return processReturnType(proxy, this.target, method, retVal);
416431
}
417432
finally {
@@ -437,7 +452,7 @@ public DynamicUnadvisedInterceptor(TargetSource targetSource) {
437452
public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
438453
Object target = this.targetSource.getTarget();
439454
try {
440-
Object retVal = methodProxy.invoke(target, args);
455+
Object retVal = invokeMethod(target, method, args, methodProxy);
441456
return processReturnType(proxy, target, method, retVal);
442457
}
443458
finally {
@@ -463,7 +478,7 @@ public Object intercept(Object proxy, Method method, Object[] args, MethodProxy
463478
Object target = this.targetSource.getTarget();
464479
try {
465480
oldProxy = AopContext.setCurrentProxy(proxy);
466-
Object retVal = methodProxy.invoke(target, args);
481+
Object retVal = invokeMethod(target, method, args, methodProxy);
467482
return processReturnType(proxy, target, method, retVal);
468483
}
469484
finally {
@@ -626,7 +641,8 @@ public Object intercept(Object proxy, Method method, Object[] args, MethodProxy
626641
// Note that the final invoker must be an InvokerInterceptor, so we know
627642
// it does nothing but a reflective operation on the target, and no hot
628643
// swapping or fancy proxying.
629-
retVal = methodProxy.invoke(target, args);
644+
Object[] argsToUse = AopProxyUtils.adaptArgumentsIfNecessary(method, args);
645+
retVal = invokeMethod(target, method, argsToUse, methodProxy);
630646
}
631647
else {
632648
// We need to create a method invocation...

0 commit comments

Comments
 (0)