Friday, 13 September 2013

Not able to pass variables to private method using reflect package

Not able to pass variables to private method using reflect package

I am trying to access private methods using reflect package. I am able to
access private methods but I am facing problem to send arguments. My code
look like
Sample s = new Sample();
java.lang.reflect.Method method [] = Sample.class.getDeclaredMethods();
System.out.println("Total Number of methods in Sample class"+method.length);
for(int i=0; i<method.length;i++){
String methodName=method[i].getName();
System.out.println(methodName);
if(methodName.equalsIgnoreCase("sampleTest1");{
method[i].setAccessible(true);
//This is calling sampleTest1 method, ITs not workoing
//System.out.println(method[i].invoke(s, new String[]{"ABC"});
//} else{
//This is calling sampleTest method its working
System.out.println(method[i].invoke(s, null);
}
}
my Sample class
public class Sample {
private String sampleTest(){
return "Private Method";
}
private String sampleTest1(String abc){
return "Private Method";
}
}
I am able to access sampleTest() method but I dont know how to pass
arguments to sampleTest1()method. Can you please help in this
Thanks,
CVSR Sarma.

No comments:

Post a Comment