本文整理汇总了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);
}
}
示例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;
}
}
}
}
示例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;
}
}
}
}