Java Code Examples for java.lang.invoke.MethodHandles.Lookup#findSpecial()

The following examples show how to use java.lang.invoke.MethodHandles.Lookup#findSpecial() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LambdaDesugaring.java    From bazel with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
  }
}
 
Example 2
Source File: MethodHandlesTest.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
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);
}
 
Example 3
Source File: MethodHandlesTest.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
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);
}
 
Example 4
Source File: MethodHandlesTest.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
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);
}
 
Example 5
Source File: MethodHandlesTest.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
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);
}
 
Example 6
Source File: MethodHandlesTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
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);
}
 
Example 7
Source File: MethodHandlesTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
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);
}
 
Example 8
Source File: MethodHandlesTest.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
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);
}
 
Example 9
Source File: MethodHandlesTest.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
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);
}
 
Example 10
Source File: MethodHandlesTest.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
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);
}
 
Example 11
Source File: MethodHandlesTest.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
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);
}
 
Example 12
Source File: MethodHandlesTest.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
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);
}
 
Example 13
Source File: MethodHandlesTest.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
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);
}
 
Example 14
Source File: MethodHandlesTest.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
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);
}