jdk.nashorn.internal.runtime.ListAdapter Java Examples

The following examples show how to use jdk.nashorn.internal.runtime.ListAdapter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: NativeJava.java    From nashorn with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Given a script object and a Java type, converts the script object into the desired Java type. Currently it
 * performs shallow creation of Java arrays, as well as wrapping of objects in Lists and Dequeues. Example:
 * <pre>
 * var anArray = [1, "13", false]
 * var javaIntArray = Java.to(anArray, "int[]")
 * print(javaIntArray[0]) // prints 1
 * print(javaIntArray[1]) // prints 13, as string "13" was converted to number 13 as per ECMAScript ToNumber conversion
 * print(javaIntArray[2]) // prints 0, as boolean false was converted to number 0 as per ECMAScript ToNumber conversion
 * </pre>
 * @param self not used
 * @param obj the script object. Can be null.
 * @param objType either a {@link #type(Object, Object) type object} or a String describing the type of the Java
 * object to create. Can not be null. If undefined, a "default" conversion is presumed (allowing the argument to be
 * omitted).
 * @return a Java object whose value corresponds to the original script object's value. Specifically, for array
 * target types, returns a Java array of the same type with contents converted to the array's component type. Does
 * not recursively convert for multidimensional arrays. For {@link List} or {@link Deque}, returns a live wrapper
 * around the object, see {@link ListAdapter} for details. Returns null if obj is null.
 * @throws ClassNotFoundException if the class described by objType is not found
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static Object to(final Object self, final Object obj, final Object objType) throws ClassNotFoundException {
    if (obj == null) {
        return null;
    }

    if (!(obj instanceof ScriptObject) && !(obj instanceof JSObject)) {
        throw typeError("not.an.object", ScriptRuntime.safeToString(obj));
    }

    final Class<?> targetClass;
    if(objType == UNDEFINED) {
        targetClass = Object[].class;
    } else {
        final StaticClass targetType;
        if(objType instanceof StaticClass) {
            targetType = (StaticClass)objType;
        } else {
            targetType = type(objType);
        }
        targetClass = targetType.getRepresentedClass();
    }

    if(targetClass.isArray()) {
        return JSType.toJavaArray(obj, targetClass.getComponentType());
    }

    if(targetClass == List.class || targetClass == Deque.class) {
        return ListAdapter.create(obj);
    }

    throw typeError("unsupported.java.to.type", targetClass.getName());
}
 
Example #2
Source File: NativeJava.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Given a script object and a Java type, converts the script object into the desired Java type. Currently it
 * performs shallow creation of Java arrays, as well as wrapping of objects in Lists, Dequeues, Queues,
 * and Collections. If conversion is not possible or fails for some reason, TypeError is thrown.
 * Example:
 * <pre>
 * var anArray = [1, "13", false]
 * var javaIntArray = Java.to(anArray, "int[]")
 * print(javaIntArray[0]) // prints 1
 * print(javaIntArray[1]) // prints 13, as string "13" was converted to number 13 as per ECMAScript ToNumber conversion
 * print(javaIntArray[2]) // prints 0, as boolean false was converted to number 0 as per ECMAScript ToNumber conversion
 * </pre>
 * @param self not used
 * @param obj the script object. Can be null.
 * @param objType either a {@link #type(Object, Object) type object} or a String describing the type of the Java
 * object to create. Can not be null. If undefined, a "default" conversion is presumed (allowing the argument to be
 * omitted).
 * @return a Java object whose value corresponds to the original script object's value. Specifically, for array
 * target types, returns a Java array of the same type with contents converted to the array's component type.
 * Converts recursively when the target type is multidimensional array. For {@link List}, {@link Deque},
 * {@link Queue}, or {@link Collection}, returns a live wrapper around the object, see {@link ListAdapter} for
 * details. Returns null if obj is null.
 * @throws ClassNotFoundException if the class described by objType is not found
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static Object to(final Object self, final Object obj, final Object objType) throws ClassNotFoundException {
    if (obj == null) {
        return null;
    }

    if (!(obj instanceof ScriptObject) && !(obj instanceof JSObject)) {
        throw typeError("not.an.object", ScriptRuntime.safeToString(obj));
    }

    final Class<?> targetClass;
    if(objType == UNDEFINED) {
        targetClass = Object[].class;
    } else {
        final StaticClass targetType;
        if(objType instanceof StaticClass) {
            targetType = (StaticClass)objType;
        } else {
            targetType = type(objType);
        }
        targetClass = targetType.getRepresentedClass();
    }

    if(targetClass.isArray()) {
        try {
            if (self instanceof SecureLookupSupplier) {
                return JSType.toJavaArrayWithLookup(obj, targetClass.getComponentType(), (SecureLookupSupplier)self);
            }
            return JSType.toJavaArray(obj, targetClass.getComponentType());
        } catch (final Exception exp) {
            throw typeError(exp, "java.array.conversion.failed", targetClass.getName());
        }
    }

    if (targetClass == List.class || targetClass == Deque.class || targetClass == Queue.class || targetClass == Collection.class) {
        return ListAdapter.create(obj);
    }

    throw typeError("unsupported.java.to.type", targetClass.getName());
}