DATAREST-606 - Allow non-public @RepositoryEventHandler methods.

AnnotatedEventHandlerInvoker now makes sure the method about to be invoked is accessible.

Original pull request: #187.
This commit is contained in:
Fabian Trampusch
2015-07-18 10:17:20 +02:00
committed by Oliver Gierke
parent da5237538f
commit e759fb8421
2 changed files with 33 additions and 1 deletions

View File

@@ -89,6 +89,7 @@ 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());
}
}

View File

@@ -22,13 +22,14 @@ import org.junit.Test;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.data.rest.core.annotation.HandleBeforeCreate;
import org.springframework.data.rest.core.annotation.RepositoryEventHandler;
import org.springframework.data.rest.core.domain.jpa.Person;
import org.springframework.data.rest.core.event.AnnotatedEventHandlerInvoker.EventHandlerMethod;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.util.MultiValueMap;
/**
* Unit tests for {@link AnnotatedEventHandlerInvoker}.
*
*
* @author Oliver Gierke
*/
public class AnnotatedEventHandlerInvokerUnitTests {
@@ -53,10 +54,40 @@ public class AnnotatedEventHandlerInvokerUnitTests {
assertThat(methods.get(BeforeCreateEvent.class), hasSize(1));
}
/**
* @see DATAREST-606
*/
@Test
public void canInvokePrivateEventHandlerMethods() {
SampleWithPrivateHandler sampleHandler = new SampleWithPrivateHandler();
AnnotatedEventHandlerInvoker invoker = new AnnotatedEventHandlerInvoker();
invoker.postProcessAfterInitialization(sampleHandler, "sampleHandler");
BeforeCreateEvent beforeCreateEvent = new BeforeCreateEvent(new Person());
invoker.onApplicationEvent(beforeCreateEvent);
assertThat(sampleHandler.wasCalled(), is(true));
}
@RepositoryEventHandler
static class Sample {
@HandleBeforeCreate
public void method(Sample sample) {}
}
@RepositoryEventHandler
static class SampleWithPrivateHandler {
private boolean wasCalled = false;
@HandleBeforeCreate
private void method(Person sample) {
wasCalled = true;
}
public boolean wasCalled() {
return wasCalled;
}
}
}