By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,797 Members | 1,336 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,797 IT Pros & Developers. It's quick & easy.

Reflection passing arrays of primitives as argument

P: n/a
Hi All, I am trying to create a BeanHelper class which will set a beans
values from the request much the same as it would be form a jsp using
'usebean'.

I can get everything to work except for methods which take arrays of
primitives. If my bean method takes an array of primitves I get a
"java.lang.IllegalArgumentException: argument type mismatch" when
method.invoke(bean, args) is called ,where 'bean' is my bean to set & 'args'
is an array of Objects.

Here is a snippet from the code which creates an Object for the args array
which works with Objects & primitives

private static Object getArgument(String parameter, Class paramType) {
Object returnObject = null;
if (paramType.equals(Boolean.class) ||
paramType.equals(boolean.class)) {
System.out.println("returning Boolean");
returnObject = new Boolean(parameter);
}
return returnObject;
}

here's some code from the method which creates Object arrays if the bean
method takes an array. This works if the method argument takes Integer[],
but not if it takes int[].

private static Object[] getArrayArgument(String[] parameters, Class
paramType) {
Object[] returnObject = null;
if (paramType.equals(Integer[].class) ||
paramType.equals(int[].class)) {
System.out.println("returning Integer[]");
Integer[] intArray = new Integer[parameters.length];
for (int i = 0; i < parameters.length; i++) {
intArray[i] = new Integer(parameters[i]);
}
returnObject = intArray;
}
return returnObject;
}
Any help greatly appreciated :-)
--
-P
Jul 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Worked it out :-)
Object returnObject = null;
if (paramType.equals(int[].class)) {
System.out.println("returning int[]");
int[] intArray = new int[parameters.length];
for (int i = 0; i < parameters.length; i++) {
intArray[i] = Integer.parseInt(parameters[i]);
}
returnObject = intArray;
}
--
-P
Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.