Java Code Examples for java.lang.invoke.MethodHandles.Lookup#findConstructor()
The following examples show how to use
java.lang.invoke.MethodHandles.Lookup#findConstructor() .
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 |
/** * 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 TencentKona-8 with GNU General Public License v2.0 | 5 votes |
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)); }
Example 3
Source File: FSClassRegistry.java From uima-uimaj with Apache License 2.0 | 5 votes |
/** * 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); } }
Example 4
Source File: MethodHandlesTest.java From jdk8u-dev-jdk with GNU General Public License v2.0 | 5 votes |
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)); }
Example 5
Source File: MethodHandlesTest.java From jdk8u-jdk with GNU General Public License v2.0 | 5 votes |
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)); }
Example 6
Source File: MethodHandlesTest.java From jdk8u_jdk with GNU General Public License v2.0 | 5 votes |
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)); }
Example 7
Source File: MethodHandlesTest.java From openjdk-8 with GNU General Public License v2.0 | 5 votes |
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)); }
Example 8
Source File: MethodHandlesTest.java From openjdk-8-source with GNU General Public License v2.0 | 5 votes |
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)); }
Example 9
Source File: MethodHandlesTest.java From hottub with GNU General Public License v2.0 | 5 votes |
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)); }
Example 10
Source File: MethodHandlesTest.java From jdk8u-jdk with GNU General Public License v2.0 | 5 votes |
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)); }
Example 11
Source File: MethodHandlesTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
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)); }
Example 12
Source File: Main.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
/** * 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); }
Example 13
Source File: StringConcatFactory.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
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); } }
Example 14
Source File: StringConcatFactory.java From Bytecoder with Apache License 2.0 | 5 votes |
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); } }
Example 15
Source File: MethodHandlesTest.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 5 votes |
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)); }
Example 16
Source File: MethodHandlesTest.java From openjdk-jdk8u with GNU General Public License v2.0 | 5 votes |
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)); }
Example 17
Source File: MethodHandlesTest.java From jdk8u60 with GNU General Public License v2.0 | 5 votes |
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)); }
Example 18
Source File: MethodHandlesTest.java From dragonwell8_jdk with GNU General Public License v2.0 | 5 votes |
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)); }
Example 19
Source File: Basic.java From openjdk-jdk9 with GNU General Public License v2.0 | 4 votes |
/** * 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(); }