0

I am trying to create an object in plain java to pass it to a js library directly (as an anonymous object) using a native method, but the library doesn't seem to "see" its fields.

For example, let's say the library has a method that is normally initialized like this:

mylibrary.initializeApp({
    appId: "myappid",
    name: "test name"}
); 

I want to create a native GWT method that has a parameter a class created in java.

For example, I want to create a class named Config (the library does not have a Config class in javascript):

public class Config {
    public String appId;
    public String name;
}

And a method to initialize the library:

public static native void initializeApp(Config config) /*-{
        console.log(config);
        $wnd.mylibrary.initializeApp(config); 
}-*/;

How can I do this using JS overlay / jsinterop or JSON object utils? (I am trying to create a simple Firebase wrapper for messaging, and I would like to do it as simple as possible)

Andrei F
  • 4,205
  • 9
  • 35
  • 66

1 Answers1

3

Using JsInterop, create your Config class thus:

import jsinterop.annotations.JsPackage;
import jsinterop.annotations.JsType;

@JsType(isNative=true, namespace=JsPackage.GLOBAL, name="Object")
public class Config {
    public String appId;
    public String name;
}

Instantiate an object of Config, which will be an anonymous native object that you can pass to the library as you are already doing in your initializeApp method (which is using a JSNI overlay)

Robert Newton
  • 1,344
  • 12
  • 19