当前位置: 首页>>代码示例>>Java>>正文


Java ListAdapter类代码示例

本文整理汇总了Java中jdk.nashorn.internal.runtime.ListAdapter的典型用法代码示例。如果您正苦于以下问题:Java ListAdapter类的具体用法?Java ListAdapter怎么用?Java ListAdapter使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


ListAdapter类属于jdk.nashorn.internal.runtime包,在下文中一共展示了ListAdapter类的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: to

import jdk.nashorn.internal.runtime.ListAdapter; //导入依赖的package包/类
/**
 * 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());
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:65,代码来源:NativeJava.java


注:本文中的jdk.nashorn.internal.runtime.ListAdapter类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。