Java nashorn compares if java objects have a specific java type

I am using instanceof, but currently it does not work as I expect. I have a variable that I am extracting from my java code inside my script. Let me call this variable myObject, which is an instance of the MyObject class, as you would expect.

if (myObject instanceof Java.type("MyObject")) {
    //The check doesn't pass; the code here doesn't execute
}

I could only find vague information about this. What is the specific way to check if myObject is an instance of the MyObject class, as I would easily do in Java?

Thank!

+4
source share
1 answer

What should work for you (of course, with full class names). For instance. this definitely works:

jjs> var x = new java.util.BitSet()
jjs> x instanceof Java.type("java.util.BitSet") 
true

: java.util.BitSet Java.type("java.util.BitSet") , , , , , .

+8

Source: https://habr.com/ru/post/1545493/


All Articles