當前位置: 首頁>>代碼示例>>Java>>正文


Java MethodHandles.constant方法代碼示例

本文整理匯總了Java中java.lang.invoke.MethodHandles.constant方法的典型用法代碼示例。如果您正苦於以下問題:Java MethodHandles.constant方法的具體用法?Java MethodHandles.constant怎麽用?Java MethodHandles.constant使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.lang.invoke.MethodHandles的用法示例。


在下文中一共展示了MethodHandles.constant方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: negativeTestData

import java.lang.invoke.MethodHandles; //導入方法依賴的package包/類
@DataProvider
static Object[][] negativeTestData() {
    MethodHandle intid = MethodHandles.identity(int.class);
    MethodHandle intco = MethodHandles.constant(int.class, 0);
    MethodHandle errTarget = MethodHandles.dropArguments(intco, 0, int.class, double.class, String.class, int.class);
    MethodHandle errCleanup = MethodHandles.dropArguments(MethodHandles.constant(int.class, 0), 0, Throwable.class,
            int.class, double.class, Object.class);
    MethodHandle voidTarget = TryFinally.MH_voidTarget;
    MethodHandle voidICleanup = MethodHandles.dropArguments(TryFinally.MH_voidCleanup, 1, int.class);
    return new Object[][]{
            {intid, MethodHandles.identity(double.class),
                    "target and return types must match: double != int"},
            {intid, MethodHandles.dropArguments(intid, 0, String.class),
                    "cleanup first argument and Throwable must match: (String,int)int != class java.lang.Throwable"},
            {intid, MethodHandles.dropArguments(intid, 0, Throwable.class, double.class),
                    "cleanup second argument and target return type must match: (Throwable,double,int)int != int"},
            {errTarget, errCleanup,
                    "cleanup parameters after (Throwable,result) and target parameter list prefix must match: " +
                            errCleanup.type() + " != " + errTarget.type()},
            {voidTarget, voidICleanup,
                    "cleanup parameters after (Throwable,result) and target parameter list prefix must match: " +
                            voidICleanup.type() + " != " + voidTarget.type()}
    };
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:25,代碼來源:TryFinallyTest.java

示例2: testCountedLoopVoidInit

import java.lang.invoke.MethodHandles; //導入方法依賴的package包/類
@Test
public static void testCountedLoopVoidInit() throws Throwable {
    MethodHandle fit5 = MethodHandles.constant(int.class, 5);
    for (int i = 0; i < 8; i++) {
        MethodHandle zero = MethodHandles.zero(void.class);
        MethodHandle init = fit5;
        MethodHandle body = Counted.MH_printHello;
        boolean useNull = (i & 1) != 0, addInitArg = (i & 2) != 0, addBodyArg = (i & 4) != 0;
        if (useNull)    zero = null;
        if (addInitArg) init = MethodHandles.dropArguments(init, 0, int.class);
        if (addBodyArg) body = MethodHandles.dropArguments(body, 1, int.class);
        System.out.println("testCountedLoopVoidInit i="+i+" : "+Arrays.asList(init, zero, body));
        MethodHandle loop = MethodHandles.countedLoop(init, zero, body);
        MethodType expectedType = Counted.MT_countedPrinting;
        if (addInitArg || addBodyArg)
            expectedType = expectedType.insertParameterTypes(0, int.class);
        assertEquals(expectedType, loop.type());
        if (addInitArg || addBodyArg)
            loop.invoke(99);
        else
            loop.invoke();
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:24,代碼來源:LoopCombinatorTest.java

示例3: countedLoopNegativeData

import java.lang.invoke.MethodHandles; //導入方法依賴的package包/類
@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))}
    };
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:20,代碼來源:LoopCombinatorTest.java

示例4: testMultipleArgs

import java.lang.invoke.MethodHandles; //導入方法依賴的package包/類
/**
 * 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);
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:36,代碼來源:ExplicitCastArgumentsTest.java

示例5: testCountedPrintingLoop

import java.lang.invoke.MethodHandles; //導入方法依賴的package包/類
@Test
public static void testCountedPrintingLoop() throws Throwable {
    MethodHandle fit5 = MethodHandles.constant(int.class, 5);
    MethodHandle loop = MethodHandles.countedLoop(fit5, null, Counted.MH_printHello);
    assertEquals(Counted.MT_countedPrinting, loop.type());
    loop.invoke();
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:8,代碼來源:LoopCombinatorTest.java

示例6: testCountedLoopNullBody

import java.lang.invoke.MethodHandles; //導入方法依賴的package包/類
@Test(expectedExceptions = NullPointerException.class)
public static void testCountedLoopNullBody() throws Throwable {
    MethodHandle h5 = MethodHandles.constant(int.class, 5);
    MethodHandle h13 = MethodHandles.constant(int.class, 13);
    MethodHandle loop = MethodHandles.countedLoop(h5, h13, null);
    assertEquals(methodType(int.class), loop.type());
    assertEquals(13, loop.invoke());
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:9,代碼來源:LoopCombinatorTest.java

示例7: testCountedLoopCounterInit

import java.lang.invoke.MethodHandles; //導入方法依賴的package包/類
@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());
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:12,代碼來源:LoopCombinatorTest.java

示例8: testShortZeroConversion

import java.lang.invoke.MethodHandles; //導入方法依賴的package包/類
@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());
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:13,代碼來源:WrapperTest.java

示例9: testIntZeroConversion

import java.lang.invoke.MethodHandles; //導入方法依賴的package包/類
@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());
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:13,代碼來源:WrapperTest.java

示例10: testLongZeroConversion

import java.lang.invoke.MethodHandles; //導入方法依賴的package包/類
@Test
public void testLongZeroConversion() throws Throwable {
    MethodHandle h1 = MethodHandles.constant(Long.class, 42L);
    MethodHandle h2 = h1.asType(MethodType.methodType(void.class));  // drop 42
    MethodHandle h3 = h2.asType(MethodType.methodType(long.class));  // add 0
    MethodHandle h4 = h3.asType(MethodType.methodType(Object.class));  // box

    Object x = h4.invokeExact();
    assertEquals(x, 0L);
    assertTrue(x == Long.valueOf(0));
    assertTrue(x == Wrapper.LONG.zero());
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:13,代碼來源:WrapperTest.java

示例11: testByteZeroConversion

import java.lang.invoke.MethodHandles; //導入方法依賴的package包/類
@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());
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:13,代碼來源:WrapperTest.java

示例12: testCharacterZeroConversion

import java.lang.invoke.MethodHandles; //導入方法依賴的package包/類
@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());
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:13,代碼來源:WrapperTest.java

示例13: constant

import java.lang.invoke.MethodHandles; //導入方法依賴的package包/類
@Override
public MethodHandle constant(final Class<?> type, final Object value) {
    final MethodHandle mh = MethodHandles.constant(type, value);
    return debug(mh, "constant", type, value);
}
 
開發者ID:SunburstApps,項目名稱:OpenJSharp,代碼行數:6,代碼來源:MethodHandleFactory.java

示例14: negativeTestData

import java.lang.invoke.MethodHandles; //導入方法依賴的package包/類
@DataProvider
static Object[][] negativeTestData() {
    MethodHandle i0 = MethodHandles.constant(int.class, 0);
    MethodHandle ii = MethodHandles.dropArguments(i0, 0, int.class, int.class);
    MethodHandle id = MethodHandles.dropArguments(i0, 0, int.class, double.class);
    MethodHandle i3 = MethodHandles.dropArguments(i0, 0, int.class, int.class, int.class);
    List<MethodHandle> inits = Arrays.asList(ii, id, i3);
    List<Class<?>> ints3 = Arrays.asList(int.class, int.class, int.class);
    List<Class<?>> ints4 = Arrays.asList(int.class, int.class, int.class, int.class);
    List<MethodHandle> finis = Arrays.asList(Fac.MH_fin, Fac.MH_inc, Counted.MH_step);
    List<MethodHandle> preds1 = Arrays.asList(null, null, null);
    List<MethodHandle> preds2 = Arrays.asList(null, Fac.MH_fin, null);
    MethodHandle eek = MethodHandles.dropArguments(i0, 0, int.class, int.class, double.class);
    List<MethodHandle> nesteps = Arrays.asList(Fac.MH_inc, eek, Fac.MH_dot);
    List<MethodHandle> nepreds = Arrays.asList(null, Fac.MH_pred, null);
    List<MethodHandle> nefinis = Arrays.asList(null, Fac.MH_fin, null);
    List<MethodHandle> lvsteps = Arrays.asList(LoopWithVirtuals.MH_inc, LoopWithVirtuals.MH_mult);
    List<MethodHandle> lvpreds = Arrays.asList(null, LoopWithVirtuals.MH_pred);
    List<MethodHandle> lvfinis = Arrays.asList(null, LoopWithVirtuals.MH_fin);
    return new Object[][] {
            {null, "null or no clauses passed"},
            {new MethodHandle[][]{}, "null or no clauses passed"},
            {new MethodHandle[][]{{null, Fac.MH_inc}, {Fac.MH_one, null, Fac.MH_mult, Fac.MH_pred, Fac.MH_fin}},
                    "All loop clauses must be represented as MethodHandle arrays with at most 4 elements."},
            {new MethodHandle[][]{{null, Fac.MH_inc}, null}, "null clauses are not allowed"},
            {new MethodHandle[][]{{Fac.MH_zero, Fac.MH_dot}},
                    "clause 0: init and step return types must match: int != void"},
            {new MethodHandle[][]{{ii}, {id}, {i3}},
                    "found non-effectively identical init parameter type lists: " + inits +
                            " (common suffix: " + ints3 + ")"},
            {new MethodHandle[][]{{null, Fac.MH_inc, null, Fac.MH_fin}, {null, Fac.MH_inc, null, Fac.MH_inc},
                    {null, Counted.MH_start, null, Counted.MH_step}},
                    "found non-identical finalizer return types: " + finis + " (return type: int)"},
            {new MethodHandle[][]{{Fac.MH_zero, Fac.MH_inc}, {Fac.MH_one, Fac.MH_mult, null, Fac.MH_fin},
                    {null, Fac.MH_dot}}, "no predicate found: " + preds1},
            {new MethodHandle[][]{{Fac.MH_zero, Fac.MH_inc}, {Fac.MH_one, Fac.MH_mult, Fac.MH_fin, Fac.MH_fin},
                    {null, Fac.MH_dot}}, "predicates must have boolean return type: " + preds2},
            {new MethodHandle[][]{{Fac.MH_zero, Fac.MH_inc}, {Fac.MH_one, eek, Fac.MH_pred, Fac.MH_fin},
                    {null, Fac.MH_dot}},
                    "found non-effectively identical parameter type lists:\nstep: " + nesteps +
                            "\npred: " + nepreds + "\nfini: " + nefinis + " (common parameter sequence: " + ints3 + ")"},
            {new MethodHandle[][]{{null, LoopWithVirtuals.MH_inc},
                    {LoopWithVirtuals.MH_one, LoopWithVirtuals.MH_mult, LoopWithVirtuals.MH_pred, LoopWithVirtuals.MH_fin}},
                    "found non-effectively identical parameter type lists:\nstep: " + lvsteps +
                            "\npred: " + lvpreds + "\nfini: " + lvfinis + " (common parameter sequence: " + ints4 + ")"}
    };
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:48,代碼來源:LoopCombinatorTest.java

示例15: nullArgs

import java.lang.invoke.MethodHandles; //導入方法依賴的package包/類
@DataProvider
static Object[][] nullArgs() {
    MethodHandle c = MethodHandles.constant(int.class, 1);
    return new Object[][]{{null, c}, {c, null}};
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:6,代碼來源:LoopCombinatorTest.java


注:本文中的java.lang.invoke.MethodHandles.constant方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。