devxlogo

Middle Tier Code Optimization With Java Reflection Package

Middle Tier Code Optimization With Java Reflection Package

In Web based programming, we usually check querystring parameters in middle tier java objects and do tedious, if not long, checks via if/else, to perform appropriate action or instantiate classes. A good workaround is to use Java reflection (java.lang.reflect). Class methods, like getMethods[] or getFields[], invoke particular methods based on http querystring params, like Method.invoke(…).
Here is an example java codelet:

 //get Class info of your object say 'a'Class c = a.getClass();//to dynamically get Object/fields based on HTTP querystring namevalue pair, say 'requestParam'Field f = c.getField(requestParam);// or else 'invoke' a particular method vis a vis requestParamMethod[] theMethods = c.getMethods();String result = (String) theMethods[0].invoke(requestParam, null);

size=3>

devx-admin

Share the Post: