Skip to content

Commit 36b8314

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 e9526f2 commit 36b8314

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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,23 @@ public void invokesEventHandlerInOrderMethods() {
9191
assertThat(orderHandler1.timestamp, is(greaterThan(orderHandler2.timestamp)));
9292
}
9393

94+
@Test // DATAREST-983
95+
public void invokesEventHandlerOnParentClass() {
96+
97+
FirstEventHandler firstHandler = new FirstEventHandler();
98+
SecondEventHandler secondHandler = new SecondEventHandler();
99+
100+
AnnotatedEventHandlerInvoker invoker = new AnnotatedEventHandlerInvoker();
101+
invoker.postProcessAfterInitialization(firstHandler, "firstHandler");
102+
invoker.postProcessAfterInitialization(secondHandler, "secondHandler");
103+
104+
invoker.onApplicationEvent(new BeforeCreateEvent(new FirstEntity()));
105+
invoker.onApplicationEvent(new BeforeCreateEvent(new SecondEntity()));
106+
107+
assertThat(firstHandler.callCount, is(1));
108+
assertThat(secondHandler.callCount, is(1));
109+
}
110+
94111
@RepositoryEventHandler
95112
static class Sample {
96113

@@ -136,4 +153,27 @@ private void method(Person sample) {
136153
timestamp = System.nanoTime();
137154
}
138155
}
156+
157+
// DATAREST-983
158+
159+
static class AbstractBaseEntityEventHandler<T extends BaseEntity> {
160+
int callCount = 0;
161+
162+
@HandleBeforeCreate
163+
private void method(T entity) {
164+
callCount += 1;
165+
}
166+
}
167+
168+
@RepositoryEventHandler
169+
static class FirstEventHandler extends AbstractBaseEntityEventHandler<FirstEntity> {}
170+
171+
@RepositoryEventHandler
172+
static class SecondEventHandler extends AbstractBaseEntityEventHandler<SecondEntity> {}
173+
174+
static abstract class BaseEntity {}
175+
176+
static class FirstEntity extends BaseEntity {}
177+
178+
static class SecondEntity extends BaseEntity {}
139179
}

0 commit comments

Comments
 (0)