Java Code Examples for sun.invoke.util.Wrapper#OBJECT

The following examples show how to use sun.invoke.util.Wrapper#OBJECT . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ExplicitCastArgumentsTest.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Tests that non-null wrapper reference is successfully converted to
 * primitive types.
 */
public static void testRef2Prim() {
    for (Wrapper from : Wrapper.values()) {
        for (Wrapper to : Wrapper.values()) {
            if (from == Wrapper.VOID || to == Wrapper.VOID
                    || to == Wrapper.OBJECT) {
                continue;
            }
            Object value = RANDOM_VALUES.get(from);
            for (TestConversionMode mode : TestConversionMode.values()) {
                if (from != Wrapper.OBJECT) {
                    Object convValue = to.wrap(value);
                    testConversion(mode, from.wrapperType(),
                            to.primitiveType(), value, convValue, false, null);
                } else {
                    testConversion(mode, from.wrapperType(),
                            to.primitiveType(), value, null,
                            true, ClassCastException.class);
                }
            }
        }
    }
}
 
Example 2
Source File: ExplicitCastArgumentsTest.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Tests that primitive is successfully converted to other primitive type.
 */
public static void testPrim2Prim() {
    for (Wrapper from : Wrapper.values()) {
        for (Wrapper to : Wrapper.values()) {
            if (from == Wrapper.VOID || to == Wrapper.VOID
                    || from == Wrapper.OBJECT || to == Wrapper.OBJECT) {
                continue;
            }
            Object value = RANDOM_VALUES.get(from);
            Object convValue = to.wrap(value);
            for (TestConversionMode mode : TestConversionMode.values()) {
                testConversion(mode, from.primitiveType(),
                        to.primitiveType(), value, convValue, false, null);
            }
        }
    }
}
 
Example 3
Source File: ExplicitCastArgumentsTest.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Tests that primitive is successfully converted to other primitive type.
 */
public static void testPrim2Prim() {
    for (Wrapper from : Wrapper.values()) {
        for (Wrapper to : Wrapper.values()) {
            if (from == Wrapper.VOID || to == Wrapper.VOID
                    || from == Wrapper.OBJECT || to == Wrapper.OBJECT) {
                continue;
            }
            Object value = RANDOM_VALUES.get(from);
            Object convValue = to.wrap(value);
            for (TestConversionMode mode : TestConversionMode.values()) {
                testConversion(mode, from.primitiveType(),
                        to.primitiveType(), value, convValue, false, null);
            }
        }
    }
}
 
Example 4
Source File: ValueConversionsTest.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
@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);
        }
    }
}
 
Example 5
Source File: ValueConversionsTest.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
@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);
        }
    }
}
 
Example 6
Source File: ValueConversionsTest.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@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);
        }
    }
}
 
Example 7
Source File: ExplicitCastArgumentsTest.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Tests that primitive is successfully converted to other primitive type.
 */
public static void testPrim2Prim() {
    for (Wrapper from : Wrapper.values()) {
        for (Wrapper to : Wrapper.values()) {
            if (from == Wrapper.VOID || to == Wrapper.VOID
                    || from == Wrapper.OBJECT || to == Wrapper.OBJECT) {
                continue;
            }
            Object value = RANDOM_VALUES.get(from);
            Object convValue = to.wrap(value);
            for (TestConversionMode mode : TestConversionMode.values()) {
                testConversion(mode, from.primitiveType(),
                        to.primitiveType(), value, convValue, false, null);
            }
        }
    }
}
 
Example 8
Source File: MethodHandles.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 9
Source File: ExplicitCastArgumentsTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Tests that primitive is successfully converted to wrapper reference
 * types, to the Number type (if possible) and to the Object type.
 */
public static void testPrim2Ref() {
    for (Wrapper from : Wrapper.values()) {
        for (Wrapper to : Wrapper.values()) {
            if (from == Wrapper.VOID || from == Wrapper.OBJECT
                    || to == Wrapper.VOID || to == Wrapper.OBJECT) {
                continue;
            }
            Object value = RANDOM_VALUES.get(from);
            for (TestConversionMode mode : TestConversionMode.values()) {
                if (from == to) {
                    testConversion(mode, from.primitiveType(),
                            to.wrapperType(), value, value, false, null);
                } else {
                    testConversion(mode, from.primitiveType(),
                            to.wrapperType(), value, null, true, ClassCastException.class);
                }
                if (from != Wrapper.BOOLEAN && from != Wrapper.CHAR) {
                    testConversion(mode, from.primitiveType(),
                            Number.class, value, value, false, null);
                } else {
                    testConversion(mode, from.primitiveType(),
                            Number.class, value, null,
                            true, ClassCastException.class);
                }
                testConversion(mode, from.primitiveType(),
                        Object.class, value, value, false, null);
            }
        }
    }
}
 
Example 10
Source File: ExplicitCastArgumentsTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Tests that primitive is successfully converted to wrapper reference
 * types, to the Number type (if possible) and to the Object type.
 */
public static void testPrim2Ref() {
    for (Wrapper from : Wrapper.values()) {
        for (Wrapper to : Wrapper.values()) {
            if (from == Wrapper.VOID || from == Wrapper.OBJECT
                    || to == Wrapper.VOID || to == Wrapper.OBJECT) {
                continue;
            }
            Object value = RANDOM_VALUES.get(from);
            for (TestConversionMode mode : TestConversionMode.values()) {
                if (from == to) {
                    testConversion(mode, from.primitiveType(),
                            to.wrapperType(), value, value, false, null);
                } else {
                    testConversion(mode, from.primitiveType(),
                            to.wrapperType(), value, null, true, ClassCastException.class);
                }
                if (from != Wrapper.BOOLEAN && from != Wrapper.CHAR) {
                    testConversion(mode, from.primitiveType(),
                            Number.class, value, value, false, null);
                } else {
                    testConversion(mode, from.primitiveType(),
                            Number.class, value, null,
                            true, ClassCastException.class);
                }
                testConversion(mode, from.primitiveType(),
                        Object.class, value, value, false, null);
            }
        }
    }
}
 
Example 11
Source File: ValueConversionsTest.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
static void testConvert(Wrapper src, Wrapper dst, long tval) throws Throwable {
    if (dst == Wrapper.OBJECT || src == Wrapper.OBJECT)  return;  // must have prims
    if (dst == Wrapper.VOID   || src == Wrapper.VOID  )  return;  // must have values
    boolean testSingleCase = (tval != 0);
    final long tvalInit = tval;
    MethodHandle conv = ValueConversions.convertPrimitive(src, dst);
    MethodType convType = MethodType.methodType(dst.primitiveType(), src.primitiveType());
    assertEquals(convType, conv.type());
    MethodHandle converter = conv.asType(conv.type().changeReturnType(Object.class));
    for (;;) {
        long n = tval;
        Object testValue = src.wrap(n);
        Object expResult = dst.cast(testValue, dst.primitiveType());
        Object result;
        switch (src) {
            case INT:     result = converter.invokeExact((int)n); break;
            case LONG:    result = converter.invokeExact(/*long*/n); break;
            case FLOAT:   result = converter.invokeExact((float)n); break;
            case DOUBLE:  result = converter.invokeExact((double)n); break;
            case CHAR:    result = converter.invokeExact((char)n); break;
            case BYTE:    result = converter.invokeExact((byte)n); break;
            case SHORT:   result = converter.invokeExact((short)n); break;
            case BOOLEAN: result = converter.invokeExact((n & 1) != 0); break;
            default:  throw new AssertionError();
        }
        assertEquals("(src,dst,n,testValue)="+Arrays.asList(src,dst,"0x"+Long.toHexString(n),testValue),
                     expResult, result);
        if (testSingleCase)  break;
        // next test value:
        tval = nextTestValue(tval);
        if (tval == tvalInit)  break;  // repeat
    }
}
 
Example 12
Source File: MethodHandles.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 13
Source File: MethodHandles.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 14
Source File: MethodHandles.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 15
Source File: DirectMethodHandle.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
private static LambdaForm makePreparedFieldLambdaForm(byte formOp, boolean isVolatile, int ftypeKind) {
    boolean isGetter  = (formOp & 1) == (AF_GETFIELD & 1);
    boolean isStatic  = (formOp >= AF_GETSTATIC);
    boolean needsInit = (formOp >= AF_GETSTATIC_INIT);
    boolean needsCast = (ftypeKind == FT_CHECKED_REF);
    Wrapper fw = (needsCast ? Wrapper.OBJECT : Wrapper.values()[ftypeKind]);
    Class<?> ft = fw.primitiveType();
    assert(ftypeKind(needsCast ? String.class : ft) == ftypeKind);
    String tname  = fw.primitiveSimpleName();
    String ctname = Character.toUpperCase(tname.charAt(0)) + tname.substring(1);
    if (isVolatile)  ctname += "Volatile";
    String getOrPut = (isGetter ? "get" : "put");
    String linkerName = (getOrPut + ctname);  // getObject, putIntVolatile, etc.
    MethodType linkerType;
    if (isGetter)
        linkerType = MethodType.methodType(ft, Object.class, long.class);
    else
        linkerType = MethodType.methodType(void.class, Object.class, long.class, ft);
    MemberName linker = new MemberName(Unsafe.class, linkerName, linkerType, REF_invokeVirtual);
    try {
        linker = IMPL_NAMES.resolveOrFail(REF_invokeVirtual, linker, null, NoSuchMethodException.class);
    } catch (ReflectiveOperationException ex) {
        throw newInternalError(ex);
    }

    // What is the external type of the lambda form?
    MethodType mtype;
    if (isGetter)
        mtype = MethodType.methodType(ft);
    else
        mtype = MethodType.methodType(void.class, ft);
    mtype = mtype.basicType();  // erase short to int, etc.
    if (!isStatic)
        mtype = mtype.insertParameterTypes(0, Object.class);
    final int DMH_THIS  = 0;
    final int ARG_BASE  = 1;
    final int ARG_LIMIT = ARG_BASE + mtype.parameterCount();
    // if this is for non-static access, the base pointer is stored at this index:
    final int OBJ_BASE  = isStatic ? -1 : ARG_BASE;
    // if this is for write access, the value to be written is stored at this index:
    final int SET_VALUE  = isGetter ? -1 : ARG_LIMIT - 1;
    int nameCursor = ARG_LIMIT;
    final int F_HOLDER  = (isStatic ? nameCursor++ : -1);  // static base if any
    final int F_OFFSET  = nameCursor++;  // Either static offset or field offset.
    final int OBJ_CHECK = (OBJ_BASE >= 0 ? nameCursor++ : -1);
    final int INIT_BAR  = (needsInit ? nameCursor++ : -1);
    final int PRE_CAST  = (needsCast && !isGetter ? nameCursor++ : -1);
    final int LINKER_CALL = nameCursor++;
    final int POST_CAST = (needsCast && isGetter ? nameCursor++ : -1);
    final int RESULT    = nameCursor-1;  // either the call or the cast
    Name[] names = arguments(nameCursor - ARG_LIMIT, mtype.invokerType());
    if (needsInit)
        names[INIT_BAR] = new Name(Lazy.NF_ensureInitialized, names[DMH_THIS]);
    if (needsCast && !isGetter)
        names[PRE_CAST] = new Name(Lazy.NF_checkCast, names[DMH_THIS], names[SET_VALUE]);
    Object[] outArgs = new Object[1 + linkerType.parameterCount()];
    assert(outArgs.length == (isGetter ? 3 : 4));
    outArgs[0] = UNSAFE;
    if (isStatic) {
        outArgs[1] = names[F_HOLDER]  = new Name(Lazy.NF_staticBase, names[DMH_THIS]);
        outArgs[2] = names[F_OFFSET]  = new Name(Lazy.NF_staticOffset, names[DMH_THIS]);
    } else {
        outArgs[1] = names[OBJ_CHECK] = new Name(Lazy.NF_checkBase, names[OBJ_BASE]);
        outArgs[2] = names[F_OFFSET]  = new Name(Lazy.NF_fieldOffset, names[DMH_THIS]);
    }
    if (!isGetter) {
        outArgs[3] = (needsCast ? names[PRE_CAST] : names[SET_VALUE]);
    }
    for (Object a : outArgs)  assert(a != null);
    names[LINKER_CALL] = new Name(linker, outArgs);
    if (needsCast && isGetter)
        names[POST_CAST] = new Name(Lazy.NF_checkCast, names[DMH_THIS], names[LINKER_CALL]);
    for (Name n : names)  assert(n != null);
    String fieldOrStatic = (isStatic ? "Static" : "Field");
    String lambdaName = (linkerName + fieldOrStatic);  // significant only for debugging
    if (needsCast)  lambdaName += "Cast";
    if (needsInit)  lambdaName += "Init";
    return new LambdaForm(lambdaName, ARG_LIMIT, names, RESULT);
}
 
