Using JsConstructor to deal with multiple constructors

214
February 05, 2018, at 2:10 PM

I have a class with two constructors:

@JsType
public class Dog implements Animal {
    String name;
    public Dog() { 
        this.name = "Scooby Doo";
    }
    public Dog(String name) {
        this.name = name;
    }
}

I get the following error when I run gwt compile [With GWT-dev 2.8]

[ERROR] Constructor 'Dog(String)' can be a JsConstructor only if all constructors in the class are delegating to it.

I have been trying to work through this error, with not much success. I am not sure how to delegate to the other constructor.

Any help is very much appreciated!

Thanks!

Answer 1

Alas, JavaScript can't handle multiple constructors! You get one and only one.

There are some things you can do to pretend to have more than one - you can check the incoming data, and assign sane defaults:

@JsConstructor
public Dog(@JsOptional String name) {
    if (name == null) {
        name = "Scooby Doo";
    }
    this.name = name;
}

You have to be careful of types here if your two constructors do not use the same types in the same position - judicious use of instanceof might work (just be aware that for JS objects you are using JS instanceof, not java!). Another option lets you be more flexible, but again, no overloaded methods - factory methods:

public static Dog withName(String name) {
    return new Dog(name);
}
public static Doc defaultImpl() {
    return new Dog();
}
@JsIgnore
public Dog() {/*...*/}
@JsIgnore
public Dog(String name) {/*...*/}

Another idea on the same theme would be to create a JsType builder. This is a bit less idiomatic for JS.

Finally, you could consider the dreaded "options object", where your one constructor takes a single Object with properties, or a JsPropertyMap<Any> holding all the possible values, then check nullness and types inside the giant constructor. I avoid this personally - this is one of the terrible things I'm hoping to avoid by writing Java in the first place.

Rent Charter Buses Company
READ ALSO
Java 8 - Improve Sort time for Large number of files by their size

Java 8 - Improve Sort time for Large number of files by their size

I looked up sorting files in directory by size using java array list && How to sort an ArrayList by its elements size in Java?

188
Firebase query - Find item with child that contains string

Firebase query - Find item with child that contains string

I'm having a bit of trouble with a Firebase QueryI want to query for objects, where the objects child value contains a certain string

352
Check all checkboxes in a listview when one is clicked

Check all checkboxes in a listview when one is clicked

I have a listview whereby each listviewitem has a checkbox that the user can select an item withI also have a checkbox above the listviewwhich is expected to automatically check all the checkboxes in my list when clicked

156
javafx+scenebuilder how to overlap/superimpose 2 or more image depending on the position? [on hold]

javafx+scenebuilder how to overlap/superimpose 2 or more image depending on the position? [on hold]

Using scenebuilder+javafx i would like to overlap/superimpose a lot of images changing the initial positioning of scenebuilderin particular when the image exceeds a certain position the other goes behind; I have to do this on many images, how can it be done?

217