Skip to content

Commit

Permalink
Restore ability to return original method for proxy-derived method
Browse files Browse the repository at this point in the history
Closes gh-32365
  • Loading branch information
jhoeller committed Mar 4, 2024
1 parent 7493ce8 commit 24759a7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,19 @@
package org.springframework.aop.support;

import java.lang.reflect.Method;
import java.util.List;

import org.junit.jupiter.api.Test;

import org.springframework.aop.ClassFilter;
import org.springframework.aop.MethodMatcher;
import org.springframework.aop.Pointcut;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.interceptor.ExposeInvocationInterceptor;
import org.springframework.aop.target.EmptyTargetSource;
import org.springframework.aop.testfixture.interceptor.NopInterceptor;
import org.springframework.beans.testfixture.beans.TestBean;
import org.springframework.core.ResolvableType;
import org.springframework.core.testfixture.io.SerializationTestUtils;
import org.springframework.lang.Nullable;
import org.springframework.util.ReflectionUtils;
Expand All @@ -37,6 +40,7 @@
* @author Rod Johnson
* @author Chris Beams
* @author Sebastien Deleuze
* @author Juergen Hoeller
*/
class AopUtilsTests {

Expand Down Expand Up @@ -99,4 +103,36 @@ void testInvokeJoinpointUsingReflection() throws Throwable {
assertThat(result).isEqualTo(name);
}

@Test // gh-32365
void mostSpecificMethodBetweenJdkProxyAndTarget() throws Exception {
Class<?> proxyClass = new ProxyFactory(new WithInterface()).getProxyClass(getClass().getClassLoader());
Method specificMethod = AopUtils.getMostSpecificMethod(proxyClass.getMethod("handle", List.class), WithInterface.class);
assertThat(ResolvableType.forMethodParameter(specificMethod, 0).getGeneric().toClass()).isEqualTo(String.class);
}

@Test // gh-32365
void mostSpecificMethodBetweenCglibProxyAndTarget() throws Exception {
Class<?> proxyClass = new ProxyFactory(new WithoutInterface()).getProxyClass(getClass().getClassLoader());
Method specificMethod = AopUtils.getMostSpecificMethod(proxyClass.getMethod("handle", List.class), WithoutInterface.class);
assertThat(ResolvableType.forMethodParameter(specificMethod, 0).getGeneric().toClass()).isEqualTo(String.class);
}


interface ProxyInterface {

void handle(List<String> list);
}

static class WithInterface implements ProxyInterface {

public void handle(List<String> list) {
}
}

static class WithoutInterface {

public void handle(List<String> list) {
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.springframework.core;

import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -86,7 +87,10 @@ public static Method findBridgedMethod(Method bridgeMethod) {
* @see org.springframework.util.ClassUtils#getMostSpecificMethod
*/
public static Method getMostSpecificMethod(Method bridgeMethod, @Nullable Class<?> targetClass) {
if (targetClass != null && !bridgeMethod.getDeclaringClass().isAssignableFrom(targetClass)) {
if (targetClass != null &&
!ClassUtils.getUserClass(bridgeMethod.getDeclaringClass()).isAssignableFrom(targetClass) &&
!Proxy.isProxyClass(bridgeMethod.getDeclaringClass())) {
// From a different class hierarchy, and not a JDK or CGLIB proxy either -> return as-is.
return bridgeMethod;
}

Expand Down

0 comments on commit 24759a7

Please sign in to comment.