I am not a Java security expert, but I think you should provide your own SecurityManager and override checkMemberAccess (). for example, to prevent all reflection
public void checkMemberAccess(Class<?> clazz, int which) throws AccessControlException { if (which != Member.PUBLIC) { throw new AccessControlException("No reflection on non-public fields allowed"); } }
Obviously, in the real world, you can only check a specific subset of the βimportantβ classes in the first argument. And, as noted in many other answers, this will cause problems for many third-party libraries.
user949300
source share