Additions to Java Reflection: bridging methods and some tool classes in spring

Source: Internet
Author: User

In a previous Article blog post: http://www.cnblogs.com/guangshan/p/4660564.html

There are some places in the source code.

This.bridgedmethod = Bridgemethodresolver.findbridgedmethod (method);

So what is Bridgedmethod?

After finding out, this is called bridging method: http://freish.iteye.com/blog/1158008

The Java compiler uses the bridge approach to compatibility with non-generic usages where generics are supposed to be used.

The following code:

 Public classTestbridgemethod { Public Static voidMain (string[] args) {p P=NewS (); P.test (NewObject ()); }}classP<t> {     Publict Test (T t) {returnT; }}classSextendsP<string>{@Override Publicstring Test (String t) {returnT; }}

P refers to an object of S, but the return value of S's test method is a string, there is no generic in jdk1.4, and the P.test (new object) is not checked so that it is reported at the time of the call ClassCastException

When declaring p, using p<string> p will not have such a problem.

To be compatible with non-generic code, the Java compiler generates two methods for test. Look at the following code:

ImportJava.lang.reflect.Method;Importjava.util.Arrays; Public classTestbridgemethod { Public Static voidMain (string[] args) {Class<?> clazz = S.class; Method[] Methods=Clazz.getmethods ();  for(Method method:methods) {System.out.println (Method.getname ()+ ":" + arrays.tostring (Method.getparametertypes ()) +Method.isbridge ()); }    }}classP<t> {     Publict Test (T t) {returnT; }}classSextendsP<string>{@Override Publicstring Test (String t) {returnT; }}

The result of the operation is:

Test:[class Java.lang.string]false

Test:[class Java.lang.object]true

Getclass:[]false

Hashcode:[]false

Equals:[class Java.lang.object]false

Tostring:[]false

Notify:[]false

Notifyall:[]false

Wait:[long, Int]false

Wait:[]false

Wait:[long]false

The compiler generates two test methods for S, one parameter is string, and is used for generics. One parameter is object, which is used for non-generics, and this method is the bridge method, which calls Method.isbridge to return true.

The previously mentioned failure to use generics correctly results in cross-type checking, which is caused by bridging methods.

There are also some useful collections of util tools in the spring source code:

Http://www.cnblogs.com/younggun/p/3247262.html

Additions to Java Reflection: bridging methods and some tool classes in spring

Related Article

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.