本文整理汇总了Java中sun.invoke.util.Wrapper.convert方法的典型用法代码示例。如果您正苦于以下问题:Java Wrapper.convert方法的具体用法?Java Wrapper.convert怎么用?Java Wrapper.convert使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sun.invoke.util.Wrapper
的用法示例。
在下文中一共展示了Wrapper.convert方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: constant
import sun.invoke.util.Wrapper; //导入方法依赖的package包/类
/**
* Produces a method handle of the requested return type which returns the given
* constant value every time it is invoked.
* <p>
* Before the method handle is returned, the passed-in value is converted to the requested type.
* If the requested type is primitive, widening primitive conversions are attempted,
* else reference conversions are attempted.
* <p>The returned method handle is equivalent to {@code identity(type).bindTo(value)}.
* @param type the return type of the desired method handle
* @param value the value to return
* @return a method handle of the given return type and no arguments, which always returns the given value
* @throws NullPointerException if the {@code type} argument is null
* @throws ClassCastException if the value cannot be converted to the required return type
* @throws IllegalArgumentException if the given type is {@code void.class}
*/
public static
MethodHandle constant(Class<?> type, Object value) {
if (type.isPrimitive()) {
if (type == void.class)
throw newIllegalArgumentException("void type");
Wrapper w = Wrapper.forPrimitiveType(type);
value = w.convert(value, type);
if (w.zero().equals(value))
return zero(w, type);
return insertArguments(identity(type), 0, value);
} else {
if (value == null)
return zero(Wrapper.OBJECT, type);
return identity(type).bindTo(value);
}
}
示例2: insertArgumentPrimitive
import sun.invoke.util.Wrapper; //导入方法依赖的package包/类
private static BoundMethodHandle insertArgumentPrimitive(BoundMethodHandle result, int pos,
Class<?> ptype, Object value) {
Wrapper w = Wrapper.forPrimitiveType(ptype);
// perform unboxing and/or primitive conversion
value = w.convert(value, ptype);
switch (w) {
case INT: return result.bindArgumentI(pos, (int)value);
case LONG: return result.bindArgumentJ(pos, (long)value);
case FLOAT: return result.bindArgumentF(pos, (float)value);
case DOUBLE: return result.bindArgumentD(pos, (double)value);
default: return result.bindArgumentI(pos, ValueConversions.widenSubword(value));
}
}