cincheo / jsweet

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

error TS2339: Property 'openStream' does not exist on type 'URL'. #593

Open xeccgtt opened 4 years ago

xeccgtt commented 4 years ago

We are getting the following errors. With transpiler: 2.3.7, jsweet.core.version: 602. We are using the URL definition in the JSweet j4ts GIT Repo. It has openStream function defined on the URL class. It is unclear why we are getting this error. I do not have access to Stack Over flow forums from my firm. I would appreciate any suggestions to work around this issue.

Error: 2020-05-29 08:27:16.016 INFO TypeScript2JavaScriptWithTscTranspiler:90 - EspBus.ts(264,58): error TS2339: Property 'openStream' does not exist on type 'URL'. 2020-05-29 08:27:16.016 ERROR output:55 - property 'openStream' does not exist on type 'URL' at EspBus.java(455)

EspBus.ts 262 let urlStr : string = this.root.toString(); 263 let url : URL = new URL(urlStr); 264 let inStream : java.io.InputStream = url.openStream(); 265 this.loop(inStream);

EspBus.java 450 String urlStr = this.root.toString(); 451 final URL url = new URL( urlStr ); 455 InputStream inStream = url.openStream (); 456 loop ( inStream );

j4ts Code public InputStream openStream() { XMLHttpRequest request = makeConnection();

    switch (request.responseType) {
        case "arraybuffer":
            return new ByteArrayInputStream(any(new Int8Array((ArrayBuffer) request.response)));
        case "blob":
            Function fileReaderSyncConstructor = self.$get("FileReaderSync");
            if (typeof(fileReaderSyncConstructor).equals("function")) {
                return new ByteArrayInputStream(((Function) fileReaderSyncConstructor.$get("readAsArrayBuffer")).$apply((Blob) request.response));
            }
            // TODO find a way to handle BLOB at main thread synchronously
            return new ByteArrayInputStream(createObjectURL(request.response).getBytes());
        default:
            return new ByteArrayInputStream(request.response.toString().getBytes());
    }
}
xeccgtt commented 4 years ago

Any suggestion on how to proceed. We are stuck and do not know how to resolve the issue.

renaudpawlak commented 4 years ago

Umm... That is intriguing. I am trying it out.

renaudpawlak commented 4 years ago

After a closer look, it seems that the generated code is wrong. It should use the URL's full qualified name:

let urlStr : string = this.root.toString();
let url : java.net.URL = new java.net.URL(urlStr); // <<<<<< fully qualified name

Looks like a unwanted type mapping... did you add any adapter or extra configuration (extension)? I will take a deeper look ASAP.

xeccgtt commented 4 years ago

I did not add any mappings or any extra extensions. By the way I could only get the J4TS to compile

2.3.6-SNAPSHOT 6.0.3 It fails when I use the transpiler 2.3.6 and 2.37.
renaudpawlak commented 4 years ago

Ok. I have good news. I have managed to reproduce the problem. Looks like a type mapping legacy issue... A real bug anyway.

Less good news is that I need some time to replay the tests and make a release because such a fix might have side effects...

If you are in a hurry and would like to apply the fix yourself (at your own risks), you may want to try the following steps:

xeccgtt commented 4 years ago

the getHosts method also has the same problem.

renaudpawlak commented 4 years ago

Yes, of course. All the methods in java.net.URL will have the same problem because what JSweet generates is a JavaScript URL type (not a java.net.URL). You can see the JavaScript URL object there: https://developer.mozilla.org/en-US/docs/Web/API/URL/URL

I know it is confusing, because they have the same name and represent the same thing, but a URL type is not the same as a java.net.URL type.

The fix I have provided will remove the mapping of java.net.URL to URL so that all the methods defined in java.net.URL will be accessible fine.

xeccgtt commented 4 years ago

Where is the source located?

lgrignon commented 4 years ago

Hello @xeccgtt I think you got it all here: https://github.com/cincheo/jsweet/issues/593

Do you miss something else?