Question

Is there a way I can do something like this:

public class Foo<Bar> {
    public Bar newBar() {
        return GWT.create( /* something useful here */ );
    }
}

Would this work, for example:

public class Foo<Bar> {
    public Bar newBar(Class<Bar> prototype) {
        return GWT.create(prototype);
    }
}

where I would for example do new Foo<MyBar>().newBar(MyBar.class)?

But, ideally, I wouldn't even need to pass the prototype object.

Was it helpful?

Solution

In this case, no. The GWT.create method must always be called with an actual class literal, never with a variable/field/parameter. This is because GWT.create is actually a special sort of new with no arguments, and that won't work with a variable either (without reflection):

Class<? extends Bar> clazz = ...;
Bar instance = new clazz;//nope

The method GWT.create looks like a method call, and in Java it really is, but when you compile to JS, it must turn into a constructor call, following the specific deferred binding rules of the given permutation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top