Skip to content

Working with generic types in Java API calls #234

Open
@tporeba

Description

@tporeba

I have a system based on Rhino and I am working on providing support for newest JavaScript by integrating graal.js as alternative engine/language. The system has a Java API that can be called from scripts. I cannot find a way to use Java API methods that expect generic collections of java primitives/wrappers or generic methods of classes typed with wrappers.

For example, lets take:

    // Java
    public ObjectList getObjects(ArrayList<Long> objectIds) {
        for (Long id: objectIds) {
            // do something with id
        }
    }

,

    // JavaScript
    var longList = new java.util.ArrayList();
    longList.add(new java.lang.Long(123456));
    util.getObjects(longList)

or one can used a class derived from generic:

    //Java
    public class LongGeneric extends ArrayList<Long>{}

,

    //JavaScript
    var longList2 = new LongGeneric();
    longList2.add(new java.lang.Long(123456));
    util.getObjects(longList2);

Now, when using these in graal.js (version 19.2.1), I'm running into trouble in both cases, because this code does not pass java.lang.Long as argument to the add() method calls. It actually passes java.lang.Integer, because of eager graal.js conversions. This sooner or later ends with casting problems in my Java API code, for example getObjects() throws:
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long.

In the first example I understand that the script does not know that I intend to use ArrayList<Long>. But still I if I could put a Long somehow into that untyped collection, it would be great. However, I don't see any way to do that.

In the second example the LongGeneric class is explicitly typed with , but still graal.js does not convert java.lang.Integer to java.lang.Long while calling add(). Maybe type erasure prevents graal from detecting the conversion possibility?

Do you have any advice on how to overcome these problems?

Note: this looks similar to discussion from #97 especially this comment. Is there any progress on that longValue() subject?

Metadata

Metadata

Assignees

Labels

interopPolyglot Language Interoperability

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions