INT-1406 removed obsolete MethodInvoker code from org.springframework.integration.util
This commit is contained in:
@@ -1,114 +0,0 @@
|
||||
/*
|
||||
* 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.util;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.beans.SimpleTypeConverter;
|
||||
import org.springframework.beans.TypeConverter;
|
||||
import org.springframework.beans.TypeMismatchException;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
/**
|
||||
* Implementation of {@link MethodInvoker} to be used when the actual {@link Method} reference is known.
|
||||
*
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class DefaultMethodInvoker implements MethodInvoker {
|
||||
|
||||
private final Log logger = LogFactory.getLog(this.getClass());
|
||||
|
||||
private final Object object;
|
||||
|
||||
private volatile Method method;
|
||||
|
||||
private volatile TypeConverter typeConverter;
|
||||
|
||||
|
||||
public DefaultMethodInvoker(Object object, Method method) {
|
||||
Assert.notNull(object, "object must not be null");
|
||||
Assert.notNull(method, "method must not be null");
|
||||
this.object = object;
|
||||
this.method = method;
|
||||
}
|
||||
|
||||
|
||||
public void setTypeConverter(TypeConverter typeConverter) {
|
||||
this.typeConverter = typeConverter;
|
||||
}
|
||||
|
||||
protected TypeConverter getTypeConverter() {
|
||||
if (this.typeConverter == null) {
|
||||
this.typeConverter = new SimpleTypeConverter();
|
||||
}
|
||||
return this.typeConverter;
|
||||
}
|
||||
|
||||
public Object invokeMethod(Object ... args) throws Exception {
|
||||
TypeConverter converter = getTypeConverter();
|
||||
int argCount = args.length;
|
||||
Class<?>[] paramTypes = this.method.getParameterTypes();
|
||||
if (paramTypes.length != argCount) {
|
||||
throw new IllegalArgumentException("Wrong number of arguments. Expected types " +
|
||||
ObjectUtils.nullSafeToString(paramTypes) + ", but received values " +
|
||||
ObjectUtils.nullSafeToString(args) + ".");
|
||||
}
|
||||
Object[] convertedArgs = new Object[argCount];
|
||||
boolean match = true;
|
||||
for (int i = 0; i < argCount && match; i++) {
|
||||
try {
|
||||
convertedArgs[i] = converter.convertIfNecessary(args[i], paramTypes[i]);
|
||||
}
|
||||
catch (TypeMismatchException e) {
|
||||
if (Map.class.isAssignableFrom(paramTypes[i])) {
|
||||
// may be able to fallback to Message Headers for this argument
|
||||
convertedArgs[i] = args[i];
|
||||
}
|
||||
else {
|
||||
throw new IllegalArgumentException("Failed to convert argument type.", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
this.method.setAccessible(true);
|
||||
if (this.logger.isDebugEnabled()) {
|
||||
logger.debug("Invoking method [" + this.method + "] with arguments [" +
|
||||
ObjectUtils.nullSafeToString(convertedArgs) + "]");
|
||||
}
|
||||
try {
|
||||
return this.method.invoke(this.object, convertedArgs);
|
||||
}
|
||||
catch (IllegalArgumentException e) {
|
||||
org.springframework.util.MethodInvoker helper = new org.springframework.util.MethodInvoker();
|
||||
helper.setTargetObject(this.object);
|
||||
helper.setTargetMethod(this.method.getName());
|
||||
helper.setArguments(convertedArgs);
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Attempting fallback based on method name [" + this.method.getName() +
|
||||
"] with arguments [" + ObjectUtils.nullSafeToString(convertedArgs) + "]");
|
||||
}
|
||||
helper.prepare();
|
||||
this.method = helper.getPreparedMethod();
|
||||
return this.method.invoke(this.object, convertedArgs);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2008 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.util;
|
||||
|
||||
/**
|
||||
* A strategy interface for invoking a method.
|
||||
* Typically used by adapters.
|
||||
*
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public interface MethodInvoker {
|
||||
|
||||
Object invokeMethod(Object ... args) throws Exception;
|
||||
|
||||
}
|
||||
@@ -1,61 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2008 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.util;
|
||||
|
||||
import org.springframework.beans.support.ArgumentConvertingMethodInvoker;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Implementation of {@link MethodInvoker} to be used when only the method name is known.
|
||||
*
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class NameResolvingMethodInvoker implements MethodInvoker {
|
||||
|
||||
private final Object object;
|
||||
|
||||
private final String methodName;
|
||||
|
||||
private volatile MethodValidator methodValidator;
|
||||
|
||||
|
||||
public NameResolvingMethodInvoker(Object object, String methodName) {
|
||||
Assert.notNull(object, "'object' must not be null");
|
||||
Assert.notNull(methodName, "'methodName' must not be null");
|
||||
this.object = object;
|
||||
this.methodName = methodName;
|
||||
}
|
||||
|
||||
|
||||
public void setMethodValidator(MethodValidator methodValidator) {
|
||||
this.methodValidator = methodValidator;
|
||||
}
|
||||
|
||||
public Object invokeMethod(Object ... args) throws Exception {
|
||||
ArgumentConvertingMethodInvoker invoker = new ArgumentConvertingMethodInvoker();
|
||||
invoker.setTargetObject(this.object);
|
||||
invoker.setTargetMethod(this.methodName);
|
||||
invoker.setArguments(args);
|
||||
invoker.prepare();
|
||||
invoker.getPreparedMethod().setAccessible(true);
|
||||
if (this.methodValidator != null) {
|
||||
this.methodValidator.validate(invoker.getPreparedMethod());
|
||||
}
|
||||
return invoker.invoke();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,139 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2010 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.util;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.TypeMismatchException;
|
||||
import org.springframework.integration.util.DefaultMethodInvoker;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class DefaultMethodInvokerTests {
|
||||
|
||||
@Test
|
||||
public void testStringArgumentWithVoidReturnAndNoConversionNecessary() throws Exception {
|
||||
TestBean testBean = new TestBean();
|
||||
String methodName = "stringArgumentWithVoidReturn";
|
||||
Method method = testBean.getClass().getMethod(methodName, String.class);
|
||||
DefaultMethodInvoker invoker = new DefaultMethodInvoker(testBean, method);
|
||||
invoker.invokeMethod("test");
|
||||
assertEquals("test", testBean.lastStringArgument);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringArgumentWithVoidReturnAndSuccessfulConversion() throws Exception {
|
||||
TestBean testBean = new TestBean();
|
||||
String methodName = "stringArgumentWithVoidReturn";
|
||||
Method method = testBean.getClass().getMethod(methodName, String.class);
|
||||
DefaultMethodInvoker invoker = new DefaultMethodInvoker(testBean, method);
|
||||
invoker.invokeMethod(new Integer(123));
|
||||
assertEquals("123", testBean.lastStringArgument);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIntegerArgumentWithVoidReturnAndSuccessfulConversion() throws Exception {
|
||||
TestBean testBean = new TestBean();
|
||||
String methodName = "integerArgumentWithVoidReturn";
|
||||
Method method = testBean.getClass().getMethod(methodName, Integer.class);
|
||||
DefaultMethodInvoker invoker = new DefaultMethodInvoker(testBean, method);
|
||||
invoker.invokeMethod("123");
|
||||
assertEquals(new Integer(123), testBean.lastIntegerArgument);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIntegerArgumentWithVoidReturnAndFailedConversion() throws Exception {
|
||||
TestBean testBean = new TestBean();
|
||||
String methodName = "integerArgumentWithVoidReturn";
|
||||
Method method = testBean.getClass().getMethod(methodName, Integer.class);
|
||||
DefaultMethodInvoker invoker = new DefaultMethodInvoker(testBean, method);
|
||||
try {
|
||||
invoker.invokeMethod("ABC");
|
||||
throw new IllegalStateException("method invocation should have failed with TypeMismatchException");
|
||||
}
|
||||
catch (Exception e) {
|
||||
assertEquals(TypeMismatchException.class, e.getCause().getClass());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTwoArgumentsAndNoConversionRequired() throws Exception {
|
||||
TestBean testBean = new TestBean();
|
||||
String methodName = "stringAndIntegerArgumentMethod";
|
||||
Method method = testBean.getClass().getMethod(methodName, String.class, Integer.class);
|
||||
DefaultMethodInvoker invoker = new DefaultMethodInvoker(testBean, method);
|
||||
Object result = invoker.invokeMethod("ABC", new Integer(456));
|
||||
assertEquals(result, "ABC:456");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTwoArgumentsAndSuccessfulConversion() throws Exception {
|
||||
TestBean testBean = new TestBean();
|
||||
String methodName = "stringAndIntegerArgumentMethod";
|
||||
Method method = testBean.getClass().getMethod(methodName, String.class, Integer.class);
|
||||
DefaultMethodInvoker invoker = new DefaultMethodInvoker(testBean, method);
|
||||
Object result = invoker.invokeMethod("ABC", "789");
|
||||
assertEquals(result, "ABC:789");
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void testTwoArgumentMethodWithOnlyOneArgumentProvided() throws Exception {
|
||||
TestBean testBean = new TestBean();
|
||||
String methodName = "stringAndIntegerArgumentMethod";
|
||||
Method method = testBean.getClass().getMethod(methodName, String.class, Integer.class);
|
||||
DefaultMethodInvoker invoker = new DefaultMethodInvoker(testBean, method);
|
||||
invoker.invokeMethod("ABC");
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void testTwoArgumentMethodWithOnlyThreeArgumentsProvided() throws Exception {
|
||||
TestBean testBean = new TestBean();
|
||||
String methodName = "stringAndIntegerArgumentMethod";
|
||||
Method method = testBean.getClass().getMethod(methodName, String.class, Integer.class);
|
||||
DefaultMethodInvoker invoker = new DefaultMethodInvoker(testBean, method);
|
||||
invoker.invokeMethod("ABC", new Integer(123), new Integer(456));
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private static class TestBean {
|
||||
|
||||
String lastStringArgument;
|
||||
|
||||
Integer lastIntegerArgument;
|
||||
|
||||
|
||||
public void stringArgumentWithVoidReturn(String s) {
|
||||
this.lastStringArgument = s;
|
||||
}
|
||||
|
||||
public void integerArgumentWithVoidReturn(Integer i) {
|
||||
this.lastIntegerArgument = i;
|
||||
}
|
||||
|
||||
public String stringAndIntegerArgumentMethod(String s, Integer i) {
|
||||
return s + ":" + i;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user