Java reflection mechanism

Source: Internet
Author: User

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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.