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


Java Wrapper.isConvertibleFrom方法代码示例

本文整理汇总了Java中sun.invoke.util.Wrapper.isConvertibleFrom方法的典型用法代码示例。如果您正苦于以下问题:Java Wrapper.isConvertibleFrom方法的具体用法?Java Wrapper.isConvertibleFrom怎么用?Java Wrapper.isConvertibleFrom使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在sun.invoke.util.Wrapper的用法示例。


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

示例1: canConvert

import sun.invoke.util.Wrapper; //导入方法依赖的package包/类
static boolean canConvert(Class<?> src, Class<?> dst) {
    // short-circuit a few cases:
    if (src == dst || src == Object.class || dst == Object.class)  return true;
    // the remainder of this logic is documented in MethodHandle.asType
    if (src.isPrimitive()) {
        // can force void to an explicit null, a la reflect.Method.invoke
        // can also force void to a primitive zero, by analogy
        if (src == void.class)  return true;  //or !dst.isPrimitive()?
        Wrapper sw = Wrapper.forPrimitiveType(src);
        if (dst.isPrimitive()) {
            // P->P must widen
            return Wrapper.forPrimitiveType(dst).isConvertibleFrom(sw);
        } else {
            // P->R must box and widen
            return dst.isAssignableFrom(sw.wrapperType());
        }
    } else if (dst.isPrimitive()) {
        // any value can be dropped
        if (dst == void.class)  return true;
        Wrapper dw = Wrapper.forPrimitiveType(dst);
        // R->P must be able to unbox (from a dynamically chosen type) and widen
        // For example:
        //   Byte/Number/Comparable/Object -> dw:Byte -> byte.
        //   Character/Comparable/Object -> dw:Character -> char
        //   Boolean/Comparable/Object -> dw:Boolean -> boolean
        // This means that dw must be cast-compatible with src.
        if (src.isAssignableFrom(dw.wrapperType())) {
            return true;
        }
        // The above does not work if the source reference is strongly typed
        // to a wrapper whose primitive must be widened.  For example:
        //   Byte -> unbox:byte -> short/int/long/float/double
        //   Character -> unbox:char -> int/long/float/double
        if (Wrapper.isWrapperType(src) &&
            dw.isConvertibleFrom(Wrapper.forWrapperType(src))) {
            // can unbox from src and then widen to dst
            return true;
        }
        // We have already covered cases which arise due to runtime unboxing
        // of a reference type which covers several wrapper types:
        //   Object -> cast:Integer -> unbox:int -> long/float/double
        //   Serializable -> cast:Byte -> unbox:byte -> byte/short/int/long/float/double
        // An marginal case is Number -> dw:Character -> char, which would be OK if there were a
        // subclass of Number which wraps a value that can convert to char.
        // Since there is none, we don't need an extra check here to cover char or boolean.
        return false;
    } else {
        // R->R always works, since null is always valid dynamically
        return true;
    }
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:52,代码来源:MethodType.java

示例2: testUnbox

import sun.invoke.util.Wrapper; //导入方法依赖的package包/类
private void testUnbox(boolean doCast, Wrapper dst, Wrapper src) throws Throwable {
    boolean expectThrow = !doCast && !dst.isConvertibleFrom(src);
    if (dst == Wrapper.OBJECT || src == Wrapper.OBJECT)  return;  // must have prims
    if (dst == Wrapper.VOID   || src == Wrapper.VOID  )  return;  // must have values
    if (dst == Wrapper.OBJECT)
        expectThrow = false;  // everything (even VOID==null here) converts to OBJECT
    try {
        for (int n = -5; n < 10; n++) {
            Object box = src.wrap(n);
            switch (src) {
                case VOID:   assertEquals(box, null); break;
                case OBJECT: box = box.toString(); break;
                case SHORT:  assertEquals(box.getClass(), Short.class); break;
                default:     assertEquals(box.getClass(), src.wrapperType()); break;
            }
            MethodHandle unboxer;
            if (doCast)
                unboxer = ValueConversions.unboxCast(dst);
            else
                unboxer = ValueConversions.unboxWiden(dst);
            Object expResult = (box == null) ? dst.zero() : dst.wrap(box);
            Object result = null;
            switch (dst) {
                case INT:     result = (int)     unboxer.invokeExact(box); break;
                case LONG:    result = (long)    unboxer.invokeExact(box); break;
                case FLOAT:   result = (float)   unboxer.invokeExact(box); break;
                case DOUBLE:  result = (double)  unboxer.invokeExact(box); break;
                case CHAR:    result = (char)    unboxer.invokeExact(box); break;
                case BYTE:    result = (byte)    unboxer.invokeExact(box); break;
                case SHORT:   result = (short)   unboxer.invokeExact(box); break;
                case BOOLEAN: result = (boolean) unboxer.invokeExact(box); break;
            }
            if (expectThrow) {
                expResult = "(need an exception)";
            }
            assertEquals("(doCast,expectThrow,dst,src,n,box)="+Arrays.asList(doCast,expectThrow,dst,src,n,box),
                         expResult, result);
        }
    } catch (RuntimeException ex) {
        if (expectThrow)  return;
        System.out.println("Unexpected throw for (doCast,expectThrow,dst,src)="+Arrays.asList(doCast,expectThrow,dst,src));
        throw ex;
    }
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:45,代码来源:ValueConversionsTest.java


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