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

The following examples show how to use java.lang.invoke.MethodHandles.Lookup#unreflectSpecial() . 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: DefaultMethodHandler.java    From feign-reactive with Apache License 2.0 5 votes vote down vote up
public DefaultMethodHandler(Method defaultMethod) {
  try {
    Class<?> declaringClass = defaultMethod.getDeclaringClass();
    Field field = Lookup.class.getDeclaredField("IMPL_LOOKUP");
    field.setAccessible(true);
    Lookup lookup = (Lookup) field.get(null);

    this.unboundHandle = lookup.unreflectSpecial(defaultMethod, declaringClass);
  } catch (NoSuchFieldException | IllegalAccessException ex) {
    throw new IllegalStateException(ex);
  }
}
 
Example 2
Source File: DefaultMethodHandler.java    From feign-reactive with Apache License 2.0 5 votes vote down vote up
public DefaultMethodHandler(Method defaultMethod) {
  try {
    Class<?> declaringClass = defaultMethod.getDeclaringClass();
    Field field = Lookup.class.getDeclaredField("IMPL_LOOKUP");
    field.setAccessible(true);
    Lookup lookup = (Lookup) field.get(null);

    this.unboundHandle = lookup.unreflectSpecial(defaultMethod, declaringClass);
  } catch (NoSuchFieldException | IllegalAccessException ex) {
    throw new IllegalStateException(ex);
  }
}
 
Example 3
Source File: MethodHandlesTest.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
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(':');
}
 
Example 4
Source File: MethodHandlesTest.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
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(':');
}
 
Example 5
Source File: MethodHandlesTest.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
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(':');
}
 
Example 6
Source File: MethodHandlesTest.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
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(':');
}
 
Example 7
Source File: MethodHandlesTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
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(':');
}
 
Example 8
Source File: MethodHandlesTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
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(':');
}
 
Example 9
Source File: MethodHandlesTest.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
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(':');
}
 
Example 10
Source File: MethodHandlesTest.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
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(':');
}
 
Example 11
Source File: MethodHandlesTest.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
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(':');
}
 
Example 12
Source File: MethodHandlesTest.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
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(':');
}
 
Example 13
Source File: MethodHandlesTest.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
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(':');
}
 
Example 14
Source File: MethodHandlesTest.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
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(':');
}
 
Example 15
Source File: MethodHandlesTest.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
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(':');
}