Java 无法使用reflect包将变量传递给私有方法

Java 无法使用reflect包将变量传递给私有方法,java,reflection,Java,Reflection,我正在尝试使用reflect包访问私有方法。我可以访问私有方法,但在发送参数时遇到问题 我的代码如下所示: 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

我正在尝试使用reflect包访问私有方法。我可以访问私有方法,但在发送参数时遇到问题

我的代码如下所示:

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("Method Name is "+methodName);
    if (methodName.equalsIgnoreCase("sampleTest1")) {
        method[i].setAccessible(true);
        //This is calling sampleTest1 method; it's not working
        //System.out.println(method[i].invoke(s, new String[]{"ABC"});
    } else {
        //This is calling sampleTest method and it's working
        System.out.println(method[i].invoke(s, null));
    }
}
我可以访问sampleTest()方法,但不知道如何将参数传递给sampleTest1()方法。你能帮忙吗

输出
您需要强制转换传递给
invoke()
null
参数

否则,它认为您正在调用没有参数的底层方法,即a
null
Object[]
。通过强制转换,编译器(和运行时)知道您正在传递一个
null
对象作为varargs
object[]
中的唯一元素

编辑后:是否删除了
setAccessible(true)
?你已经把你的
if-else
交织在一起了。使用

if(methodName.equalsIgnoreCase("sampleTest1")){
    method[i].setAccessible(true);
    System.out.println(method[i].invoke(s, (Object) null));
} else{
    method[i].setAccessible(true);
    System.out.println(method[i].invoke(s, null));
}

或者将
方法[i].setAccessible(true)放入
if-else

之外,您需要将传递给
invoke()
null
参数强制转换

否则,它认为您正在调用没有参数的底层方法,即a
null
Object[]
。通过强制转换,编译器(和运行时)知道您正在传递一个
null
对象作为varargs
object[]
中的唯一元素

编辑后:是否删除了
setAccessible(true)
?你已经把你的
if-else
交织在一起了。使用

if(methodName.equalsIgnoreCase("sampleTest1")){
    method[i].setAccessible(true);
    System.out.println(method[i].invoke(s, (Object) null));
} else{
    method[i].setAccessible(true);
    System.out.println(method[i].invoke(s, null));
}

或者将
方法[i].setAccessible(true)放入
if else

之外,需要传递一个Object数组作为调用()的第二个参数:

或者没有任何论据:

method[i].invoke(s, (Object[]) null);

您需要传递一个Object数组作为调用()的第二个参数:

或者没有任何论据:

method[i].invoke(s, (Object[]) null);
它起作用了

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("Method Name is "+methodName);
            method[i].setAccessible(true);
            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));
            }
    }
    }

public class Sample {

    private String sampleTest(){
        return "Private Method";
    }

    private String sampleTest1(String abc){
        return "Private Method "+abc;
    }
}
Sample s=新样本();
java.lang.reflect.Method[]=Sample.class.getDeclaredMethods();
System.out.println(“示例类中的方法总数”+方法长度);
对于(inti=0;i它的工作

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("Method Name is "+methodName);
            method[i].setAccessible(true);
            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));
            }
    }
    }

public class Sample {

    private String sampleTest(){
        return "Private Method";
    }

    private String sampleTest1(String abc){
        return "Private Method "+abc;
    }
}
Sample s=新样本();
java.lang.reflect.Method[]=Sample.class.getDeclaredMethods();
System.out.println(“示例类中的方法总数”+方法长度);

对于(inti=0;i这应该是一种通用方法。我们检索方法参数并创建实例 同一类的,作为方法参数传递

公共类示例
{
@抑制警告(“未使用”)
私有字符串sampleTest(){返回“私有方法”;}
@抑制警告(“未使用”)
私有字符串sampleTest1(字符串s){返回“私有方法:字符串”;}
@抑制警告(“未使用”)
私有字符串sampleTest2(对象o){返回“私有方法:对象”;}
@抑制警告(“未使用”)
私有字符串sampleTest3(ArrayList列表){返回“私有方法:列表”;}
公共静态void main(字符串[]args)抛出IllegalArgumentException、IllegalAccessException、InvocationTargetException、,
实例化异常
{
样本s=新样本();
java.lang.reflect.Method[]=Sample.class.getDeclaredMethods();
for(int i=0;i

这种方法只有在方法的参数有一个没有参数的构造函数或者完全可以实例化的情况下才有效。例如,
List
是一个接口,因此不起作用,
Double
有一个非空的c'tor,也不起作用等等

输出

sampleTest
Private Method
sampleTest1
Private Method: String
sampleTest2
Private Method: Object
sampleTest3
Private Method: List<String>
sampleTest
私有方法
样本测试1
私有方法:字符串
样本测试2
私有方法:对象
样本测试3
私有方法:列表

这应该是一种通用方法。我们检索方法参数并创建实例 同一类的,作为方法参数传递

公共类示例
{
@抑制警告(“未使用”)
私有字符串sampleTest(){返回“私有方法”;}
@抑制警告(“未使用”)
私有字符串sampleTest1(字符串s){返回“私有方法:字符串”;}
@抑制警告(“未使用”)
私有字符串sampleTest2(对象o){返回“私有方法:对象”;}
@抑制警告(“未使用”)
私有字符串sampleTest3(ArrayList列表){返回“私有方法:列表”;}
公共静态void main(字符串[]args)抛出IllegalArgumentException、IllegalAccessException、InvocationTargetException、,
实例化异常
{
样本s=新样本();
java.lang.reflect.Method[]=Sample.class.getDeclaredMethods();
for(int i=0;i

这种方法只有在方法的参数有一个没有参数的构造函数或者可以实例化时才有效。例如,
List
是一个interfa
sampleTest
Private Method
sampleTest1
Private Method: String
sampleTest2
Private Method: Object
sampleTest3
Private Method: List<String>