A little thought about Java generic "erase"

Source: Internet
Author: User

The first time to write a blog, what to say is not difficult, on the question of generics, is a burst of learning in Jackson encountered.

The following will be what I think of, meet, share out.

Generics are an attribute after JDK1.5, an application of a parameter type that can be declared in a class, interface, or method, just like a local variable defined in our method, and in work we often use it in a collection object such as List,map,set, or in a class we declare ourselves.

When it comes to generics, you must not mention the concept of "erase", compared to C #, Java generics exist only in the program's source code, in the compiled class file does not exist, this process is-generic "erase"; so, for new arraylist<string> and For new arraylist<integer>, two objects are the same after compilation, and any type of object can be added to the collection by reflection;

As you can see, in the compiled class file, the generic type declared by ArrayList does not exist; So, if you want to get generics during a program run, it seems like an impossible thing to do!

Until after studying Jackson, I found that the Java generic "erase" is not generalize, at least through the TypeReference interface, it is possible to obtain the generic type in the object;

Starting with Jackson, the associated API is not very familiar, and JSON has not been converted into a Studententity object, but instead becomes a linkedhashmap; after study, it is found that the TypeReference object can be used, To implement the JSON conversion of the parameterized type;

Some questions? As previously mentioned, because of the generic "erase" reason, generics do not exist in the compiled class file, only the original type (the concept in the type), then how to implement TypeReference, get the specific generic type it!

View the source code of TypeReference, found in its construction method, mainly used to Getgenericsuperclass () and getactualtypearguments
() Two methods, Getgenericsuperclass () returns the parent class with "generics" for this object, and Getactualtypearguments () returns an array of type objects of the actual type parameters in this parent class. The plain is the generics in typereference<>;

Through debug, I found that I actually got the generics in new typereference<map<string,studententity>>, so I decided to write a parent class, a subclass, and see how it works.

Again through debug found, and did not get the specific generics, but to get to the Parameterizedtypeimpl object (the concept of type); At this time, for generics more puzzled, feel heart has thousands grass mud horse in the Pentium!

So, looking back at the previous code new Typereference<map<string,studententity>> () {}, found that this code is actually an anonymous internal class, and when the compiler is compiled, This anonymous inner class is generated by a separate class file with the following naming conventions: Main class +$+ (...)

After the anti-compilation, The Truth at a glance!

Created anonymous inner class new Typereference<map<string,studententity>> () {}, in the generated class file, the compiler defaults to generics in TypeReference map<string,studententity> original type, and will not be erased ! Like we actually create a class typereference<map<string,studententity>>{}, Class test<t>{}, after compiling the MAP, T will still exist!

Thus, for Java generics "erase" does not generalize, during the run, if there is a generic anonymous inner class in the method, then the generic will still be preserved, we can get to the actual generic type by the corresponding Method!

Above, mention the concept of type, primitive type, mentioned in the next blog post!



Jabbo
Links: Https://www.jianshu.com/p/1f608fd05e20
Source: Pinterest
Copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source.

A little thought about Java generic "erase"

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.