Skip to content

Commit

Permalink
Consistent fallback in case of fast-class generation failure
Browse files Browse the repository at this point in the history
Closes spring-projectsgh-28138

(cherry picked from commit 7aed627)
  • Loading branch information
jhoeller authored and Benjamin Reed committed Jul 26, 2022
1 parent e934a6e commit b66479c
Showing 1 changed file with 22 additions and 7 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -345,6 +345,21 @@ private Callback[] getCallbacks(Class<?> rootClass) throws Exception {
return callbacks;
}

/**
* Invoke the given method with a CGLIB MethodProxy if possible, falling back
* to a plain reflection invocation in case of a fast-class generation failure.
*/
private static Object invokeMethod(Object target, Method method, Object[] args, MethodProxy methodProxy)
throws Throwable {
try {
return methodProxy.invoke(target, args);
}
catch (CodeGenerationException ex) {
logger.warn("Unable to fast-class invoke method [" + method + "] on target [" + target + "]. Falling back to reflection.");
return AopUtils.invokeJoinpointUsingReflection(target, method, args);
}
}

/**
* Process a return value. Wraps a return of {@code this} if necessary to be the
* {@code proxy} and also verifies that {@code null} is not returned as a primitive.
Expand Down Expand Up @@ -401,7 +416,7 @@ public StaticUnadvisedInterceptor(Object target) {

@Override
public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
Object retVal = methodProxy.invoke(this.target, args);
Object retVal = invokeMethod(this.target, method, args, methodProxy);
return processReturnType(proxy, this.target, method, retVal);
}
}
Expand All @@ -424,7 +439,7 @@ public Object intercept(Object proxy, Method method, Object[] args, MethodProxy
Object oldProxy = null;
try {
oldProxy = AopContext.setCurrentProxy(proxy);
Object retVal = methodProxy.invoke(this.target, args);
Object retVal = invokeMethod(this.target, method, args, methodProxy);
return processReturnType(proxy, this.target, method, retVal);
}
finally {
Expand All @@ -451,7 +466,7 @@ public DynamicUnadvisedInterceptor(TargetSource targetSource) {
public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
Object target = this.targetSource.getTarget();
try {
Object retVal = methodProxy.invoke(target, args);
Object retVal = invokeMethod(target, method, args, methodProxy);
return processReturnType(proxy, target, method, retVal);
}
finally {
Expand All @@ -478,7 +493,7 @@ public Object intercept(Object proxy, Method method, Object[] args, MethodProxy
Object target = this.targetSource.getTarget();
try {
oldProxy = AopContext.setCurrentProxy(proxy);
Object retVal = methodProxy.invoke(target, args);
Object retVal = invokeMethod(target, method, args, methodProxy);
return processReturnType(proxy, target, method, retVal);
}
finally {
Expand Down Expand Up @@ -648,7 +663,7 @@ public Object intercept(Object proxy, Method method, Object[] args, MethodProxy
// it does nothing but a reflective operation on the target, and no hot
// swapping or fancy proxying.
Object[] argsToUse = AopProxyUtils.adaptArgumentsIfNecessary(method, args);
retVal = methodProxy.invoke(target, argsToUse);
retVal = invokeMethod(target, method, argsToUse, methodProxy);
}
else {
// We need to create a method invocation...
Expand Down Expand Up @@ -717,7 +732,7 @@ public CglibMethodInvocation(Object proxy, Object target, Method method, Object[
@Override
protected Object invokeJoinpoint() throws Throwable {
if (this.publicMethod) {
return this.methodProxy.invoke(this.target, this.arguments);
return methodProxy.invoke(this.target, this.arguments);
}
else {
return super.invokeJoinpoint();
Expand Down

0 comments on commit b66479c

Please sign in to comment.