I would like to cast the ScriptObjectMirror object to other class called Point that I defined by myself as follow:
public class Point {
    public double x;
    public double y;
    public Point(double x, double y) {
        this.x=x;
        this.y=y;
    }
}
My javascript code is something like this:
function getCenter(points) {
    var center = {x: 0, y: 0};
    print(points);   
    print(points.length);
    for (var i =0; i < points.length; ++i ) {
        center.x += points[i].x;
        center.y += points[i].y;
    }
    center.x /= points.length;
    center.y /= points.length;
    return center;
}
and I would like to invoke getCenter function in the js by supplying multiple Points using the code:
    ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
    //      engine.eval("print('Hello World!');");
    engine.eval(new FileReader("circleintersection.js"));
    Invocable invocable = (Invocable) engine;
    Point a = new Point(3,2);
    Point b = new Point(5,3);
    Point c = new Point(1,4);
    Point d = new Point(2,5);
    Point e = new Point(6,6);
    Point[] points = {a,b,c,d,e};
    Point result = (Point) invocable.invokeFunction("getCenter", points);
    System.out.println(result.x);
But it gave me error like
Exception in thread "main" java.lang.ClassCastException: jdk.nashorn.api.scripting.ScriptObjectMirror cannot be cast to Point
I tried another method to convert to Map using the code:
@SuppressWarnings("restriction")
    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;
        }
    }
and in main() method the code is something like this:
ScriptObjectMirror result = (ScriptObjectMirror) invocable.invokeFunction("getCenter", points);
    Object con = convertIntoJavaObject(result);
    Map<String, Object> map = (Map<String, Object>) con;
    for (Map.Entry<String, Object> entry : map.entrySet()) {
        System.out.println(entry.getKey() + ":" + entry.getValue());
    }
but it prints x:NaN and y:NaN as the results.
Is there something wrong with the way I insert the variables to javascript code or how to get the result?
 
    