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


Java Lookup.unreflectSpecial方法代碼示例

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


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

示例1: testUnreflectMaybeSpecial

import java.lang.invoke.MethodHandles.Lookup; //導入方法依賴的package包/類
void testUnreflectMaybeSpecial(Class<?> specialCaller,
                               boolean positive, Lookup lookup,
                               Class<?> defc, Class<?> rcvc, Class<?> ret, String name, Class<?>... params) throws Throwable {
    countTest(positive);
    String methodName = name.substring(1 + name.indexOf('/'));  // foo/bar => foo
    MethodType type = MethodType.methodType(ret, params);
    Lookup specialLookup = (specialCaller != null ? maybeMoveIn(lookup, specialCaller) : null);
    boolean specialAccessOK = (specialCaller != null &&
                               specialLookup.lookupClass() == specialCaller &&
                               (specialLookup.lookupModes() & Lookup.PRIVATE) != 0);
    Method rmethod = defc.getDeclaredMethod(methodName, params);
    MethodHandle target = null;
    Exception noAccess = null;
    boolean isStatic = (rcvc == null);
    boolean isSpecial = (specialCaller != null);
    try {
        if (verbosity >= 4)  System.out.println("lookup via "+lookup+" of "+defc+" "+name+type);
        if (isSpecial)
            target = specialLookup.unreflectSpecial(rmethod, specialCaller);
        else
            target = maybeMoveIn(lookup, defc).unreflect(rmethod);
    } catch (ReflectiveOperationException ex) {
        noAccess = ex;
        assertExceptionClass(
            IllegalAccessException.class,  // NSME is impossible, since it was already reflected
            noAccess);
        if (verbosity >= 5)  ex.printStackTrace(System.out);
    }
    if (verbosity >= 3)
        System.out.println("unreflect"+(isSpecial?"Special":"")+" "+defc.getName()+"."+name+"/"+type
                           +(!isSpecial ? "" : " specialCaller="+specialCaller)
                           +( isStatic  ? "" : " receiver="+rcvc)
                           +" => "+target
                           +(noAccess == null ? "" : " !! "+noAccess));
    if (positive && noAccess != null)  throw noAccess;
    assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, target != null);
    if (!positive)  return; // negative test failed as expected
    assertEquals(isStatic, Modifier.isStatic(rmethod.getModifiers()));
    Class<?>[] paramsMaybeWithSelf = params;
    if (!isStatic) {
        paramsMaybeWithSelf = cat(array(Class[].class, (Class)rcvc), params);
    }
    MethodType typeMaybeWithSelf = MethodType.methodType(ret, paramsMaybeWithSelf);
    if (isStatic) {
        assertEquals(typeMaybeWithSelf, target.type());
    } else {
        if (isSpecial)
            assertEquals(specialCaller, target.type().parameterType(0));
        else
            assertEquals(defc, target.type().parameterType(0));
        assertEquals(typeMaybeWithSelf, target.type().changeParameterType(0, rcvc));
    }
    Object[] argsMaybeWithSelf = randomArgs(paramsMaybeWithSelf);
    printCalled(target, name, argsMaybeWithSelf);
    target.invokeWithArguments(argsMaybeWithSelf);
    assertCalled(name, argsMaybeWithSelf);
    if (verbosity >= 1)
        System.out.print(':');
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:60,代碼來源:MethodHandlesTest.java


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