Skip to content

Commit 972449e

Browse files
mp911deodrotbohm
authored andcommitted
DATACMNS-1556 - Limit default method invocation only to interfaces that actually declare default methods.
1 parent 293bbd9 commit 972449e

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/main/java/org/springframework/data/projection/DefaultMethodInvokingMethodInterceptor.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import org.aopalliance.intercept.MethodInterceptor;
2828
import org.aopalliance.intercept.MethodInvocation;
29+
2930
import org.springframework.aop.ProxyMethodInvocation;
3031
import org.springframework.data.util.Lazy;
3132
import org.springframework.lang.Nullable;
@@ -45,6 +46,26 @@ public class DefaultMethodInvokingMethodInterceptor implements MethodInterceptor
4546
private final MethodHandleLookup methodHandleLookup = MethodHandleLookup.getMethodHandleLookup();
4647
private final Map<Method, MethodHandle> methodHandleCache = new ConcurrentReferenceHashMap<>(10, ReferenceType.WEAK);
4748

49+
/**
50+
* Returns whether the {@code interfaceClass} declares {@link Method#isDefault() default methods}.
51+
*
52+
* @param interfaceClass the {@link Class} to inspect.
53+
* @return {@literal true} if {@code interfaceClass} declares a default method.
54+
* @since 2.2
55+
*/
56+
public static boolean hasDefaultMethods(Class<?> interfaceClass) {
57+
58+
Method[] methods = ReflectionUtils.getAllDeclaredMethods(interfaceClass);
59+
60+
for (Method method : methods) {
61+
if (method.isDefault()) {
62+
return true;
63+
}
64+
}
65+
66+
return false;
67+
}
68+
4869
/*
4970
* (non-Javadoc)
5071
* @see org.aopalliance.intercept.MethodInterceptor#invoke(org.aopalliance.intercept.MethodInvocation)

src/main/java/org/springframework/data/repository/core/support/RepositoryFactorySupport.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535

3636
import org.aopalliance.intercept.MethodInterceptor;
3737
import org.aopalliance.intercept.MethodInvocation;
38+
3839
import org.springframework.aop.framework.ProxyFactory;
3940
import org.springframework.aop.interceptor.ExposeInvocationInterceptor;
4041
import org.springframework.beans.BeanUtils;
@@ -310,12 +311,13 @@ public <T> T getRepository(Class<T> repositoryInterface, RepositoryFragments fra
310311
result.addAdvice(new MethodInvocationValidator());
311312
}
312313

313-
result.addAdvice(SurroundingTransactionDetectorMethodInterceptor.INSTANCE);
314314
result.addAdvisor(ExposeInvocationInterceptor.ADVISOR);
315315

316316
postProcessors.forEach(processor -> processor.postProcess(result, information));
317317

318-
result.addAdvice(new DefaultMethodInvokingMethodInterceptor());
318+
if (DefaultMethodInvokingMethodInterceptor.hasDefaultMethods(repositoryInterface)) {
319+
result.addAdvice(new DefaultMethodInvokingMethodInterceptor());
320+
}
319321

320322
ProjectionFactory projectionFactory = getProjectionFactory(classLoader, beanFactory);
321323
result.addAdvice(new QueryExecutorMethodInterceptor(information, projectionFactory));

0 commit comments

Comments
 (0)