Java Reflection and Dynamic proxy

Source: Internet
Author: User

There are two things that are not clear, today record, Java reflection and dynamic proxy

Java reflection: 1. Ability to analyze classes at run time

2, in the run is to view the object

3, implement the operation code of the array

4. Use Method Object

In the Java.lang.reflect package, there are three classes field, method, and constructor, respectively, that describe the class's fields, methods, and constructors, and these three classes have a method called GetName, which is used to return the corresponding name.

The field class has a GetType method that returns a class type Object that describes the number of types of fields. The method and Contructor classes contain methods that can report the type of the parameter, and the type of the method can also report a return type

Here are three classes and a method called GetModifiers, which returns an integer value that describes the use of public and static modifiers using a different bit switch setting.

In addition , the integer value returned by the getmodifiers can be analyzed using the static method of the modifier class in the Java.lang.reflect package. such as modifire.ispublic,isprivate,isfinal, etc.

The Getfields,getmethods and GetConstructors methods in class classes will return the public domain supported by the class, the method and the constructor array, including the common members of the superclass. Getdeclarefields of Class

The Getdeclaremethods and Getdeclaredconstructors methods return all of the methods and constructor arrays declared in the class, including private and protected members, but not members of the superclass, and the following Java code is a small piece of code that is used for reflection

Class<test> C=test.class; Test t=c.newinstance (); T.setid (1); Field F=c.getdeclaredfield ("id"); f.setaccessible (true); System.out.println (F.get (t)); f.setaccessible (false); Method M=c.getmethod ("GetId"); Object O=m.invoke (t); System.out.print (o);

Dynamic Proxy: proxy mode, which is a design pattern while Java reflection is the Java language Analysis class structure, the dynamic execution Java program itself has a capability, and the mode does not matter
Proxy mode is a common Java design pattern, his characteristic is that the proxy class and the delegate class have the same interface, the proxy class is mainly responsible for the delegate class preprocessing messages, filtering messages, forwarding messages to the delegate class, and post-processing messages. There is usually an association between the proxy class and the delegate class, and the object of a proxy class is associated with an object of a delegate class, and the object of the proxy class does not actually implement the service, but instead provides a specific service by invoking the related method of the object of the delegate class.

Of course, the two can be combined to do a lot of things, such as AOP programming, Spring's AOP is so dry, there is not much to say, and many of the framework is reflected, in the execution of a method, get parameters will be used, reflection is not to get the method execution parameters

Data reference: Java Core Technology First edition


Java Reflection and Dynamic proxy

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.