Reputation: 33
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:
package com.example.jsinterop.client;
import jsinterop.annotations.*;
@JsType
public class Foo {
public int x;
public int y;
public int sum() {
return x + y;
}
}
public static native int callfoo() /*-{
var foo = new com.example.jsinterop.client.Foo();
foo.x = 1;
foo.y = 2;
return foo.sum();
}-*/;
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
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
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