cincheo / jsweet

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

Cannot find name 'Collections' even with j4ts dependency #708

Closed lalalic closed 2 years ago

lalalic commented 2 years ago

I'm working on 1.8JDK, 3.0.0 jsweet on Mac, I have a very simple java class as following: import java.util.Collections;

public final class Test {

public static int test(){
    Collections.unmodifiableMap(null);
    return 1;
}

}

and pom.xml looks like:

<project>
...
    <dependencies>
        <dependency>
            <groupId>org.jsweet</groupId>
            <artifactId>jsweet-core</artifactId>
            <version>${jsweet.core.version}</version>
        </dependency>
        <dependency>
            <groupId>org.jsweet</groupId>
            <artifactId>j4ts</artifactId>
            <version>2.0.0</version>
        </dependency>
    </dependencies>
</project>

JDK is 1.8, jsweet is 3.0.0, then I got error Cannot find name 'Collections': the following ts created:

/* Generated from Java with JSweet 3.0.0 - http://www.jsweet.org */
export class Test {
    public static test(): number {
        Collections.unmodifiableMap<any, any>(null);
        return 1;
    }
}
Test["__class"] = "Test";

the log is as following:

2021-10-25 20:58:39.039 ERROR output:55 - [ts] Cannot find name 'Collections'. at /Users/lir/Workspace/jsweet-quickstart/src/main/java/com/docscience/pathfinder/font/Test.java(8)
2021-10-25 20:58:39.039 INFO  JSweetTranspiler:834 - transpilation process finished in 3059 ms 
> java2ts: 710.940533ms
> ts2js: 2286.094849ms

[ERROR] transpilation failed
org.apache.maven.plugin.MojoFailureException: transpilation failed with 1 error(s) and 0 warning(s)

=========================================
TRANSPILATION ERRORS SUMMARY:
* /Users/xxx/Workspace/jsweet-quickstart/src/main/java/Test.java(8,45)[ts] Cannot find name 'Collections'.

=========================================
    at org.jsweet.AbstractJSweetMojo.transpile (AbstractJSweetMojo.java:694)
    at org.jsweet.JSweetMojo.execute (JSweetMojo.java:43)
...

Am I missing something?

thanks

lalalic commented 2 years ago

jdk14,jsweet3.1.0 got the same error, and there is java.util.Collections in candies/typings/.../j4ts/2.0.0/bundle.d.ts

lgrignon commented 2 years ago

Can you please start from https://github.com/lgrignon/j4ts-example And tell me if it's better for you?

lalalic commented 2 years ago

Yes, https://github.com/lgrignon/j4ts-example works because of all ts bundled.

doing more testing, and found it's because of module configuration.

If set module as any of "amd,system,commonjs,es2015", the namespace, java.util, java.lang, will not be prepended to class name. But if set bundle as true, it's always correct with full qualified name. It should be a bug, right? otherwise how can Collections be resolved to java.util.Collections in compile-time, or runtime.

thank you, @lgrignon