Recently coding uses the reflection mechanism in Java, the basic features are not introduced, refer to a post for future reference (http://wenku.baidu.com/view/d7be6b22aaea998fcc220e67.html ).
The problems I encountered are briefly described as follows:
Class Pa. A encapsulates reflection calls. Class Pb. B implements the class to be reflected. Pa and Pb are different packages. When the class reflected by B is B itself or B's internal class, it can be reflected normally. When B reflects the implementation class of an interface class (such as IA), whether it is an anonymous class or a famous class, it will cause an ensurememaccess error, that is, Java determines that you have no permission to access this method, all interfaces, implementations, and methods are public.
This problem occurs because it is related to the package access permission. When both classes are put under the same package, everything is normal.
If you must place the code structure in different packages and use the reflection mechanism, we recommend that you open the access permission by obtaining the method object that calls the reflection method, before calling invoke, call setaccessible (true) to release the permission. In this way, the reflection mechanism can continue to work normally.