INT-797 Any method originally defined on Object is now excluded from the candidates for Message-handling.
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user