tags:

views:

70

answers:

3

Hi there,

I am calling a JavaScript function using the rhino API:

Function fct = context.compileFunction(scope, script, "script", 1, null);
Scriptable result = (Scriptable) fct.call(
            context, scope, attrs, new Object[0]);
Object obj = result.get("objectClass", result);

Now, how can I test if the value of the "objectClass" property is an array?

A: 

You can check if any object is an array by doing object.getClass().isArray()

oli
I meant a JavaScript array
Maurice Perry
But this question is tagged as JAVA question. Mine answer too is for JAVA :(
YoK
It is also tagged rhino, which is a javascript interpreter written in java. It is not a javascript question though: I want to know how to do this in Java, not in JavaScript (which is obvious).
Maurice Perry
In that case, what type do arrays come down as - is it sun.org.mozilla.javascript.internal.NativeArray ? If so, just do `object instanceof NativeArray`.
oli
A: 

boolean b = object.getClass().isArray(); if (b) { // object is an array }

Above solution is for checking JAVA arrays.

If you are looking for javascript arrays in JAVA code, Then you need to know what object is returned by JAVASCRIPT for Array and check using "instanceof".

YoK
This will always be false. I meant a JavaScript array.
Maurice Perry
A: 

This might give you some idea: Declare a function to determine whether an object is an array and call that function passing your object.

engine.eval("function isArray(obj) {" +
                "  return obj.constructor == Array;" +
                "}");
Object obj = engine.eval("[1,2,3,4]");
Invocable invocableEngine = (Invocable) engine;
System.out.println(invocableEngine.invokeFunction("isArray", obj)); //true
Marimuthu Madasamy