Java reflection and dynamic proxy, java reflection dynamic proxy

Source: Internet
Author: User

Java reflection and dynamic proxy, java reflection dynamic proxy

The two things that have been unclear are recorded today, java reflection and dynamic proxy

Java reflection: 1. The ability to analyze classes at runtime

2. view the object during running

3. array operation code

4. Use the Method object

In java. lang. the reflect package contains three classes: Field, Method, and Constructor, which are used to describe the class domain, Method, and Constructor. Each of these classes has a Method called getName, returns the corresponding name.

The Field Class has a getType method to return Class type objects that describe the number of fields. The Method and Contructor classes contain methods that can report parameter types. The Method type can also report return types.

There are three classes and a method called getModifiers. It returns an integer value and uses different bit switches to describe the public and static modifiers.

In addition, you can use the static method of the Modifier class in the java. lang. reflect package to analyze the integer values returned by getModifiers. For example, Modifire. isPublic, isPrivate, and isFinal.

The getFields, getMethods, and getConstructors methods in the class return the public domains, methods, and constructors arrays supported by the class, including the common members of the super class. GetDeclareFields of the Class

The getDeclareMethods and getDeclaredConstructors Methods return all and, method and constructor arrays declared in the class respectively, including private and protected members, but not super class members, the following java code is a short piece of code that uses 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: the proxy mode. This is a design mode, while java reflection is a structure of the java language analysis class. Dynamic execution of the java program itself has the ability to do nothing with the mode.
The proxy mode is a common java design mode. It features that the proxy class has the same interface as the delegate class. The proxy class is mainly responsible for preprocessing, filtering, and forwarding messages to the delegate class, and post-processing messages. There is usually an association between the proxy class and the delegate class. A proxy class object is associated with a delegate class object. The proxy class object itself does not actually implement the service, but calls the relevant methods of the delegate class object, to provide specific services.

Of course, the combination of the two can do a lot of work, for example, for aop programming, Spring's aop is doing this. I will not mention it here, and many frameworks use reflection to execute a method, it is used when obtaining parameters. Reflection cannot obtain parameters for method execution.

Reference: the first version of java Core Technology

Post-reprinted please indicate the source: http://blog.csdn.net/xh199110/article/details/39620473 Apsara blog

Thank you.



What is the relationship between java reflection and dynamic proxy?

Of course, launch is the most important underlying knowledge of Java. Dynamic proxy actually relies on reflection.

What is the proxy class in the proxy mode of the JAVA language reflection mechanism?

Simply put, it inherits the subclass of this class and is generated by JAVA dynamically.
 

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.