JAVA and dynamic proxy reflection

       Recent work in contact with a dynamic proxy mode on JAVA; its principle is the use of JAVA reflection at runtime to generate proxy classes, dynamic agent benefits compared to static agent is a larger number of class when you need to be proxied, can be reduced program development phase of a large number of predefined proxy class, and the work generated proxy class on the stage so that the program is running JVM process.

       AIDL static proxy will be used in the development, AIDL server program defined the interface, and then AIDL client program calls the server program through a proxy class interface to achieve the purpose of communication.

       Current dynamic contact MVVM agent under development model, the message sender callback proxy class by passing the message to the real callback class, and finally transferred to the recipient; callback since the number of very large, so the dynamic proxies, as follows:

Proxy.newProxyInstance(

target.getClass().getClassLoader(), target.getClass().getInterfaces(), new InvocationHandler() {

@Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable

{ return null; } })

Which targe to be a proxy for the class, the core dynamic proxy implementation is InvocationHandler class.

       The core technology used in dynamic proxy is a reflection and polymorphism; just remember when learning JAVA teacher to explain the variables and methods through private property is not in JAVA other classes can not be accessed and modified method is implemented through reflection.

        I.e., at run time when the program first JVM to execute a new object, such as Person person = new Person (); 

        At this point the JVM to load compiled Person.class file and generate a Class object; a class has only one Class object, reflection of nature that is the reverse of the Person object to obtain information by this Class object.

        Code sample reflection, assuming Person class id attribute is private, the id attribute may be changed by the following code:

        Class personClazz = Class.forName("Person");

        Person person = personClazz.newInstance();

        Filed  id = personClazz.getDeclaredFiled("id");      

         id.setAccesible(true);

         id.set(person,1);

        Polymorphism is the definition of a parent or parent interface, passing a superclass variable in the development stage in the program development phase, JVM dynamically loaded at runtime by a subclass, and call the relevant properties and functions subclasses.

        Summary: When some classes do not want to be directly exposed to other categories eligible for the program operation, you can use proxy mode; when the number of classes that need to be static proxy agent can be used, and if a large number of dynamic proxies should be used, dynamic principle proxy is JAVA program at runtime JVM can get information to the parent class subclass object reference points to their class.

Published 11 original articles · won praise 2 · views 10000 +

Guess you like

Origin blog.csdn.net/zhengyin_tmac/article/details/104961857
Recommended