java.lang.invoke.MethodHandles.Lookup#findSpecial ( )源码实例Demo

下面列出了java.lang.invoke.MethodHandles.Lookup#findSpecial ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: bazel   文件: LambdaDesugaring.java
/**
 * Produces a {@link MethodHandle} using either the context or {@link #targetLoader} class
 * loader, depending on {@code target}.
 */
private MethodHandle toMethodHandle(Lookup lookup, Handle asmHandle, boolean target)
    throws ReflectiveOperationException {
  Class<?> owner = loadFromInternal(asmHandle.getOwner());
  MethodType signature =
      MethodType.fromMethodDescriptorString(
          asmHandle.getDesc(),
          target ? targetLoader : Thread.currentThread().getContextClassLoader());
  switch (asmHandle.getTag()) {
    case Opcodes.H_INVOKESTATIC:
      return lookup.findStatic(owner, asmHandle.getName(), signature);
    case Opcodes.H_INVOKEVIRTUAL:
    case Opcodes.H_INVOKEINTERFACE:
      return lookup.findVirtual(owner, asmHandle.getName(), signature);
    case Opcodes.H_INVOKESPECIAL: // we end up calling these using invokevirtual
      return lookup.findSpecial(owner, asmHandle.getName(), signature, owner);
    case Opcodes.H_NEWINVOKESPECIAL:
      return lookup.findConstructor(owner, signature);
    default:
      throw new UnsupportedOperationException("Cannot resolve " + asmHandle);
  }
}
 
