简体   繁体   English

从 Java 中的 class 外部调用的私有方法

[英]Private method invoking from outside the class in Java

I have one class with private method now i want to access that private method outside the class, which is possible using reflection package in java.我有一个带有私有方法的 class 现在我想在 class 之外访问该私有方法,这可以在 Z921F725A048432 中使用反射 package 来访问。 But what if we make class constructor as private, then how to access that method.但是,如果我们将 class 构造函数设为私有,那么如何访问该方法。 In below code consider that PrivateMethodClass is having private method m1 and private constructor.在下面的代码中,考虑 PrivateMethodClass 具有私有方法 m1 和私有构造函数。

package allprograms;
import java.lang.reflect.*;
public class RecursionDemo {

    public static void main(String[] args) {

        try 
         {

            PrivateMethodClass p = PrivateMethodClass.getInstance();
            //Class c = Class.forName("allprograms.PrivateMethodClass");  //1 
            Class c = Class.class.asSubclass(p.getClass());
            //Object o = c.newInstance();                                   //2
            Method m = c.getDeclaredMethod("m1", null);                 //3
            m.setAccessible(true);
            m.invoke(p, null);                                          //4

        } /*
             * catch(ClassNotFoundException e) { //for 1
             * System.out.println("ClassNotFound"); }
             */catch (IllegalAccessException/* | InstantiationException */e) { // for 2
            System.out.println("Illigal Access Exception or Instantiation Exception");
        } catch(NoSuchMethodException e) {  //for 3 
            System.out.println("No such Method Exception e");
        } catch(Exception e) {   //for 4
            System.out.println("Invocation Target Exception ");
        }
    }

}

I don't understand what your issue is.我不明白你的问题是什么。 Static method getInstance() in class PrivateMethodClass is public so no problem to call it, which means it doesn't matter if the constructor of PrivateMethodClass is private or not. Static class 中的getInstance()方法PrivateMethodClass是公共的,所以调用它没有问题,这意味着PrivateMethodClass的构造函数是否为私有都没有关系。

If, on the other hand, you are asking how to instantiate class PrivateMethodClass without using method getInstance() , this is also not a problem.另一方面,如果您询问如何在使用方法getInstance()的情况下实例化 class PrivateMethodClass ,这也不是问题。 Just as you use getDeclaredMethod() to invoke ( private ) method m1() , you can call method getDeclaredConstructor() to get a reference to the private constructor.正如您使用getDeclaredMethod()调用(私有)方法m1()一样,您可以调用方法getDeclaredConstructor()来获取对私有构造函数的引用。 Example code follows:示例代码如下:

Class<?> c = PrivateMethodClass.class;
try {
    Constructor<?> ctor = c.getDeclaredConstructor();
    ctor.setAccessible(true);
    Object obj = ctor.newInstance();
    System.out.println(obj);
    if (obj instanceof PrivateMethodClass) {
        PrivateMethodClass p = (PrivateMethodClass) obj;
        Method m = c.getDeclaredMethod("m1");
        m.setAccessible(true);
        m.invoke(p);
    }
}
catch (Exception x) {
    x.printStackTrace();
}

Am I missing something?我错过了什么吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM