DATAREST-1075 - AnnotatedEventHandlerInvoker skips methods introduced by proxy.

This commit is contained in:
Oliver Gierke
2017-07-06 13:08:50 +02:00
parent c3f1c23ff8
commit 7aa2de04ff
3 changed files with 44 additions and 27 deletions

View File

@@ -45,7 +45,6 @@ import org.springframework.data.rest.core.annotation.HandleBeforeLinkDelete;
import org.springframework.data.rest.core.annotation.HandleBeforeLinkSave;
import org.springframework.data.rest.core.annotation.HandleBeforeSave;
import org.springframework.data.rest.core.annotation.RepositoryEventHandler;
import org.springframework.data.rest.core.util.Methods;
import org.springframework.util.ClassUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
@@ -123,27 +122,19 @@ public class AnnotatedEventHandlerInvoker implements ApplicationListener<Reposit
return bean;
}
ReflectionUtils.doWithMethods(beanType, new ReflectionUtils.MethodCallback() {
for (Method method : ReflectionUtils.getUniqueDeclaredMethods(beanType)) {
/*
* (non-Javadoc)
* @see org.springframework.util.ReflectionUtils.MethodCallback#doWith(java.lang.reflect.Method)
*/
@Override
public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
inspect(bean, method, HandleBeforeCreate.class, BeforeCreateEvent.class);
inspect(bean, method, HandleAfterCreate.class, AfterCreateEvent.class);
inspect(bean, method, HandleBeforeSave.class, BeforeSaveEvent.class);
inspect(bean, method, HandleAfterSave.class, AfterSaveEvent.class);
inspect(bean, method, HandleBeforeLinkSave.class, BeforeLinkSaveEvent.class);
inspect(bean, method, HandleAfterLinkSave.class, AfterLinkSaveEvent.class);
inspect(bean, method, HandleBeforeDelete.class, BeforeDeleteEvent.class);
inspect(bean, method, HandleAfterDelete.class, AfterDeleteEvent.class);
inspect(bean, method, HandleBeforeLinkDelete.class, BeforeLinkDeleteEvent.class);
inspect(bean, method, HandleAfterLinkDelete.class, AfterLinkDeleteEvent.class);
}
}, Methods.USER_METHODS);
inspect(bean, method, HandleBeforeCreate.class, BeforeCreateEvent.class);
inspect(bean, method, HandleAfterCreate.class, AfterCreateEvent.class);
inspect(bean, method, HandleBeforeSave.class, BeforeSaveEvent.class);
inspect(bean, method, HandleAfterSave.class, AfterSaveEvent.class);
inspect(bean, method, HandleBeforeLinkSave.class, BeforeLinkSaveEvent.class);
inspect(bean, method, HandleAfterLinkSave.class, AfterLinkSaveEvent.class);
inspect(bean, method, HandleBeforeDelete.class, BeforeDeleteEvent.class);
inspect(bean, method, HandleAfterDelete.class, AfterDeleteEvent.class);
inspect(bean, method, HandleBeforeLinkDelete.class, BeforeLinkDeleteEvent.class);
inspect(bean, method, HandleAfterLinkDelete.class, AfterLinkDeleteEvent.class);
}
return bean;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2015 the original author or authors.
* Copyright 2012-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -25,7 +25,9 @@ import org.springframework.util.ReflectionUtils;
*
* @author Jon Brisbin
* @author Oliver Gierke
* @deprecated prefer {@link ReflectionUtils#getUniqueDeclaredMethods(Class)}, to be removed with 3.0
*/
@Deprecated
public abstract class Methods {
private Methods() {}
@@ -38,12 +40,12 @@ public abstract class Methods {
*/
@Override
public boolean matches(Method method) {
return !method.isSynthetic() && //
!method.isBridge() && //
!ReflectionUtils.isObjectMethod(method) && //
!ClassUtils.isCglibProxyClass(method.getDeclaringClass()) && //
!ReflectionUtils.isCglibRenamedMethod(method);
!method.isBridge() && //
!ReflectionUtils.isObjectMethod(method) && //
!ClassUtils.isCglibProxyClass(method.getDeclaringClass()) && //
!ReflectionUtils.isCglibRenamedMethod(method);
}
};
}

View File

@@ -17,10 +17,12 @@ package org.springframework.data.rest.core.event;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import org.junit.Test;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.core.annotation.Order;
import org.springframework.data.rest.core.annotation.HandleAfterCreate;
import org.springframework.data.rest.core.annotation.HandleBeforeCreate;
import org.springframework.data.rest.core.annotation.RepositoryEventHandler;
import org.springframework.data.rest.core.domain.Person;
@@ -102,6 +104,19 @@ public class AnnotatedEventHandlerInvokerUnitTests {
assertThat(secondHandler.callCount, is(1));
}
@Test // DATAREST-1075
public void doesInvokeMethodOnlyOnceForMockitoSpy() {
EventHandler handler = spy(new EventHandler());
AnnotatedEventHandlerInvoker invoker = new AnnotatedEventHandlerInvoker();
invoker.postProcessAfterInitialization(handler, "handler");
Payload payload = new Payload();
invoker.onApplicationEvent(new AfterCreateEvent(payload));
verify(handler, times(1)).doAfterCreate(payload);
}
@RepositoryEventHandler
static class Sample {
@@ -170,4 +185,13 @@ public class AnnotatedEventHandlerInvokerUnitTests {
static class FirstEntity extends BaseEntity {}
static class SecondEntity extends BaseEntity {}
@RepositoryEventHandler
static class EventHandler {
@HandleAfterCreate
public void doAfterCreate(Payload bar) {}
}
static class Payload {}
}