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


Java MethodHandle.invoke方法代碼示例

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


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

示例1: testReturnFromArg

import java.lang.invoke.MethodHandle; //導入方法依賴的package包/類
@Test
public void testReturnFromArg() throws Throwable {
    MethodHandles.Lookup l = MethodHandles.lookup();

    MethodHandle consumeIdentity = dropArguments(
            identity(String.class), 1, int.class, int.class);
    MethodHandle consumeVoid = l.findStatic(
            PermuteArgsReturnVoidTest.class, "consumeVoid",
            MethodType.methodType(void.class, String.class, int.class, int.class));

    MethodHandle f = MethodHandles.foldArguments(consumeIdentity, consumeVoid);

    MethodHandle p = MethodHandles.permuteArguments(f, MethodType.methodType(String.class, String.class, int.class, int.class), 0, 2, 1);

    String s = (String) p.invoke("IN", 0, 0);
    Assert.assertEquals(s.getClass(), String.class);
    Assert.assertEquals(s, "IN");
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:19,代碼來源:PermuteArgsReturnVoidTest.java

示例2: init

import java.lang.invoke.MethodHandle; //導入方法依賴的package包/類
public synchronized boolean init() {
    try {

        Class<?> vmIdentifierClass = Tools.loadJDKToolClass(VM_IDENTIFIER_CLASS_NAME);
        Class<?> monitoredHostClass = Tools.loadJDKToolClass(MONITORED_HOST_CLASS_NAME);
        Class<?> monitordVmClass = Tools.loadJDKToolClass(MONITORED_VM_CLASS_NAME);
        Class<?> monitorClass = Tools.loadJDKToolClass(MONITOR_CLASS_NAME);


        Constructor constructor = vmIdentifierClass.getDeclaredConstructor(String.class);
        constructor.setAccessible(true);
        Object vmIdentifier = constructor.newInstance(Tools.currentPID());
        MethodHandle getMonitoredHostMH = MethodHandles.lookup().findStatic(monitoredHostClass, "getMonitoredHost",
                MethodType.methodType(monitoredHostClass, vmIdentifierClass) );
        Object monitoredHost = getMonitoredHostMH.invoke(vmIdentifier);

        MethodHandle getMonitoredVMMH = MethodHandles.lookup().findVirtual(monitoredHostClass,"getMonitoredVm",
                MethodType.methodType(monitordVmClass, vmIdentifierClass));

        detachMH = MethodHandles.lookup().findVirtual(monitordVmClass,"detach",MethodType.methodType(void.class));
        findByNameMH = MethodHandles.lookup().findVirtual(monitordVmClass,"findByName",
                MethodType.methodType(monitorClass,String.class));
        getValueMH = MethodHandles.lookup().findVirtual(monitorClass,"getValue",MethodType.methodType(Object.class));
        VM = getMonitoredVMMH.invoke(monitoredHost,vmIdentifier);


    }catch (Throwable e){
        logger.warn("attach failed!",e);
        return false;
    }
    return true;
}
 
開發者ID:ctripcorp,項目名稱:cornerstone,代碼行數:33,代碼來源:VMMonitor.java

示例3: testLoopVoid1

import java.lang.invoke.MethodHandle; //導入方法依賴的package包/類
@Test
public static void testLoopVoid1() throws Throwable {
    // construct a post-checked loop that only does one iteration and has a void body and void local state
    MethodHandle loop = MethodHandles.loop(new MethodHandle[]{Empty.MH_f, Empty.MH_f, Empty.MH_pred, null});
    assertEquals(MethodType.methodType(void.class), loop.type());
    loop.invoke();
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:8,代碼來源:LoopCombinatorTest.java

示例4: testCountedLoopEmpty

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

示例5: testCountedPrintingLoop

import java.lang.invoke.MethodHandle; //導入方法依賴的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: convert

import java.lang.invoke.MethodHandle; //導入方法依賴的package包/類
/**
 * Convert the given object to the given type.
 *
 * @param obj object to be converted
 * @param type destination type to convert to
 * @return converted object
 */
public static Object convert(final Object obj, final Object type) {
    if (obj == null) {
        return null;
    }

    final Class<?> clazz;
    if (type instanceof Class) {
        clazz = (Class<?>)type;
    } else if (type instanceof StaticClass) {
        clazz = ((StaticClass)type).getRepresentedClass();
    } else {
        throw new IllegalArgumentException("type expected");
    }

    final LinkerServices linker = Bootstrap.getLinkerServices();
    final Object objToConvert = unwrap(obj);
    final MethodHandle converter = linker.getTypeConverter(objToConvert.getClass(),  clazz);
    if (converter == null) {
        // no supported conversion!
        throw new UnsupportedOperationException("conversion not supported");
    }

    try {
        return converter.invoke(objToConvert);
    } catch (final RuntimeException | Error e) {
        throw e;
    } catch (final Throwable t) {
        throw new RuntimeException(t);
    }
}
 
開發者ID:SunburstApps,項目名稱:OpenJSharp,代碼行數:38,代碼來源:ScriptUtils.java

示例7: testInstanceOfCallerSubclass

import java.lang.invoke.MethodHandle; //導入方法依賴的package包/類
@Test
public void testInstanceOfCallerSubclass() throws Throwable {
    MethodHandle bound = lookup().bind(new B() , "m1", MethodType.methodType(String.class));
    // MethodHandle bound = lookup().findVirtual(B.class,  "m1", MethodType.methodType(String.class)).bindTo(new B());
    String x = (String)bound.invoke();
    assertEquals(x, "B");
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:8,代碼來源:TestMethodHandleBind.java

示例8: testMethodHandle

import java.lang.invoke.MethodHandle; //導入方法依賴的package包/類
static void testMethodHandle() {
    MethodHandles.Lookup lookup = MethodHandles.lookup();
    try {
        MethodHandle handle = lookup.findStatic(DumpStackTest.class, "methodHandle",
                                                MethodType.methodType(void.class));
        handle.invoke();
    } catch(Throwable t) {
        throw new RuntimeException(t);
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:11,代碼來源:DumpStackTest.java

示例9: testIteratePrint

import java.lang.invoke.MethodHandle; //導入方法依賴的package包/類
@Test(dataProvider = "iteratorInits")
public static void testIteratePrint(MethodHandle iterator) throws Throwable {
    MethodHandle body = Iterate.MH_printStep;
    MethodType expectedType = Iterate.MT_print;
    int barity = body.type().parameterCount();
    Class<?> iteratorSource = iterator == null ? null : iterator.type().parameterType(0);
    if (iterator != null && iteratorSource != body.type().parameterType(barity-1)) {
        // adjust body to accept the other type
        body = body.asType(body.type().changeParameterType(barity-1, iteratorSource));
        expectedType = expectedType.changeParameterType(0, iteratorSource);
    }
    MethodHandle loop = MethodHandles.iteratedLoop(iterator, null, body);
    assertEquals(expectedType, loop.type());
    loop.invoke(Arrays.asList("hello", "world"));
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:16,代碼來源:LoopCombinatorTest.java

示例10: testInstanceOfReceiverClass

import java.lang.invoke.MethodHandle; //導入方法依賴的package包/類
@Test
public void testInstanceOfReceiverClass() throws Throwable {
    try {
        MethodHandle bound = lookup().bind(new pkg.A() , "m1", MethodType.methodType(String.class));
        bound.invoke();
        fail("IllegalAccessException expected");
    } catch (IllegalAccessException e) {
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:10,代碼來源:TestMethodHandleBind.java

示例11: testCountedRangeLoopEmpty

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

示例12: testCountedArrayLoop

import java.lang.invoke.MethodHandle; //導入方法依賴的package包/類
@Test
public static void testCountedArrayLoop() throws Throwable {
    // int[] a = new int[]{0}; for (int i = 0; i < 13; ++i) { ++a[0]; } => a[0] == 13
    MethodHandle fit13 = MethodHandles.dropArguments(MethodHandles.constant(int.class, 13), 0, int[].class);
    MethodHandle loop = MethodHandles.countedLoop(fit13, null, Counted.MH_stepUpdateArray);
    assertEquals(Counted.MT_arrayCounted, loop.type());
    int[] a = new int[]{0};
    loop.invoke(a);
    assertEquals(13, a[0]);
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:11,代碼來源:LoopCombinatorTest.java

示例13: testDoWhileVoidInit

import java.lang.invoke.MethodHandle; //導入方法依賴的package包/類
@Test
public static void testDoWhileVoidInit() throws Throwable {
    While w = new While();
    int v = 5;
    MethodHandle loop = MethodHandles.doWhileLoop(While.MH_voidInit.bindTo(w), While.MH_voidBody.bindTo(w),
            While.MH_voidPred.bindTo(w));
    assertEquals(While.MT_void, loop.type());
    loop.invoke(v);
    assertEquals(v, w.i);
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:11,代碼來源:LoopCombinatorTest.java

示例14: testConstant

import java.lang.invoke.MethodHandle; //導入方法依賴的package包/類
@Test
public void testConstant() throws Throwable {
    MethodHandle mh = (MethodHandle)t3.getDeclaredMethod("getMethodHandle").invoke(null);
    int result = (int)mh.invoke(t3.newInstance());
    assertEquals(result, 1); // T1.m should be invoked.
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:7,代碼來源:SpecialStatic.java

示例15: passesGuard

import java.lang.invoke.MethodHandle; //導入方法依賴的package包/類
private static boolean passesGuard(final Object obj, final MethodHandle guard) throws Throwable {
    return guard == null || (boolean)guard.invoke(obj);
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:4,代碼來源:NativeObject.java


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