Skip to content

Commit 73bcef5

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 c04a21b commit 73bcef5

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

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

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 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.
@@ -373,6 +373,21 @@ private static boolean implementsInterface(Method method, Set<Class<?>> ifcs) {
373373
return false;
374374
}
375375

376+
/**
377+
* Invoke the given method with a CGLIB MethodProxy if possible, falling back
378+
* to a plain reflection invocation in case of a fast-class generation failure.
379+
*/
380+
private static Object invokeMethod(Object target, Method method, Object[] args, MethodProxy methodProxy)
381+
throws Throwable {
382+
try {
383+
return methodProxy.invoke(target, args);
384+
}
385+
catch (CodeGenerationException ex) {
386+
logger.warn("Unable to fast-class invoke method [" + method + "] on target [" + target + "]. Falling back to reflection.");
387+
return AopUtils.invokeJoinpointUsingReflection(target, method, args);
388+
}
389+
}
390+
376391
/**
377392
* Process a return value. Wraps a return of {@code this} if necessary to be the
378393
* {@code proxy} and also verifies that {@code null} is not returned as a primitive.
@@ -418,7 +433,7 @@ public StaticUnadvisedInterceptor(Object target) {
418433

419434
@Override
420435
public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
421-
Object retVal = methodProxy.invoke(this.target, args);
436+
Object retVal = invokeMethod(this.target, method, args, methodProxy);
422437
return processReturnType(proxy, this.target, method, retVal);
423438
}
424439
}
@@ -441,7 +456,7 @@ public Object intercept(Object proxy, Method method, Object[] args, MethodProxy
441456
Object oldProxy = null;
442457
try {
443458
oldProxy = AopContext.setCurrentProxy(proxy);
444-
Object retVal = methodProxy.invoke(this.target, args);
459+
Object retVal = invokeMethod(this.target, method, args, methodProxy);
445460
return processReturnType(proxy, this.target, method, retVal);
446461
}
447462
finally {
@@ -468,7 +483,7 @@ public DynamicUnadvisedInterceptor(TargetSource targetSource) {
468483
public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
469484
Object target = this.targetSource.getTarget();
470485
try {
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 {
@@ -495,7 +510,7 @@ public Object intercept(Object proxy, Method method, Object[] args, MethodProxy
495510
Object target = this.targetSource.getTarget();
496511
try {
497512
oldProxy = AopContext.setCurrentProxy(proxy);
498-
Object retVal = methodProxy.invoke(target, args);
513+
Object retVal = invokeMethod(target, method, args, methodProxy);
499514
return processReturnType(proxy, target, method, retVal);
500515
}
501516
finally {
@@ -665,7 +680,7 @@ public Object intercept(Object proxy, Method method, Object[] args, MethodProxy
665680
// it does nothing but a reflective operation on the target, and no hot
666681
// swapping or fancy proxying.
667682
Object[] argsToUse = AopProxyUtils.adaptArgumentsIfNecessary(method, args);
668-
retVal = methodProxy.invoke(target, argsToUse);
683+
retVal = invokeMethod(target, method, argsToUse, methodProxy);
669684
}
670685
else {
671686
// We need to create a method invocation...
@@ -734,7 +749,7 @@ public CglibMethodInvocation(Object proxy, Object target, Method method, Object[
734749
@Override
735750
protected Object invokeJoinpoint() throws Throwable {
736751
if (this.publicMethod) {
737-
return this.methodProxy.invoke(this.target, this.arguments);
752+
return methodProxy.invoke(this.target, this.arguments);
738753
}
739754
else {
740755
return super.invokeJoinpoint();

0 commit comments

Comments
 (0)