當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。