正如标题所述,我正在尝试使用Javassist访问封闭类的私有实例字段。这绝对没有目的,我只是想知道是否有可能以及如何实现?
public class Outer {
private String outer_field = "hello";
public void test() {
new Inner().innertest();
}
private class Inner {
void innertest() {}
}
}
还有Main
类进行测试。
public class Main {
public static void main(String[] args) throws Exception {
ClassPool cp = ClassPool.getDefault();
CtClass inner = cp.get("Outer$Inner");
for (CtMethod m : inner.getDeclaredMethods()) {
if (m.getName().equals("innertest")) {
m.insertBefore("System.out.println(this$0.outer_field);"); // how?
}
}
inner.writeFile();
inner.toClass();
new Outer().test(); // Hoping to print "hello"
}
}
答案 0 :(得分:0)
您尝试使用
Python3.6