Monday, June 17, 2019

Re: JSNI - access inner class attribute from external JS?

Thanks for your help Jens.  Is there some documentation that explains this syntax to any degree?  All I can find is this and it isn't enough -

Thats the only information on the official site. contains a short description how a JSNI method call is constructed.

I've tried what you describe but when I call it from my external javascript I get an error indicating that the function I'm calling is not defined. 

Be aware that GWT apps load asynchronous and you have to wait for it so that $wnd.setValueFromExternal is available once your external JS executes.

It passes the GWT compiler but at runtime it can't find the function.  I feel like the syntax isn't correct but I'm not seeing it.  Should "Bar" be the instance name of the internal class within the outer class?  I tried that too, same result.

So it sounds like you want setValue() to be an instance method. But at the same time it sounds like your external JS does not know anything about the Bar instance on which the external JS needs to call setValue(). In that case I would probably do

class Foo {

class Bar {

static void setValueFromExternal(int value) {
Bar barInstance = getBarInstanceSomeHow();


public static native void exportSetValueFromExternalFunction() /*-{
  // Note: when exporting functions do not write "setValueFromExternal(I)()" because that will call the function
  $wnd.setValueFromExternal = $entry(@com.example.Foo.Bar::setValueFromExternal(I));

The above assumes that it is more natural to use Java code to get the Bar instance somehow. Of course you could also do it within the JSNI method but I would try to avoid real code in JSNI methods because that makes it easier to switch to JsInterop (something you should consider if you want that GWT code to be GWT 3 compatible).

-- J.

You received this message because you are subscribed to the Google Groups "GWT Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To view this discussion on the web visit

No comments:

Post a Comment