Example usage for jdk.nashorn.internal.objects NativeJava to

List of usage examples for jdk.nashorn.internal.objects NativeJava to

Introduction

In this page you can find the example usage for jdk.nashorn.internal.objects NativeJava to.

Prototype

@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static Object to(final Object self, final Object obj, final Object objType)
        throws ClassNotFoundException 

Source Link

Document

Given a script object and a Java type, converts the script object into the desired Java type.

Usage

From source file:com.threecrickets.scripturian.adapter.NashornAdapter.java

License:LGPL

@Override
public Object enter(String entryPointName, Executable executable, ExecutionContext executionContext,
        Object... arguments) throws NoSuchMethodException, ParsingException, ExecutionException {
    ScriptObject oldGlobal = Context.getGlobal();
    try {/* ww  w . j  a v a2s . c o  m*/
        ScriptObject globalScope = getGlobalScope(executionContext);

        Object entryPoint = globalScope.get(entryPointName);
        if (!(entryPoint instanceof ScriptFunction))
            throw new NoSuchMethodException(entryPointName);

        try {
            ScriptFunction function = (ScriptFunction) entryPoint;
            Object r = ScriptRuntime.apply(function, null, arguments);
            if (r instanceof NativeArray)
                r = NativeJava.to(null, r, "java.util.List");
            return r;
        } catch (ClassNotFoundException x) {
            throw new ExecutionException(executable.getDocumentName(), x);
        } catch (Throwable x) {
            throw createExecutionException(x, executable.getDocumentName());
        } finally {
            context.getOut().flush();
            context.getErr().flush();
        }
    } finally {
        if (oldGlobal != null)
            Context.setGlobal(oldGlobal);
    }
}