3

In my GWT project, I really need to implement the same interface between several overlays (JavaScriptObject).

According to the documentation :

Starting with GWT 2.0, it is permissible for JavaScriptObject subtypes to implement interfaces. Every method defined in an interface may map to at most one method declared in a JavaScriptObject subtype. Practically speaking, this means that only one JavaScriptObject type may implement any given interface, but any number of non-JavaScriptObject types may also implement that interface.

So, I would like to "cast" my JavaScriptObject to a real Java object. It allows me to reduce considerably a lot of duplication code.

Is it possible ? Thanks.

1 Answer 1

1

How about creating classes that wrap a JSO instead of inherting it?

public class PersonJso extends JavaScriptObject{
    protected PersonJso() {}
    public static native PersonJso create(String name) /*-{
        return {name : name};
    }-*/;
    public final native String getName() /*-{
        return this.name;
    }-*/;
}

public class AnimalJso extends JavaScriptObject{
    protected AnimalJso() {}
    public static native PersonJso create(String name) /*-{
        return {name : name};
    }-*/;
    public final native String getName() /*-{
        return this.name;
    }-*/;
}
public class AnimalWrapper implements hasName{
    AnimalJso jso;
    public AnimalWrapper(){}
    public AnimalWrapper(AnimalJso jso){
        this.jso = jso;
    }
    @Override
    public String getName() {
        return jso.getName();
    }
}
public class PersonWrapper implements hasName{
    PersonJso jso;  
    public PersonWrapper(){}
    public PersonWrapper(PersonJso jso){
        this.jso = jso;
    }
    @Override
    public String getName() {
        return jso.getName();
    }   
}
Sign up to request clarification or add additional context in comments.

2 Comments

yeah, I think that it's a solution but I have to write a lot of wrappers (exactly the double !). Do you think that it's the unique solution ?
I guess another workaround could be to add methods which you intend to use as interfaces with empty bodies and create a CustomJSO. Then you reproduce your JSO's to extend CustomJSO.That way you can write a single wrapper that wraps any of your JSO's as CustomJSO so that your wrapper can implement any interface which includes methods that you already have added to your CustomJSO.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.