Tuesday, August 29, 2017

Weird behavior from JsInterop for uninitialized fields/attributes.

Hello, 

This is a minor remark, and it does affect the functioning of JsInterop, as far as I experienced, but may be worth looking into by GWT folks. I have the following Js object that I am wrapping in Java:

var Hotel = function() {
 
this.guests = [];
};


Hotel.prototype = {
 addGuest
: function(guest) {
 
this.guests.push(guest);
 
},
 getGuestCount
: function(){
 
return this.guests.length;
 
}
}


@JsType(isNative=true, namespace=GLOBAL)
public class Hotel {
 
@JsMethod
 
public native final int getGuestCount();
 
 
@JsMethod
 
public native final void addGuest(Guest guest);
}

I pass a Guest object to the addGuest Method

@JsType(isNative=true, namespace=GLOBAL, name="Object")
public class Guest {
 
 
public int guestId;


 
public String firstName;


 
public String lastName;
 
 
public String roomNumber;
}


public class Test implements EntryPoint {


 
String uninitializedString;
 
 
public void onModuleLoad() {


   
Hotel hotel = new Hotel();


   
Guest guest = new Guest();
     guest
.firstName= "test";
   
//passed object is guest = {firstName: "test"}
     hotel
.addGuest(guest);


   
Guest guest2 = new Guest();
    guest2
.firstName = "test";
    guest2
.lastName = uninitializedString;
   
//passed object is guest = {firstName: "test", lastName: undefined}, should also be guest = {firstName: "test"}
    hotel
.addGuest(guest2);
}

}


In short, if you are assigning an uninitialized variable to an uninitialized Object field, the field will not be ignored anymore by JsInterop, and will be transpiled as undefined, although it is still uninitialized. This happens often with the builder pattern.

--
You received this message because you are subscribed to the Google Groups "GWT Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at https://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment