Skip to content

Instantly share code, notes, and snippets.

@vinsim24
Created February 11, 2017 05:24
Show Gist options
  • Save vinsim24/81d11048df432469bf02994ae65fc8ce to your computer and use it in GitHub Desktop.
Save vinsim24/81d11048df432469bf02994ae65fc8ce to your computer and use it in GitHub Desktop.
Java:Util Class for using Reflection
import org.apache.log4j.Logger;
import java.lang.reflect.Method;
import java.util.Map;
public class ReflectionUtil
{
private static Logger logger = Logger.getLogger(ReflectionUtil.class);
/**
* This is used to run a static method in a class.
* @param className - Fully qualified classname including package name., e.g.,org.w3c.dom.Node(got by doing Node.class.getName()
* @param methodName - Method inside the class which needs to be executed.
* @param paramsMap - Key is fully qualified class name for the argument - Value is the actual argument value
* @return
* @throws Exception
*/
public static Object executeStaticMethod(String className, String methodName, Map<String, Object> paramsMap) throws Exception
{
return executeMethod(className, methodName, paramsMap, true);
}
/**
* Overloaded method for above method, instead of passing the fully qualified classname as String, passing the class object,
* got by doing e.g., Class.forName(Node.class.getName()). This is used to run a static method in a class.
* @param aClass
* @param methodName
* @param paramsMap
* @return
* @throws Exception
*/
public static Object executeStaticMethod(Class<?> aClass, String methodName, Map<String, Object> paramsMap) throws Exception
{
return executeMethod(aClass, methodName, paramsMap, true);
}
/**
* This is used to run any method in a class.
* @param className - Fully qualified classname including package name., e.g.,org.w3c.dom.Node(got by doing Node.class.getName()
* @param methodName - Method inside the class which needs to be executed.
* @param paramsMap - Key is fully qualified class name for the argument - Value is the actual argument value
* @param isStatic - true for static method, false for regular method
* @return
* @throws Exception
*/
public static Object executeMethod(String className, String methodName, Map<String, Object> paramsMap,boolean isStatic) throws Exception
{
Class<?> aClass = Class.forName(className);
return executeMethod(aClass,methodName, paramsMap,isStatic);
}
/**
* Overloaded method for above method, instead of passing the fully qualified classname as String, passing the class object,
* got by doing e.g., Class.forName(Node.class.getName()).
* @param aClass
* @param methodName
* @param paramsMap
* @param isStatic - true for static method, false for regular method
* @return
* @throws Exception
*/
public static Object executeMethod(Class<?> aClass, String methodName, Map<String, Object> paramsMap,boolean isStatic) throws Exception
{
logger.debug("Loaded class: " + aClass+"::method::"+methodName);
Object returnVal = null;
if(paramsMap != null && paramsMap.size() > 0)
{
Class<?> paramClassArr[] = new Class[paramsMap.size()];
int i = 0;
Object paramObjArr[] = new Object[paramsMap.size()];
for(Map.Entry<String, Object> entry: paramsMap.entrySet())
{
paramClassArr[i] = Class.forName(entry.getKey());
paramObjArr[i] = entry.getValue();
i++;
}
Method method = aClass.getMethod(methodName, paramClassArr);
returnVal = method.invoke((isStatic?null:aClass.newInstance()), paramObjArr);
}
return returnVal;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment