Java Code Examples for java.lang.invoke.MethodHandles#constant()

The following examples show how to use java.lang.invoke.MethodHandles#constant() . 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: Selector.java    From groovy with Apache License 2.0 6 votes vote down vote up
private void handleBoolean() {
    if (handle != null) return;

    // boolean->boolean, Boolean->boolean, boolean->Boolean
    // is handled by compiler
    // that leaves (T)Z and (T)Boolean, where T is the static type
    // but runtime type of T might be Boolean

    boolean primitive = staticTargetType == boolean.class;
    if (!primitive && staticTargetType != Boolean.class) return;
    if (args[0] == null) {
        if (primitive) {
            handle = MethodHandles.constant(boolean.class, false);
            handle = MethodHandles.dropArguments(handle, 0, staticSourceType);
        } else {
            handle = BOOLEAN_IDENTITY;
        }
    } else if (args[0] instanceof Boolean) {
        // give value through or unbox
        handle = BOOLEAN_IDENTITY;
    } else {
        //call asBoolean
        name = "asBoolean";
        super.setCallSiteTarget();
    }
}
 
Example 2
Source File: LoopCombinatorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@DataProvider
static Object[][] countedLoopNegativeData() {
    MethodHandle dummy = MethodHandles.zero(void.class);
    MethodHandle one = MethodHandles.constant(int.class, 1);
    MethodHandle oneString = MethodHandles.dropArguments(one, 0, String.class);
    MethodHandle oneDouble = MethodHandles.dropArguments(one, 0, double.class);
    return new Object[][]{
            {dummy, one, dummy, dummy, String.format("start/end must return int %s, %s", dummy, one)},
            {one, dummy, dummy, dummy, String.format("start/end must return int %s, %s", one, dummy)},
            {oneString, oneDouble, dummy, dummy,
                    String.format("start and end parameter types must match: %s != %s", oneString.type(),
                            oneDouble.type())},
            {oneString, oneString, dummy, dummy,
                    String.format("start/end and init parameter types must match: %s != %s", oneString.type(),
                            dummy.type())},
            {one, one, null, dummy, String.format("actual and expected body signatures must match: %s != %s",
                    dummy.type(), dummy.type().appendParameterTypes(int.class))}
    };
}
 
Example 3
Source File: MethodHandlesProxiesTest.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Test
    public static void testOverriddenDefaultMethods() throws Throwable {
    MethodHandle target = MethodHandles.constant(String.class, "F");
    Override proxy = MethodHandleProxies.asInterfaceInstance(Override.class, target);

    assertEquals(proxy.a(), "OA");
    assertEquals(proxy.b(), "OB");
    assertEquals(proxy.c(), "OC");
}
 
Example 4
Source File: ExplicitCastArgumentsTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Tests that MHs.eCA method works correctly with MHs with multiple arguments.
 * @throws Throwable
 */
public static void testMultipleArgs() throws Throwable {
    int arity = 1 + RNG.nextInt(Helper.MAX_ARITY / 2 - 2);
    int arityMinus = RNG.nextInt(arity);
    int arityPlus = arity + RNG.nextInt(Helper.MAX_ARITY / 2 - arity) + 1;
    MethodType mType = Helper.randomMethodTypeGenerator(arity);
    MethodType mTypeNew = Helper.randomMethodTypeGenerator(arity);
    MethodType mTypeNewMinus = Helper.randomMethodTypeGenerator(arityMinus);
    MethodType mTypeNewPlus = Helper.randomMethodTypeGenerator(arityPlus);
    Class<?> rType = mType.returnType();
    MethodHandle original;
    if (rType.equals(void.class)) {
        MethodType mt = MethodType.methodType(void.class);
        original = MethodHandles.publicLookup()
                .findStatic(THIS_CLASS, "retVoid", mt);
    } else {
        Object rValue = Helper.castToWrapper(1, rType);
        original = MethodHandles.constant(rType, rValue);
    }
    original = Helper.addTrailingArgs(original, arity, mType.parameterList());
    MethodHandle target = MethodHandles
                .explicitCastArguments(original, mTypeNew);
    Object[] parList = Helper.randomArgs(mTypeNew.parameterList());
    for (int i = 0; i < parList.length; i++) {
        if (parList[i] instanceof String) {
            parList[i] = null; //getting rid of Stings produced by randomArgs
        }
    }
    target.invokeWithArguments(parList);
    checkForWrongMethodTypeException(original, mTypeNewMinus);
    checkForWrongMethodTypeException(original, mTypeNewPlus);
}
 
Example 5
Source File: WrapperTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testIntZeroConversion() throws Throwable {
    MethodHandle h1 = MethodHandles.constant(Integer.class, 42);
    MethodHandle h2 = h1.asType(MethodType.methodType(void.class));  // drop 42
    MethodHandle h3 = h2.asType(MethodType.methodType(int.class));  // add 0
    MethodHandle h4 = h3.asType(MethodType.methodType(Object.class));  // box

    Object x = h4.invokeExact();
    assertEquals(x, 0);
    assertTrue(x == Integer.valueOf(0));
    assertTrue(x == Wrapper.INT.zero());
}
 
Example 6
Source File: ExplicitCastArgumentsTest.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Tests that MHs.eCA method works correctly with MHs with multiple arguments.
 * @throws Throwable
 */
public static void testMultipleArgs() throws Throwable {
    int arity = 1 + RNG.nextInt(Helper.MAX_ARITY / 2 - 2);
    int arityMinus = RNG.nextInt(arity);
    int arityPlus = arity + RNG.nextInt(Helper.MAX_ARITY / 2 - arity) + 1;
    MethodType mType = Helper.randomMethodTypeGenerator(arity);
    MethodType mTypeNew = Helper.randomMethodTypeGenerator(arity);
    MethodType mTypeNewMinus = Helper.randomMethodTypeGenerator(arityMinus);
    MethodType mTypeNewPlus = Helper.randomMethodTypeGenerator(arityPlus);
    Class<?> rType = mType.returnType();
    MethodHandle original;
    if (rType.equals(void.class)) {
        MethodType mt = MethodType.methodType(void.class);
        original = MethodHandles.publicLookup()
                .findStatic(THIS_CLASS, "retVoid", mt);
    } else {
        Object rValue = Helper.castToWrapper(1, rType);
        original = MethodHandles.constant(rType, rValue);
    }
    original = Helper.addTrailingArgs(original, arity, mType.parameterList());
    MethodHandle target = MethodHandles
                .explicitCastArguments(original, mTypeNew);
    Object[] parList = Helper.randomArgs(mTypeNew.parameterList());
    for (int i = 0; i < parList.length; i++) {
        if (parList[i] instanceof String) {
            parList[i] = null; //getting rid of Stings produced by randomArgs
        }
    }
    target.invokeWithArguments(parList);
    checkForWrongMethodTypeException(original, mTypeNewMinus);
    checkForWrongMethodTypeException(original, mTypeNewPlus);
}
 
Example 7
Source File: WrapperTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testShortZeroConversion() throws Throwable {
    MethodHandle h1 = MethodHandles.constant(Short.class, (short)42);
    MethodHandle h2 = h1.asType(MethodType.methodType(void.class));  // drop 42
    MethodHandle h3 = h2.asType(MethodType.methodType(short.class));  // add 0
    MethodHandle h4 = h3.asType(MethodType.methodType(Object.class));  // box

    Object x = h4.invokeExact();
    assertEquals(x, (short)0);
    assertTrue(x == Short.valueOf((short)0));
    assertTrue(x == Wrapper.SHORT.zero());
}
 
Example 8
Source File: WrapperTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testByteZeroConversion() throws Throwable {
    MethodHandle h1 = MethodHandles.constant(Byte.class, (byte)42);
    MethodHandle h2 = h1.asType(MethodType.methodType(void.class));  // drop 42
    MethodHandle h3 = h2.asType(MethodType.methodType(byte.class));  // add 0
    MethodHandle h4 = h3.asType(MethodType.methodType(Object.class));  // box

    Object x = h4.invokeExact();
    assertEquals(x, (byte)0);
    assertTrue(x == Byte.valueOf((byte)0));
    assertTrue(x == Wrapper.BYTE.zero());
}
 
Example 9
Source File: WrapperTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testCharacterZeroConversion() throws Throwable {
    MethodHandle h1 = MethodHandles.constant(Character.class, (char)42);
    MethodHandle h2 = h1.asType(MethodType.methodType(void.class));  // drop 42
    MethodHandle h3 = h2.asType(MethodType.methodType(char.class));  // add 0
    MethodHandle h4 = h3.asType(MethodType.methodType(Object.class));  // box

    Object x = h4.invokeExact();
    assertEquals(x, (char)0);
    assertTrue(x == Character.valueOf((char)0));
    assertTrue(x == Wrapper.CHAR.zero());
}
 
Example 10
Source File: MethodHandlesProxiesTest.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Test
    public static void testOverriddenDefaultMethods() throws Throwable {
    MethodHandle target = MethodHandles.constant(String.class, "F");
    Override proxy = MethodHandleProxies.asInterfaceInstance(Override.class, target);

    assertEquals(proxy.a(), "OA");
    assertEquals(proxy.b(), "OB");
    assertEquals(proxy.c(), "OC");
}
 
Example 11
Source File: LoopCombinatorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public static void testCountedLoopCounterInit() throws Throwable {
    // int x = 0; for (int i = 0; i < 5; ++i) { x += i; } return x; => 10
    // (only if counter's first value in body is 0)
    MethodHandle iter = MethodHandles.constant(int.class, 5);
    MethodHandle init = MethodHandles.constant(int.class, 0);
    MethodHandle body = Counted.MH_addCounter;
    MethodHandle loop = MethodHandles.countedLoop(iter, init, body);
    assertEquals(Counted.MT_counterInit, loop.type());
    assertEquals(10, loop.invoke());
}
 
Example 12
Source File: ExplicitCastArgumentsTest.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Tests that MHs.eCA method works correctly with MHs with multiple arguments.
 * @throws Throwable
 */
public static void testMultipleArgs() throws Throwable {
    int arity = 1 + RNG.nextInt(Helper.MAX_ARITY / 2 - 2);
    int arityMinus = RNG.nextInt(arity);
    int arityPlus = arity + RNG.nextInt(Helper.MAX_ARITY / 2 - arity) + 1;
    MethodType mType = Helper.randomMethodTypeGenerator(arity);
    MethodType mTypeNew = Helper.randomMethodTypeGenerator(arity);
    MethodType mTypeNewMinus = Helper.randomMethodTypeGenerator(arityMinus);
    MethodType mTypeNewPlus = Helper.randomMethodTypeGenerator(arityPlus);
    Class<?> rType = mType.returnType();
    MethodHandle original;
    if (rType.equals(void.class)) {
        MethodType mt = MethodType.methodType(void.class);
        original = MethodHandles.publicLookup()
                .findStatic(THIS_CLASS, "retVoid", mt);
    } else {
        Object rValue = Helper.castToWrapper(1, rType);
        original = MethodHandles.constant(rType, rValue);
    }
    original = Helper.addTrailingArgs(original, arity, mType.parameterList());
    MethodHandle target = MethodHandles
                .explicitCastArguments(original, mTypeNew);
    Object[] parList = Helper.randomArgs(mTypeNew.parameterList());
    for (int i = 0; i < parList.length; i++) {
        if (parList[i] instanceof String) {
            parList[i] = null; //getting rid of Stings produced by randomArgs
        }
    }
    target.invokeWithArguments(parList);
    checkForWrongMethodTypeException(original, mTypeNewMinus);
    checkForWrongMethodTypeException(original, mTypeNewPlus);
}
 
