本文整理汇总了Java中sun.invoke.util.ValueConversions.unbox方法的典型用法代码示例。如果您正苦于以下问题:Java ValueConversions.unbox方法的具体用法?Java ValueConversions.unbox怎么用?Java ValueConversions.unbox使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sun.invoke.util.ValueConversions
的用法示例。
在下文中一共展示了ValueConversions.unbox方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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);
}
}
示例2: testUnbox
import sun.invoke.util.ValueConversions; //导入方法依赖的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.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.primitiveType());
else
unboxer = ValueConversions.unbox(dst.primitiveType());
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 OBJECT: result = (Object) unboxer.invokeExact(box); break;
case BOOLEAN: result = (boolean) unboxer.invokeExact(box); break;
case VOID: result = null; 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;
}
}