Example 16
Source File: DirectMethodHandle.java    From jdk-1.7-annotated with Apache License 2.0 4 votes vote down vote up
private static LambdaForm makePreparedFieldLambdaForm(byte formOp, boolean isVolatile, int ftypeKind) {
    boolean isGetter  = (formOp & 1) == (AF_GETFIELD & 1);
    boolean isStatic  = (formOp >= AF_GETSTATIC);
    boolean needsInit = (formOp >= AF_GETSTATIC_INIT);
    boolean needsCast = (ftypeKind == FT_CHECKED_REF);
    Wrapper fw = (needsCast ? Wrapper.OBJECT : Wrapper.values()[ftypeKind]);
    Class<?> ft = fw.primitiveType();
    assert(ftypeKind(needsCast ? String.class : ft) == ftypeKind);
    String tname  = fw.primitiveSimpleName();
    String ctname = Character.toUpperCase(tname.charAt(0)) + tname.substring(1);
    if (isVolatile)  ctname += "Volatile";
    String getOrPut = (isGetter ? "get" : "put");
    String linkerName = (getOrPut + ctname);  // getObject, putIntVolatile, etc.
    MethodType linkerType;
    if (isGetter)
        linkerType = MethodType.methodType(ft, Object.class, long.class);
    else
        linkerType = MethodType.methodType(void.class, Object.class, long.class, ft);
    MemberName linker = new MemberName(Unsafe.class, linkerName, linkerType, REF_invokeVirtual);
    try {
        linker = IMPL_NAMES.resolveOrFail(REF_invokeVirtual, linker, null, NoSuchMethodException.class);
    } catch (ReflectiveOperationException ex) {
        throw newInternalError(ex);
    }

    // What is the external type of the lambda form?
    MethodType mtype;
    if (isGetter)
        mtype = MethodType.methodType(ft);
    else
        mtype = MethodType.methodType(void.class, ft);
    mtype = mtype.basicType();  // erase short to int, etc.
    if (!isStatic)
        mtype = mtype.insertParameterTypes(0, Object.class);
    final int DMH_THIS  = 0;
    final int ARG_BASE  = 1;
    final int ARG_LIMIT = ARG_BASE + mtype.parameterCount();
    // if this is for non-static access, the base pointer is stored at this index:
    final int OBJ_BASE  = isStatic ? -1 : ARG_BASE;
    // if this is for write access, the value to be written is stored at this index:
    final int SET_VALUE  = isGetter ? -1 : ARG_LIMIT - 1;
    int nameCursor = ARG_LIMIT;
    final int F_HOLDER  = (isStatic ? nameCursor++ : -1);  // static base if any
    final int F_OFFSET  = nameCursor++;  // Either static offset or field offset.
    final int OBJ_CHECK = (OBJ_BASE >= 0 ? nameCursor++ : -1);
    final int INIT_BAR  = (needsInit ? nameCursor++ : -1);
    final int PRE_CAST  = (needsCast && !isGetter ? nameCursor++ : -1);
    final int LINKER_CALL = nameCursor++;
    final int POST_CAST = (needsCast && isGetter ? nameCursor++ : -1);
    final int RESULT    = nameCursor-1;  // either the call or the cast
    Name[] names = arguments(nameCursor - ARG_LIMIT, mtype.invokerType());
    if (needsInit)
        names[INIT_BAR] = new Name(Lazy.NF_ensureInitialized, names[DMH_THIS]);
    if (needsCast && !isGetter)
        names[PRE_CAST] = new Name(Lazy.NF_checkCast, names[DMH_THIS], names[SET_VALUE]);
    Object[] outArgs = new Object[1 + linkerType.parameterCount()];
    assert(outArgs.length == (isGetter ? 3 : 4));
    outArgs[0] = UNSAFE;
    if (isStatic) {
        outArgs[1] = names[F_HOLDER]  = new Name(Lazy.NF_staticBase, names[DMH_THIS]);
        outArgs[2] = names[F_OFFSET]  = new Name(Lazy.NF_staticOffset, names[DMH_THIS]);
    } else {
        outArgs[1] = names[OBJ_CHECK] = new Name(Lazy.NF_checkBase, names[OBJ_BASE]);
        outArgs[2] = names[F_OFFSET]  = new Name(Lazy.NF_fieldOffset, names[DMH_THIS]);
    }
    if (!isGetter) {
        outArgs[3] = (needsCast ? names[PRE_CAST] : names[SET_VALUE]);
    }
    for (Object a : outArgs)  assert(a != null);
    names[LINKER_CALL] = new Name(linker, outArgs);
    if (needsCast && isGetter)
        names[POST_CAST] = new Name(Lazy.NF_checkCast, names[DMH_THIS], names[LINKER_CALL]);
    for (Name n : names)  assert(n != null);
    String fieldOrStatic = (isStatic ? "Static" : "Field");
    String lambdaName = (linkerName + fieldOrStatic);  // significant only for debugging
    if (needsCast)  lambdaName += "Cast";
    if (needsInit)  lambdaName += "Init";
    return new LambdaForm(lambdaName, ARG_LIMIT, names, RESULT);
}
 
