From 745650963f052a612cd67ab8865e4d9c997942ac Mon Sep 17 00:00:00 2001 From: Mark Fisher Date: Tue, 22 Sep 2009 19:32:52 +0000 Subject: [PATCH] INT-797 Any method originally defined on Object is now excluded from the candidates for Message-handling. --- .../handler/HandlerMethodUtils.java | 19 +++- .../handler/HandlerMethodUtilsTests.java | 101 ++++++++++++++++++ 2 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 org.springframework.integration/src/test/java/org/springframework/integration/handler/HandlerMethodUtilsTests.java diff --git a/org.springframework.integration/src/main/java/org/springframework/integration/handler/HandlerMethodUtils.java b/org.springframework.integration/src/main/java/org/springframework/integration/handler/HandlerMethodUtils.java index 0f2d135ead..1c354c9b4a 100644 --- a/org.springframework.integration/src/main/java/org/springframework/integration/handler/HandlerMethodUtils.java +++ b/org.springframework.integration/src/main/java/org/springframework/integration/handler/HandlerMethodUtils.java @@ -26,6 +26,7 @@ import java.util.Map; import org.springframework.aop.support.AopUtils; import org.springframework.integration.annotation.Header; import org.springframework.integration.annotation.Headers; +import org.springframework.util.ReflectionUtils; /** * Utility methods for common behavior related to Message-handling methods. @@ -42,7 +43,7 @@ abstract class HandlerMethodUtils { * object. Others must be annotated for accepting Message header values. */ public static boolean isValidHandlerMethod(Method method) { - if (method.getDeclaringClass().equals(Object.class)) { + if (isMethodDefinedOnObjectClass(method)) { return false; } if (!Modifier.isPublic(method.getModifiers())) { @@ -96,4 +97,20 @@ abstract class HandlerMethodUtils { return false; } + private static boolean isMethodDefinedOnObjectClass(Method method) { + if (method == null) { + return false; + } + if (method.getDeclaringClass().equals(Object.class)) { + return true; + } + if (ReflectionUtils.isEqualsMethod(method) || + ReflectionUtils.isHashCodeMethod(method) || + ReflectionUtils.isToStringMethod(method) || + AopUtils.isFinalizeMethod(method)) { + return true; + } + return (method.getName().equals("clone") && method.getParameterTypes().length == 0); + } + } diff --git a/org.springframework.integration/src/test/java/org/springframework/integration/handler/HandlerMethodUtilsTests.java b/org.springframework.integration/src/test/java/org/springframework/integration/handler/HandlerMethodUtilsTests.java new file mode 100644 index 0000000000..f0db52e4fc --- /dev/null +++ b/org.springframework.integration/src/test/java/org/springframework/integration/handler/HandlerMethodUtilsTests.java @@ -0,0 +1,101 @@ +/* + * Copyright 2002-2009 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.integration.handler; + +import static org.junit.Assert.assertEquals; + +import java.lang.reflect.Method; + +import org.aopalliance.intercept.MethodInterceptor; +import org.aopalliance.intercept.MethodInvocation; +import org.junit.Test; + +import org.springframework.aop.framework.ProxyFactory; + +/** + * @author Mark Fisher + */ +public class HandlerMethodUtilsTests { + + @Test + public void testNonProxyIgnoresMethodsFromObjectClass() { + TestBean testBean = new TestBean(); + Method[] methods = HandlerMethodUtils.getCandidateHandlerMethods(testBean); + assertEquals(2, methods.length); + } + + @Test + public void testDynamicProxyIgnoresMethodsFromObjectClass() { + ProxyFactory pf = new ProxyFactory(TestInterface.class, new TestInterceptor()); + pf.setTarget(new TestBean()); + Object proxy = pf.getProxy(); + Method[] methods = HandlerMethodUtils.getCandidateHandlerMethods(proxy); + assertEquals(2, methods.length); + } + + @Test + public void testCglibProxyIgnoresMethodsFromObjectClass() { + ProxyFactory pf = new ProxyFactory(TestInterface.class, new TestInterceptor()); + pf.setTarget(new TestBean()); + pf.setProxyTargetClass(true); + Object proxy = pf.getProxy(); + Method[] methods = HandlerMethodUtils.getCandidateHandlerMethods(proxy); + assertEquals(2, methods.length); + } + + + public static class TestBean implements TestInterface { + + public void foo() { + } + + public void foo(String s) { + } + + public String toString() { + return "test"; + } + + public boolean equals(Object o) { + return (this == o); + } + + public int hashCode() { + return 23; + } + + public Object clone() { + return new TestBean(); + } + + public void finalize() { + } + } + + + public static interface TestInterface { + void foo(); + } + + + public static class TestInterceptor implements MethodInterceptor { + public Object invoke(MethodInvocation invocation) throws Throwable { + return invocation.proceed(); + } + } + +}