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.
This commit is contained in:
Oliver Gierke
2015-07-06 11:42:35 +02:00
parent 906d76a196
commit be8964c465
4 changed files with 154 additions and 5 deletions

View File

@@ -108,8 +108,8 @@ public class AnnotatedEventHandlerInvoker implements ApplicationListener<Reposit
*/
@Override
public Object postProcessAfterInitialization(final Object bean, String beanName) throws BeansException {
final Class<?> 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<Reposit
handlerMethods.add(eventType, handlerMethod);
}
private static class EventHandlerMethod {
static class EventHandlerMethod {
final Class<?> targetType;
final Method method;
final Object handler;
private EventHandlerMethod(Class<?> targetType, Object handler, Method method) {
this.targetType = targetType;
this.method = method;
this.handler = handler;

View File

@@ -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);
}
};
}

View File

@@ -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<Class<? extends RepositoryEvent>, EventHandlerMethod> methods = (MultiValueMap<Class<? extends RepositoryEvent>, EventHandlerMethod>) ReflectionTestUtils
.getField(invoker, "handlerMethods");
assertThat(methods.get(BeforeCreateEvent.class), hasSize(1));
}
@RepositoryEventHandler
static class Sample {
@HandleBeforeCreate
public void method(Sample sample) {}
}
}

View File

@@ -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<Method> methods = new HashSet<Method>();
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() {}
}
}