Hi all,
I'm trying to get a servlet to dynamically load a bean with data that it recived in the parameters. I'm using introspection to find th methods of the bean created with Class.forName() (we do not wihs to hadr code anything). The problem is when I call method.invoke() using the created class as the one which the method is to be invoked upon I get java.lang.IllegalArgumentException: object is not an instance of declaring class.
The basic code is:
String beanName = "IntrospectTestBean";
Class bean = Class.forName(beanName);
Class[] parameterTypes = new Class[] {String.class};
while (parameterNames.hasMoreElements()){
try{
String param = (String)parameterNames.nextElement();
Method method = bean.getMethod("set" + param, parameterTypes);
method.invoke(bean, new Object[]{request.getParameter(param)});
}catch (NoSuchMethodException e){
}catch (IllegalAccessException e) { out.println(e);
}catch (IllegalArgumentException e) { out.println(e);
}catch (InvocationTargetException e) { out.println(e);
}
I did notice that if I explicity declare the class (i.e. the one refered to as bean) then it does work.
Any info?
Thanx
Wiszh
I'm trying to get a servlet to dynamically load a bean with data that it recived in the parameters. I'm using introspection to find th methods of the bean created with Class.forName() (we do not wihs to hadr code anything). The problem is when I call method.invoke() using the created class as the one which the method is to be invoked upon I get java.lang.IllegalArgumentException: object is not an instance of declaring class.
The basic code is:
String beanName = "IntrospectTestBean";
Class bean = Class.forName(beanName);
Class[] parameterTypes = new Class[] {String.class};
while (parameterNames.hasMoreElements()){
try{
String param = (String)parameterNames.nextElement();
Method method = bean.getMethod("set" + param, parameterTypes);
method.invoke(bean, new Object[]{request.getParameter(param)});
}catch (NoSuchMethodException e){
}catch (IllegalAccessException e) { out.println(e);
}catch (IllegalArgumentException e) { out.println(e);
}catch (InvocationTargetException e) { out.println(e);
}
I did notice that if I explicity declare the class (i.e. the one refered to as bean) then it does work.
Any info?
Thanx
Wiszh