Example 13
Source File: MethodHandlesProxiesTest.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test
    public static void testOverriddenDefaultMethods() throws Throwable {
    MethodHandle target = MethodHandles.constant(String.class, "F");
    Override proxy = MethodHandleProxies.asInterfaceInstance(Override.class, target);

    assertEquals(proxy.a(), "OA");
    assertEquals(proxy.b(), "OB");
    assertEquals(proxy.c(), "OC");
}
 
Example 14
Source File: ExplicitCastArgumentsTest.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Tests that MHs.eCA method works correctly with MHs with multiple arguments.
 * @throws Throwable
 */
public static void testMultipleArgs() throws Throwable {
    int arity = 1 + RNG.nextInt(Helper.MAX_ARITY / 2 - 2);
    int arityMinus = RNG.nextInt(arity);
    int arityPlus = arity + RNG.nextInt(Helper.MAX_ARITY / 2 - arity) + 1;
    MethodType mType = Helper.randomMethodTypeGenerator(arity);
    MethodType mTypeNew = Helper.randomMethodTypeGenerator(arity);
    MethodType mTypeNewMinus = Helper.randomMethodTypeGenerator(arityMinus);
    MethodType mTypeNewPlus = Helper.randomMethodTypeGenerator(arityPlus);
    Class<?> rType = mType.returnType();
    MethodHandle original;
    if (rType.equals(void.class)) {
        MethodType mt = MethodType.methodType(void.class);
        original = MethodHandles.publicLookup()
                .findStatic(THIS_CLASS, "retVoid", mt);
    } else {
        Object rValue = Helper.castToWrapper(1, rType);
        original = MethodHandles.constant(rType, rValue);
    }
    original = Helper.addTrailingArgs(original, arity, mType.parameterList());
    MethodHandle target = MethodHandles
                .explicitCastArguments(original, mTypeNew);
    Object[] parList = Helper.randomArgs(mTypeNew.parameterList());
    for (int i = 0; i < parList.length; i++) {
        if (parList[i] instanceof String) {
            parList[i] = null; //getting rid of Stings produced by randomArgs
        }
    }
    target.invokeWithArguments(parList);
    checkForWrongMethodTypeException(original, mTypeNewMinus);
    checkForWrongMethodTypeException(original, mTypeNewPlus);
}
 
Example 15
Source File: MethodHandleFactory.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public MethodHandle constant(final Class<?> type, final Object value) {
    final MethodHandle mh = MethodHandles.constant(type, value);
    return debug(mh, "constant", type, value);
}
 
Example 16
Source File: MethodHandleFactory.java    From nashorn with GNU General Public License v2.0 4 votes vote down vote up
@Override
public MethodHandle constant(final Class<?> type, final Object value) {
    return MethodHandles.constant(type, value);
}
 
Example 17
Source File: MethodHandleFactory.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
@Override
public MethodHandle constant(final Class<?> type, final Object value) {
    final MethodHandle mh = MethodHandles.constant(type, value);
    return debug(mh, "constant", type, value);
}
 
Example 18
Source File: MethodHandleFactory.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
@Override
public MethodHandle constant(final Class<?> type, final Object value) {
    final MethodHandle mh = MethodHandles.constant(type, value);
    return debug(mh, "constant", type, value);
}
 
Example 19
Source File: MethodHandleFactory.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public MethodHandle constant(final Class<?> type, final Object value) {
    return MethodHandles.constant(type, value);
}
 
Example 20
Source File: LoopCombinatorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@DataProvider
static Object[][] nullArgs() {
    MethodHandle c = MethodHandles.constant(int.class, 1);
    return new Object[][]{{null, c}, {c, null}};
}