[英]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的摘录:
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: 同样:
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.