Java Reflection Mechanism Basics:
Reference: http://lavasoft.blog.51cto.com/62575/43218/http://java.ccidnet.com/art/3539/20070924/1222147_1.html
Call to JAVA Invoke:
Like the next class
public class classa{
private int Method1 (string[] strs) {
int num=0;
for (String str:strs)
num++;
return num;
}
}
Now we need to test the Method1 method in this class, and according to the Java reflection mechanism we can run this private method with the following code:
Class Ownerclass=classa.class;
Method method= Ownerclass.getdeclaredmethod ("Method1");
Method.setaccessible (true);
String[] strs= new string[]{"SSSs", "bbbb"};
ClassA classainstance= New ClassA ();
Method.invoke (Classainstance,strs);
An exception that produces a mismatch in the number of arguments is caused by the dynamic parameter matching of the Invoke function, which tells STRs to map to each parameter instead of an array variable, solution:
Class Ownerclass=classa.class;
Method method= Ownerclass.getdeclaredmethod ("Method1");
Method.setaccessible (true);
String[] strs= new string[]{"SSSs", "bbbb"};
Object[] Args=new object[1];
Args[0]=strs;
ClassA classainstance= New ClassA ();
Method.invoke (Classainstance,args);