Skip to content

Commit b66479c

Browse files
jhoellerBenjamin Reed
authored andcommitted
Consistent fallback in case of fast-class generation failure
Closes spring-projectsgh-28138 (cherry picked from commit 7aed627)
1 parent e934a6e commit b66479c

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

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

402417
@Override
403418
public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
404-
Object retVal = methodProxy.invoke(this.target, args);
419+
Object retVal = invokeMethod(this.target, method, args, methodProxy);
405420
return processReturnType(proxy, this.target, method, retVal);
406421
}
407422
}
@@ -424,7 +439,7 @@ public Object intercept(Object proxy, Method method, Object[] args, MethodProxy
424439
Object oldProxy = null;
425440
try {
426441
oldProxy = AopContext.setCurrentProxy(proxy);
427-
Object retVal = methodProxy.invoke(this.target, args);
442+
Object retVal = invokeMethod(this.target, method, args, methodProxy);
428443
return processReturnType(proxy, this.target, method, retVal);
429444
}
430445
finally {
@@ -451,7 +466,7 @@ public DynamicUnadvisedInterceptor(TargetSource targetSource) {
451466
public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
452467
Object target = this.targetSource.getTarget();
453468
try {
454-
Object retVal = methodProxy.invoke(target, args);
469+
Object retVal = invokeMethod(target, method, args, methodProxy);
455470
return processReturnType(proxy, target, method, retVal);
456471
}
457472
finally {
@@ -478,7 +493,7 @@ public Object intercept(Object proxy, Method method, Object[] args, MethodProxy
478493
Object target = this.targetSource.getTarget();
479494
try {
480495
oldProxy = AopContext.setCurrentProxy(proxy);
481-
Object retVal = methodProxy.invoke(target, args);
496+
Object retVal = invokeMethod(target, method, args, methodProxy);
482497
return processReturnType(proxy, target, method, retVal);
483498
}
484499
finally {
@@ -648,7 +663,7 @@ public Object intercept(Object proxy, Method method, Object[] args, MethodProxy
648663
// it does nothing but a reflective operation on the target, and no hot
649664
// swapping or fancy proxying.
650665
Object[] argsToUse = AopProxyUtils.adaptArgumentsIfNecessary(method, args);
651-
retVal = methodProxy.invoke(target, argsToUse);
666+
retVal = invokeMethod(target, method, argsToUse, methodProxy);
652667
}
653668
else {
654669
// We need to create a method invocation...
@@ -717,7 +732,7 @@ public CglibMethodInvocation(Object proxy, Object target, Method method, Object[
717732
@Override
718733
protected Object invokeJoinpoint() throws Throwable {
719734
if (this.publicMethod) {
720-
return this.methodProxy.invoke(this.target, this.arguments);
735+
return methodProxy.invoke(this.target, this.arguments);
721736
}
722737
else {
723738
return super.invokeJoinpoint();

0 commit comments

Comments
 (0)