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