private static MethodHandle findConstructor(String className, MethodType methodType) { try { return MethodHandles.publicLookup().findConstructor(Class.forName(className), methodType); } catch (ReflectiveOperationException e) { throw new RuntimeException(e); } }
private static <T extends TemplateEngine> Supplier<T> tryConstructor(Class<T> clazz, Object... args) { return () -> { try { Object[] nonNullArgs = Arrays.stream(args).filter(Objects::nonNull).toArray(Object[]::new); Class[] argClasses = Arrays.stream(nonNullArgs).map(Object::getClass).toArray(Class[]::new); MethodType type = MethodType.methodType(void.class, argClasses); return (T) MethodHandles.lookup().findConstructor(clazz, type).invokeWithArguments(nonNullArgs); } catch (NoSuchMethodException ignored) { return null; } catch (Throwable t) { throw Sneaky.throwAnyway(t); } }; } }
private static <T extends SqlParser> Supplier<T> tryConstructor(Class<T> clazz, Object... args) { return () -> { try { Object[] nonNullArgs = Arrays.stream(args).filter(Objects::nonNull).toArray(Object[]::new); Class[] argClasses = Arrays.stream(nonNullArgs).map(Object::getClass).toArray(Class[]::new); MethodType type = MethodType.methodType(void.class, argClasses); return (T) MethodHandles.lookup().findConstructor(clazz, type).invokeWithArguments(nonNullArgs); } catch (NoSuchMethodException ignored) { return null; } catch (Throwable t) { throw Sneaky.throwAnyway(t); } }; } }
/** * Returns a correctly typed {@link MethodHandle} for the no-arg ctor of the given class. */ static final MethodHandle findAttributeImplCtor(Class<? extends AttributeImpl> clazz) { try { return lookup.findConstructor(clazz, NO_ARG_CTOR).asType(NO_ARG_RETURNING_ATTRIBUTEIMPL); } catch (NoSuchMethodException | IllegalAccessException e) { throw new IllegalArgumentException("Cannot lookup accessible no-arg constructor for: " + clazz.getName(), e); } }
DtoMetaConstructor(TypeManager typeManager, Constructor<?> constructor, Class<?> someClass) throws NoSuchMethodException, IllegalAccessException { this.types = constructor.getParameterTypes(); this.scalarTypes = new ScalarType[types.length]; for (int i = 0; i < types.length; i++) { scalarTypes[i] = typeManager.getScalarType(types[i]); } MethodHandles.Lookup lookup = MethodHandles.publicLookup(); this.handle = lookup.findConstructor(someClass, typeFor(types)); }
public static <N extends Number> StringFunction<N> create(Class<N> type) throws Throwable { MethodType methodType = MethodType.methodType(type, String.class); MethodHandles.Lookup lookup = MethodHandles.lookup(); MethodHandle handle = lookup.findConstructor(type, MethodType.methodType(void.class, String.class)); StringFunction<N> f = (StringFunction<N>) LambdaMetafactory.metafactory(lookup, "apply", MethodType.methodType(StringFunction.class), methodType.changeReturnType(Number.class), handle, methodType).getTarget().invokeExact(); return f; }
private MethodHandle findConstructor(Class<?> factoryType) { try { return LOOKUP.findConstructor(factoryType, CTOR_METHOD); } catch (NoSuchMethodException | IllegalAccessException e) { throw new RuntimeException(e); } }
private static Object valueOf(Class<?> numClass, String s) throws Exception { MethodHandle mh; try { mh = publicLookup.findStatic(numClass, "valueOf", methodType(numClass, String.class)); } catch (NoSuchMethodException | IllegalAccessException e) { mh = publicLookup.findConstructor(numClass, stringConstructor); } try { return mh.invoke(s); } catch (Throwable e) { throw Exceptions.duck(e); } }
private MethodHandle findConstructor(Class<?> factoryType) { try { return LOOKUP.findConstructor(factoryType, CTOR_METHOD); } catch (NoSuchMethodException | IllegalAccessException e) { throw new RuntimeException(e); } }
@Override public CallSite invokeConstructor(Lookup caller, MethodType signature, Class<?> owner) throws ReflectiveOperationException { try { return new ConstantCallSite(caller.findConstructor(owner, signature)); } catch (ReflectiveOperationException e) { return null; } }
private static MethodHandle lookupNewJarFile() { try { return MethodHandles.lookup().findConstructor(java.util.jar.JarFile.class, MethodType.methodType(void.class, java.io.File.class, java.lang.Boolean.TYPE, Integer.TYPE, runtimeVersionClass())); } catch (Throwable t) { return null; } } private static Class<?> runtimeVersionClass() throws ClassNotFoundException {
MethodType methodType = MethodType.methodType(Integer.class, String.class); MethodHandles.Lookup lookup = MethodHandles.lookup(); MethodHandle handle = lookup.findConstructor( Integer.class, MethodType.methodType(void.class, String.class)); Function<String,Integer> f=(Function<String,Integer>) LambdaMetafactory.metafactory(lookup, "apply", MethodType.methodType(Function.class), methodType.generic(), handle, methodType).getTarget().invokeExact();
Map map = ((Map) a); metaModel.addAssociation( (Association) lookup.findConstructor( Class.forName((String) map.get(Association.CLASS)), methodType
DBProxy(String dbName) { try { MethodHandles.Lookup lookup = MethodHandles.lookup(); db = dbName == null ? lookup.findConstructor(Classes.DB, methodType(void.class)).invoke() : lookup.findConstructor(Classes.DB, methodType(void.class, String.class)).invoke(dbName); open = lookup.findVirtual(Classes.DB, "open", methodType(Classes.DB)); name = lookup.findVirtual(Classes.DB, "name", methodType(String.class)); open4 = lookup.findVirtual(Classes.DB, "open", methodType(Classes.DB, String.class, String.class, String.class, String.class)); close = lookup.findVirtual(Classes.DB, "close", methodType(void.class)); } catch(Throwable t) { throw new InstrumentationException(t); } }
DtoMetaConstructor(TypeManager typeManager, Constructor<?> constructor, Class<?> someClass) throws NoSuchMethodException, IllegalAccessException { this.types = constructor.getParameterTypes(); this.scalarTypes = new ScalarType[types.length]; for (int i = 0; i < types.length; i++) { scalarTypes[i] = typeManager.getScalarType(types[i]); } MethodHandles.Lookup lookup = MethodHandles.publicLookup(); this.handle = lookup.findConstructor(someClass, typeFor(types)); }
/** * Returns a correctly typed {@link MethodHandle} for the no-arg ctor of the given class. */ static final MethodHandle findAttributeImplCtor(Class<? extends AttributeImpl> clazz) { try { return lookup.findConstructor(clazz, NO_ARG_CTOR).asType(NO_ARG_RETURNING_ATTRIBUTEIMPL); } catch (NoSuchMethodException | IllegalAccessException e) { throw new IllegalArgumentException("Cannot lookup accessible no-arg constructor for: " + clazz.getName(), e); } }
@SuppressWarnings("unchecked") private static <S> Supplier<S> constructorOf(Class<S> impl) { try { return (Supplier<S>) Unchecked.supplier(MethodHandles.lookup().findConstructor(impl, MethodType.methodType(void.class))::invoke); } catch (ReflectiveOperationException e) { throw new IllegalArgumentException("Couldn't find public constructor of " + impl, e); } }
@Test public void testConstructorMethodHandleUpdated() throws Throwable { assert __version__() == 0; MethodHandles.Lookup lookup = MethodHandles.lookup(); MethodHandle handle = lookup.findConstructor(A.class, MethodType.methodType(void.class, int.class)); assertEquals(12, ((A) handle.invoke(12)).field); __toVersion__(1); assertEquals(120, ((A) handle.invoke(12)).field); __toVersion__(0); assert __version__() == 0; }
private static MethodHandle lookupNewJarFile() { try { return MethodHandles.lookup().findConstructor(java.util.jar.JarFile.class, MethodType.methodType(void.class, java.io.File.class, java.lang.Boolean.TYPE, Integer.TYPE, runtimeVersionClass())); } catch (Throwable t) { return null; } } private static Class<?> runtimeVersionClass() throws ClassNotFoundException {
private UnaryOperator<Key> checkForCopyCtor(final Class<?> declType, final Class<?> paramType) { final MethodHandles.Lookup lookup = MethodHandles.lookup(); final MethodHandle handle = doPrivileged((PrivilegedAction<MethodHandle>) () -> { try { return lookup.findConstructor(declType, MethodType.methodType(void.class, paramType)); } catch (NoSuchMethodException | IllegalAccessException e) { return null; } }); return handle == null ? null : produceOp(handle); }