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);

See also  How to Create and Deploy QR Codes Online: A Comprehensive Guide
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist