Nashorn ScriptObjectMirror JS -> Java type conversion

This code can do ScriptObjectMirror JS -> Java conversion

private static Object convertIntoJavaObject(Object scriptObj) {
    if (scriptObj instanceof ScriptObjectMirror) {
        ScriptObjectMirror scriptObjectMirror = (ScriptObjectMirror) scriptObj;
        if (scriptObjectMirror.isArray()) {
            List<Object> list = Lists.newArrayList();
            for (Map.Entry<String, Object> entry : scriptObjectMirror.entrySet()) {
                list.add(convertIntoJavaObject(entry.getValue()));
            }
            return list;
        } else {
            Map<String, Object> map = Maps.newHashMap();
            for (Map.Entry<String, Object> entry : scriptObjectMirror.entrySet()) {
                map.put(entry.getKey(), convertIntoJavaObject(entry.getValue()));
            }
            return map;
        }
    } else {
        return scriptObj;
    }
}

public static void main(String[] args) throws ScriptException, NoSuchMethodException {
    final ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
    engine.eval("function objProvider(){return {a:1, b:'2','c': true,'d': {'e':[],'f':['1',{'g':45}]}};}");
    final Object scriptObj = ((Invocable) engine).invokeFunction("objProvider");

    Object javaObj = convertIntoJavaObject(scriptObj);
    System.out.println(javaObj);
    //{a=1, b=2, c=true, d={e=[], f=[1, {g=45}]}}
}

Recommended approach is to check "instanceof java.lang.Number" from java code -- if you expect JavaScript "number" value. Once casted to Number, you can convert to int, long, double by calling methods such as intValue, longValue etc.