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


Java Wrapper.wrap方法代码示例

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


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

示例1: testRef2Prim

import sun.invoke.util.Wrapper; //导入方法依赖的package包/类
/**
 * Tests that non-null wrapper reference is successfully converted to
 * primitive types.
 */
public static void testRef2Prim() {
    for (Wrapper from : Wrapper.values()) {
        for (Wrapper to : Wrapper.values()) {
            if (from == Wrapper.VOID || to == Wrapper.VOID
                    || to == Wrapper.OBJECT) {
                continue;
            }
            Object value = RANDOM_VALUES.get(from);
            for (TestConversionMode mode : TestConversionMode.values()) {
                if (from != Wrapper.OBJECT) {
                    Object convValue = to.wrap(value);
                    testConversion(mode, from.wrapperType(),
                            to.primitiveType(), value, convValue, false, null);
                } else {
                    testConversion(mode, from.wrapperType(),
                            to.primitiveType(), value, null,
                            true, ClassCastException.class);
                }
            }
        }
    }
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:27,代码来源:ExplicitCastArgumentsTest.java

示例2: testPrim2Prim

import sun.invoke.util.Wrapper; //导入方法依赖的package包/类
/**
 * Tests that primitive is successfully converted to other primitive type.
 */
public static void testPrim2Prim() {
    for (Wrapper from : Wrapper.values()) {
        for (Wrapper to : Wrapper.values()) {
            if (from == Wrapper.VOID || to == Wrapper.VOID
                    || from == Wrapper.OBJECT || to == Wrapper.OBJECT) {
                continue;
            }
            Object value = RANDOM_VALUES.get(from);
            Object convValue = to.wrap(value);
            for (TestConversionMode mode : TestConversionMode.values()) {
                testConversion(mode, from.primitiveType(),
                        to.primitiveType(), value, convValue, false, null);
            }
        }
    }
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:20,代码来源:ExplicitCastArgumentsTest.java

示例3: testBox

import sun.invoke.util.Wrapper; //导入方法依赖的package包/类
@Test
public void testBox() throws Throwable {
    for (Wrapper w : Wrapper.values()) {
        if (w == Wrapper.VOID)    continue;  // skip this; no unboxed form
        if (w == Wrapper.OBJECT)  continue;  // skip this; already unboxed
        for (int n = -5; n < 10; n++) {
            Object box = w.wrap(n);
            MethodHandle boxer = ValueConversions.boxExact(w);
            Object expResult = box;
            Object result = null;
            switch (w) {
                case INT:     result = (Integer) boxer.invokeExact(/*int*/n); break;
                case LONG:    result = (Long)    boxer.invokeExact((long)n); break;
                case FLOAT:   result = (Float)   boxer.invokeExact((float)n); break;
                case DOUBLE:  result = (Double)  boxer.invokeExact((double)n); break;
                case CHAR:    result = (Character) boxer.invokeExact((char)n); break;
                case BYTE:    result = (Byte)    boxer.invokeExact((byte)n); break;
                case SHORT:   result = (Short)   boxer.invokeExact((short)n); break;
                case BOOLEAN: result = (Boolean) boxer.invokeExact((n & 1) != 0); break;
            }
            assertEquals("(dst,src,n,box)="+Arrays.asList(w,w,n,box),
                         expResult, result);
        }
    }
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:26,代码来源:ValueConversionsTest.java

示例4: testConvert

import sun.invoke.util.Wrapper; //导入方法依赖的package包/类
static void testConvert(Wrapper src, Wrapper dst, long tval) throws Throwable {
    if (dst == Wrapper.OBJECT || src == Wrapper.OBJECT)  return;  // must have prims
    if (dst == Wrapper.VOID   || src == Wrapper.VOID  )  return;  // must have values
    boolean testSingleCase = (tval != 0);
    final long tvalInit = tval;
    MethodHandle conv = ValueConversions.convertPrimitive(src, dst);
    MethodType convType = MethodType.methodType(dst.primitiveType(), src.primitiveType());
    assertEquals(convType, conv.type());
    MethodHandle converter = conv.asType(conv.type().changeReturnType(Object.class));
    for (;;) {
        long n = tval;
        Object testValue = src.wrap(n);
        Object expResult = dst.cast(testValue, dst.primitiveType());
        Object result;
        switch (src) {
            case INT:     result = converter.invokeExact((int)n); break;
            case LONG:    result = converter.invokeExact(/*long*/n); break;
            case FLOAT:   result = converter.invokeExact((float)n); break;
            case DOUBLE:  result = converter.invokeExact((double)n); break;
            case CHAR:    result = converter.invokeExact((char)n); break;
            case BYTE:    result = converter.invokeExact((byte)n); break;
            case SHORT:   result = converter.invokeExact((short)n); break;
            case BOOLEAN: result = converter.invokeExact((n & 1) != 0); break;
            default:  throw new AssertionError();
        }
        assertEquals("(src,dst,n,testValue)="+Arrays.asList(src,dst,"0x"+Long.toHexString(n),testValue),
                     expResult, result);
        if (testSingleCase)  break;
        // next test value:
        tval = nextTestValue(tval);
        if (tval == tvalInit)  break;  // repeat
    }
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:34,代码来源:ValueConversionsTest.java

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