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


Java Wrapper.forPrimitiveType方法代码示例

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


在下文中一共展示了Wrapper.forPrimitiveType方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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);
    }
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:32,代码来源:MethodHandles.java

示例2: toArrayString

import sun.invoke.util.Wrapper; //导入方法依赖的package包/类
private static String toArrayString(Object a) {
    if (a == null)  return "null";
    Class<?> elemType = a.getClass().getComponentType();
    if (elemType == null)  return a.toString();
    if (elemType.isPrimitive()) {
        switch (Wrapper.forPrimitiveType(elemType)) {
            case INT:      return Arrays.toString((int[])a);
            case BYTE:     return Arrays.toString((byte[])a);
            case BOOLEAN:  return Arrays.toString((boolean[])a);
            case SHORT:    return Arrays.toString((short[])a);
            case CHAR:     return Arrays.toString((char[])a);
            case FLOAT:    return Arrays.toString((float[])a);
            case LONG:     return Arrays.toString((long[])a);
            case DOUBLE:   return Arrays.toString((double[])a);
        }
    }
    return Arrays.toString((Object[])a);
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:19,代码来源:VarargsArrayTest.java

示例3: makeGuardWithCatch

import sun.invoke.util.Wrapper; //导入方法依赖的package包/类
static
MethodHandle makeGuardWithCatch(MethodHandle target,
                                Class<? extends Throwable> exType,
                                MethodHandle catcher) {
    MethodType type = target.type();
    LambdaForm form = makeGuardWithCatchForm(type.basicType());

    // Prepare auxiliary method handles used during LambdaForm interpreation.
    // Box arguments and wrap them into Object[]: ValueConversions.array().
    MethodType varargsType = type.changeReturnType(Object[].class);
    MethodHandle collectArgs = varargsArray(type.parameterCount()).asType(varargsType);
    // Result unboxing: ValueConversions.unbox() OR ValueConversions.identity() OR ValueConversions.ignore().
    MethodHandle unboxResult;
    Class<?> rtype = type.returnType();
    if (rtype.isPrimitive()) {
        if (rtype == void.class) {
            unboxResult = ValueConversions.ignore();
        } else {
            Wrapper w = Wrapper.forPrimitiveType(type.returnType());
            unboxResult = ValueConversions.unboxExact(w);
        }
    } else {
        unboxResult = MethodHandles.identity(Object.class);
    }

    BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_LLLLL();
    BoundMethodHandle mh;
    try {
        mh = (BoundMethodHandle)
                data.constructor().invokeBasic(type, form, (Object) target, (Object) exType, (Object) catcher,
                                               (Object) collectArgs, (Object) unboxResult);
    } catch (Throwable ex) {
        throw uncaughtException(ex);
    }
    assert(mh.type() == type);
    return mh;
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:38,代码来源:MethodHandleImpl.java

示例4: identity

import sun.invoke.util.Wrapper; //导入方法依赖的package包/类
/**
 * Produces a method handle which returns its sole argument when invoked.
 * @param type the type of the sole parameter and return value of the desired method handle
 * @return a unary method handle which accepts and returns the given type
 * @throws NullPointerException if the argument is null
 * @throws IllegalArgumentException if the given type is {@code void.class}
 */
public static
MethodHandle identity(Class<?> type) {
    Wrapper btw = (type.isPrimitive() ? Wrapper.forPrimitiveType(type) : Wrapper.OBJECT);
    int pos = btw.ordinal();
    MethodHandle ident = IDENTITY_MHS[pos];
    if (ident == null) {
        ident = setCachedMethodHandle(IDENTITY_MHS, pos, makeIdentity(btw.primitiveType()));
    }
    if (ident.type().returnType() == type)
        return ident;
    // something like identity(Foo.class); do not bother to intern these
    assert(btw == Wrapper.OBJECT);
    return makeIdentity(type);
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:22,代码来源:MethodHandles.java

示例5: 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));
    }
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:14,代码来源:MethodHandles.java

示例6: unboxResultHandle

