Reflection
public class reflection_test
{
public static void main(String[] args)
{
ArrayList<Object> obj =new ArrayList<Object>();
obj.add("hi"); //passing String
obj.add(123); //Passing int
reflection_class.Caller_method("method2",obj);
}
}
class reflection_class
{
// reflection_class rc_instance=new reflection_class();
// Class rc_Class=rc_instance.getClass();
public static void Caller_method (String method_name,ArrayList<Object> parameters)
{
//Method m=rc_Class.getMethod("method1", ArrayList.class );
Method m=reflection_class.class.getMethod(method_name, ArrayList.class);
//getdeclaredmethod can also be used
m.invoke(method_name, parameters);
}
public static void method1(ArrayList<Object> parameters)
{
System.out.println("method1: "+parameters.get(0));
System.out.println("method1: "+parameters.get(1));
}
public static void method2(ArrayList<Object> parameters)
{
System.out.println("method2:"+parameters.get(0));
}
}
No comments:
Post a Comment