Permalink
Browse files

Fix primitive handling in setParameters

  • Loading branch information...
1 parent 736d89a commit ca6115b6525d5fd0bbbfa7c0fc090cd3d4e1f34d @stuartwdouglas stuartwdouglas committed May 20, 2011
Showing with 26 additions and 5 deletions.
  1. +26 −5 src/main/java/org/jboss/invocation/InterceptorContext.java
@@ -22,6 +22,7 @@
package org.jboss.invocation;
+import javax.interceptor.InvocationContext;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.HashMap;
@@ -30,8 +31,6 @@
import java.util.ListIterator;
import java.util.Map;
-import javax.interceptor.InvocationContext;
-
import static org.jboss.invocation.InvocationMessages.msg;
/**
@@ -42,6 +41,20 @@
public final class InterceptorContext implements Cloneable {
private static final List<Interceptor> EMPTY = Collections.<Interceptor>emptyList();
private static final ListIterator<Interceptor> EMPTY_ITR = EMPTY.listIterator();
+ private static final Map<Class<?>, Class<?>> PRIMITIVES;
+
+ static {
+ final HashMap<Class<?>, Class<?>> map = new HashMap<Class<?>, Class<?>>();
+ map.put(Boolean.TYPE, Boolean.class);
+ map.put(Character.TYPE, Character.class);
+ map.put(Byte.TYPE, Byte.class);
+ map.put(Short.TYPE, Short.class);
+ map.put(Integer.TYPE, Integer.class);
+ map.put(Long.TYPE, Long.class);
+ map.put(Float.TYPE, Float.class);
+ map.put(Double.TYPE, Double.class);
+ PRIMITIVES = map;
+ }
private Object target;
private Method method;
@@ -316,7 +329,7 @@ public Method getMethod() {
}
public void setParameters(final Object[] params) {
- if(params == null) {
+ if (params == null) {
throw new IllegalArgumentException("Parameters must not be null");
}
if (method != null) {
@@ -325,9 +338,17 @@ public void setParameters(final Object[] params) {
throw new IllegalArgumentException("Number of parameters must match number of method arguments");
}
for (int i = 0; i < params.length; ++i) {
+ final Object param = params[i];
final Class<?> type = parameterTypes[i];
- if (!type.isAssignableFrom(params[i].getClass())) {
- throw new IllegalArgumentException("Parameter " + i + " (" + params[i] + ") is not assignable to method parameter type " + parameterTypes[i]);
+ if (param == null) {
+ if (type.isPrimitive()) {
+ throw new IllegalArgumentException("Null cannot be assigned to primitive parameter " + i + " (" + parameterTypes[i] + ")");
+ }
+ } else {
+ final Class<?> wrappedType = type.isPrimitive() ? PRIMITIVES.get(type) : type;
+ if (!wrappedType.isAssignableFrom(param.getClass())) {
+ throw new IllegalArgumentException("Parameter " + i + " (" + param + ") is not assignable to method parameter type " + parameterTypes[i]);
+ }
}
}
}

0 comments on commit ca6115b

Please sign in to comment.