cincheo / jsweet

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

JSweet3-RC4 NPE with itext7 kernel code base #624

Closed rgladwell closed 3 years ago

rgladwell commented 3 years ago

Getting multiple NPEs on transpilation:

java.lang.NullPointerException
  at org.jsweet.transpiler.Java2TypeScriptTranslator.printMethodParamsTest(Java2TypeScriptTranslator.java:3315)
  at org.jsweet.transpiler.Java2TypeScriptTranslator.printCoreOverloadMethod(Java2TypeScriptTranslator.java:2724)
  at org.jsweet.transpiler.Java2TypeScriptTranslator.visitMethod(Java2TypeScriptTranslator.java:2623)
  at org.jsweet.transpiler.Java2TypeScriptTranslator.visitMethod(Java2TypeScriptTranslator.java:149)
  at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCMethodDecl.accept(JCTree.java:916)
  at jdk.compiler/com.sun.source.util.TreePathScanner.scan(TreePathScanner.java:82)
  at org.jsweet.transpiler.util.AbstractTreeScanner.scan(AbstractTreeScanner.java:279)
  at org.jsweet.transpiler.util.AbstractTreePrinter.print(AbstractTreePrinter.java:155)
  at org.jsweet.transpiler.Java2TypeScriptTranslator.visitClass(Java2TypeScriptTranslator.java:1850)
  at org.jsweet.transpiler.Java2TypeScriptTranslator.visitClass(Java2TypeScriptTranslator.java:149)
  at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCClassDecl.accept(JCTree.java:823)
  at jdk.compiler/com.sun.source.util.TreePathScanner.scan(TreePathScanner.java:82)
  at org.jsweet.transpiler.util.AbstractTreeScanner.scan(AbstractTreeScanner.java:279)
  at org.jsweet.transpiler.util.AbstractTreePrinter.print(AbstractTreePrinter.java:155)
  at org.jsweet.transpiler.Java2TypeScriptTranslator.visitCompilationUnit(Java2TypeScriptTranslator.java:918)
  at org.jsweet.transpiler.Java2TypeScriptTranslator.visitCompilationUnit(Java2TypeScriptTranslator.java:149)
  at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCCompilationUnit.accept(JCTree.java:603)
  at jdk.compiler/com.sun.source.util.TreePathScanner.scan(TreePathScanner.java:82)
  at org.jsweet.transpiler.util.AbstractTreeScanner.scan(AbstractTreeScanner.java:279)
  at org.jsweet.transpiler.util.AbstractTreePrinter.print(AbstractTreePrinter.java:155)
  at org.jsweet.transpiler.JSweetTranspiler.generateTsFiles(JSweetTranspiler.java:941)
  at org.jsweet.transpiler.JSweetTranspiler.java2ts(JSweetTranspiler.java:896)
  at org.jsweet.transpiler.JSweetTranspiler.transpile(JSweetTranspiler.java:819)
  at org.jsweet.JSweetCommandLineLauncher$JSweetTranspilationTask.run(JSweetCommandLineLauncher.java:857)
  at org.jsweet.JSweetCommandLineLauncher.transpileWithArgs(JSweetCommandLineLauncher.java:267)
  at org.jsweet.JSweetCommandLineLauncher.main(JSweetCommandLineLauncher.java:235)
  at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.base/java.lang.reflect.Method.invoke(Method.java:564)
  at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:282)
  at java.base/java.lang.Thread.run(Thread.java:832)
lgrignon commented 3 years ago

@rgladwell Would it be possible to have the code which fails to transpile please? Could you also please confirm this works with JSweet 2?

I will add unit tests to cover your case. Thanks

rgladwell commented 3 years ago

It's failing with that exception in many places, one such place is here:

https://github.com/ink-elements/itext7-js/blob/master/itext7/kernel/src/main/java/com/itextpdf/kernel/counter/ContextManager.java#L100

Unfortunately, I can't tell if it was failing in JSweet 2 because the previous compiler issues may have concealed this bug.

lgrignon commented 3 years ago

Okay, it seems like a huge code base and if JSweet 2 did not manage to transpile it, JSweet 3 won't.

I will try to clone itext repo and see how many errors pop in, but there might be too many things to change / fix.

For instance, I guess itext relies on external jar dependencies, which cannot be supported in JSweet except if you already have a JS runnable version

lgrignon commented 3 years ago

Indeed, itext7 kernel depends on Java library https://github.com/ink-elements/itext7-js/blob/master/itext7/kernel/pom.xml#L16 which cannot be transpiled to something runnable in JS

Did you have something in mind for this?

rgladwell commented 3 years ago

This code base would not transpile in JSweet 2 because of previous bugs which I believe were only addressed in JSweet 3.

The library only has a logging dependency which I already mocked for JSweet:

https://github.com/itext/itext7/blob/develop/pom.xml#L134

I also stripped out the encryption parts of the library. I'm in the process of fixing bugs, removing dependencies and consolidating the code base until this transpiles

But I feel this is orthogonal to the NPE being thrown. I suspect it would be an issue even for smaller code bases.

rgladwell commented 3 years ago

It already sounds like this is a wont-fix for this project. Could you please clarify?

lgrignon commented 3 years ago

Hello @rgladwell There are 2 things:

1) You hit something that looks like a bug, so we will do everything we can to try to fix it, but it would be better to have a small repro to analyze it. I will make some tests today to isolate a small repro but I can sense it won't be reproducable easily, and then, try a blind null safe fix if I cannot get anything else.

2) Transpiling the itext7 codebase: I am very glad that you work on this because that's exactly what JSweet is for. Unfortunately, it seems that there are still some unsupported side cases, or dependent libs which would require some work on your side. I get that you are working on it so that's perfect, we can go step by step and enhance what JSweet cannot do yet (because there are still gray areas) and fix what's just failing.

One thing I want to add is that the NPE we get can result from a codebase with unrecognized dependencies. That's why I checked it yesterday.

I will keep you informed here about this NPE (and renaming the issue to focus on this). Can you then try with RC5 when ready, and if you hit more bug, try to narrow down to a small repro case for us to analyze, add to test suite, and fix?

lgrignon commented 3 years ago

@rgladwell I have a simple repro, that's nice. It will be fixed pretty quickly now.

lgrignon commented 3 years ago

That should be ok, I will deploy a RC5 soon. Thanks for reporting.

Please keep us informed here about your progress on itext7, this is a nice project. If you find a bug, the best is trying to provide a unit test, or a small repro on a dedicated issue.

Thanks again

lgrignon commented 3 years ago

3.0.0-RC5 is deployed, please test it on this version. groupId is org.jsweet.v3 for almost all artifacts now (please see pom.xml & build.gradle here: https://github.com/cincheo/jsweet-examples)

rgladwell commented 3 years ago

@lgrignon Thanks for the quick fix, just tested and it resolved the NPE.