random thoughts

Reflection in Action

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.

New Features in JDBC 4.0

Whenever i write code for database connection , i use to wonder why i have to load a jdbc driver with Class.forName(). why shouldn the api do this for us , coz its the common thing everyone does.

Finally JDBC 4.0 helped…

if JDBC 4.0 is used , we need not load the JDBC driver , since the api does this for us , provided the driver is in classpath.

and some more explanation about the new features of JDBC 4.0 is available here

i havent tried this api yet and so i have some queries ,

like ….

what if there are two or more jdbc drives in the classpath , which one will be loaded … and so on

will update more about this api , once i finish my experiments :)….

Scripting Languages in Java 5.0 JVM

We know that Tiger (Java 5.0) has given shelter in its JVM , to some of the scripting languages that are well known to us.I was googling to find a list of those scripting languages, and i found it in a wiki page. Here is the list of the scripting languages.

  1. Groovy Groovy is an object oriented programming language for the Java platform. Since it resembles other scripting languages like Python ,Ruby, Perl, it is categgorised under scripting languages. Refer the JSR 241 for more information.
  2. Javascript Javascript is a scripting language implementing the ECMAScript standards
  3. Jython Jython is Python written Java
  4. Jelly Jelly is a Java based scripting languages to process XML
  5. BeanShell BeanShell is yet another scripting language written Java
  6. JRuby JRuby is Ruby written in Java
  7. Tcl/Java Tcl/Java is a bridge between Tcl and Java , that means we can invole Java from Tcl and vice versa
  8. Sleep Sleep stands for Simple Language for Environment Extensions Purposes. …another scripting language written in Java (i dont know how many times i have to write it:))
  9. Bean Scripting Framework Bean Scripting Framework provides a set of Java classes which provides scripting language support within Java applications … other scripting languages written in and for Java Pnuts JudoScript and many more to come …