import org.apache.commons.beanutils.MethodUtils;
public class subscriber
{
public calculator cal=new calculator();
String funcName = "";
int result;
public void getValues(String strfuncName)
{
funcName= strfuncName; //has the function name of class calculator
}
public void giveResult()
{
result=cal.funcName();
//i want it to call the function whose name is stored in funcName
}
}
How do i do this using the reflection and
importing org.apache.commons.beanutils.MethodUtils
And as the function call can be dynamic that is any of the function can be called I cannot predefine it.
Method method = obj.getClass().getMethod("Methodname", new Class[] {});
String output = (String) method.invoke(obj, new Object[] {}); // String return type here, Sorry i mean yourType is your return type..
You can give something like this :
public class subscriber
{
public calculator cal=new calculator();
java.lang.reflect.Method funcName;
int result;
public void getValues(java.lang.reflect.Method strfuncName)
{
funcName= strfuncName; //has the function name of class calculator
}
public void giveResult()
{
result= method.invoke(cal, arg1, arg2,...);
}
}
And Somewhere You can get method , and pass method like this :
subscriber sub=new subscriber();
java.lang.reflect.Method method;
try {
method = obj.getClass().getMethod(methodName, param1.class, param2.class, ..);
sub.getValues(method);
} catch (SecurityException e) {
// ...
} catch (NoSuchMethodException e) {
// ...
}
he parameters identify the very specific method you need (if there are several overloaded available, if the method has no arguments, only give methodName).
Read about Java Reflection
From my blog
/**
* If it exists, invoke methodName on receiver - passing
* parameters (if they exist) as arguments. If
* receiver.methodName(parameters) returns, return the
* returned value.
*
* @param receiver
* The receiver to invoke.
* @param methodName
* The name of the method to call.
* @param parameters
* The arguments to pass to the method.
* @return The value returned from invoking methodName on
* receiver.
* @throws Exception
* Any Exception thrown by invoking the method
* with the passed parameters.
*/
public static Object callMethod(Object receiver,
String methodName, Object... parameters)
throws Exception {
if (receiver == null || methodName == null) {
return null;
}
methodName = methodName.trim();
if (methodName.length() == 0) {
return null;
}
Class<?> cls = receiver.getClass();
Method toInvoke = null;
outer: for (Method method : cls.getMethods()) {
if (!methodName.equals(method.getName())) {
continue;
}
Class<?>[] mTypes = method.getParameterTypes();
if (parameters == null && mTypes == null) {
toInvoke = method;
break;
} else if (safeSize(mTypes) == 0
|| safeSize(parameters) == 0) {
continue;
} else if (safeSize(mTypes) != safeSize(parameters)) {
continue;
}
for (int i = 0; i < mTypes.length; ++i) {
if (!mTypes[i].isAssignableFrom(parameters[i]
.getClass())) {
continue outer;
}
}
toInvoke = method;
break;
}
if (toInvoke != null) {
try {
return toInvoke.invoke(receiver, parameters);
} catch (Exception t) {
throw t;
}
}
return null;
}
/**
* Returns the size (or length) of an array.
*
* @param obj
* The Array to find the size of .
* @return The length of the array (if object is an
* array), or 0.
*/
private static int safeSize(Object obj) {
if (obj != null) {
return Array.getLength(obj);
}
return 0;
}