当前位置: 首页>>代码示例>>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;未经允许,请勿转载。