DATAREST-606 - Polishing.

Moved the accessibility tweak to the EventHandlerMethod's constructor so that the modification only occurs at detection time. Polished unit test.

Original pull request: #187.
This commit is contained in:
Oliver Gierke
2015-07-19 12:53:47 +02:00
parent 698643cf18
commit de5a98d47b
2 changed files with 11 additions and 12 deletions

View File

@@ -89,7 +89,6 @@ public class AnnotatedEventHandlerInvoker implements ApplicationListener<Reposit
LOG.debug("Invoking {} handler for {}.", event.getClass().getSimpleName(), event.getSource());
}
ReflectionUtils.makeAccessible(handlerMethod.method);
ReflectionUtils.invokeMethod(handlerMethod.method, handlerMethod.handler, parameters.toArray());
}
}
@@ -109,7 +108,7 @@ public class AnnotatedEventHandlerInvoker implements ApplicationListener<Reposit
*/
@Override
public Object postProcessAfterInitialization(final Object bean, String beanName) throws BeansException {
Class<?> beanType = ClassUtils.getUserClass(bean);
RepositoryEventHandler typeAnno = AnnotationUtils.findAnnotation(beanType, RepositoryEventHandler.class);
@@ -182,10 +181,12 @@ public class AnnotatedEventHandlerInvoker implements ApplicationListener<Reposit
final Object handler;
private EventHandlerMethod(Class<?> targetType, Object handler, Method method) {
this.targetType = targetType;
this.method = method;
this.handler = handler;
ReflectionUtils.makeAccessible(this.method);
}
/*

View File

@@ -31,6 +31,7 @@ import org.springframework.util.MultiValueMap;
* Unit tests for {@link AnnotatedEventHandlerInvoker}.
*
* @author Oliver Gierke
* @author Fabian Trampusch
*/
public class AnnotatedEventHandlerInvokerUnitTests {
@@ -58,15 +59,16 @@ public class AnnotatedEventHandlerInvokerUnitTests {
* @see DATAREST-606
*/
@Test
public void canInvokePrivateEventHandlerMethods() {
public void invokesPrivateEventHandlerMethods() {
SampleWithPrivateHandler sampleHandler = new SampleWithPrivateHandler();
AnnotatedEventHandlerInvoker invoker = new AnnotatedEventHandlerInvoker();
invoker.postProcessAfterInitialization(sampleHandler, "sampleHandler");
BeforeCreateEvent beforeCreateEvent = new BeforeCreateEvent(new Person());
invoker.onApplicationEvent(beforeCreateEvent);
invoker.onApplicationEvent(new BeforeCreateEvent(new Person()));
assertThat(sampleHandler.wasCalled(), is(true));
assertThat(sampleHandler.wasCalled, is(true));
}
@RepositoryEventHandler
@@ -79,15 +81,11 @@ public class AnnotatedEventHandlerInvokerUnitTests {
@RepositoryEventHandler
static class SampleWithPrivateHandler {
private boolean wasCalled = false;
boolean wasCalled = false;
@HandleBeforeCreate
private void method(Person sample) {
wasCalled = true;
}
public boolean wasCalled() {
return wasCalled;
}
}
}