如何从其他包中的类访问包私有类? [英] How to Access package private Class from a Class in some other package?
问题描述
我有以下课程
Hello.java
package speak.hello;
import java.util.Map;
import speak.hi.CustomMap;
import speak.hi.Hi;
public class Hello {
private Hi hi;
Hello(Hi hi) {
this.hi = hi;
}
public String sayHello() {
return "Hello";
}
public String sayHi() {
return hi.sayHi();
}
public Map<String, Object> getMap() {
return hi.getMap();
}
public void clearMap() {
hi.getMap().clear();
}
public void discardMap() {
CustomMap map = (CustomMap) hi.getMap();
map.discard();
}
public static void main(String[] args) {
Hello hello = new Hello(new Hi());
System.out.println(hello.sayHello());
System.out.println(hello.sayHi());
System.out.println(hello.getMap());
hello.clearMap();
System.out.println("--");
hello.discardMap();
}
}
Hi.java
package speak.hi;
import java.util.HashMap;
import java.util.Map;
public class Hi {
public String sayHi() {
return "Hi";
}
public Map<String, Object> getMap() {
return new CustomMap<String, Object>();
}
}
CustomMap.java
package speak.hi;
import java.util.HashMap;
public class CustomMap<K, V> extends HashMap<K, V> {
private static final long serialVersionUID = -7979398843650044928L;
public void discard() {
System.out.println("Discarding Map");
this.clearCache();
this.clear();
}
@Override
public void clear() {
System.out.println("Clearing Map");
super.clear();
}
private void clearCache() {
System.out.println("Clearing Map");
}
}
这一切正常,直到我删除 public
访问说明符来自 CustomMap
This works fine until I remove public
access specifier from CustomMap
package speak.hi;
import java.util.HashMap;
class CustomMap<K, V> extends HashMap<K, V> {
private static final long serialVersionUID = -7979398843650044928L;
public void discard() {
System.out.println("Discarding Map");
this.clearCache();
this.clear();
}
@Override
public void clear() {
System.out.println("Clearing Map");
super.clear();
}
private void clearCache() {
System.out.println("Clearing Map");
}
}
编译器大喊
类型speak.hi.CustomMap不是可见
现在如果我没有选项来修改 speak.hi.CustomMap
(第三方jar等等)。有什么方法我仍然可以使用 CustomMap
来自 speak.hello.Hello
?
Now If I don't have options to modify speak.hi.CustomMap
(third party jar etc..) Is there any way I could still use CustomMap
from speak.hello.Hello
?
我知道的一个选择是将 speak.hello.Hello
移至 speak.hi.Hello
as Now Hello在包 speak.hi
它可以访问包私有类您好
One option that I know is to move speak.hello.Hello
to speak.hi.Hello
as Now Hello is in package speak.hi
it can access package private Class Hi
还有其他办法吗?也许使用反射?
Is there any other way to do this ? Using reflection perhaps ?
编辑:根据@StephenC的要求更新了其他详细信息
EDIT :Updated with additional details as requested by @StephenC
推荐答案
以下方法调用默认
使用反射的范围类方法
Following method Invokes default
scoped class method using reflection
public void discardMap() {
//CustomMap map = (CustomMap) hi.getMap();
//map.discard();
try {
Object o =hi.getClass().getMethod("getMap").invoke(hi);
Method m = o.getClass().getMethod("discard");
m.setAccessible(true);
m.invoke(o);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
这篇关于如何从其他包中的类访问包私有类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!