Using Asserts with IllegalArgumentException/IllegalStateException instead of ConfigurationException.

This commit is contained in:
Mark Fisher
2008-09-29 13:25:43 +00:00
parent 5bf96eeb08
commit e6f22df1bb
4 changed files with 73 additions and 82 deletions

View File

@@ -19,11 +19,12 @@ package org.springframework.integration.aggregator;
import java.lang.reflect.Method;
import java.util.List;
import org.springframework.integration.ConfigurationException;
import org.springframework.integration.message.Message;
import org.springframework.util.Assert;
/**
* Adapter for methods annotated with {@link org.springframework.integration.annotation.CompletionStrategy @CompletionStrategy}
* Adapter for methods annotated with
* {@link org.springframework.integration.annotation.CompletionStrategy @CompletionStrategy}
* and for '<code>completion-strategy</code>' elements that include a '<code>method</code>'
* attribute (e.g. &lt;completion-strategy ref="beanReference" method="methodName"/&gt;).
*
@@ -42,16 +43,14 @@ public class CompletionStrategyAdapter extends MessageListMethodAdapter implemen
}
private void assertMethodReturnsBoolean() {
if (!Boolean.class.equals(this.getMethod().getReturnType())
&& !boolean.class.equals(this.getMethod().getReturnType())) {
throw new ConfigurationException("Method '" + getMethod().getName()
+ "' does not return a boolean value");
}
}
public boolean isComplete(List<Message<?>> messages) {
return ((Boolean) executeMethod(messages)).booleanValue();
}
private void assertMethodReturnsBoolean() {
Assert.isTrue(Boolean.class.equals(this.getMethod().getReturnType())
|| boolean.class.equals(this.getMethod().getReturnType()),
"Method '" + getMethod().getName() + "' does not return a boolean value");
}
}

View File

@@ -23,7 +23,6 @@ import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import org.springframework.integration.ConfigurationException;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessagingException;
import org.springframework.integration.util.DefaultMethodInvoker;
@@ -40,31 +39,33 @@ public abstract class MessageListMethodAdapter {
private final DefaultMethodInvoker invoker;
protected volatile Method method;
protected final Method method;
public MessageListMethodAdapter(Object object, String methodName) {
Assert.notNull(object, "'object' must not be null");
Assert.notNull(methodName, "'methodName' must not be null");
this.method = ReflectionUtils.findMethod(object.getClass(), methodName, new Class<?>[] { List.class });
if (this.method == null) {
throw new ConfigurationException("Method '" + methodName +
"(List<?> args)' not found on '" + object.getClass().getName() + "'.");
}
Assert.notNull(this.method, "Method '" + methodName +
"(List<?> args)' not found on '" + object.getClass().getName() + "'.");
this.invoker = new DefaultMethodInvoker(object, this.method);
}
public MessageListMethodAdapter(Object object, Method method) {
Assert.notNull(object, "'object' must not be null");
Assert.notNull(method, "'method' must not be null");
if (method.getParameterTypes().length != 1 || !method.getParameterTypes()[0].equals(List.class)) {
throw new ConfigurationException("Method must accept exactly one parameter, and it must be a List.");
}
Assert.isTrue(method.getParameterTypes().length == 1
&& method.getParameterTypes()[0].equals(List.class),
"Method must accept exactly one parameter, and it must be a List.");
this.method = method;
this.invoker = new DefaultMethodInvoker(object, this.method);
}
protected Method getMethod() {
return method;
}
private static boolean isActualTypeParameterizedMessage(Method method) {
return (getCollectionActualType(method) instanceof ParameterizedType)
&& Message.class.isAssignableFrom((Class<?>) ((ParameterizedType) getCollectionActualType(method)).getRawType());
@@ -77,11 +78,12 @@ public abstract class MessageListMethodAdapter {
return this.invoker.invokeMethod(messages);
}
return this.invoker.invokeMethod(extractPayloadsFromMessages(messages));
} catch (InvocationTargetException e) {
}
catch (InvocationTargetException e) {
throw new MessagingException(
"Method '" + this.method + "' threw an Exception.", e.getTargetException());
}
catch (Throwable e) {
catch (Exception e) {
throw new MessagingException("Failed to invoke method '" + this.method + "'.");
}
}
@@ -111,12 +113,4 @@ public abstract class MessageListMethodAdapter {
&& method.getGenericParameterTypes()[0] instanceof ParameterizedType;
}
public Method getMethod() {
return method;
}
public void setMethod(Method method) {
this.method = method;
}
}