Example 17
Source File: ValueConversionsTest.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
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.VOID   || src == Wrapper.VOID  )  return;  // must have values
    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);
            else
                unboxer = ValueConversions.unboxWiden(dst);
            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 BOOLEAN: result = (boolean) 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;
    }
}
 
Example 18
Source File: ValueConversionsTest.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
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.VOID   || src == Wrapper.VOID  )  return;  // must have values
    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);
            else
                unboxer = ValueConversions.unboxWiden(dst);
            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 BOOLEAN: result = (boolean) 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;
    }
}
 
Example 19
Source File: ValueConversionsTest.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
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.VOID   || src == Wrapper.VOID  )  return;  // must have values
    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);
            else
                unboxer = ValueConversions.unboxWiden(dst);
            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 BOOLEAN: result = (boolean) 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;
    }
}
 
Example 20
Source File: DirectMethodHandle.java    From jdk1.8-source-analysis with Apache License 2.0 4 votes vote down vote up
private static LambdaForm makePreparedFieldLambdaForm(byte formOp, boolean isVolatile, int ftypeKind) {
    boolean isGetter  = (formOp & 1) == (AF_GETFIELD & 1);
    boolean isStatic  = (formOp >= AF_GETSTATIC);
    boolean needsInit = (formOp >= AF_GETSTATIC_INIT);
    boolean needsCast = (ftypeKind == FT_CHECKED_REF);
    Wrapper fw = (needsCast ? Wrapper.OBJECT : Wrapper.values()[ftypeKind]);
    Class<?> ft = fw.primitiveType();
    assert(ftypeKind(needsCast ? String.class : ft) == ftypeKind);
    String tname  = fw.primitiveSimpleName();
    String ctname = Character.toUpperCase(tname.charAt(0)) + tname.substring(1);
    if (isVolatile)  ctname += "Volatile";
    String getOrPut = (isGetter ? "get" : "put");
    String linkerName = (getOrPut + ctname);  // getObject, putIntVolatile, etc.
    MethodType linkerType;
    if (isGetter)
        linkerType = MethodType.methodType(ft, Object.class, long.class);
    else
        linkerType = MethodType.methodType(void.class, Object.class, long.class, ft);
    MemberName linker = new MemberName(Unsafe.class, linkerName, linkerType, REF_invokeVirtual);
    try {
        linker = IMPL_NAMES.resolveOrFail(REF_invokeVirtual, linker, null, NoSuchMethodException.class);
    } catch (ReflectiveOperationException ex) {
        throw newInternalError(ex);
    }

    // What is the external type of the lambda form?
    MethodType mtype;
    if (isGetter)
        mtype = MethodType.methodType(ft);
    else
        mtype = MethodType.methodType(void.class, ft);
    mtype = mtype.basicType();  // erase short to int, etc.
    if (!isStatic)
        mtype = mtype.insertParameterTypes(0, Object.class);
    final int DMH_THIS  = 0;
    final int ARG_BASE  = 1;
    final int ARG_LIMIT = ARG_BASE + mtype.parameterCount();
    // if this is for non-static access, the base pointer is stored at this index:
    final int OBJ_BASE  = isStatic ? -1 : ARG_BASE;
    // if this is for write access, the value to be written is stored at this index:
    final int SET_VALUE  = isGetter ? -1 : ARG_LIMIT - 1;
    int nameCursor = ARG_LIMIT;
    final int F_HOLDER  = (isStatic ? nameCursor++ : -1);  // static base if any
    final int F_OFFSET  = nameCursor++;  // Either static offset or field offset.
    final int OBJ_CHECK = (OBJ_BASE >= 0 ? nameCursor++ : -1);
    final int INIT_BAR  = (needsInit ? nameCursor++ : -1);
    final int PRE_CAST  = (needsCast && !isGetter ? nameCursor++ : -1);
    final int LINKER_CALL = nameCursor++;
    final int POST_CAST = (needsCast && isGetter ? nameCursor++ : -1);
    final int RESULT    = nameCursor-1;  // either the call or the cast
    Name[] names = arguments(nameCursor - ARG_LIMIT, mtype.invokerType());
    if (needsInit)
        names[INIT_BAR] = new Name(Lazy.NF_ensureInitialized, names[DMH_THIS]);
    if (needsCast && !isGetter)
        names[PRE_CAST] = new Name(Lazy.NF_checkCast, names[DMH_THIS], names[SET_VALUE]);
    Object[] outArgs = new Object[1 + linkerType.parameterCount()];
    assert(outArgs.length == (isGetter ? 3 : 4));
    outArgs[0] = UNSAFE;
    if (isStatic) {
        outArgs[1] = names[F_HOLDER]  = new Name(Lazy.NF_staticBase, names[DMH_THIS]);
        outArgs[2] = names[F_OFFSET]  = new Name(Lazy.NF_staticOffset, names[DMH_THIS]);
    } else {
        outArgs[1] = names[OBJ_CHECK] = new Name(Lazy.NF_checkBase, names[OBJ_BASE]);
        outArgs[2] = names[F_OFFSET]  = new Name(Lazy.NF_fieldOffset, names[DMH_THIS]);
    }
    if (!isGetter) {
        outArgs[3] = (needsCast ? names[PRE_CAST] : names[SET_VALUE]);
    }
    for (Object a : outArgs)  assert(a != null);
    names[LINKER_CALL] = new Name(linker, outArgs);
    if (needsCast && isGetter)
        names[POST_CAST] = new Name(Lazy.NF_checkCast, names[DMH_THIS], names[LINKER_CALL]);
    for (Name n : names)  assert(n != null);
    String fieldOrStatic = (isStatic ? "Static" : "Field");
    String lambdaName = (linkerName + fieldOrStatic);  // significant only for debugging
    if (needsCast)  lambdaName += "Cast";
    if (needsInit)  lambdaName += "Init";
    return new LambdaForm(lambdaName, ARG_LIMIT, names, RESULT);
}