Skip to content

Commit 6113dfe

Browse files
committed
DATAREST-970 - AnnotatedEventHandlerInvoker now resolves generic handler method argument.
We now resolve the handler method argument type of an annotated repository event handler against the concrete handler type to make sure generics are resolved properly.
1 parent 8e1af29 commit 6113dfe

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

spring-data-rest-core/src/main/java/org/springframework/data/rest/core/event/AnnotatedEventHandlerInvoker.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.springframework.beans.factory.config.BeanPostProcessor;
3232
import org.springframework.context.ApplicationEvent;
3333
import org.springframework.context.ApplicationListener;
34+
import org.springframework.core.ResolvableType;
3435
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
3536
import org.springframework.core.annotation.AnnotationUtils;
3637
import org.springframework.data.rest.core.annotation.HandleAfterCreate;
@@ -165,13 +166,12 @@ private <T extends Annotation> void inspect(Object handler, Method method, Class
165166
return;
166167
}
167168

168-
Class<?>[] parameterTypes = method.getParameterTypes();
169-
170-
if (parameterTypes.length == 0) {
169+
if (method.getParameterCount() == 0) {
171170
throw new IllegalStateException(String.format(PARAMETER_MISSING, method));
172171
}
173172

174-
EventHandlerMethod handlerMethod = EventHandlerMethod.of(parameterTypes[0], handler, method);
173+
ResolvableType parameter = ResolvableType.forMethodParameter(method, 0, handler.getClass());
174+
EventHandlerMethod handlerMethod = EventHandlerMethod.of(parameter.resolve(), handler, method);
175175

176176
if (LOG.isDebugEnabled()) {
177177
LOG.debug("Annotated handler method found: {}", handlerMethod);

spring-data-rest-core/src/test/java/org/springframework/data/rest/core/event/AnnotatedEventHandlerInvokerUnitTests.java

+40
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,23 @@ public void invokesEventHandlerInOrderMethods() {
8585
assertThat(orderHandler1.timestamp, is(greaterThan(orderHandler2.timestamp)));
8686
}
8787

88+
@Test // DATAREST-983
89+
public void invokesEventHandlerOnParentClass() {
90+
91+
FirstEventHandler firstHandler = new FirstEventHandler();
92+
SecondEventHandler secondHandler = new SecondEventHandler();
93+
94+
AnnotatedEventHandlerInvoker invoker = new AnnotatedEventHandlerInvoker();
95+
invoker.postProcessAfterInitialization(firstHandler, "firstHandler");
96+
invoker.postProcessAfterInitialization(secondHandler, "secondHandler");
97+
98+
invoker.onApplicationEvent(new BeforeCreateEvent(new FirstEntity()));
99+
invoker.onApplicationEvent(new BeforeCreateEvent(new SecondEntity()));
100+
101+
assertThat(firstHandler.callCount, is(1));
102+
assertThat(secondHandler.callCount, is(1));
103+
}
104+
88105
@RepositoryEventHandler
89106
static class Sample {
90107

@@ -130,4 +147,27 @@ private void method(Person sample) {
130147
timestamp = System.nanoTime();
131148
}
132149
}
150+
151+
// DATAREST-983
152+
153+
static class AbstractBaseEntityEventHandler<T extends BaseEntity> {
154+
int callCount = 0;
155+
156+
@HandleBeforeCreate
157+
private void method(T entity) {
158+
callCount += 1;
159+
}
160+
}
161+
162+
@RepositoryEventHandler
163+
static class FirstEventHandler extends AbstractBaseEntityEventHandler<FirstEntity> {}
164+
165+
@RepositoryEventHandler
166+
static class SecondEventHandler extends AbstractBaseEntityEventHandler<SecondEntity> {}
167+
168+
static abstract class BaseEntity {}
169+
170+
static class FirstEntity extends BaseEntity {}
171+
172+
static class SecondEntity extends BaseEntity {}
133173
}

0 commit comments

Comments
 (0)