user1465576
user1465576

Reputation: 33

gwt 2.8.0 jsinterop export not working

Reviewing this document: https://docs.google.com/document/d/10fmlEYIHcyead_4R1S5wKGs1t2I7Fnp_PaNaa7XTEk0/edit

Exporting a java class and preserving its method names in javascript, should be as easy as annotating a class, and does not seem to work.

Here are the steps:

  • Create a new GWT Web Application in Eclipse. Project name="jsinterop" , package="com.example.jsinterop", use GWT2.8.0 and uncheck App Engine and click finish.
  • Create new class

    package com.example.jsinterop.client;
    
    import jsinterop.annotations.*;
    
    @JsType
    public class Foo {
      public int x;
      public int y;
    
      public int sum() {
        return x + y;
      }
    }
  • add JSNI method to Entry point class

    public static native int callfoo() /*-{
    		var foo = new com.example.jsinterop.client.Foo();
    		foo.x = 1;
    		foo.y = 2;
    
    		return foo.sum();
    
      }-*/;
  • call this method in the Entry point.

    callfoo();

  • Running the application will produce a javascript error like this in Chrome:

    Uncaught ReferenceError: com is not defined
        at rb_g$ (Jsinterop.java:155)
        at qb_g$.sb_g$ [as onModuleLoad_0_g$] (Jsinterop.java:77)
        at Array.cyc_g$ (com_00046example_00046jsinterop_00046Jsinterop__EntryMethodHolder.java:3)
        at initializeModules_0_g$ (ModuleUtils.java:44)
        at MJ_g$ (Impl.java:239)
        at PJ_g$ (Impl.java:298)
        at Impl.java:77
        at vxc_g$ (ModuleUtils.java:55)
        at StringHashCache.java:23

    How can I get this simple implementation to work? Is there something I'm missing?

    Thanks in advance.

    Upvotes: 3

    Views: 1772

    Answers (2)

    Thomas Broyer
    Thomas Broyer

    Reputation: 64541

    Nothing is exported by default, you have to ask for it by passing --generateJsInteropExports to GWT (compiler or superdevmode).

    Note that the next version of GWT 2.x will allow you to whitelist packages/classes to export vs the current all-or-nothing.

    Upvotes: 4

    Vincent
    Vincent

    Reputation: 21

    You can use @JsType with a specific namespace like for example:

    @JsType(namespace = JsPackage.GLOBAL)
    class Foo{...}
    

    should make Foo available in JS without "com.example.jsinterop.client."

    agree ?

    and consider the use of @JsMethod(isNative = true, namespace = JsPackage.GLOBAL) as a replacement of JSNI.

    Upvotes: 1

    Related Questions