Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Java
Expertise: Advanced
Feb 20, 2001



Building the Right Environment to Support AI, Machine Learning and Deep Learning

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 

Method[] theMethods = c.getMethods();
String result = (String) theMethods[0].invoke
(requestParam, null);
Khurram Zaheer
Thanks for your registration, follow us on our social networks to keep up-to-date