public static CallSite lambdaCapture( MethodHandles.Lookup callerLookup, String name, MethodType type, MethodType samMethodType, MethodHandle implMethod, MethodType instantiatedMethodType) { try { // delegate to metafactory, we may choose to generate code ourselves in the future. return LambdaMetafactory.metafactory( callerLookup, name, type, samMethodType, implMethod, instantiatedMethodType); } catch (LambdaConversionException e) { throwIfUnchecked(e); throw new RuntimeException(e); } } }
? LambdaMetafactory.altMetafactory(caller, "apply", invokedType, target.type().generic(), target, target.type(), 1) : LambdaMetafactory.metafactory(caller, "apply", invokedType,
private Function createGetterFunction(MethodHandles.Lookup lookup) { Class<?> declaringClass = getterMethod.getDeclaringClass(); CallSite getterSite; try { getterSite = LambdaMetafactory.metafactory(lookup, "apply", MethodType.methodType(Function.class), MethodType.methodType(Object.class, Object.class), lookup.findVirtual(declaringClass, getterMethod.getName(), MethodType.methodType(propertyType)), MethodType.methodType(propertyType, declaringClass)); } catch (LambdaConversionException | NoSuchMethodException | IllegalAccessException e) { throw new IllegalArgumentException("Lambda creation failed for getterMethod (" + getterMethod + ").", e); } try { return (Function) getterSite.getTarget().invokeExact(); } catch (Throwable e) { throw new IllegalArgumentException("Lambda creation failed for getterMethod (" + getterMethod + ").", e); } }
private BiConsumer createSetterFunction(MethodHandles.Lookup lookup) { if (setterMethod == null) { return null; } Class<?> declaringClass = setterMethod.getDeclaringClass(); CallSite setterSite; try { setterSite = LambdaMetafactory.metafactory(lookup, "accept", MethodType.methodType(BiConsumer.class), MethodType.methodType(void.class, Object.class, Object.class), lookup.findVirtual(declaringClass, setterMethod.getName(), MethodType.methodType(void.class, propertyType)), MethodType.methodType(void.class, declaringClass, propertyType)); } catch (LambdaConversionException | NoSuchMethodException | IllegalAccessException e) { throw new IllegalArgumentException("Lambda creation failed for setterMethod (" + setterMethod + ").", e); } try { return (BiConsumer) setterSite.getTarget().invokeExact(); } catch (Throwable e) { throw new IllegalArgumentException("Lambda creation failed for setterMethod (" + setterMethod + ").", e); } }
final MethodType subscription = MethodType.methodType(void.class, parameterClazz); final MethodHandle target = caller.findVirtual(clazz, method.getName(), subscription); final CallSite site = LambdaMetafactory.metafactory( caller, "invoke",
final MethodHandles.Lookup lookup = MethodHandles.lookup(); MethodHandle mh=lookup.unreflect(reflected); IntBinaryOperator lambda=(IntBinaryOperator)LambdaMetafactory.metafactory( lookup, "applyAsInt", MethodType.methodType(IntBinaryOperator.class), mh.type(), mh, mh.type()).getTarget().invokeExact();
@SuppressWarnings("unchecked") public static <T> T createLambda(Object instance, Method instanceMethod, Class<?> functionalIntfCls) { try { Lookup lookup = LOOKUP.in(instanceMethod.getDeclaringClass()); allowedModesField.set(lookup, ALL_MODES); Method intfMethod = findAbstractMethod(functionalIntfCls); MethodHandle methodHandle = lookup.unreflect(instanceMethod); MethodType intfMethodType = MethodType.methodType(intfMethod.getReturnType(), intfMethod.getParameterTypes()); MethodType instanceMethodType = MethodType .methodType(instanceMethod.getReturnType(), instanceMethod.getParameterTypes()); CallSite callSite = LambdaMetafactory.metafactory( lookup, intfMethod.getName(), MethodType.methodType(functionalIntfCls, instance.getClass()), intfMethodType, methodHandle, instanceMethodType); return (T) callSite.getTarget().bindTo(instance).invoke(); } catch (Throwable e) { throw new IllegalStateException("Failed to create lambda from " + instanceMethod, e); } }
@SuppressWarnings("unchecked") public static <T> T createLambda(Method instanceMethod, Class<?> functionalIntfCls) { try { Lookup lookup = LOOKUP.in(instanceMethod.getDeclaringClass()); allowedModesField.set(lookup, ALL_MODES); Method intfMethod = findAbstractMethod(functionalIntfCls); MethodHandle methodHandle = lookup.unreflect(instanceMethod); MethodType intfMethodType = MethodType.methodType(intfMethod.getReturnType(), intfMethod.getParameterTypes()); MethodType instanceMethodType = methodHandle.type(); CallSite callSite = LambdaMetafactory.metafactory( lookup, intfMethod.getName(), MethodType.methodType(functionalIntfCls), intfMethodType, methodHandle, instanceMethodType); return (T) callSite.getTarget().invoke(); } catch (Throwable e) { throw new IllegalStateException("Failed to create lambda from " + instanceMethod, e); } }
private static CallSite createCallSite(String signatureName, MethodHandles.Lookup lookup, MethodHandle methodHandle, MethodType instantiatedMethodType, MethodType signature, Class<?> interfaceClass) throws LambdaConversionException { return LambdaMetafactory.metafactory( lookup, signatureName, MethodType.methodType(interfaceClass), signature, methodHandle, instantiatedMethodType); }
public static CallSite lambdaCapture( MethodHandles.Lookup callerLookup, String name, MethodType type, MethodType samMethodType, MethodHandle implMethod, MethodType instantiatedMethodType) { try { // delegate to metafactory, we may choose to generate code ourselves in the future. return LambdaMetafactory.metafactory( callerLookup, name, type, samMethodType, implMethod, instantiatedMethodType); } catch (LambdaConversionException e) { throwIfUnchecked(e); throw new RuntimeException(e); } } }
public static CallSite lambdaCapture( MethodHandles.Lookup callerLookup, String name, MethodType type, MethodType samMethodType, MethodHandle implMethod, MethodType instantiatedMethodType) { try { // delegate to metafactory, we may choose to generate code ourselves in the future. return LambdaMetafactory.metafactory( callerLookup, name, type, samMethodType, implMethod, instantiatedMethodType); } catch (LambdaConversionException e) { throwIfUnchecked(e); throw new RuntimeException(e); } } }
import java.lang.invoke.*; import java.lang.ref.WeakReference; public class LambdaClassUnloading { public static void main(String[] args) throws Throwable { MethodHandles.Lookup l = MethodHandles.lookup(); MethodType t=MethodType.methodType(void.class); Runnable r=(Runnable)LambdaMetafactory.metafactory(l, "run", MethodType.methodType(Runnable.class), t, l.findStatic(LambdaClassUnloading.class, "testMethod", t), t) .getTarget().invokeExact(); System.out.println("generated "+r); r.run(); WeakReference<Class<?>> ref=new WeakReference<>(r.getClass()); r=null; while(ref.get()!=null) System.gc(); System.out.println("class collected"); } private static void testMethod() { System.out.println("testMethod() called"); } }
import java.lang.invoke.*; public class ManualLambda { public static void main(String[] args) throws Throwable { MethodHandles.Lookup me=MethodHandles.lookup(); MethodType t=MethodType.methodType(void.class); MethodType rt=MethodType.methodType(Runnable.class); CallSite site = LambdaMetafactory.metafactory( me, "run", rt, t, me.findStatic(ManualLambda.class, "sayHello", t), t); MethodHandle factory=site.getTarget(); Runnable r=(Runnable)factory.invoke(); System.out.println("created lambda: "+r); r.run(); } private static void sayHello() { System.out.println("hello world"); } }
public static Object asFunctionalInterface(Lookup caller, Class<?> type, MethodHandle handle) throws Throwable { for (Method method : type.getMethods()) { if (!method.isDefault() && !isStatic(method.getModifiers())) { MethodType lambdaType = methodType(method.getReturnType(), method.getParameterTypes()); CallSite callSite = LambdaMetafactory.metafactory( caller, method.getName(), methodType(type), lambdaType, handle, lambdaType); return callSite.dynamicInvoker().invoke(); } } throw new RuntimeException(message("handle_conversion_failed", handle, type)); }
private static void createSupplier() throws Throwable { MethodHandles.Lookup caller = MethodHandles.lookup(); MethodType methodType = MethodType.methodType(Object.class); MethodType actualMethodType = MethodType.methodType(String.class); MethodType invokedType = MethodType.methodType(Supplier.class); CallSite site = LambdaMetafactory.metafactory(caller, "get", invokedType, methodType, caller.findStatic(LambdaFactory.class, "print", actualMethodType), methodType); MethodHandle factory = site.getTarget(); Supplier<String> r = (Supplier<String>) factory.invoke(); System.out.println(r.get()); } }
protected <T, R> T getConstructorAs(Class<T> targetClass, String methodName, Class<? extends R> clazz, Class<?>... args) { try { MethodType methodType = methodType(clazz, args); MethodHandles.Lookup lookup = MethodHandles.lookup(); MethodHandle handle = lookup.findConstructor(clazz, methodType(void.class, args)); MethodType targetType = methodType(targetClass); CallSite callSite = LambdaMetafactory.metafactory(lookup, methodName, targetType, methodType.generic(), handle, methodType); return (T) callSite.getTarget().invoke(); } catch (Throwable t) { throw new RuntimeException("Unable to create function for constructor of " + clazz.getName(), t); } } }
public MethodHandle getConsumerMethodFactory(Class<?> ownerClass, MethodHandle methodHandle, Class<?> eventClass) { MethodHandle lambdaMethodFactory; try { lambdaMethodFactory = lambdaMethodsCache.get(methodHandle, () -> { MethodType type = MethodType.methodType(void.class, eventClass); MethodType consumerType = MethodType.methodType(Consumer.class, ownerClass); MethodHandles.Lookup caller = lambdaLookupProvider.apply(ownerClass); CallSite site; try { site = LambdaMetafactory.metafactory( caller, "accept", consumerType, type.changeParameterType(0, Object.class), methodHandle, type); } catch (LambdaConversionException e) { throw new RuntimeException("Unable to build lambda consumer " + methodHandle ,e); } return site.getTarget(); }); } catch (ExecutionException e) { throw new RuntimeException("Unable to get lambda factory", e); } return lambdaMethodFactory; }
@Override public Callable<Void> launchService(String[] arguments, ITransformingClassLoader launchClassLoader) { try { Class<?> callableLaunch = Class.forName(System.getProperty("test.harness.callable")); final MethodHandles.Lookup lookup = MethodHandles.lookup(); final CallSite site = LambdaMetafactory.metafactory(lookup, "get", MethodType.methodType(Supplier.class), MethodType.methodType(Object.class), lookup.findStatic(callableLaunch, "supplier", MethodType.methodType(Callable.class)), MethodType.methodType(Supplier.class)); final Supplier<Callable<Void>> supplier = (Supplier<Callable<Void>>) site.getTarget().invoke(); return supplier.get(); } catch (ClassNotFoundException | NoSuchMethodException | LambdaConversionException | IllegalAccessException | InstantiationException e) { throw new RuntimeException(e); } catch (Throwable throwable) { throwable.printStackTrace(); } return ()-> null; } }
protected Object createLambda(ConfigKey itemPrefix, Method factoryMethod) { MethodHandles.Lookup lookup = MethodHandles.lookup(); // The consumer is the last parameter Class<?> consumerType = factoryMethod.getParameterTypes()[factoryMethod.getParameterCount() - 1]; try { Method acceptMethod = null; for (Method method : consumerType.getMethods()) { if (method.getName().equals(ACCEPT)) { acceptMethod = method; } } if (acceptMethod == null) { return null; } MethodHandle target = lookup.findVirtual(ConfigurableManager.class, "subresourceAdded", MethodType.methodType(void.class, ConfigKey.class, Object.class)); MethodType samType = MethodType.methodType(void.class, acceptMethod.getParameterTypes()[0]); MethodHandle mh = LambdaMetafactory.metafactory( lookup, ACCEPT, MethodType.methodType(consumerType, ConfigurableManager.class, ConfigKey.class), samType, target, samType) .getTarget(); return mh.invoke(this, itemPrefix); } catch (Throwable t) { throw new RuntimeException(t); } }
public static <T> T createLambda(Object instance, Method instanceMethod, Class<?> functionalIntfCls) throws Throwable { Method intfMethod = findAbstractMethod(functionalIntfCls); MethodHandle methodHandle = lookup.unreflect(instanceMethod); MethodType intfMethodType = MethodType.methodType(intfMethod.getReturnType(), intfMethod.getParameterTypes()); MethodType instanceMethodType = MethodType .methodType(instanceMethod.getReturnType(), instanceMethod.getParameterTypes()); CallSite callSite = LambdaMetafactory.metafactory( lookup, intfMethod.getName(), MethodType.methodType(functionalIntfCls, instance.getClass()), intfMethodType, methodHandle, instanceMethodType); //noinspection unchecked return (T) callSite.getTarget().bindTo(instance).invoke(); }