Skip to content

Commit fd51809

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 fa6e16b commit fd51809

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

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

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 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.
@@ -335,6 +335,21 @@ private Callback[] getCallbacks(Class<?> rootClass) throws Exception {
335335
return callbacks;
336336
}
337337

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

392407
@Override
393408
public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
394-
Object retVal = methodProxy.invoke(this.target, args);
409+
Object retVal = invokeMethod(this.target, method, args, methodProxy);
395410
return processReturnType(proxy, this.target, method, retVal);
396411
}
397412
}
@@ -414,7 +429,7 @@ public Object intercept(Object proxy, Method method, Object[] args, MethodProxy
414429
Object oldProxy = null;
415430
try {
416431
oldProxy = AopContext.setCurrentProxy(proxy);
417-
Object retVal = methodProxy.invoke(this.target, args);
432+
Object retVal = invokeMethod(this.target, method, args, methodProxy);
418433
return processReturnType(proxy, this.target, method, retVal);
419434
}
420435
finally {
@@ -441,7 +456,7 @@ public DynamicUnadvisedInterceptor(TargetSource targetSource) {
441456
public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
442457
Object target = this.targetSource.getTarget();
443458
try {
444-
Object retVal = methodProxy.invoke(target, args);
459+
Object retVal = invokeMethod(target, method, args, methodProxy);
445460
return processReturnType(proxy, target, method, retVal);
446461
}
447462
finally {
@@ -468,7 +483,7 @@ public Object intercept(Object proxy, Method method, Object[] args, MethodProxy
468483
Object target = this.targetSource.getTarget();
469484
try {
470485
oldProxy = AopContext.setCurrentProxy(proxy);
471-
Object retVal = methodProxy.invoke(target, args);
486+
Object retVal = invokeMethod(target, method, args, methodProxy);
472487
return processReturnType(proxy, target, method, retVal);
473488
}
474489
finally {
@@ -637,7 +652,8 @@ public Object intercept(Object proxy, Method method, Object[] args, MethodProxy
637652
// Note that the final invoker must be an InvokerInterceptor, so we know
638653
// it does nothing but a reflective operation on the target, and no hot
639654
// swapping or fancy proxying.
640-
retVal = methodProxy.invoke(target, args);
655+
Object[] argsToUse = AopProxyUtils.adaptArgumentsIfNecessary(method, args);
656+
retVal = invokeMethod(target, method, argsToUse, methodProxy);
641657
}
642658
else {
643659
// We need to create a method invocation...
@@ -705,7 +721,7 @@ public CglibMethodInvocation(Object proxy, Object target, Method method, Object[
705721
@Override
706722
protected Object invokeJoinpoint() throws Throwable {
707723
if (this.publicMethod) {
708-
return this.methodProxy.invoke(this.target, this.arguments);
724+
return methodProxy.invoke(this.target, this.arguments);
709725
}
710726
else {
711727
return super.invokeJoinpoint();

0 commit comments

Comments
 (0)