DATAREST-1075 - AnnotatedEventHandlerInvoker skips methods introduced by proxy.

Removed now unused Methods deprecated in Ingalls / Hopper.
This commit is contained in:
Oliver Gierke
2017-07-06 13:08:50 +02:00
parent 2ad963074c
commit 182d32b715
4 changed files with 36 additions and 121 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,47 +0,0 @@
/*
* Copyright 2012-2015 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.rest.core.util;
import java.lang.reflect.Method;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;
/**
* Simple helper with utilities to work with {@link Method}s.
*
* @author Jon Brisbin
* @author Oliver Gierke
*/
public interface Methods {
static final ReflectionUtils.MethodFilter USER_METHODS = new ReflectionUtils.MethodFilter() {
/*
* (non-Javadoc)
* @see org.springframework.util.ReflectionUtils.MethodFilter#matches(java.lang.reflect.Method)
*/
@Override
public boolean matches(Method method) {
return !method.isSynthetic() && //
!method.isBridge() && //
!ReflectionUtils.isObjectMethod(method) && //
!ClassUtils.isCglibProxyClass(method.getDeclaringClass()) && //
!ReflectionUtils.isCglibRenamedMethod(method);
}
};
}

View File

@@ -16,10 +16,12 @@
package org.springframework.data.rest.core.event;
import static org.assertj.core.api.Assertions.*;
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;
@@ -101,6 +103,19 @@ public class AnnotatedEventHandlerInvokerUnitTests {
assertThat(secondHandler.callCount).isEqualTo(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 {
@@ -169,4 +184,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 {}
}

View File

@@ -1,53 +0,0 @@
/*
* Copyright 2015-2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.rest.core.util;
import static org.assertj.core.api.Assertions.*;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Set;
import org.junit.Test;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.util.ReflectionUtils;
/**
* Unit tests for {@link Methods}.
*
* @author Oliver Gierke
*/
public class MethodsUnitTests {
@Test // DATAREST-582
public void userMethodsFilterSkipsMethodsIntroducedByProxying() throws Exception {
ProxyFactory factory = new ProxyFactory();
factory.setTarget(new Sample());
factory.setProxyTargetClass(true);
Set<Method> methods = new HashSet<Method>();
ReflectionUtils.doWithMethods(factory.getProxy().getClass(), method -> methods.add(method), Methods.USER_METHODS);
assertThat(methods).hasSize(1);
assertThat(methods).contains(Sample.class.getMethod("method"));
}
static class Sample {
public void method() {}
}
}