本文整理汇总了Java中sun.invoke.util.ValueConversions类的典型用法代码示例。如果您正苦于以下问题:Java ValueConversions类的具体用法?Java ValueConversions怎么用?Java ValueConversions使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ValueConversions类属于sun.invoke.util包,在下文中一共展示了ValueConversions类的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: bindSingle
import sun.invoke.util.ValueConversions; //导入依赖的package包/类
static BoundMethodHandle bindSingle(MethodType type, LambdaForm form, BasicType xtype, Object x) {
// for some type signatures, there exist pre-defined concrete BMH classes
try {
switch (xtype) {
case L_TYPE:
return bindSingle(type, form, x); // Use known fast path.
case I_TYPE:
return (BoundMethodHandle) SpeciesData.EMPTY.extendWith(I_TYPE).constructor().invokeBasic(type, form, ValueConversions.widenSubword(x));
case J_TYPE:
return (BoundMethodHandle) SpeciesData.EMPTY.extendWith(J_TYPE).constructor().invokeBasic(type, form, (long) x);
case F_TYPE:
return (BoundMethodHandle) SpeciesData.EMPTY.extendWith(F_TYPE).constructor().invokeBasic(type, form, (float) x);
case D_TYPE:
return (BoundMethodHandle) SpeciesData.EMPTY.extendWith(D_TYPE).constructor().invokeBasic(type, form, (double) x);
default : throw newInternalError("unexpected xtype: " + xtype);
}
} catch (Throwable t) {
throw newInternalError(t);
}
}
示例2: testBox
import sun.invoke.util.ValueConversions; //导入依赖的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);
}
}
}
示例3: testCast
import sun.invoke.util.ValueConversions; //导入依赖的package包/类
@Test
public void testCast() throws Throwable {
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().bindTo(dst);
assertEquals(caster.type(), MethodHandles.identity(Object.class).type());
for (Object obj : objects) {
Class<?> src = obj.getClass();
boolean canCast = dst.isAssignableFrom(src);
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;
}
}
}
}
示例4: bindSingle
import sun.invoke.util.ValueConversions; //导入依赖的package包/类
static BoundMethodHandle bindSingle(MethodType type, LambdaForm form, BasicType xtype, Object x) {
// for some type signatures, there exist pre-defined concrete BMH classes
try {
switch (xtype) {
case L_TYPE:
return bindSingle(type, form, x); // Use known fast path.
case I_TYPE:
return (BoundMethodHandle) SpeciesData.EMPTY.extendWith(I_TYPE).constructor().invokeBasic(type, form, ValueConversions.widenSubword(x));
case J_TYPE:
return (BoundMethodHandle) SpeciesData.EMPTY.extendWith(J_TYPE).constructor().invokeBasic(type, form, (long) x);
case F_TYPE:
return (BoundMethodHandle) SpeciesData.EMPTY.extendWith(F_TYPE).constructor().invokeBasic(type, form, (float) x);
case D_TYPE:
return (BoundMethodHandle) SpeciesData.EMPTY.extendWith(D_TYPE).constructor().invokeBasic(type, form, (double) x);
default : throw newInternalError("unexpected xtype: " + xtype);
}
} catch (Throwable t) {
throw uncaughtException(t);
}
}
示例5: bindSingle
import sun.invoke.util.ValueConversions; //导入依赖的package包/类
static MethodHandle bindSingle(MethodType type, LambdaForm form, char xtype, Object x) {
// for some type signatures, there exist pre-defined concrete BMH classes
try {
switch (xtype) {
case 'L':
if (true) return bindSingle(type, form, x); // Use known fast path.
return (BoundMethodHandle) SpeciesData.EMPTY.extendWithType('L').constructor[0].invokeBasic(type, form, x);
case 'I':
return (BoundMethodHandle) SpeciesData.EMPTY.extendWithType('I').constructor[0].invokeBasic(type, form, ValueConversions.widenSubword(x));
case 'J':
return (BoundMethodHandle) SpeciesData.EMPTY.extendWithType('J').constructor[0].invokeBasic(type, form, (long) x);
case 'F':
return (BoundMethodHandle) SpeciesData.EMPTY.extendWithType('F').constructor[0].invokeBasic(type, form, (float) x);
case 'D':
return (BoundMethodHandle) SpeciesData.EMPTY.extendWithType('D').constructor[0].invokeBasic(type, form, (double) x);
default : throw new InternalError("unexpected xtype: " + xtype);
}
} catch (Throwable t) {
throw newInternalError(t);
}
}
示例6: makePrimCast
import sun.invoke.util.ValueConversions; //导入依赖的package包/类
/** Factory method: Truncate the given argument with zero or sign extension,
* and/or convert between single and doubleword versions of integer or float.
* The convType is the target of the conversion, and can be any type
* with a null conversion to the corresponding target parameter.
* Return null if this cannot be done.
*/
static MethodHandle makePrimCast(MethodType newType, MethodHandle target,
int arg, Class<?> convType) {
Class<?> src = newType.parameterType(arg);
if (canPrimCast(src, convType))
return makePrimCastOnly(newType, target, arg, convType);
Class<?> dst = convType;
boolean sflt = Wrapper.forPrimitiveType(src).isFloating();
boolean dflt = Wrapper.forPrimitiveType(dst).isFloating();
if (sflt | dflt) {
MethodHandle convMethod;
if (sflt)
convMethod = ((src == double.class)
? ValueConversions.convertFromDouble(dst)
: ValueConversions.convertFromFloat(dst));
else
convMethod = ((dst == double.class)
? ValueConversions.convertToDouble(src)
: ValueConversions.convertToFloat(src));
long conv = makeConv(OP_COLLECT_ARGS, arg, basicType(src), basicType(dst));
return new AdapterMethodHandle(target, newType, conv, convMethod);
}
throw new InternalError("makePrimCast");
}
示例7: makeBoxArgument
import sun.invoke.util.ValueConversions; //导入依赖的package包/类
/** Factory method: Box the given argument.
* Return null if this cannot be done.
*/
static MethodHandle makeBoxArgument(MethodType newType, MethodHandle target,
int arg, Class<?> convType) {
MethodType oldType = target.type();
Class<?> src = newType.parameterType(arg);
Class<?> dst = oldType.parameterType(arg);
Class<?> boxType = Wrapper.asWrapperType(convType);
Class<?> primType = Wrapper.asPrimitiveType(convType);
if (!canBoxArgument(newType, oldType, arg, convType)) {
return null;
}
if (!VerifyType.isNullConversion(boxType, dst))
target = makeCheckCast(oldType.changeParameterType(arg, boxType), target, arg, dst);
MethodHandle boxerMethod = ValueConversions.box(Wrapper.forPrimitiveType(primType));
long conv = makeConv(OP_PRIM_TO_REF, arg, basicType(primType), T_OBJECT);
return new AdapterMethodHandle(target, newType, conv, boxerMethod);
}
示例8: computeReturnConversion
import sun.invoke.util.ValueConversions; //导入依赖的package包/类
/**
* The typed target will be called according to targetType.
* The adapter code will in fact see the raw result from internalType,
* and must box it into an object. Produce a converter for this.
*/
private static MethodHandle computeReturnConversion(
MethodType targetType, MethodType internalType) {
Class<?> tret = targetType.returnType();
Class<?> iret = internalType.returnType();
Wrapper wrap = Wrapper.forBasicType(tret);
if (!iret.isPrimitive()) {
assert(iret == Object.class);
return ValueConversions.identity();
} else if (wrap.primitiveType() == iret) {
return ValueConversions.box(wrap);
} else {
assert(tret == double.class ? iret == long.class : iret == int.class);
return ValueConversions.boxRaw(wrap);
}
}
示例9: makeRawArgumentFilter
import sun.invoke.util.ValueConversions; //导入依赖的package包/类
/** A generic argument list will be created by a call of type 'raw'.
* The values need to be reboxed for to match 'cooked'.
* Do this on the fly.
*/
// TO DO: Use a generic argument converter in a different file
static MethodHandle makeRawArgumentFilter(MethodHandle invoker,
MethodType raw, MethodType cooked) {
MethodHandle filteredInvoker = null;
for (int i = 0, nargs = raw.parameterCount(); i < nargs; i++) {
Class<?> src = raw.parameterType(i);
Class<?> dst = cooked.parameterType(i);
if (src == dst) continue;
assert(src.isPrimitive() && dst.isPrimitive());
if (filteredInvoker == null) {
filteredInvoker =
AdapterMethodHandle.makeCheckCast(
invoker.type().generic(), invoker, 0, MethodHandle.class);
if (filteredInvoker == null) throw new UnsupportedOperationException("NYI");
}
MethodHandle reboxer = ValueConversions.rebox(dst);
filteredInvoker = FilterGeneric.makeArgumentFilter(1+i, reboxer, filteredInvoker);
if (filteredInvoker == null) throw new InternalError();
}
if (filteredInvoker == null) return invoker;
return AdapterMethodHandle.makeRetypeOnly(invoker.type(), filteredInvoker);
}
示例10: 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);
}
}
示例11: makeInstance
import sun.invoke.util.ValueConversions; //导入依赖的package包/类
Adapter makeInstance(MethodType type, MethodHandle genericTarget) {
genericTarget.getClass(); // check for NPE
MethodHandle convert = returnConversion;
if (primsAtEndOrder != null)
// reorder arguments passed to genericTarget, if primsAtEndOrder
throw new UnsupportedOperationException("NYI");
if (type == entryType) {
if (convert == null) convert = ValueConversions.identity();
return adapter.makeInstance(entryPoint, invoker, convert, genericTarget);
}
// my erased-type is not exactly the same as the desired type
assert(type.erase() == entryType); // else we are busted
if (convert == null)
convert = computeReturnConversion(type, rawEntryType, true);
// retype erased reference arguments (the cast makes it safe to do this)
MethodType tepType = type.insertParameterTypes(0, adapter.getClass());
MethodHandle typedEntryPoint =
AdapterMethodHandle.makeRetypeRaw(tepType, entryPoint);
return adapter.makeInstance(typedEntryPoint, invoker, convert, genericTarget);
}
示例12: 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;
}
}
}
}