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


Java ValueConversions.cast方法代码示例

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


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

示例1: computeReturnConversion

import sun.invoke.util.ValueConversions; //导入方法依赖的package包/类
/**
 * Caller will be expecting a result from a call to {@code type},
 * while the internal adapter entry point is rawEntryType.
 * Also, the internal target method will be returning a boxed value,
 * as an untyped object.
 * <p>
 * Produce a value converter which will be typed to convert from
 * {@code Object} to the return value of {@code rawEntryType}, and will
 * in fact ensure that the value is compatible with the return type of
 * {@code type}.
 */
private static MethodHandle computeReturnConversion(
        MethodType type, MethodType rawEntryType, boolean mustCast) {
    Class<?> tret = type.returnType();
    Class<?> rret = rawEntryType.returnType();
    if (mustCast || !tret.isPrimitive()) {
        assert(!tret.isPrimitive());
        assert(!rret.isPrimitive());
        if (rret == Object.class && !mustCast)
            return null;
        return ValueConversions.cast(tret);
    } else if (tret == rret) {
        return ValueConversions.unbox(tret);
    } else {
        assert(rret.isPrimitive());
        assert(tret == double.class ? rret == long.class : rret == int.class);
        return ValueConversions.unboxRaw(tret);
    }
}
 
开发者ID:openjdk,项目名称:jdk7-jdk,代码行数:30,代码来源:ToGeneric.java

示例2: testCast

import sun.invoke.util.ValueConversions; //导入方法依赖的package包/类
@Test
public void testCast() throws Throwable {
    //System.out.println("cast");
    Class<?>[] types = { Object.class, Serializable.class, String.class, Number.class, Integer.class };
    Object[] objects = { new Object(), Boolean.FALSE,      "hello",      (Long)12L,    (Integer)6    };
    for (Class<?> dst : types) {
        MethodHandle caster = ValueConversions.cast(dst);
        assertEquals(caster.type(), ValueConversions.identity().type());
        for (Object obj : objects) {
            Class<?> src = obj.getClass();
            boolean canCast = dst.isAssignableFrom(src);
            //System.out.println("obj="+obj+" <: dst="+dst+(canCast ? " (OK)" : " (will fail)"));
            try {
                Object result = caster.invokeExact(obj);
                if (canCast)
                    assertEquals(obj, result);
                else
                    assertEquals("cast should not have succeeded", dst, obj);
            } catch (ClassCastException ex) {
                if (canCast)
                    throw ex;
            }
        }
    }
}
 
开发者ID:RedlineResearch,项目名称:OLD-OpenJDK8,代码行数:26,代码来源:ValueConversionsTest.java

示例3: testCast

import sun.invoke.util.ValueConversions; //导入方法依赖的package包/类
@Test
public void testCast() throws Throwable {
    //System.out.println("cast");
    Class<?>[] types = { Object.class, Serializable.class, String.class, Number.class, Integer.class };
    Object[] objects = { new Object(), Boolean.FALSE,      "hello",      (Long)12L,    (Integer)6    };
    for (Class<?> dst : types) {
        MethodHandle caster = ValueConversions.cast(dst);
        assertEquals(caster.type(), ValueConversions.identity().type());
        for (Object obj : objects) {
            Class<?> src = obj.getClass();
            boolean canCast;
            if (dst.isInterface()) {
                canCast = true;
            } else {
                canCast = dst.isAssignableFrom(src);
                assertEquals(canCast, dst.isInstance(obj));
            }
            //System.out.println("obj="+obj+" <: dst="+dst);
            try {
                Object result = caster.invokeExact(obj);
                if (canCast)
                    assertEquals(obj, result);
                else
                    assertEquals("cast should not have succeeded", dst, obj);
            } catch (ClassCastException ex) {
                if (canCast)
                    throw ex;
            }
        }
    }
}
 
开发者ID:openjdk,项目名称:jdk7-jdk,代码行数:32,代码来源:ValueConversionsTest.java


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