是否可以使用Java反射从另一个类实例化私有内部类.例如,如果我采用此代码
public class Main {
public static void main(String[] args) {}
}
class OtherClass {
private class Test {}
}
Run Code Online (Sandbox Code Playgroud)
是否可以从类main中的main方法实例化并获得对Test的访问权限.
如何限制开发人员使用反射来访问Java中的私有方法和构造函数?
使用普通的Java代码,我们无法访问类外的私有构造函数或私有方法.但是通过使用反射,我们可以访问Java类中的任何私有方法和构造函数.
那么我们如何为Java代码提供安全保障呢?
关于这个问题,我一直在阅读关于Stackoverflow的很多问题,但无法找到解决方案或解决我的问题.如果已经有人,如果有人提示,我将不胜感激......
我的问题/问题是,是否可以完全禁用不值得信赖的代码的反射?功能如getDeclaredMethods()(见test.java).我已经有了一个Java安全管理器,如果代码试图写/读/等,它会抛出安全例外....
如果有可能,有人可以告诉我怎么样?
布鲁诺
test.java
TestClass cls = new TestClass();
Class c = cls.getClass();
// returns the array of Method objects
Method[] m = c.getDeclaredMethods();
for(int i = 0; i < m.length; i++) {
System.out.println("method = " + m[i].toString());
}
Run Code Online (Sandbox Code Playgroud) I am writing a plugin of another large java program .
I want to modify some byte code of some java method of the java program during runtime, so that I can intercept the method calls (namely, inject some hooking code into the method).
Any way can achieve this?
PS:
I've checked the following approaches:
1.change the classloader of the java program. (we CANNOT change it)
2.use java proxy. (We CANNOT use java proxy, because java proxy would create a …