1

I have the following class:

@JsType
public class Options {

    @JsProperty
    public boolean extractUrlsWithoutProtocol;

    public Options(boolean extractUrlsWithoutProtocol) {
        this.extractUrlsWithoutProtocol = extractUrlsWithoutProtocol;
    }
}

Now I pass it into a javascript method and when I use developer tools to inspect I get that property name is extractUrlsWithoutProtocol_0_g$

What's more if I remove the @JsProperty annotation I get no change to the generated code...

Update: What does work is

   public native void setExtractUrlsWithoutProtocol(boolean extractUrlsWIthoutProtocol_)
/*-{
    this.extractUrlsWithoutProtocol = extractUrlsWIthoutProtocol_;
}-*/;
Michael Wiles
  • 20,902
  • 18
  • 71
  • 101

1 Answers1

2

Edit: I assume you're talking about GWT 2.8. If other, my answer doesn't apply.

I think you're missing a @JsType annotation on the class (not sure about this, but I think the GWT compiler might be ignoring types not annotated with @JsType, even though you do have @JsProperty). Also, if your problem is ONLY when compiling in production mode, please be aware that you need a special compiler flag - generateJsInteropExports (the default is NOT to honor JS Interop annotations).

Andrei
  • 1,613
  • 3
  • 16
  • 36
  • Adding @JsType and ensuring -generateJsInteropExports is on makes no difference – Michael Wiles May 09 '17 at 11:37
  • added the @JsType to my original question – Michael Wiles May 09 '17 at 11:40
  • How about your Options class? Can you find this name with a simple text search in your output JS? I'm asking because compiler discards stuff that doesn't match certain restrictions (and look for [WARN] statements in your output). This document describes those restrictions: https://docs.google.com/document/d/10fmlEYIHcyead_4R1S5wKGs1t2I7Fnp_PaNaa7XTEk0/edit – Andrei May 09 '17 at 11:44
  • Also, you might want to try with a no-arg constructor. It doesn't say so in the doc, but it does mention that when using native types you have troubles without no-arg constructors. – Andrei May 09 '17 at 11:50
  • Thanks for your help... for some reason it started working ;-). Not 100% sure why. Maybe because I restarted the compiler server I don't know – Michael Wiles May 09 '17 at 11:53
  • 1
    Sorry to nitpick; was just wondering how you're checking that "it's working". If you're simply including the .nocache.js and run a script that makes use of those functions, you will be getting some sort of race conditions (because .nocache.js loads browser-specific permutations asynchronously). I'd advise to text-search your output JS first. – Andrei May 09 '17 at 11:56
  • So, the answer was `-generateJsInteropExports` only and not `@JsType`? – Thomas Broyer May 10 '17 at 00:20
  • I don't really know tbh. If you don't use`-generateJsInteropExports`, then definitely you don't have exports. On `@JsType`, I don't know. Also, see my last comment above: with the async loading of JS, it is easy to get tricked and think it doesn't work, if you're simply invoking the code from a different JS (it happened to me). – Andrei May 10 '17 at 06:21
  • yes, according to my reading of the docs I shouldn't need @JsType. Btw, the finished project is at https://github.com/Afrozaar/gwt-twitter-text I ended up using gin bindings to ensure the java script was injected before everything. Also means I can just install a module and then inject the class that has the jsinterop bindings on it. Would love to spend more time on it but other priorities :-( – Michael Wiles May 11 '17 at 10:47