import sun.invoke.util.Wrapper; //导入方法依赖的package包/类
/** Result unboxing: ValueConversions.unbox() OR ValueConversions.identity() OR ValueConversions.ignore(). */
private static MethodHandle unboxResultHandle(Class<?> returnType) {
    if (returnType.isPrimitive()) {
        if (returnType == void.class) {
            return ValueConversions.ignore();
        } else {
            Wrapper w = Wrapper.forPrimitiveType(returnType);
            return ValueConversions.unboxExact(w);
        }
    } else {
        return MethodHandles.identity(Object.class);
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:14,代码来源:MethodHandleImpl.java

示例7: identity

import sun.invoke.util.Wrapper; //导入方法依赖的package包/类
/**
 * Produces a method handle which returns its sole argument when invoked.
 * @param type the type of the sole parameter and return value of the desired method handle
 * @return a unary method handle which accepts and returns the given type
 * @throws NullPointerException if the argument is null
 * @throws IllegalArgumentException if the given type is {@code void.class}
 */
public static
MethodHandle identity(Class<?> type) {
    Wrapper btw = (type.isPrimitive() ? Wrapper.forPrimitiveType(type) : Wrapper.OBJECT);
    int pos = btw.ordinal();
    MethodHandle ident = IDENTITY_MHS[pos];
    if (ident == null) {
        ident = setCachedMethodHandle(IDENTITY_MHS, pos, makeIdentity(btw.primitiveType()));
    }
    if (ident.type().returnType() == type)
        return ident;
    // something like identity(Foo.class); do not bother to intern these
    assert (btw == Wrapper.OBJECT);
    return makeIdentity(type);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:22,代码来源:MethodHandles.java

示例8: canConvert

import sun.invoke.util.Wrapper; //导入方法依赖的package包/类
static boolean canConvert(Class<?> src, Class<?> dst) {
    // short-circuit a few cases:
    if (src == dst || src == Object.class || dst == Object.class)  return true;
    // the remainder of this logic is documented in MethodHandle.asType
    if (src.isPrimitive()) {
        // can force void to an explicit null, a la reflect.Method.invoke
        // can also force void to a primitive zero, by analogy
        if (src == void.class)  return true;  //or !dst.isPrimitive()?
        Wrapper sw = Wrapper.forPrimitiveType(src);
        if (dst.isPrimitive()) {
            // P->P must widen
            return Wrapper.forPrimitiveType(dst).isConvertibleFrom(sw);
        } else {
            // P->R must box and widen
            return dst.isAssignableFrom(sw.wrapperType());
        }
    } else if (dst.isPrimitive()) {
        // any value can be dropped
        if (dst == void.class)  return true;
        Wrapper dw = Wrapper.forPrimitiveType(dst);
        // R->P must be able to unbox (from a dynamically chosen type) and widen
        // For example:
        //   Byte/Number/Comparable/Object -> dw:Byte -> byte.
        //   Character/Comparable/Object -> dw:Character -> char
        //   Boolean/Comparable/Object -> dw:Boolean -> boolean
        // This means that dw must be cast-compatible with src.
        if (src.isAssignableFrom(dw.wrapperType())) {
            return true;
        }
        // The above does not work if the source reference is strongly typed
        // to a wrapper whose primitive must be widened.  For example:
        //   Byte -> unbox:byte -> short/int/long/float/double
        //   Character -> unbox:char -> int/long/float/double
        if (Wrapper.isWrapperType(src) &&
            dw.isConvertibleFrom(Wrapper.forWrapperType(src))) {
            // can unbox from src and then widen to dst
            return true;
        }
        // We have already covered cases which arise due to runtime unboxing
        // of a reference type which covers several wrapper types:
        //   Object -> cast:Integer -> unbox:int -> long/float/double
        //   Serializable -> cast:Byte -> unbox:byte -> byte/short/int/long/float/double
        // An marginal case is Number -> dw:Character -> char, which would be OK if there were a
        // subclass of Number which wraps a value that can convert to char.
        // Since there is none, we don't need an extra check here to cover char or boolean.
        return false;
    } else {
        // R->R always works, since null is always valid dynamically
        return true;
    }
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:52,代码来源:MethodType.java


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