Java 类jdk.nashorn.internal.runtime.ListAdapter 实例源码

项目:openjdk-jdk10    文件:NativeJava.java   
/**
 * 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());
}
项目:openjdk9    文件:NativeJava.java   
/**
 * 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());
}