Search code examples
javarhino

How can one know if a property is an array?


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?


Solution

  • 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