From be8964c46538a8de802e67ba23f64fbfbd5b4105 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Mon, 6 Jul 2015 11:42:35 +0200 Subject: [PATCH] DATAREST-582 - Fixed potential double invocation of annotated event handlers. AnnotatedEventHandlerInvoker now uses the user class to detect handler methods on to make sure we don't accidentally find CGLib generated methods and thus invoke the handler multiple times later on. Extended MethodFilter Methods.USER_METHODS to filter methods on CGLib proxy classes, too. Added missing license header and Javadocs where necessary. --- .../event/AnnotatedEventHandlerInvoker.java | 7 +- .../data/rest/core/util/Methods.java | 26 +++++++- ...AnnotatedEventHandlerInvokerUnitTests.java | 62 ++++++++++++++++++ .../data/rest/core/util/MethodsUnitTests.java | 64 +++++++++++++++++++ 4 files changed, 154 insertions(+), 5 deletions(-) create mode 100644 spring-data-rest-core/src/test/java/org/springframework/data/rest/core/event/AnnotatedEventHandlerInvokerUnitTests.java create mode 100644 spring-data-rest-core/src/test/java/org/springframework/data/rest/core/util/MethodsUnitTests.java diff --git a/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/event/AnnotatedEventHandlerInvoker.java b/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/event/AnnotatedEventHandlerInvoker.java index 9ae69a2b2..883303f0a 100644 --- a/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/event/AnnotatedEventHandlerInvoker.java +++ b/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/event/AnnotatedEventHandlerInvoker.java @@ -108,8 +108,8 @@ public class AnnotatedEventHandlerInvoker implements ApplicationListener beanType = bean.getClass(); - + + Class beanType = ClassUtils.getUserClass(bean); RepositoryEventHandler typeAnno = AnnotationUtils.findAnnotation(beanType, RepositoryEventHandler.class); if (typeAnno == null) { @@ -174,13 +174,14 @@ public class AnnotatedEventHandlerInvoker implements ApplicationListener targetType; final Method method; final Object handler; private EventHandlerMethod(Class targetType, Object handler, Method method) { + this.targetType = targetType; this.method = method; this.handler = handler; diff --git a/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/util/Methods.java b/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/util/Methods.java index a1774208c..2bcd086c8 100644 --- a/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/util/Methods.java +++ b/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/util/Methods.java @@ -1,10 +1,28 @@ +/* + * 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 */ @@ -20,8 +38,12 @@ public abstract class Methods { */ @Override public boolean matches(Method method) { - return (!method.isSynthetic() && !method.isBridge() && method.getDeclaringClass() != Object.class && !method - .getName().contains("$")); + + return !method.isSynthetic() && // + !method.isBridge() && // + !ReflectionUtils.isObjectMethod(method) && // + !ClassUtils.isCglibProxyClass(method.getDeclaringClass()) && // + !ReflectionUtils.isCglibRenamedMethod(method); } }; } diff --git a/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/event/AnnotatedEventHandlerInvokerUnitTests.java b/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/event/AnnotatedEventHandlerInvokerUnitTests.java new file mode 100644 index 000000000..c4a7d9f6c --- /dev/null +++ b/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/event/AnnotatedEventHandlerInvokerUnitTests.java @@ -0,0 +1,62 @@ +/* + * Copyright 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.event; + +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.*; + +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.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 { + + /** + * @see DATAREST-582 + */ + @Test + @SuppressWarnings("unchecked") + public void doesNotDiscoverMethodsOnProxyClasses() { + + ProxyFactory factory = new ProxyFactory(); + factory.setTarget(new Sample()); + factory.setProxyTargetClass(true); + + AnnotatedEventHandlerInvoker invoker = new AnnotatedEventHandlerInvoker(); + invoker.postProcessAfterInitialization(factory.getProxy(), "proxy"); + + MultiValueMap, EventHandlerMethod> methods = (MultiValueMap, EventHandlerMethod>) ReflectionTestUtils + .getField(invoker, "handlerMethods"); + + assertThat(methods.get(BeforeCreateEvent.class), hasSize(1)); + } + + @RepositoryEventHandler + static class Sample { + + @HandleBeforeCreate + public void method(Sample sample) {} + } +} diff --git a/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/util/MethodsUnitTests.java b/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/util/MethodsUnitTests.java new file mode 100644 index 000000000..1f2a89eb7 --- /dev/null +++ b/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/util/MethodsUnitTests.java @@ -0,0 +1,64 @@ +/* + * Copyright 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 static org.hamcrest.Matchers.*; +import static org.junit.Assert.*; + +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; +import org.springframework.util.ReflectionUtils.MethodCallback; + +/** + * Unit tests for {@link Methods}. + * + * @author Oliver Gierke + */ +public class MethodsUnitTests { + + /** + * @see DATAREST-582 + */ + @Test + public void userMethodsFilterSkipsMethodsIntroducedByProxying() throws Exception { + + ProxyFactory factory = new ProxyFactory(); + factory.setTarget(new Sample()); + factory.setProxyTargetClass(true); + + final Set methods = new HashSet(); + + ReflectionUtils.doWithMethods(factory.getProxy().getClass(), new MethodCallback() { + + @Override + public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException { + methods.add(method); + } + }, Methods.USER_METHODS); + + assertThat(methods, hasSize(1)); + assertThat(methods, contains(Sample.class.getMethod("method"))); + } + + static class Sample { + public void method() {} + } +}