Discovering Java compiler flaws from a Java generic code

Source: Internet
Author: User

Recently when writing Java code, need to do object conversion, because the field name is different, beanutils can not meet the requirements, so think of the Java world there is no similar to C # AutoMapper library, found the Modelmapper

Take the official getting started as an example

Source Object

Assume getters and setters on each classclass Order {  customer customer;  Address billingaddress;} Class Customer {  name name;} Class Name {  String firstName;  String LastName;} Class Address {  String Street;  String City;

Target Object

Assume getters and Settersclass orderdto {  String customerfirstname;  String Customerlastname;  String Billingstreet;  String billingcity;}

Since the definition of the above object conforms to the agreed rules, no configuration is required and the conversion of the completed type

Modelmapper modelmapper = new Modelmapper (); Orderdto orderdto = Modelmapper.map (order, orderdto.class);

  

The point is, sometimes we need to convert the whole list, so we don't have to convert every object in the list, but fortunately, the official also provides the appropriate API

Modelmapper modelmapper = new Modelmapper (); Modelmapper.createtypemap (Order.class, Orderdto.class); list<order> orderlist = new arraylist<> ()//todo:orderlist add element type ListType = new typetoken<list< Orderdto>> () {}.gettype (); list<orderdto> dtolist = Modelmapper.map (orderlist, ListType);

  it looks as if everything is OK, and the results of the program run are correct, but look at the last line of code

list<orderdto> dtolist = Modelmapper.map (orderlist, ListType);

The Modelmapper.map method receives 2 parameter types, one is List<order>, the other is type, the compiler cannot know at compile time what type is contained in the type, and how does it know that the type returned by this method is list< Orderdto>? So I changed this line of code to

list<order> dtolist = Modelmapper.map (orderlist, ListType);
The compiler does not have warnings and error prompts, run-time view dtolist objects in the structure of orderdto, view Modelmapper source, Discover
public class Modelmapper {public <D> D map (Object source, Type destinationtype) {...}
From the signature of the method, the type of D is not deterministic (although the method can construct an object by reading the type contained in the type), that is to say, the only thing that can be determined is that the method returns an object, and through the smart hint of idea, this method does return an object, So why is the compiler not warning when assigning object to a list object?


We use Java and C # to make a simple contrast test of this generic notation
Java code
public class Mapper {public    <D> D convert (Object obj) {        return (d) obj;    }}
C # code
public class mapper{Public    T convert (Object obj) {        return (T) obj;    }}

The Java code is compiled, successfully passed, on the contrary, C # code to compile, failed, there is an error, "error cs0246:the type or namespace name ' T ' could not being found (is you missing A using directive or an assembly reference?) (CS0246) ", explicitly stating that the type of T was not found,
From my own understanding, generics in Java because of the runtime type erasure, only at compile time to play the role of type checking, but the above-mentioned notation to make the compiler type check invalidation, it should not let such code through compilation, compiler semantic analysis of the existence of defects.

In addition to spit the type of Java writing, C # through the AutoMapper to the list of the conversion can be written
list<orderdto> dtolist = mapper.map<list<orderdto>> (orderlist);
But there's no list<orderdto>.class in Java, it's a very diaphragm thing.

Since just into Java from C #, understanding will be biased, please correct me

Discovering Java compiler flaws from a Java generic code

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.