当前位置: 首页>>代码示例>>Java>>正文


Java MethodHandles.Lookup方法代码示例

本文整理汇总了Java中java.lang.invoke.MethodHandles.Lookup方法的典型用法代码示例。如果您正苦于以下问题:Java MethodHandles.Lookup方法的具体用法?Java MethodHandles.Lookup怎么用?Java MethodHandles.Lookup使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.lang.invoke.MethodHandles的用法示例。


在下文中一共展示了MethodHandles.Lookup方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: testReturnFromArg

import java.lang.invoke.MethodHandles; //导入方法依赖的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: bootstrapInternal

import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
private static CallSite bootstrapInternal(final MethodHandles.Lookup caller, final String name, final MethodType type) {
    return dynamicLinker.link(new MonomorphicCallSite(CallSiteDescriptorFactory.create(caller, name, type)));
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:4,代码来源:DefaultBootstrapper.java

示例3: findSpecial

import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
@Override
public MethodHandle findSpecial(final MethodHandles.Lookup explicitLookup, final Class<?> clazz, final String name, final MethodType type, final Class<?> thisClass) {
    try {
        final MethodHandle mh = explicitLookup.findSpecial(clazz, name, type, thisClass);
        return debug(mh, "findSpecial", explicitLookup, clazz, name, type);
    } catch (final NoSuchMethodException | IllegalAccessException e) {
        throw new LookupException(e);
    }
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:10,代码来源:MethodHandleFactory.java

示例4: staticSetter

import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
@Override
public MethodHandle staticSetter(final MethodHandles.Lookup explicitLookup, final Class<?> clazz, final String name, final Class<?> type) {
    try {
        final MethodHandle mh = explicitLookup.findStaticSetter(clazz, name, type);
        return debug(mh, "static setter", explicitLookup, clazz, name, type);
    } catch (final NoSuchFieldException | IllegalAccessException e) {
        throw new LookupException(e);
    }
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:10,代码来源:MethodHandleFactory.java

示例5: main

import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
public static void main(String[] args) throws Throwable {
    Test8015436 testObj = new Test8015436();
    testObj.someMethod();
    testObj.defaultMethod(DEFAULT_MTD_INVOKED_DIRECTLY);

    MethodHandles.Lookup lookup = MethodHandles.lookup();
    MethodType   mt = MethodType.methodType(void.class, String.class);
    MethodHandle mh = lookup.findVirtual(Test8015436.class, "defaultMethod", mt);
    mh.invokeExact(testObj, DEFAULT_MTD_INVOKED_MH);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:11,代码来源:Test8015436.java

示例6: findVirtual

import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
@Override
public MethodHandle findVirtual(final MethodHandles.Lookup explicitLookup, final Class<?> clazz, final String name, final MethodType type) {
    try {
        final MethodHandle mh = explicitLookup.findVirtual(clazz, name, type);
        return debug(mh, "findVirtual", explicitLookup, clazz, name, type);
    } catch (final NoSuchMethodException | IllegalAccessException e) {
        throw new LookupException(e);
    }
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:10,代码来源:MethodHandleFactory.java

示例7: callMethodHandleRefl

import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
void callMethodHandleRefl() {
    MethodHandles.Lookup lookup = MethodHandles.publicLookup();
    try {
        MethodHandle mh = lookup.findStatic(GetCallerClassTest.class,
                                            "reflectiveGetCallerClass",
                                            methodType);
        mh.invokeExact(walker, ReflectionTest.class, expectUOE);
    } catch (Throwable e) {
        throw new RuntimeException(e);
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:12,代码来源:GetCallerClassTest.java

示例8: testFindSpecial

import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
@Test
public void testFindSpecial() throws Throwable {
    MethodHandles.Lookup lookup = (MethodHandles.Lookup)t3.getDeclaredMethod("getLookup").invoke(null);
    MethodHandle mh = lookup.findSpecial(t1, "m", MethodType.methodType(int.class), t3);
    int result = (int)mh.invoke(t3.newInstance());
    assertEquals(result, 1); // T1.m should be invoked.
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:8,代码来源:SpecialStatic.java

示例9: testInvokePolymorphicRange

import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
public void testInvokePolymorphicRange() {
  MethodType mt = MethodType.methodType(String.class, byte.class, char.class, short.class,
      float.class, double.class, long.class, Integer.class, int.class, String.class);
  MethodHandles.Lookup lk = MethodHandles.lookup();

  try {
    MethodHandle mh = lk.findVirtual(getClass(), "buildString", mt);
    System.out.println(
        mh.invoke(this, (byte) 2, 'a', (short) 0xFFFF, 1.1f, 2.24d, 12345678L, null,
            1, "string"));
  } catch (Throwable t) {
    t.printStackTrace();
  }
}
 
开发者ID:inferjay,项目名称:r8,代码行数:15,代码来源:InvokePolymorphic.java

示例10: getArrayConverter

import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
/**
 * Returns a guarded invocation that converts from a source type that is NativeArray to a Java array or List or
 * Queue or Deque or Collection type.
 * @param sourceType the source type (presumably NativeArray a superclass of it)
 * @param targetType the target type (presumably an array type, or List or Queue, or Deque, or Collection)
 * @return a guarded invocation that converts from the source type to the target type. null is returned if
 * either the source type is neither NativeArray, nor a superclass of it, or if the target type is not an array
 * type, List, Queue, Deque, or Collection.
 */
private static GuardedInvocation getArrayConverter(final Class<?> sourceType, final Class<?> targetType, final Supplier<MethodHandles.Lookup> lookupSupplier) {
    final boolean isSourceTypeNativeArray = sourceType == NativeArray.class;
    // If source type is more generic than NativeArray class, we'll need to use a guard
    final boolean isSourceTypeGeneric = !isSourceTypeNativeArray && sourceType.isAssignableFrom(NativeArray.class);

    if (isSourceTypeNativeArray || isSourceTypeGeneric) {
        final MethodHandle guard = isSourceTypeGeneric ? IS_NATIVE_ARRAY : null;
        if(targetType.isArray()) {
            final MethodHandle mh = ARRAY_CONVERTERS.get(targetType);
            final MethodHandle mhWithLookup;
            if (mh.type().parameterCount() == 2) {
                assert mh.type().parameterType(1) == SecureLookupSupplier.class;
                // We enter this branch when the array's ultimate component
                // type is a SAM type; we use a handle to JSType.toJavaArrayWithLookup
                // for these in the converter MH and must bind it here with
                // a secure supplier for the current lookup. By retrieving
                // the lookup, we'll also (correctly) inform the type
                // converter that this array converter is lookup specific.
                // We then need to wrap the returned lookup into a
                // new SecureLookupSupplier in order to bind it to the
                // JSType.toJavaArrayWithLookup() parameter.
                mhWithLookup = MH.insertArguments(mh, 1,
                        new SecureLookupSupplier(getCurrentLookup(lookupSupplier)));
            } else {
                mhWithLookup = mh;
            }
            return new GuardedInvocation(mhWithLookup, guard);
        } else if(targetType == List.class) {
            return new GuardedInvocation(TO_LIST, guard);
        } else if(targetType == Deque.class) {
            return new GuardedInvocation(TO_DEQUE, guard);
        } else if(targetType == Queue.class) {
            return new GuardedInvocation(TO_QUEUE, guard);
        } else if(targetType == Collection.class) {
            return new GuardedInvocation(TO_COLLECTION, guard);
        }
    }
    return null;
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:49,代码来源:NashornLinker.java

示例11: WhiteBox

import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
WhiteBox() throws ReflectiveOperationException {
    Class<?> qClass = ConcurrentLinkedQueue.class;
    Class<?> nodeClass = Class.forName(qClass.getName() + "$Node");
    MethodHandles.Lookup lookup
        = MethodHandles.privateLookupIn(qClass, MethodHandles.lookup());
    HEAD = lookup.findVarHandle(qClass, "head", nodeClass);
    TAIL = lookup.findVarHandle(qClass, "tail", nodeClass);
    NEXT = lookup.findVarHandle(nodeClass, "next", nodeClass);
    ITEM = lookup.findVarHandle(nodeClass, "item", Object.class);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:11,代码来源:WhiteBox.java

示例12: run

import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
static void run(StackWalker walker) {
    MethodHandles.Lookup lookup = MethodHandles.lookup();
    MethodHandle handle = null;
    try {
        handle = lookup.findStatic(C2.class, "call",
                MethodType.methodType(void.class, StackWalker.class));
        handle.invoke(walker);
    } catch(Throwable t) {
        throw new RuntimeException(t);
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:12,代码来源:EmbeddedStackWalkTest.java

示例13: convertToType

import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
@Override
public GuardedInvocation convertToType(final Class<?> sourceType, final Class<?> targetType, final Supplier<MethodHandles.Lookup> lookupSupplier) throws Exception {
    final GuardedInvocation gi = convertToTypeNoCast(sourceType, targetType);
    return gi == null ? null : gi.asType(MH.type(targetType, sourceType));
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:6,代码来源:NashornBottomLinker.java

示例14: testSetter

import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
public void testSetter(boolean positive, MethodHandles.Lookup lookup,
                       Object fieldRef, Object value, int testMode) throws Throwable {
    testAccessor(positive, lookup, fieldRef, value, testMode | TEST_SETTER);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:5,代码来源:MethodHandlesTest.java

示例15: registerAnnotated

import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
/**
 * Register the annotated methods in a given event handler object using a different object as the handler owner
 *
 * @param registeredHandler - the object to search for handler methods
 * @param eventHandler      - the object to act as the handler owner
 *
 * @see com.sergey.spacegame.common.event.EventHandle
 */
public void registerAnnotated(Object registeredHandler, Object eventHandler) {
    if (handlers.containsKey(eventHandler)) {
        throw new IllegalStateException("This handler has already been registered.");
    }
    
    Method[]                           methods     = eventHandler.getClass().getMethods();
    HashMap<Class, List<EventHandler>> thisHandler = new HashMap<>();
    
    for (Method method : methods) {
        EventHandle handleAnn = method.getAnnotation(EventHandle.class);
        if (handleAnn != null) {
            //Find methods with @EventHandle annotations
            
            //Make sure the parameter type is correct
            if (method.getParameterCount() != 1 ||
                method.getParameters()[0].getType().isAssignableFrom(Event.class)) {
                throw new IllegalStateException("Methods annotated with @EventHandle must only have 1 parameter that extends Event.");
            }
            
            //parameter extends Event.class
            Class parameter = method.getParameters()[0].getType();
            
            if (!handles.containsKey(parameter)) {
                handles.put(parameter, getNewList());
            }
            if (!thisHandler.containsKey(parameter)) {
                thisHandler.put(parameter, getNewList());
            }
            
            try {
                //Construct the lambda
                MethodHandles.Lookup lookup       = MethodHandles.lookup();
                MethodHandle         methodHandle = lookup.unreflect(method);
                MethodType invokedType = MethodType.methodType(EventHandler.class, eventHandler
                        .getClass());//, handler.getClass()
                MethodType samType                = MethodType.methodType(void.class, Event.class);
                MethodType instantiatedMethodType = MethodType.methodType(void.class, parameter);
                EventHandler lambda = (EventHandler) LambdaMetafactory.metafactory(lookup, "accept", invokedType, samType, methodHandle, instantiatedMethodType)
                        .getTarget()
                        .invoke(eventHandler);
                
                //Add it to the handles
                handles.get(parameter).add(lambda);
                thisHandler.get(parameter).add(lambda);
            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }
        }
    }
    
    //Register the handler owner
    if (!thisHandler.isEmpty()) {
        if (handlers.containsKey(registeredHandler)) {
            handlers.get(registeredHandler).putAll(thisHandler);
        } else {
            handlers.put(registeredHandler, thisHandler);
        }
    }
}
 
开发者ID:SergeySave,项目名称:SpaceGame,代码行数:68,代码来源:EventBus.java


注:本文中的java.lang.invoke.MethodHandles.Lookup方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。