源代码2 项目: dragonwell8_jdk   文件: MethodHandlesTest.java
void testFindSpecial(boolean positive, Lookup lookup, Class<?> specialCaller,
                     Class<?> defc, 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 = maybeMoveIn(lookup, specialCaller);
    boolean specialAccessOK = (specialLookup.lookupClass() == specialCaller &&
                               (specialLookup.lookupModes() & Lookup.PRIVATE) != 0);
    MethodHandle target = null;
    Exception noAccess = null;
    try {
        if (verbosity >= 4)  System.out.println("lookup via "+lookup+" of "+defc+" "+name+type);
        if (verbosity >= 5)  System.out.println("  lookup => "+specialLookup);
        target = specialLookup.findSpecial(defc, methodName, type, specialCaller);
    } catch (ReflectiveOperationException ex) {
        noAccess = ex;
        assertExceptionClass(
            (!specialAccessOK)  // this check should happen first
            ?   IllegalAccessException.class
            : (name.contains("bogus") || INIT_REF_CAUSES_NSME && name.contains("<init>"))
            ?   NoSuchMethodException.class
            : IllegalAccessException.class,
            noAccess);
        if (verbosity >= 5)  ex.printStackTrace(System.out);
    }
    if (verbosity >= 3)
        System.out.println("findSpecial from "+specialCaller.getName()+" to "+defc.getName()+"."+name+"/"+type+" => "+target
                           +(target == null ? "" : target.type())
                           +(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(specialCaller, target.type().parameterType(0));
    assertEquals(type,          target.type().dropParameterTypes(0,1));
    Class<?>[] paramsWithSelf = cat(array(Class[].class, (Class)specialCaller), params);
    MethodType typeWithSelf = MethodType.methodType(ret, paramsWithSelf);
    assertNameStringContains(target, methodName);
    Object[] args = randomArgs(paramsWithSelf);
    printCalled(target, name, args);
    target.invokeWithArguments(args);
    assertCalled(name, args);
}
 
源代码3 项目: TencentKona-8   文件: MethodHandlesTest.java
void testFindSpecial(boolean positive, Lookup lookup, Class<?> specialCaller,
                     Class<?> defc, 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 = maybeMoveIn(lookup, specialCaller);
    boolean specialAccessOK = (specialLookup.lookupClass() == specialCaller &&
                               (specialLookup.lookupModes() & Lookup.PRIVATE) != 0);
    MethodHandle target = null;
    Exception noAccess = null;
    try {
        if (verbosity >= 4)  System.out.println("lookup via "+lookup+" of "+defc+" "+name+type);
        if (verbosity >= 5)  System.out.println("  lookup => "+specialLookup);
        target = specialLookup.findSpecial(defc, methodName, type, specialCaller);
    } catch (ReflectiveOperationException ex) {
        noAccess = ex;
        assertExceptionClass(
            (!specialAccessOK)  // this check should happen first
            ?   IllegalAccessException.class
            : (name.contains("bogus") || INIT_REF_CAUSES_NSME && name.contains("<init>"))
            ?   NoSuchMethodException.class
            : IllegalAccessException.class,
            noAccess);
        if (verbosity >= 5)  ex.printStackTrace(System.out);
    }
    if (verbosity >= 3)
        System.out.println("findSpecial from "+specialCaller.getName()+" to "+defc.getName()+"."+name+"/"+type+" => "+target
                           +(target == null ? "" : target.type())
                           +(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(specialCaller, target.type().parameterType(0));
    assertEquals(type,          target.type().dropParameterTypes(0,1));
    Class<?>[] paramsWithSelf = cat(array(Class[].class, (Class)specialCaller), params);
    MethodType typeWithSelf = MethodType.methodType(ret, paramsWithSelf);
    assertNameStringContains(target, methodName);
    Object[] args = randomArgs(paramsWithSelf);
    printCalled(target, name, args);
    target.invokeWithArguments(args);
    assertCalled(name, args);
}
 
源代码4 项目: jdk8u60   文件: MethodHandlesTest.java
void testFindSpecial(boolean positive, Lookup lookup, Class<?> specialCaller,
                     Class<?> defc, 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 = maybeMoveIn(lookup, specialCaller);
    boolean specialAccessOK = (specialLookup.lookupClass() == specialCaller &&
                               (specialLookup.lookupModes() & Lookup.PRIVATE) != 0);
    MethodHandle target = null;
    Exception noAccess = null;
    try {
        if (verbosity >= 4)  System.out.println("lookup via "+lookup+" of "+defc+" "+name+type);
        if (verbosity >= 5)  System.out.println("  lookup => "+specialLookup);
        target = specialLookup.findSpecial(defc, methodName, type, specialCaller);
    } catch (ReflectiveOperationException ex) {
        noAccess = ex;
        assertExceptionClass(
            (!specialAccessOK)  // this check should happen first
            ?   IllegalAccessException.class
            : (name.contains("bogus") || INIT_REF_CAUSES_NSME && name.contains("<init>"))
            ?   NoSuchMethodException.class
            : IllegalAccessException.class,
            noAccess);
        if (verbosity >= 5)  ex.printStackTrace(System.out);
    }
    if (verbosity >= 3)
        System.out.println("findSpecial from "+specialCaller.getName()+" to "+defc.getName()+"."+name+"/"+type+" => "+target
                           +(target == null ? "" : target.type())
                           +(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(specialCaller, target.type().parameterType(0));
    assertEquals(type,          target.type().dropParameterTypes(0,1));
    Class<?>[] paramsWithSelf = cat(array(Class[].class, (Class)specialCaller), params);
    MethodType typeWithSelf = MethodType.methodType(ret, paramsWithSelf);
    assertNameStringContains(target, methodName);
    Object[] args = randomArgs(paramsWithSelf);
    printCalled(target, name, args);
    target.invokeWithArguments(args);
    assertCalled(name, args);
}
 
源代码5 项目: openjdk-jdk8u   文件: MethodHandlesTest.java
void testFindSpecial(boolean positive, Lookup lookup, Class<?> specialCaller,
                     Class<?> defc, 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 = maybeMoveIn(lookup, specialCaller);
    boolean specialAccessOK = (specialLookup.lookupClass() == specialCaller &&
                               (specialLookup.lookupModes() & Lookup.PRIVATE) != 0);
    MethodHandle target = null;
    Exception noAccess = null;
    try {
        if (verbosity >= 4)  System.out.println("lookup via "+lookup+" of "+defc+" "+name+type);
        if (verbosity >= 5)  System.out.println("  lookup => "+specialLookup);
        target = specialLookup.findSpecial(defc, methodName, type, specialCaller);
    } catch (ReflectiveOperationException ex) {
        noAccess = ex;
        assertExceptionClass(
            (!specialAccessOK)  // this check should happen first
            ?   IllegalAccessException.class
            : (name.contains("bogus") || INIT_REF_CAUSES_NSME && name.contains("<init>"))
            ?   NoSuchMethodException.class
            : IllegalAccessException.class,
            noAccess);
        if (verbosity >= 5)  ex.printStackTrace(System.out);
    }
    if (verbosity >= 3)
        System.out.println("findSpecial from "+specialCaller.getName()+" to "+defc.getName()+"."+name+"/"+type+" => "+target
                           +(target == null ? "" : target.type())
                           +(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(specialCaller, target.type().parameterType(0));
    assertEquals(type,          target.type().dropParameterTypes(0,1));
    Class<?>[] paramsWithSelf = cat(array(Class[].class, (Class)specialCaller), params);
    MethodType typeWithSelf = MethodType.methodType(ret, paramsWithSelf);
    assertNameStringContains(target, methodName);
    Object[] args = randomArgs(paramsWithSelf);
    printCalled(target, name, args);
    target.invokeWithArguments(args);
    assertCalled(name, args);
}
 
源代码6 项目: openjdk-jdk8u-backup   文件: MethodHandlesTest.java
void testFindSpecial(boolean positive, Lookup lookup, Class<?> specialCaller,
                     Class<?> defc, 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 = maybeMoveIn(lookup, specialCaller);
    boolean specialAccessOK = (specialLookup.lookupClass() == specialCaller &&
                               (specialLookup.lookupModes() & Lookup.PRIVATE) != 0);
    MethodHandle target = null;
    Exception noAccess = null;
    try {
        if (verbosity >= 4)  System.out.println("lookup via "+lookup+" of "+defc+" "+name+type);
        if (verbosity >= 5)  System.out.println("  lookup => "+specialLookup);
        target = specialLookup.findSpecial(defc, methodName, type, specialCaller);
    } catch (ReflectiveOperationException ex) {
        noAccess = ex;
        assertExceptionClass(
            (!specialAccessOK)  // this check should happen first
            ?   IllegalAccessException.class
            : (name.contains("bogus") || INIT_REF_CAUSES_NSME && name.contains("<init>"))
            ?   NoSuchMethodException.class
            : IllegalAccessException.class,
            noAccess);
        if (verbosity >= 5)  ex.printStackTrace(System.out);
    }
    if (verbosity >= 3)
        System.out.println("findSpecial from "+specialCaller.getName()+" to "+defc.getName()+"."+name+"/"+type+" => "+target
                           +(target == null ? "" : target.type())
                           +(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(specialCaller, target.type().parameterType(0));
    assertEquals(type,          target.type().dropParameterTypes(0,1));
    Class<?>[] paramsWithSelf = cat(array(Class[].class, (Class)specialCaller), params);
    MethodType typeWithSelf = MethodType.methodType(ret, paramsWithSelf);
    assertNameStringContains(target, methodName);
    Object[] args = randomArgs(paramsWithSelf);
    printCalled(target, name, args);
    target.invokeWithArguments(args);
    assertCalled(name, args);
}
 
源代码7 项目: openjdk-jdk9   文件: MethodHandlesTest.java
void testFindSpecial(boolean positive, Lookup lookup, Class<?> specialCaller,
                     Class<?> defc, 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 = maybeMoveIn(lookup, specialCaller);
    boolean specialAccessOK = (specialLookup.lookupClass() == specialCaller &&
                               (specialLookup.lookupModes() & Lookup.PRIVATE) != 0);
    MethodHandle target = null;
    Exception noAccess = null;
    try {
        if (verbosity >= 4)  System.out.println("lookup via "+lookup+" of "+defc+" "+name+type);
        if (verbosity >= 5)  System.out.println("  lookup => "+specialLookup);
        target = specialLookup.findSpecial(defc, methodName, type, specialCaller);
    } catch (ReflectiveOperationException ex) {
        noAccess = ex;
        assertExceptionClass(
            (!specialAccessOK)  // this check should happen first
            ?   IllegalAccessException.class
            : (name.contains("bogus") || INIT_REF_CAUSES_NSME && name.contains("<init>"))
            ?   NoSuchMethodException.class
            : IllegalAccessException.class,
            noAccess);
        if (verbosity >= 5)  ex.printStackTrace(System.out);
    }
    if (verbosity >= 3)
        System.out.println("findSpecial from "+specialCaller.getName()+" to "+defc.getName()+"."+name+"/"+type+" => "+target
                           +(target == null ? "" : target.type())
                           +(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(specialCaller, target.type().parameterType(0));
    assertEquals(type,          target.type().dropParameterTypes(0,1));
    Class<?>[] paramsWithSelf = cat(array(Class[].class, (Class)specialCaller), params);
    MethodType typeWithSelf = MethodType.methodType(ret, paramsWithSelf);
    assertNameStringContains(target, methodName);
    Object[] args = randomArgs(paramsWithSelf);
    printCalled(target, name, args);
    target.invokeWithArguments(args);
    assertCalled(name, args);
}
 
源代码8 项目: jdk8u-jdk   文件: MethodHandlesTest.java
void testFindSpecial(boolean positive, Lookup lookup, Class<?> specialCaller,
                     Class<?> defc, 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 = maybeMoveIn(lookup, specialCaller);
    boolean specialAccessOK = (specialLookup.lookupClass() == specialCaller &&
                               (specialLookup.lookupModes() & Lookup.PRIVATE) != 0);
    MethodHandle target = null;
    Exception noAccess = null;
    try {
        if (verbosity >= 4)  System.out.println("lookup via "+lookup+" of "+defc+" "+name+type);
        if (verbosity >= 5)  System.out.println("  lookup => "+specialLookup);
        target = specialLookup.findSpecial(defc, methodName, type, specialCaller);
    } catch (ReflectiveOperationException ex) {
        noAccess = ex;
        assertExceptionClass(
            (!specialAccessOK)  // this check should happen first
            ?   IllegalAccessException.class
            : (name.contains("bogus") || INIT_REF_CAUSES_NSME && name.contains("<init>"))
            ?   NoSuchMethodException.class
            : IllegalAccessException.class,
            noAccess);
        if (verbosity >= 5)  ex.printStackTrace(System.out);
    }
    if (verbosity >= 3)
        System.out.println("findSpecial from "+specialCaller.getName()+" to "+defc.getName()+"."+name+"/"+type+" => "+target
                           +(target == null ? "" : target.type())
                           +(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(specialCaller, target.type().parameterType(0));
    assertEquals(type,          target.type().dropParameterTypes(0,1));
    Class<?>[] paramsWithSelf = cat(array(Class[].class, (Class)specialCaller), params);
    MethodType typeWithSelf = MethodType.methodType(ret, paramsWithSelf);
    assertNameStringContains(target, methodName);
    Object[] args = randomArgs(paramsWithSelf);
    printCalled(target, name, args);
    target.invokeWithArguments(args);
    assertCalled(name, args);
}
 
源代码9 项目: hottub   文件: MethodHandlesTest.java
void testFindSpecial(boolean positive, Lookup lookup, Class<?> specialCaller,
                     Class<?> defc, 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 = maybeMoveIn(lookup, specialCaller);
    boolean specialAccessOK = (specialLookup.lookupClass() == specialCaller &&
                               (specialLookup.lookupModes() & Lookup.PRIVATE) != 0);
    MethodHandle target = null;
    Exception noAccess = null;
    try {
        if (verbosity >= 4)  System.out.println("lookup via "+lookup+" of "+defc+" "+name+type);
        if (verbosity >= 5)  System.out.println("  lookup => "+specialLookup);
        target = specialLookup.findSpecial(defc, methodName, type, specialCaller);
    } catch (ReflectiveOperationException ex) {
        noAccess = ex;
        assertExceptionClass(
            (!specialAccessOK)  // this check should happen first
            ?   IllegalAccessException.class
            : (name.contains("bogus") || INIT_REF_CAUSES_NSME && name.contains("<init>"))
            ?   NoSuchMethodException.class
            : IllegalAccessException.class,
            noAccess);
        if (verbosity >= 5)  ex.printStackTrace(System.out);
    }
    if (verbosity >= 3)
        System.out.println("findSpecial from "+specialCaller.getName()+" to "+defc.getName()+"."+name+"/"+type+" => "+target
                           +(target == null ? "" : target.type())
                           +(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(specialCaller, target.type().parameterType(0));
    assertEquals(type,          target.type().dropParameterTypes(0,1));
    Class<?>[] paramsWithSelf = cat(array(Class[].class, (Class)specialCaller), params);
    MethodType typeWithSelf = MethodType.methodType(ret, paramsWithSelf);
    assertNameStringContains(target, methodName);
    Object[] args = randomArgs(paramsWithSelf);
    printCalled(target, name, args);
    target.invokeWithArguments(args);
    assertCalled(name, args);
}
 
源代码10 项目: openjdk-8-source   文件: MethodHandlesTest.java
void testFindSpecial(boolean positive, Lookup lookup, Class<?> specialCaller,
                     Class<?> defc, 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 = maybeMoveIn(lookup, specialCaller);
    boolean specialAccessOK = (specialLookup.lookupClass() == specialCaller &&
                               (specialLookup.lookupModes() & Lookup.PRIVATE) != 0);
    MethodHandle target = null;
    Exception noAccess = null;
    try {
        if (verbosity >= 4)  System.out.println("lookup via "+lookup+" of "+defc+" "+name+type);
        if (verbosity >= 5)  System.out.println("  lookup => "+specialLookup);
        target = specialLookup.findSpecial(defc, methodName, type, specialCaller);
    } catch (ReflectiveOperationException ex) {
        noAccess = ex;
        assertExceptionClass(
            (!specialAccessOK)  // this check should happen first
            ?   IllegalAccessException.class
            : (name.contains("bogus") || INIT_REF_CAUSES_NSME && name.contains("<init>"))
            ?   NoSuchMethodException.class
            : IllegalAccessException.class,
            noAccess);
        if (verbosity >= 5)  ex.printStackTrace(System.out);
    }
    if (verbosity >= 3)
        System.out.println("findSpecial from "+specialCaller.getName()+" to "+defc.getName()+"."+name+"/"+type+" => "+target
                           +(target == null ? "" : target.type())
                           +(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(specialCaller, target.type().parameterType(0));
    assertEquals(type,          target.type().dropParameterTypes(0,1));
    Class<?>[] paramsWithSelf = cat(array(Class[].class, (Class)specialCaller), params);
    MethodType typeWithSelf = MethodType.methodType(ret, paramsWithSelf);
    assertNameStringContains(target, methodName);
    Object[] args = randomArgs(paramsWithSelf);
    printCalled(target, name, args);
    target.invokeWithArguments(args);
    assertCalled(name, args);
}
 
源代码11 项目: openjdk-8   文件: MethodHandlesTest.java
void testFindSpecial(boolean positive, Lookup lookup, Class<?> specialCaller,
                     Class<?> defc, 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 = maybeMoveIn(lookup, specialCaller);
    boolean specialAccessOK = (specialLookup.lookupClass() == specialCaller &&
                               (specialLookup.lookupModes() & Lookup.PRIVATE) != 0);
    MethodHandle target = null;
    Exception noAccess = null;
    try {
        if (verbosity >= 4)  System.out.println("lookup via "+lookup+" of "+defc+" "+name+type);
        if (verbosity >= 5)  System.out.println("  lookup => "+specialLookup);
        target = specialLookup.findSpecial(defc, methodName, type, specialCaller);
    } catch (ReflectiveOperationException ex) {
        noAccess = ex;
        assertExceptionClass(
            (!specialAccessOK)  // this check should happen first
            ?   IllegalAccessException.class
            : (name.contains("bogus") || INIT_REF_CAUSES_NSME && name.contains("<init>"))
            ?   NoSuchMethodException.class
            : IllegalAccessException.class,
            noAccess);
        if (verbosity >= 5)  ex.printStackTrace(System.out);
    }
    if (verbosity >= 3)
        System.out.println("findSpecial from "+specialCaller.getName()+" to "+defc.getName()+"."+name+"/"+type+" => "+target
                           +(target == null ? "" : target.type())
                           +(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(specialCaller, target.type().parameterType(0));
    assertEquals(type,          target.type().dropParameterTypes(0,1));
    Class<?>[] paramsWithSelf = cat(array(Class[].class, (Class)specialCaller), params);
    MethodType typeWithSelf = MethodType.methodType(ret, paramsWithSelf);
    assertNameStringContains(target, methodName);
    Object[] args = randomArgs(paramsWithSelf);
    printCalled(target, name, args);
    target.invokeWithArguments(args);
    assertCalled(name, args);
}
 
源代码12 项目: jdk8u_jdk   文件: MethodHandlesTest.java
void testFindSpecial(boolean positive, Lookup lookup, Class<?> specialCaller,
                     Class<?> defc, 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 = maybeMoveIn(lookup, specialCaller);
    boolean specialAccessOK = (specialLookup.lookupClass() == specialCaller &&
                               (specialLookup.lookupModes() & Lookup.PRIVATE) != 0);
    MethodHandle target = null;
    Exception noAccess = null;
    try {
        if (verbosity >= 4)  System.out.println("lookup via "+lookup+" of "+defc+" "+name+type);
        if (verbosity >= 5)  System.out.println("  lookup => "+specialLookup);
        target = specialLookup.findSpecial(defc, methodName, type, specialCaller);
    } catch (ReflectiveOperationException ex) {
        noAccess = ex;
        assertExceptionClass(
            (!specialAccessOK)  // this check should happen first
            ?   IllegalAccessException.class
            : (name.contains("bogus") || INIT_REF_CAUSES_NSME && name.contains("<init>"))
            ?   NoSuchMethodException.class
            : IllegalAccessException.class,
            noAccess);
        if (verbosity >= 5)  ex.printStackTrace(System.out);
    }
    if (verbosity >= 3)
        System.out.println("findSpecial from "+specialCaller.getName()+" to "+defc.getName()+"."+name+"/"+type+" => "+target
                           +(target == null ? "" : target.type())
                           +(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(specialCaller, target.type().parameterType(0));
    assertEquals(type,          target.type().dropParameterTypes(0,1));
    Class<?>[] paramsWithSelf = cat(array(Class[].class, (Class)specialCaller), params);
    MethodType typeWithSelf = MethodType.methodType(ret, paramsWithSelf);
    assertNameStringContains(target, methodName);
    Object[] args = randomArgs(paramsWithSelf);
    printCalled(target, name, args);
    target.invokeWithArguments(args);
    assertCalled(name, args);
}
 
源代码13 项目: jdk8u-jdk   文件: MethodHandlesTest.java
void testFindSpecial(boolean positive, Lookup lookup, Class<?> specialCaller,
                     Class<?> defc, 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 = maybeMoveIn(lookup, specialCaller);
    boolean specialAccessOK = (specialLookup.lookupClass() == specialCaller &&
                               (specialLookup.lookupModes() & Lookup.PRIVATE) != 0);
    MethodHandle target = null;
    Exception noAccess = null;
    try {
        if (verbosity >= 4)  System.out.println("lookup via "+lookup+" of "+defc+" "+name+type);
        if (verbosity >= 5)  System.out.println("  lookup => "+specialLookup);
        target = specialLookup.findSpecial(defc, methodName, type, specialCaller);
    } catch (ReflectiveOperationException ex) {
        noAccess = ex;
        assertExceptionClass(
            (!specialAccessOK)  // this check should happen first
            ?   IllegalAccessException.class
            : (name.contains("bogus") || INIT_REF_CAUSES_NSME && name.contains("<init>"))
            ?   NoSuchMethodException.class
            : IllegalAccessException.class,
            noAccess);
        if (verbosity >= 5)  ex.printStackTrace(System.out);
    }
    if (verbosity >= 3)
        System.out.println("findSpecial from "+specialCaller.getName()+" to "+defc.getName()+"."+name+"/"+type+" => "+target
                           +(target == null ? "" : target.type())
                           +(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(specialCaller, target.type().parameterType(0));
    assertEquals(type,          target.type().dropParameterTypes(0,1));
    Class<?>[] paramsWithSelf = cat(array(Class[].class, (Class)specialCaller), params);
    MethodType typeWithSelf = MethodType.methodType(ret, paramsWithSelf);
    assertNameStringContains(target, methodName);
    Object[] args = randomArgs(paramsWithSelf);
    printCalled(target, name, args);
    target.invokeWithArguments(args);
    assertCalled(name, args);
}
 
源代码14 项目: jdk8u-dev-jdk   文件: MethodHandlesTest.java
void testFindSpecial(boolean positive, Lookup lookup, Class<?> specialCaller,
                     Class<?> defc, 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 = maybeMoveIn(lookup, specialCaller);
    boolean specialAccessOK = (specialLookup.lookupClass() == specialCaller &&
                               (specialLookup.lookupModes() & Lookup.PRIVATE) != 0);
    MethodHandle target = null;
    Exception noAccess = null;
    try {
        if (verbosity >= 4)  System.out.println("lookup via "+lookup+" of "+defc+" "+name+type);
        if (verbosity >= 5)  System.out.println("  lookup => "+specialLookup);
        target = specialLookup.findSpecial(defc, methodName, type, specialCaller);
    } catch (ReflectiveOperationException ex) {
        noAccess = ex;
        assertExceptionClass(
            (!specialAccessOK)  // this check should happen first
            ?   IllegalAccessException.class
            : (name.contains("bogus") || INIT_REF_CAUSES_NSME && name.contains("<init>"))
            ?   NoSuchMethodException.class
            : IllegalAccessException.class,
            noAccess);
        if (verbosity >= 5)  ex.printStackTrace(System.out);
    }
    if (verbosity >= 3)
        System.out.println("findSpecial from "+specialCaller.getName()+" to "+defc.getName()+"."+name+"/"+type+" => "+target
                           +(target == null ? "" : target.type())
                           +(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(specialCaller, target.type().parameterType(0));
    assertEquals(type,          target.type().dropParameterTypes(0,1));
    Class<?>[] paramsWithSelf = cat(array(Class[].class, (Class)specialCaller), params);
    MethodType typeWithSelf = MethodType.methodType(ret, paramsWithSelf);
    assertNameStringContains(target, methodName);
    Object[] args = randomArgs(paramsWithSelf);
    printCalled(target, name, args);
    target.invokeWithArguments(args);
    assertCalled(name, args);
}