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

下面列出了java.lang.invoke.MethodHandles.Lookup#findConstructor ( ) 实例代码,或者点击链接到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 testFindConstructor(boolean positive, Lookup lookup,
                         Class<?> defc, Class<?>... params) throws Throwable {
    countTest(positive);
    MethodType type = MethodType.methodType(void.class, params);
    MethodHandle target = null;
    Exception noAccess = null;
    try {
        if (verbosity >= 4)  System.out.println("lookup via "+lookup+" of "+defc+" <init>"+type);
        target = lookup.findConstructor(defc, type);
    } catch (ReflectiveOperationException ex) {
        noAccess = ex;
        assertTrue(noAccess.getClass().getName(), noAccess instanceof IllegalAccessException);
    }
    if (verbosity >= 3)
        System.out.println("findConstructor "+defc.getName()+".<init>/"+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(type.changeReturnType(defc), target.type());
    Object[] args = randomArgs(params);
    printCalled(target, defc.getSimpleName(), args);
    Object obj = target.invokeWithArguments(args);
    if (!(defc == Example.class && params.length < 2))
        assertCalled(defc.getSimpleName()+".<init>", args);
    assertTrue("instance of "+defc.getName(), defc.isInstance(obj));
}
 
源代码3 项目: TencentKona-8   文件: MethodHandlesTest.java
void testFindConstructor(boolean positive, Lookup lookup,
                         Class<?> defc, Class<?>... params) throws Throwable {
    countTest(positive);
    MethodType type = MethodType.methodType(void.class, params);
    MethodHandle target = null;
    Exception noAccess = null;
    try {
        if (verbosity >= 4)  System.out.println("lookup via "+lookup+" of "+defc+" <init>"+type);
        target = lookup.findConstructor(defc, type);
    } catch (ReflectiveOperationException ex) {
        noAccess = ex;
        assertTrue(noAccess.getClass().getName(), noAccess instanceof IllegalAccessException);
    }
    if (verbosity >= 3)
        System.out.println("findConstructor "+defc.getName()+".<init>/"+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(type.changeReturnType(defc), target.type());
    Object[] args = randomArgs(params);
    printCalled(target, defc.getSimpleName(), args);
    Object obj = target.invokeWithArguments(args);
    if (!(defc == Example.class && params.length < 2))
        assertCalled(defc.getSimpleName()+".<init>", args);
    assertTrue("instance of "+defc.getName(), defc.isInstance(obj));
}
 
源代码4 项目: jdk8u60   文件: MethodHandlesTest.java
void testFindConstructor(boolean positive, Lookup lookup,
                         Class<?> defc, Class<?>... params) throws Throwable {
    countTest(positive);
    MethodType type = MethodType.methodType(void.class, params);
    MethodHandle target = null;
    Exception noAccess = null;
    try {
        if (verbosity >= 4)  System.out.println("lookup via "+lookup+" of "+defc+" <init>"+type);
        target = lookup.findConstructor(defc, type);
    } catch (ReflectiveOperationException ex) {
        noAccess = ex;
        assertTrue(noAccess.getClass().getName(), noAccess instanceof IllegalAccessException);
    }
    if (verbosity >= 3)
        System.out.println("findConstructor "+defc.getName()+".<init>/"+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(type.changeReturnType(defc), target.type());
    Object[] args = randomArgs(params);
    printCalled(target, defc.getSimpleName(), args);
    Object obj = target.invokeWithArguments(args);
    if (!(defc == Example.class && params.length < 2))
        assertCalled(defc.getSimpleName()+".<init>", args);
    assertTrue("instance of "+defc.getName(), defc.isInstance(obj));
}
 
源代码5 项目: openjdk-jdk8u   文件: MethodHandlesTest.java
void testFindConstructor(boolean positive, Lookup lookup,
                         Class<?> defc, Class<?>... params) throws Throwable {
    countTest(positive);
    MethodType type = MethodType.methodType(void.class, params);
    MethodHandle target = null;
    Exception noAccess = null;
    try {
        if (verbosity >= 4)  System.out.println("lookup via "+lookup+" of "+defc+" <init>"+type);
        target = lookup.findConstructor(defc, type);
    } catch (ReflectiveOperationException ex) {
        noAccess = ex;
        assertTrue(noAccess.getClass().getName(), noAccess instanceof IllegalAccessException);
    }
    if (verbosity >= 3)
        System.out.println("findConstructor "+defc.getName()+".<init>/"+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(type.changeReturnType(defc), target.type());
    Object[] args = randomArgs(params);
    printCalled(target, defc.getSimpleName(), args);
    Object obj = target.invokeWithArguments(args);
    if (!(defc == Example.class && params.length < 2))
        assertCalled(defc.getSimpleName()+".<init>", args);
    assertTrue("instance of "+defc.getName(), defc.isInstance(obj));
}
 
源代码6 项目: openjdk-jdk8u-backup   文件: MethodHandlesTest.java
void testFindConstructor(boolean positive, Lookup lookup,
                         Class<?> defc, Class<?>... params) throws Throwable {
    countTest(positive);
    MethodType type = MethodType.methodType(void.class, params);
    MethodHandle target = null;
    Exception noAccess = null;
    try {
        if (verbosity >= 4)  System.out.println("lookup via "+lookup+" of "+defc+" <init>"+type);
        target = lookup.findConstructor(defc, type);
    } catch (ReflectiveOperationException ex) {
        noAccess = ex;
        assertTrue(noAccess.getClass().getName(), noAccess instanceof IllegalAccessException);
    }
    if (verbosity >= 3)
        System.out.println("findConstructor "+defc.getName()+".<init>/"+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(type.changeReturnType(defc), target.type());
    Object[] args = randomArgs(params);
    printCalled(target, defc.getSimpleName(), args);
    Object obj = target.invokeWithArguments(args);
    if (!(defc == Example.class && params.length < 2))
        assertCalled(defc.getSimpleName()+".<init>", args);
    assertTrue("instance of "+defc.getName(), defc.isInstance(obj));
}
 
源代码7 项目: Bytecoder   文件: StringConcatFactory.java
static MethodHandle lookupConstructor(Lookup lookup, Class<?> refc, Class<?> ptypes) {
    try {
        return lookup.findConstructor(refc, MethodType.methodType(void.class, ptypes));
    } catch (NoSuchMethodException | IllegalAccessException e) {
        throw new AssertionError(e);
    }
}
 
源代码8 项目: openjdk-jdk9   文件: StringConcatFactory.java
static MethodHandle lookupConstructor(Lookup lookup, Class<?> refc, Class<?> ptypes) {
    try {
        return lookup.findConstructor(refc, MethodType.methodType(void.class, ptypes));
    } catch (NoSuchMethodException | IllegalAccessException e) {
        throw new AssertionError(e);
    }
}
 
源代码9 项目: openjdk-jdk9   文件: Main.java
/**
 * Invokes Lookup findConstructor with a method type constructored from the
 * given return and parameter types.
 */
static MethodHandle findConstructor(Lookup lookup,
                                    Class<?> clazz,
                                    Class<?> rtype,
                                    Class<?>... ptypes) throws Exception {
    MethodType mt = MethodType.methodType(rtype, ptypes);
    return lookup.findConstructor(clazz, mt);
}
 
源代码10 项目: openjdk-jdk9   文件: MethodHandlesTest.java
void testFindConstructor(boolean positive, Lookup lookup,
                         Class<?> defc, Class<?>... params) throws Throwable {
    countTest(positive);
    MethodType type = MethodType.methodType(void.class, params);
    MethodHandle target = null;
    Exception noAccess = null;
    try {
        if (verbosity >= 4)  System.out.println("lookup via "+lookup+" of "+defc+" <init>"+type);
        target = lookup.findConstructor(defc, type);
    } catch (ReflectiveOperationException ex) {
        noAccess = ex;
        assertTrue(noAccess.getClass().getName(), noAccess instanceof IllegalAccessException);
    }
    if (verbosity >= 3)
        System.out.println("findConstructor "+defc.getName()+".<init>/"+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(type.changeReturnType(defc), target.type());
    Object[] args = randomArgs(params);
    printCalled(target, defc.getSimpleName(), args);
    Object obj = target.invokeWithArguments(args);
    if (!(defc == Example.class && params.length < 2))
        assertCalled(defc.getSimpleName()+".<init>", args);
    assertTrue("instance of "+defc.getName(), defc.isInstance(obj));
}
 
源代码11 项目: jdk8u-jdk   文件: MethodHandlesTest.java
void testFindConstructor(boolean positive, Lookup lookup,
                         Class<?> defc, Class<?>... params) throws Throwable {
    countTest(positive);
    MethodType type = MethodType.methodType(void.class, params);
    MethodHandle target = null;
    Exception noAccess = null;
    try {
        if (verbosity >= 4)  System.out.println("lookup via "+lookup+" of "+defc+" <init>"+type);
        target = lookup.findConstructor(defc, type);
    } catch (ReflectiveOperationException ex) {
        noAccess = ex;
        assertTrue(noAccess.getClass().getName(), noAccess instanceof IllegalAccessException);
    }
    if (verbosity >= 3)
        System.out.println("findConstructor "+defc.getName()+".<init>/"+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(type.changeReturnType(defc), target.type());
    Object[] args = randomArgs(params);
    printCalled(target, defc.getSimpleName(), args);
    Object obj = target.invokeWithArguments(args);
    if (!(defc == Example.class && params.length < 2))
        assertCalled(defc.getSimpleName()+".<init>", args);
    assertTrue("instance of "+defc.getName(), defc.isInstance(obj));
}
 
源代码12 项目: hottub   文件: MethodHandlesTest.java
void testFindConstructor(boolean positive, Lookup lookup,
                         Class<?> defc, Class<?>... params) throws Throwable {
    countTest(positive);
    MethodType type = MethodType.methodType(void.class, params);
    MethodHandle target = null;
    Exception noAccess = null;
    try {
        if (verbosity >= 4)  System.out.println("lookup via "+lookup+" of "+defc+" <init>"+type);
        target = lookup.findConstructor(defc, type);
    } catch (ReflectiveOperationException ex) {
        noAccess = ex;
        assertTrue(noAccess.getClass().getName(), noAccess instanceof IllegalAccessException);
    }
    if (verbosity >= 3)
        System.out.println("findConstructor "+defc.getName()+".<init>/"+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(type.changeReturnType(defc), target.type());
    Object[] args = randomArgs(params);
    printCalled(target, defc.getSimpleName(), args);
    Object obj = target.invokeWithArguments(args);
    if (!(defc == Example.class && params.length < 2))
        assertCalled(defc.getSimpleName()+".<init>", args);
    assertTrue("instance of "+defc.getName(), defc.isInstance(obj));
}
 
源代码13 项目: openjdk-8-source   文件: MethodHandlesTest.java
void testFindConstructor(boolean positive, Lookup lookup,
                         Class<?> defc, Class<?>... params) throws Throwable {
    countTest(positive);
    MethodType type = MethodType.methodType(void.class, params);
    MethodHandle target = null;
    Exception noAccess = null;
    try {
        if (verbosity >= 4)  System.out.println("lookup via "+lookup+" of "+defc+" <init>"+type);
        target = lookup.findConstructor(defc, type);
    } catch (ReflectiveOperationException ex) {
        noAccess = ex;
        assertTrue(noAccess.getClass().getName(), noAccess instanceof IllegalAccessException);
    }
    if (verbosity >= 3)
        System.out.println("findConstructor "+defc.getName()+".<init>/"+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(type.changeReturnType(defc), target.type());
    Object[] args = randomArgs(params);
    printCalled(target, defc.getSimpleName(), args);
    Object obj = target.invokeWithArguments(args);
    if (!(defc == Example.class && params.length < 2))
        assertCalled(defc.getSimpleName()+".<init>", args);
    assertTrue("instance of "+defc.getName(), defc.isInstance(obj));
}
 
源代码14 项目: openjdk-8   文件: MethodHandlesTest.java
void testFindConstructor(boolean positive, Lookup lookup,
                         Class<?> defc, Class<?>... params) throws Throwable {
    countTest(positive);
    MethodType type = MethodType.methodType(void.class, params);
    MethodHandle target = null;
    Exception noAccess = null;
    try {
        if (verbosity >= 4)  System.out.println("lookup via "+lookup+" of "+defc+" <init>"+type);
        target = lookup.findConstructor(defc, type);
    } catch (ReflectiveOperationException ex) {
        noAccess = ex;
        assertTrue(noAccess.getClass().getName(), noAccess instanceof IllegalAccessException);
    }
    if (verbosity >= 3)
        System.out.println("findConstructor "+defc.getName()+".<init>/"+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(type.changeReturnType(defc), target.type());
    Object[] args = randomArgs(params);
    printCalled(target, defc.getSimpleName(), args);
    Object obj = target.invokeWithArguments(args);
    if (!(defc == Example.class && params.length < 2))
        assertCalled(defc.getSimpleName()+".<init>", args);
    assertTrue("instance of "+defc.getName(), defc.isInstance(obj));
}
 
源代码15 项目: jdk8u_jdk   文件: MethodHandlesTest.java
void testFindConstructor(boolean positive, Lookup lookup,
                         Class<?> defc, Class<?>... params) throws Throwable {
    countTest(positive);
    MethodType type = MethodType.methodType(void.class, params);
    MethodHandle target = null;
    Exception noAccess = null;
    try {
        if (verbosity >= 4)  System.out.println("lookup via "+lookup+" of "+defc+" <init>"+type);
        target = lookup.findConstructor(defc, type);
    } catch (ReflectiveOperationException ex) {
        noAccess = ex;
        assertTrue(noAccess.getClass().getName(), noAccess instanceof IllegalAccessException);
    }
    if (verbosity >= 3)
        System.out.println("findConstructor "+defc.getName()+".<init>/"+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(type.changeReturnType(defc), target.type());
    Object[] args = randomArgs(params);
    printCalled(target, defc.getSimpleName(), args);
    Object obj = target.invokeWithArguments(args);
    if (!(defc == Example.class && params.length < 2))
        assertCalled(defc.getSimpleName()+".<init>", args);
    assertTrue("instance of "+defc.getName(), defc.isInstance(obj));
}
 
源代码16 项目: jdk8u-jdk   文件: MethodHandlesTest.java
void testFindConstructor(boolean positive, Lookup lookup,
                         Class<?> defc, Class<?>... params) throws Throwable {
    countTest(positive);
    MethodType type = MethodType.methodType(void.class, params);
    MethodHandle target = null;
    Exception noAccess = null;
    try {
        if (verbosity >= 4)  System.out.println("lookup via "+lookup+" of "+defc+" <init>"+type);
        target = lookup.findConstructor(defc, type);
    } catch (ReflectiveOperationException ex) {
        noAccess = ex;
        assertTrue(noAccess.getClass().getName(), noAccess instanceof IllegalAccessException);
    }
    if (verbosity >= 3)
        System.out.println("findConstructor "+defc.getName()+".<init>/"+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(type.changeReturnType(defc), target.type());
    Object[] args = randomArgs(params);
    printCalled(target, defc.getSimpleName(), args);
    Object obj = target.invokeWithArguments(args);
    if (!(defc == Example.class && params.length < 2))
        assertCalled(defc.getSimpleName()+".<init>", args);
    assertTrue("instance of "+defc.getName(), defc.isInstance(obj));
}
 
源代码17 项目: jdk8u-dev-jdk   文件: MethodHandlesTest.java
void testFindConstructor(boolean positive, Lookup lookup,
                         Class<?> defc, Class<?>... params) throws Throwable {
    countTest(positive);
    MethodType type = MethodType.methodType(void.class, params);
    MethodHandle target = null;
    Exception noAccess = null;
    try {
        if (verbosity >= 4)  System.out.println("lookup via "+lookup+" of "+defc+" <init>"+type);
        target = lookup.findConstructor(defc, type);
    } catch (ReflectiveOperationException ex) {
        noAccess = ex;
        assertTrue(noAccess.getClass().getName(), noAccess instanceof IllegalAccessException);
    }
    if (verbosity >= 3)
        System.out.println("findConstructor "+defc.getName()+".<init>/"+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(type.changeReturnType(defc), target.type());
    Object[] args = randomArgs(params);
    printCalled(target, defc.getSimpleName(), args);
    Object obj = target.invokeWithArguments(args);
    if (!(defc == Example.class && params.length < 2))
        assertCalled(defc.getSimpleName()+".<init>", args);
    assertTrue("instance of "+defc.getName(), defc.isInstance(obj));
}
 
源代码18 项目: uima-uimaj   文件: FSClassRegistry.java
/**
 * Return a Functional Interface for a generator for creating instances of a type.
 *   Function takes a casImpl arg, and returning an instance of the JCas type.
 * @param jcasClass the class of the JCas type to construct
 * @param typeImpl the UIMA type
 * @return a Functional Interface whose createFS method takes a casImpl 
 *         and when subsequently invoked, returns a new instance of the class
 */
private static FsGenerator3 createGenerator(Class<?> jcasClass, Lookup lookup) {
  try {
    
    MethodHandle mh = lookup.findConstructor(jcasClass, findConstructorJCasCoverType);
    MethodType mtThisGenerator = methodType(jcasClass, TypeImpl.class, CASImpl.class);
 
    CallSite callSite = LambdaMetafactory.metafactory(
        lookup, // lookup context for the constructor 
        "createFS", // name of the method in the Function Interface 
        callsiteFsGenerator, // signature of callsite, return type is functional interface, args are captured args if any
        fsGeneratorType,  // samMethodType signature and return type of method impl by function object 
        mh,  // method handle to constructor 
        mtThisGenerator);
    return (FsGenerator3) callSite.getTarget().invokeExact();
  } catch (Throwable e) {
    if (e instanceof NoSuchMethodException) {
      String classname = jcasClass.getName();
      add2errors(errorSet, new CASRuntimeException(e, CASRuntimeException.JCAS_CAS_NOT_V3, 
          classname,
          jcasClass.getClassLoader().getResource(classname.replace('.', '/') + ".class").toString()
          ));
      return null;
    }
    /** An internal error occurred, please report to the Apache UIMA project; nested exception if present: {0} */
    throw new UIMARuntimeException(e, UIMARuntimeException.INTERNAL_ERROR);
  }
}
 
源代码19 项目: openjdk-jdk9   文件: Basic.java
/**
 * Produces the method handle for the no-arg constructor and invokes it
 */
Object findNoArgConstructorAndInvoke(Class<?> clazz, Lookup lookup) throws Throwable {
    MethodType mt = MethodType.methodType(void.class);
    MethodHandle mh = lookup.findConstructor(clazz, mt);
    return mh.invoke();
}