cincheo / jsweet

A Java to JavaScript transpiler.
http://www.jsweet.org
Other
1.46k stars 160 forks source link

[3.0.0-RC1] issue with interface default methods #567

Closed gjoseph closed 4 years ago

gjoseph commented 4 years ago

Sorry in advance - this might be the wrong repo to report this issue, since it involves JSweet, the maven plugin, and j4ts. Happy to move it elsewhere, let me know!

I am trying to generate/transpile some Java 11 code to typescript, so I'm giving 3.0.0-RC1 a shot. The transpiler seems to be choking on runtime exceptions, specifically code like throw new IllegalStateException("this should not happen"), and it does so with an NPE unfortunately:

2020-03-03 21:02:11.011 ERROR output:55 - internal transpiler error at [...].java(43)
dumping transpiler's strack trace:
   [JCNewClass] new IllegalStateException("not... ([...]/Foo.java(43,13))
   [JCThrow] throw new IllegalStateExceptio... ([...]/Foo.java(43,7))
   [JCBlock] {... ([...]/Foo.java(42,18))
   [JCMethodDecl] default P findIn(Set<Bar> han... ([...]/Foo.java(41,48))
   [JCClassDecl] public static class Factory im... ([...]/Foo.java(48,5))
   [JCClassDecl] public class Foo extends Ab... ([...]/Foo.java(16,1))
   [JCCompilationUnit] package lala.land... ([...]/Foo.java(1,1))
java.lang.NullPointerException
    at org.jsweet.transpiler.TypeChecker.checkType(TypeChecker.java:175)
    at org.jsweet.transpiler.Java2TypeScriptTranslator.printDefaultNewClass(Java2TypeScriptTranslator.java:4613)

This is happening with <tsOnly>true</tsOnly> - interestingly enough, if I remove the j4ts dependency, the build passes. But the ts-to-js compilation fails miserably because Cannot find name 'java'.. I'm not sure I understand why the transpile phase fails when I introduce j4ts.

FWIW I'm using j4ts 0.6.0.

Is this simply because j4ts doesn't know about IllegalStateException? (I tried throw RuntimeException, which was easy to change without having to patch everything, but that failed the same way)

gjoseph commented 4 years ago

Actually - the IllegalStateExceptions in question were thrown by default methods on the interface that Foo implements, in the example above. Removing those fixes transpilation.

So maybe that's a problem with JSweet's support of default methods? If this isn't a known issue, happy to try and isolate it in a sample repo, let me know.

lgrignon commented 4 years ago

Hey, thanks for reporting. J4TS is actually currently not supported with JSweet 3. Overriding JDK classes with modules requires a lot of work, if even possible.

If you plan to use J4TS, I would recommend to stick to stable 2.3.x

gjoseph commented 4 years ago

But JSweet 2.x doesn't support Java 11, does it ?:)

lgrignon commented 4 years ago

Nope, there is currently no easy way to use j4ts with Java 9+, sorry about that.

If you are willing to contribute, you can give it a shot. We have a j4ts branch with some pending work in progress (which I could explain). That would be amazing :)

lgrignon commented 4 years ago

Closing as moved to J4TS: https://github.com/j4ts/j4ts/issues/56