Java Reflection API has been an interesting API for me to learn.
Here is my code share
Here is the utility class to read getter setter methods in a bean. public
class RequestUtil { /** * This method returns a list of Methods of a bean .
Methods with String as * paramterTypes * * @param clazz * @return * @throws
SecurityException * @throws NoSuchMethodException * @throws
IntrospectionException */ public static Map getWriteMethods(Class clazz)
throws SecurityException, NoSuchMethodException, IntrospectionException { Map
writeMethods = new HashMap(); BeanInfo info = Introspector.getBeanInfo(clazz);
PropertyDescriptor[] pd = info.getPropertyDescriptors(); for (int i = 0; i <
pd.length; i++) { if (pd[i].getWriteMethod() != null) { Method m =
pd[i].getWriteMethod(); writeMethods.put(m.getName().toLowerCase(),
clazz.getMethod(m .getName(), m.getParameterTypes())); } } return
writeMethods; }
public static Object populateBean(Class clazz, Map requestMap)
throws ClassNotFoundExceptio,InstantiationException,
IllegalAccessException,IllegalArgumentException,
InvocationTargetException,SecurityException,NoSuchMethodException,Introspecti
onException{
Class c = Class.forName(clazz.getName());
Object obj = c.newInstance();
Set keySet = requestMap.keySet();
Object param = null;
Map methodMap = null; methodMap = RequestUtil.getWriteMethods(c);
for(Iterator iter = keySet.iterator(); iter.hasNext()){
String key = (String)iter.next();
String setXXX = ("set"+key).toLowerCase();
System.out.println(setXXX);
Method m = (Method)methodMap.get(setXXX);
Class[] pt = m.getParameterTypes();
/* * more conditions to check other primitive types to be added / if (pt[0].equals(int.class) || pt[0].equals(Integer.class)) { param = Integer.valueOf((String) requestMap.get(key)); } else { param = requestMap.get(key); } m.invoke(obj, new Object[] { param }); } return obj; }
public static void main(String[] args) { try {
/* * assume this is the request parameter Map from getParameterMap() / Map req = new HashMap(); req.put(“userName”, “rajesh”); req.put(“passwd”, “pass”); req.put(“age”, “20”);
SampleBean bean = (SampleBean) populateBean(SampleBean.class, req); System.out.println(“Password >>>>>>>>>> ” + bean.getPasswd()); System.out.println(“UserName >>>>>>>>>> ” + bean.getUserName()); System.out.println(“Age >>>>>>>>>>>>>>> ” + bean.getAge());
} catch (Exception e) { e.printStackTrace();
}
} }
The getWriteMethods(Class clazz) method in RequestUtil class returns a Map
that has name of the setter methods of SampleBean.java as key and the Method
object of the corresponding method as value.
Class class and the Method class of the Reflection API is used to get the handler of each setter method of the SampleBean.java
The populateBean(Class clazz, Map map) takes Class and Map as the parameters.The map argument should be the Map that can be got from HttpServletRequest#getParameterMap().The clazz parameter is the .class of the bean (in this case SampleBean.class) in which the parameter has to be populated.The populateBean method uses the Reflection API to populate the bean object.
you can download the source files here
Note: The work is still under progress and lot of change in the code is expected.