简体   繁体   English

更好地使用反射或我的小技巧来访问私有方法?

[英]Better to use reflection or my little hack to access a private method?

I need to access a private method from another class. 我需要从另一个类访问私有方法。 I have two ways of accessing it. 我有两种访问方法。 The first is the obvious reflection. 首先是明显的反思。 The second is sort of a hack. 第二种是骇客。 The private method I need to call is being called from a protected inner class's accessPrivateMethod method. 我需要调用的私有方法是从受保护的内部类的accessPrivateMethod方法中调用的。 This method will literally only call the private method I need. 实际上,此方法只会调用我需要的私有方法。 So, is it better to access it using reflection or is it better to sort of "hack" it by extending the protected inner class that calls it. 因此,最好是使用反射来访问它,还是最好通过扩展受保护的调用它的内部类来“破解”它。 See code: 看到代码:

method = object.getClass().getDeclaredMethod("privateMethod");
method.setAccessible(true);
Object r = method.invoke(object);

Or: (ProtectedInnerClass is a protected inner class in the class whose private method I want to access.) 或:(ProtectedInnerClass是我要访问其私有方法的类中的一个受保护的内部类。)

class Hack extends ProtectedInnerClass {
    public void accessPrivateMethod() {
        // callPrivateMethod literally only calls the private method
        // I need to call.
        super.callPrivateMethod();
    }
}
...
Hack.accessPrivateMethod();

Some additional thoughts: 一些其他想法:

1) I've seen many people on here say to use reflection only as a last resort. 1)我已经看到很多人在这里说仅将反射作为最后的手段。

2) Reflection could cause Security issues? 2)反思会导致安全问题吗? (SecurityManager can deny the setAccessible sometimes?) This needs to work all the time on any machine/setup. (SecurityManager有时会拒绝setAccessible吗?)这需要一直在任何计算机/设置上工作。

If my hack isn't clear please say so and I will try to elaborate more. 如果不清楚,请这样说,我将尝试详细说明。 Thanks! 谢谢!

PS: the private method I need to access is in the JUNG libraries. PS:我需要访问的私有方法在JUNG库中。 Calling it fixes a bug. 调用它可以修复错误。 AKA I'm trying to find a workaround without having to edit any of the JUNG jars. 我又想找到一种解决方法,而无需编辑任何JUNG罐子。

1) I've seen many people on here say to use reflection only as a last resort. 1)我已经看到很多人在这里说仅将反射作为最后的手段。

Assuming your hack actually works, it is better to use that, rather than using reflection. 假设您的hack确实有效,那么最好使用它,而不是使用反射。 This is because using reflection is way more expensive. 这是因为使用反射会更昂贵。

Here's an extract on Java's API concerning reflection : 这是有关反射的Java API的摘录:

  • Because reflection involves types that are dynamically resolved, certain Java virtual machine optimizations can not be performed. 由于反射涉及动态解析的类型,因此无法执行某些Java虚拟机优化。 Consequently, reflective operations have slower performance than their non-reflective counterparts, and should be avoided in sections of code which are called frequently in performance-sensitive applications. 因此,反射操作的性能要比非反射操作慢,因此应该避免在对性能敏感的应用程序中经常调用的代码段中。

2) Reflection could cause Security issues? 2)反思会导致安全问题吗? (SecurityManager can deny the setAccessible sometimes?) This needs to work all the time on any machine/setup. (SecurityManager有时会拒绝setAccessible吗?)这需要一直在任何计算机/设置上工作。

Likewise: 同样:

  • Reflection requires a runtime permission which may not be present when running under a security manager. 反射需要运行时许可,而在安全管理器下运行时可能不存在。 This is in an important consideration for code which has to run in a restricted security context, such as in an Applet. 对于必须在受限的安全上下文(例如Applet)中运行的代码,这是一个重要的考虑因素。

So, not only the setAccessible method may be denied, but the reflection usage overall. 因此,不仅可以拒绝setAccessible方法,还可以拒绝整个反射用法。

Another consideration is that in order to call your Hack class method without instantiation, you need to set the inner method as static. 另一个考虑因素是,为了不实例化地调用Hack类方法,您需要将内部方法设置为静态方法。

class Hack extends ProtectedInnerClass {
   public static void accessPrivateMethod() {
       super.callPrivateMethod();
   }
}
Hack.accessPrivateMethod();

The fact that this question rises is probably caused by bad design or the fact that Java doesn't allow "sub-package" visibility. 出现此问题的事实可能是由于设计不良或Java不允许“子包”可见性这一事实引起的。 However, if performance is a concern, go for the "little" hack. 但是,如果要考虑性能,请尝试“小”技巧。 Otherwise, choose the esthetic solution using reflections. 否则,请使用反射选择美学解决方案。 But in first place, try to find out if your design is good. 但是首先,请尝试确定您的设计是否良好。

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

相关问题 在私有方法和变量的访问中使用反射API是否很好? - Is it good to use reflection API on access of private method and variable? 如何使用反射来访问私有方法? - How do I use reflection to access a private method? 使用 getter 还是直接访问私有成员更好? - Is it better to use getters or to access private members directly? 有效使用Java反射 - 这是一个黑客,还是这种标准做法? - Effective Use of Java Reflection - is this a hack, or is this standard practice? 无法使用反射API访问Java中的私有方法 - Unable to access private method in java using Reflection API Java反射:访问内部类中的私有方法 - Java reflection: access private method inside inner class 有没有办法在有或没有反射的情况下访问内部类中的私有方法 - Is there a way to access private method in inner-class with or without reflection 如何限制开发人员使用反射来访问Java中的私有方法和构造函数? - How to restrict developers to use reflection to access private methods and constructors in Java? 扩展私有内部类:使用反射技巧或复制/粘贴基类更好吗? - Extending private inner classes: Is it better to use reflection hacks or copy/paste the base class? 通过反射调用私有静态方法 - Invoking by reflection a private static method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM