Skip to content

Commit 6d656a4

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 c9eb54d commit 6d656a4

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

+23-7
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.
@@ -344,6 +344,21 @@ private Callback[] getCallbacks(Class<?> rootClass) throws Exception {
344344
return callbacks;
345345
}
346346

347+
/**
348+
* Invoke the given method with a CGLIB MethodProxy if possible, falling back
349+
* to a plain reflection invocation in case of a fast-class generation failure.
350+
*/
351+
private static Object invokeMethod(Object target, Method method, Object[] args, MethodProxy methodProxy)
352+
throws Throwable {
353+
try {
354+
return methodProxy.invoke(target, args);
355+
}
356+
catch (CodeGenerationException ex) {
357+
logger.warn("Unable to fast-class invoke method [" + method + "] on target [" + target + "]. Falling back to reflection.");
358+
return AopUtils.invokeJoinpointUsingReflection(target, method, args);
359+
}
360+
}
361+
347362
/**
348363
* Process a return value. Wraps a return of {@code this} if necessary to be the
349364
* {@code proxy} and also verifies that {@code null} is not returned as a primitive.
@@ -400,7 +415,7 @@ public StaticUnadvisedInterceptor(Object target) {
400415

401416
@Override
402417
public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
403-
Object retVal = methodProxy.invoke(this.target, args);
418+
Object retVal = invokeMethod(this.target, method, args, methodProxy);
404419
return processReturnType(proxy, this.target, method, retVal);
405420
}
406421
}
@@ -423,7 +438,7 @@ public Object intercept(Object proxy, Method method, Object[] args, MethodProxy
423438
Object oldProxy = null;
424439
try {
425440
oldProxy = AopContext.setCurrentProxy(proxy);
426-
Object retVal = methodProxy.invoke(this.target, args);
441+
Object retVal = invokeMethod(this.target, method, args, methodProxy);
427442
return processReturnType(proxy, this.target, method, retVal);
428443
}
429444
finally {
@@ -450,7 +465,7 @@ public DynamicUnadvisedInterceptor(TargetSource targetSource) {
450465
public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
451466
Object target = this.targetSource.getTarget();
452467
try {
453-
Object retVal = methodProxy.invoke(target, args);
468+
Object retVal = invokeMethod(target, method, args, methodProxy);
454469
return processReturnType(proxy, target, method, retVal);
455470
}
456471
finally {
@@ -477,7 +492,7 @@ public Object intercept(Object proxy, Method method, Object[] args, MethodProxy
477492
Object target = this.targetSource.getTarget();
478493
try {
479494
oldProxy = AopContext.setCurrentProxy(proxy);
480-
Object retVal = methodProxy.invoke(target, args);
495+
Object retVal = invokeMethod(target, method, args, methodProxy);
481496
return processReturnType(proxy, target, method, retVal);
482497
}
483498
finally {
@@ -646,7 +661,8 @@ public Object intercept(Object proxy, Method method, Object[] args, MethodProxy
646661
// Note that the final invoker must be an InvokerInterceptor, so we know
647662
// it does nothing but a reflective operation on the target, and no hot
648663
// swapping or fancy proxying.
649-
retVal = methodProxy.invoke(target, args);
664+
Object[] argsToUse = AopProxyUtils.adaptArgumentsIfNecessary(method, args);
665+
retVal = invokeMethod(target, method, argsToUse, methodProxy);
650666
}
651667
else {
652668
// We need to create a method invocation...
@@ -715,7 +731,7 @@ public CglibMethodInvocation(Object proxy, Object target, Method method, Object[
715731
@Override
716732
protected Object invokeJoinpoint() throws Throwable {
717733
if (this.publicMethod) {
718-
return this.methodProxy.invoke(this.target, this.arguments);
734+
return methodProxy.invoke(this.target, this.arguments);
719735
}
720736
else {
721737
return super.invokeJoinpoint();

0 commit comments

Comments
 (0)