如何从JSNI中实例化一个java类

时间:2012-03-06 13:51:55

标签: gwt jsni

我正在尝试使用JSO的package-private ::new()方法在JSNI方法体内实例化java类型,但所有实例都作为null句柄返回。 怎么来的?

是否可以使用与Java类似的语法实例化该类?

以下是相关代码:

public class OnChangeHandlerJso extends JavaScriptObject {

    protected OnChangeHandlerJso() {};

    public static native JavaScriptObject create() /*-{

        return function(cm, changeInfo) {

            var eventBus = @com.gigaspaces.codemirror_gwt.client.events.EventUtils::CODE_MIRROR_EVENT_BUS;

            var event = @com.gigaspaces.codemirror_gwt.client.events.EditorContentChangeEvent::new()();

            // could something like this work?
            // var event = new @com.gigaspaces.codemirror_gwt.client.events.EditorContentChangeEvent;

            event.@com.gigaspaces.codemirror_gwt.client.events.EditorContentChangeEvent::setCm(Lcom/gigaspaces/codemirror_gwt/client/jsni/CodeMirrorJso;)(cm);
            event.@com.gigaspaces.codemirror_gwt.client.events.EditorContentChangeEvent::setChangeInfo(Lcom/gigaspaces/codemirror_gwt/client/jsni/ChangeInfoJso;)(changeInfo);
            eventBus.@com.google.gwt.event.shared.EventBus::fireEvent(Lcom/google/gwt/event/shared/GwtEvent;)(event);
        };
    }-*/;
}

1 个答案:

答案 0 :(得分:0)

像这样解决:

叠加类型

public class OnChangeHandlerJso extends JavaScriptObject {

    protected OnChangeHandlerJso() {};

    public static native OnChangeHandlerJso create() /*-{
        return function(cm, changeInfo) {
            @com.gigaspaces.codemirror_gwt.client.vo.OnChangeHandler::handle(Lcom/gigaspaces/codemirror_gwt/client/jsni/CodeMirrorJso;Lcom/gigaspaces/codemirror_gwt/client/jsni/ChangeInfoJso;)(cm, changeInfo);
        };
    }-*/;
}

处理程序

public class OnChangeHandler implements JsoWrapper<OnChangeHandlerJso> {

    public static void handle(CodeMirrorJso cmJso, ChangeInfoJso changeInfoJso) {
        // ...
        EventUtils.CODE_MIRROR_EVENT_BUS.fireEvent(new EditorContentChangeEvent(cm, changeInfo));
    }
}