Skip to content

GH-3912: Handler: ignore Groovy generated methods #3919

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
import org.springframework.core.MethodParameter;
import org.springframework.core.ParameterNameDiscoverer;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.convert.ConversionFailedException;
Expand Down Expand Up @@ -759,44 +760,24 @@ else if (!Modifier.isPublic(method1.getModifiers())) {
ambiguousFallbackType, ambiguousFallbackMessageGenericType, matchesAnnotation, handlerMethod1);
}

}, new UniqueMethodFilter(targetClass));
}, ((ReflectionUtils.MethodFilter) this::isHandlerMethod).and(new UniqueMethodFilter(targetClass)));

if (candidateMethods.isEmpty() && candidateMessageMethods.isEmpty() && fallbackMethods.isEmpty()
&& fallbackMessageMethods.isEmpty()) {
findSingleSpecificMethodOnInterfacesIfProxy(candidateMessageMethods, candidateMethods);
}
}

@Nullable
private HandlerMethod obtainHandlerMethodIfAny(Method methodToProcess) {
HandlerMethod handlerMethodToUse = null;
if (isMethodEligible(methodToProcess)) {
try {
handlerMethodToUse = createHandlerMethod(
AopUtils.selectInvocableMethod(methodToProcess, ClassUtils.getUserClass(this.targetObject)));
}
catch (Exception ex) {
LOGGER.debug(ex, "Method [" + methodToProcess + "] is not eligible for Message handling.");
return null;
}

if (AnnotationUtils.getAnnotation(methodToProcess, Default.class) != null) {
Assert.state(this.defaultHandlerMethod == null,
() -> "Only one method can be @Default, but there are more for: " + this.targetObject);
this.defaultHandlerMethod = handlerMethodToUse;
}
}

return handlerMethodToUse;
}

private boolean isMethodEligible(Method methodToProcess) {
return !(methodToProcess.isBridge() || // NOSONAR boolean complexity
isMethodDefinedOnObjectClass(methodToProcess) ||
methodToProcess.getDeclaringClass().equals(Proxy.class) ||
(this.requiresReply && void.class.equals(methodToProcess.getReturnType())) ||
(this.methodName != null && !this.methodName.equals(methodToProcess.getName())) ||
(this.methodName == null && isPausableMethod(methodToProcess)));
private boolean isHandlerMethod(Method method) {
Class<?> declaringClass = method.getDeclaringClass();
return !(method.isSynthetic() ||
ReflectionUtils.isObjectMethod(method) ||
AnnotatedElementUtils.isAnnotated(method, "groovy.transform.Generated") ||
declaringClass.getName().equals("groovy.lang.GroovyObject") ||
declaringClass.equals(Proxy.class) ||
(this.requiresReply && void.class.equals(method.getReturnType())) ||
(this.methodName != null && !this.methodName.equals(method.getName())) ||
(this.methodName == null && isPausableMethod(method)));
}

private boolean isPausableMethod(Method pausableMethod) {
Expand All @@ -811,6 +792,27 @@ private boolean isPausableMethod(Method pausableMethod) {
return pausable;
}

@Nullable
private HandlerMethod obtainHandlerMethodIfAny(Method methodToProcess) {
HandlerMethod handlerMethodToUse;
try {
handlerMethodToUse = createHandlerMethod(
AopUtils.selectInvocableMethod(methodToProcess, ClassUtils.getUserClass(this.targetObject)));
}
catch (Exception ex) {
LOGGER.debug(ex, "Method [" + methodToProcess + "] is not eligible for Message handling.");
return null;
}

if (AnnotationUtils.getAnnotation(methodToProcess, Default.class) != null) {
Assert.state(this.defaultHandlerMethod == null,
() -> "Only one method can be @Default, but there are more for: " + this.targetObject);
this.defaultHandlerMethod = handlerMethodToUse;
}

return handlerMethodToUse;
}

private void populateHandlerMethod(Map<Class<?>, HandlerMethod> candidateMethods,
Map<Class<?>, HandlerMethod> candidateMessageMethods, Map<Class<?>, HandlerMethod> fallbackMethods,
Map<Class<?>, HandlerMethod> fallbackMessageMethods, AtomicReference<Class<?>> ambiguousFallbackType,
Expand Down Expand Up @@ -1013,8 +1015,7 @@ private HandlerMethod findClosestMatch(Class<?> payloadType) {

private static boolean isMethodDefinedOnObjectClass(Method method) {
return method != null && // NOSONAR
(method.getDeclaringClass().equals(Object.class) || ReflectionUtils.isEqualsMethod(method) ||
ReflectionUtils.isHashCodeMethod(method) || ReflectionUtils.isToStringMethod(method) ||
(ReflectionUtils.isObjectMethod(method) ||
AopUtils.isFinalizeMethod(method) || (method.getName().equals("clone")
&& method.getParameterTypes().length == 0));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package org.springframework.integration.groovy.dsl.test


import groovy.transform.CompileStatic
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.BeanFactory
import org.springframework.beans.factory.annotation.Autowired
Expand Down Expand Up @@ -45,6 +45,7 @@ import reactor.core.publisher.Flux
import reactor.test.StepVerifier

import java.time.Duration
import java.util.concurrent.atomic.AtomicReference
import java.util.function.Function

import static org.springframework.integration.groovy.dsl.IntegrationGroovyDsl.integrationFlow
Expand Down Expand Up @@ -196,6 +197,19 @@ class GroovyDslTests {
assert this.wireTapChannel.receive(10_000)?.payload == 'test'
}

@Autowired
@Qualifier('externalServiceFlow.input')
private MessageChannel externalServiceFlowInput

@Autowired
GroovyTestService groovyTestService

@Test
void 'handle service'() {
this.externalServiceFlowInput.send(new GenericMessage<Object>('test'))
assert groovyTestService.result.get() == 'TEST'
}

@Configuration
@EnableIntegration
static class Config {
Expand Down Expand Up @@ -301,6 +315,30 @@ class GroovyDslTests {
}
}

@Bean
myService() {
new GroovyTestService()
}

@Bean
externalServiceFlow(GroovyTestService groovyTestService) {
integrationFlow {
handle groovyTestService
}

}

}

@CompileStatic
static class GroovyTestService {

AtomicReference<String> result = new AtomicReference<>()

void handlePayload(String payload) {
result.set payload.toUpperCase()
}

}

}