Below code is an example for it
import java.lang.reflect.Method;
import java.lang.reflect.Type;
public class MethodReflection {
public static void main(String a[]) throws Exception
{
RuleEvaluator evaluator = new RuleEvaluator();
Method[] allMethods = evaluator.getClass().getDeclaredMethods();
for (Method m : allMethods) {
String mname = m.getName();
if(mname.startsWith("difference"))
{
Type[] pType = m.getGenericParameterTypes();
for (int i = 0; i < pType.length; i++)
{
System.out.println("pType is :"+pType[i]);
}
Object o = m.invoke(evaluator,1,2,3,4);
System.out.println("difference is:"+(Integer)o);
}
}
}
}
class RuleEvaluator
{
public int difference(int srcLat, int srcLon, int dstLon, int dstLat)
{
System.out.println("--Invoking difference --");
return srcLat - srcLon - dstLon - dstLon;
}
public int total(int one, int two)
{
return 10;
}
public int sumOf(int[] args)
{
return 10;
}
}
References:
https://docs.oracle.com/javase/tutorial/reflect/member/methodInvocation.html
No comments:
Post a Comment