That's not correct. It's now doing the check twice.
Once when you do it and once inside the JDK reflection api.
That's horribly inefficient, since these reflection calls are used everywhere.
The whole point of ReflectionUtils is give more reasonable error messages
after reflection calls fail, because the JDK is usually hopeless.
e.g. you'll get something like
| java.lang.IllegalArgumentException: wrong type java.lang.String
|
Without it telling you what invocation is being attempted or which
parameter is wrong.
What's required is to fix "handleErrors" to also check whether
IllegalArgumentException
is due to the wrong target object being passed for non-status invocations
and not just assuming that it is due to an invalid parameter being passed.
public static Throwable handleErrors(String context, Object target,
+ Class<?> clazz,
+ Object instance,
Class<?>[] parameters, Object[] arguments, Throwable t) throws Throwable
{
if (t instanceof IllegalArgumentException)
{
if (target == null)
throw new IllegalArgumentException("Null target for " + context);
+ if (clazz != null && instance != null &&
clazz.isAssignableFrom(instance) == false)
+ throw new IllegalArgumentException("Wrong target. " +
instance.getClass().getName() + " is not a " + clazz.getName());
ArrayList expected = new ArrayList();
if (parameters != null)
{
for (int i = 0; i < parameters.length; ++i)
expected.add(parameters.getName());
}
ArrayList actual = new ArrayList();
if (arguments != null)
{
for (int i = 0; i < arguments.length; ++i)
{
if (arguments == null)
actual.add(null);
else
actual.add(arguments.getClass().getName());
}
}
throw new IllegalArgumentException("Wrong arguments. " + context +
" for target " + target + " expected=" + expected + "
actual=" + actual);
}
else if (t instanceof InvocationTargetException)
{
throw ((InvocationTargetException) t).getTargetException();
}
throw t;
}
and
| public static Object invoke(Method method, Object target, Object[] arguments)
throws Throwable
| {
| if (method == null)
| throw new IllegalArgumentException("Null method");
| try
| {
| return method.invoke(target, arguments);
| }
| catch (Throwable t)
| {
| throw handleErrors(method.getName(), Strings.defaultToString(target),
| + method.getDeclaringClass(), target,
| method.getParameterTypes(), arguments, t);
| }
| }
|
View the original post :
http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4157396#...
Reply to the post :
http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&a...