google / j2cl

Java to Closure JavaScript transpiler
Apache License 2.0
1.23k stars 144 forks source link

Only compiling in dev mode #133

Closed thomasAtToem closed 3 years ago

thomasAtToem commented 3 years ago

Hi,

i have the following java wrapper:

  package de.toem.toolkits.pattern.scripting;
  import jsinterop.annotations.JsType;
  @JsType(isNative = true, namespace = "de.toem.toolkits.pattern.scripting")  
  public class ScriptRunner{
      public ScriptRunner() {
      }

      public native void setSymbol(String key, Object value); 
      public native Object run(String script);
      public native Object invoke(String function, Object[] args);
      public native boolean hasFunction(String function) ;
  }

and this js implementation:

  goog.module('de.toem.toolkits.pattern.scripting.ScriptRunner');
  const IPaint = goog.require('de.toem.impulse.paint.IPaint');

  /** @suppress {checkTypes, unusedLocalVariables} */ 
  function ScriptRunner() { 

      var DIAGRAM_NONE = IPaint.DIAGRAM_NONE; 
      this.setSymbol = function(key, object) { 
          this[key]=object;
      };
      this.run = function(script) { 
          return eval(script);
      };
      this.invoke = function(funcname, args) { 
          this[funcname](...args);
      };
      this.hasFunction = function(funcname) { 
          return true;
      };
  };

  exports = ScriptRunner;

Its working in dev mode , but in release mode i get these errors in classes refererencing the runner. Can anybody help ? thanks, thomas

ERROR: /home/thomas/Workspaces/impulse/de.toem.impulse.html.frontend/build/src/main/java/app/BUILD:31:17: Compiling 12 JavaScript files to src/main/java/app/impulse.js failed: (Exit 1): ClosureWorker failed: error executing command bazel-out/host/bin/external/io_bazel_rules_closure/java/io/bazel/rules/closure/ClosureWorker @@bazel-out/k8-fastbuild/bin/src/main/java/app/impulse.js-0.params bazel-out/k8-fastbuild/bin/src/main/java/de/toem/toem.js.zip!/de/toem/toolkits/pattern/scripting/HtmlScripting.impl.java.js:51: Originally at: bazel-out/k8-fastbuild/bin/src/main/java/de/toem/toem.js.zip!/de/toem/toolkits/pattern/scripting/HtmlScripting.java:23: ERROR - cannot instantiate non-constructor ProTip: "JSC_NOT_A_CONSTRUCTOR" or "checkTypes" can be added to the suppress attribute of: //src/main/java/de/toem:toem Alternatively /* @suppress {checkTypes} / can be added to the source file.

bazel-out/k8-fastbuild/bin/src/main/java/de/toem/toem.js.zip!/de/toem/toolkits/pattern/scripting/HtmlScripting.impl.java.js:54: Originally at: bazel-out/k8-fastbuild/bin/src/main/java/de/toem/toem.js.zip!/de/toem/toolkits/pattern/scripting/HtmlScripting.java:25: ERROR - Bad type annotation. Unknown type module$exports$de$toem$toolkits$pattern$scripting$ScriptRunner ProTip: "JSC_UNRECOGNIZED_TYPE_ERROR" or "checkTypes" or "unrecognizedTypeError" can be added to the suppress attribute of: //src/main/java/de/toem:toem Alternatively /* @suppress {checkTypes} / can be added to the source file.

bazel-out/k8-fastbuild/bin/src/main/java/de/toem/toem.js.zip!/de/toem/toolkits/pattern/scripting/HtmlScripting.impl.java.js:59: Originally at: bazel-out/k8-fastbuild/bin/src/main/java/de/toem/toem.js.zip!/de/toem/toolkits/pattern/scripting/HtmlScripting.java:35: ERROR - Bad type annotation. Unknown type module$exports$de$toem$toolkits$pattern$scripting$ScriptRunner ProTip: "JSC_UNRECOGNIZED_TYPE_ERROR" or "checkTypes" or "unrecognizedTypeError" can be added to the suppress attribute of: //src/main/java/de/toem:toem Alternatively /* @suppress {checkTypes} / can be added to the source file.

bazel-out/k8-fastbuild/bin/src/main/java/de/toem/toem.js.zip!/de/toem/toolkits/pattern/scripting/HtmlScripting.impl.java.js:68: Originally at: bazel-out/k8-fastbuild/bin/src/main/java/de/toem/toem.js.zip!/de/toem/toolkits/pattern/scripting/HtmlScripting.java:49: ERROR - Bad type annotation. Unknown type module$exports$de$toem$toolkits$pattern$scripting$ScriptRunner ProTip: "JSC_UNRECOGNIZED_TYPE_ERROR" or "checkTypes" or "unrecognizedTypeError" can be added to the suppress attribute of: //src/main/java/de/toem:toem Alternatively /* @suppress {checkTypes} / can be added to the source file.

4 error(s), 0 warning(s), 98.6% typed

gkdn commented 3 years ago

The code you have is not Closure style (or in general object-orient style of JS). That's why you are having so many errors from Closure compiler's checking.

Something like following should achieve what you are trying to accomplish.

  goog.module('de.toem.toolkits.pattern.scripting.ScriptRunner');
  const IPaint = goog.require('de.toem.impulse.paint.IPaint');

  class ScriptRunner { 
      /**
       * @param {string} symbol
       * @param {?} object 
       */
      setSymbol(key, object) { 
     this[key]=object;
      }

      /**
       * @param {string} script
       * @return {?}
       */
      run(script) { 
      return eval(script);
      }
  };

  exports = ScriptRunner;