• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Java ListAdapter类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了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;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Java ConfBlock类代码示例发布时间:2022-05-22
下一篇:
Java NSAutoreleasePool类代码示例发布时间:2022-05-22
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap