use of java.lang.invoke.CallSite in project LanternServer by LanternPowered.
the class LambdaFactory method createSupplier.
/**
* Creates a {@link Supplier} to create objects of the type
* {@link T}. The target method must be static and have zero
* parameters.
*
* @param method The method
* @param <T> The object type
* @return The supplier
*/
public static <T> Supplier<T> createSupplier(Method method) {
checkMethodArgument(Modifier.isStatic(method.getModifiers()), "The method \"%s\" must be static.", method);
checkMethodArgument(method.getParameterCount() == 0, "The method \"%s\" may not have any parameters.", method);
checkMethodArgument(method.getReturnType().equals(void.class), "The method \"%s\" return type must not be void.", method);
try {
final MethodHandles.Lookup lookup = MethodHandleMagic.trustedLookup().in(method.getDeclaringClass());
final MethodHandle methodHandle = lookup.unreflect(method);
// Generate the lambda class
final CallSite callSite = LambdaMetafactory.metafactory(lookup, "get", supplierMethodType, supplierGetMethodType, methodHandle, methodHandle.type());
// Create the supplier
return (Supplier<T>) callSite.getTarget().invokeExact();
} catch (Throwable e) {
throw new IllegalStateException("Something went wrong for \"" + formatMethod(method) + "\"", e);
}
}
use of java.lang.invoke.CallSite in project java-chassis by ServiceComb.
the class LambdaMetafactoryUtils method createLambda.
@SuppressWarnings("unchecked")
public static <T> T createLambda(Method instanceMethod, Class<?> functionalIntfCls) {
if (Modifier.isNative(instanceMethod.getModifiers())) {
// fix "Failed to create lambda from public final native java.lang.Class java.lang.Object.getClass()"
return null;
}
try {
Method intfMethod = findAbstractMethod(functionalIntfCls);
MethodHandle methodHandle = LOOKUP.unreflect(instanceMethod);
MethodType intfMethodType = MethodType.methodType(intfMethod.getReturnType(), intfMethod.getParameterTypes());
// the return type of fluent setter is object instead of void, but we can assume the return type is void. it doesn't matter
MethodType instanceMethodType = MethodType.methodType(intfMethod.getReturnType(), methodHandle.type().parameterList());
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);
}
}
use of java.lang.invoke.CallSite in project robolectric by robolectric.
the class AndroidInterceptorsIntegrationTest method invokeDynamic.
@SuppressWarnings({ "unchecked", "TypeParameterUnusedInFormals" })
private static <T> T invokeDynamic(Class<?> cls, String methodName, Class<?> returnType, ClassParameter<?>... params) throws Throwable {
MethodType methodType = MethodType.methodType(returnType, Arrays.stream(params).map(param -> param.clazz).toArray(Class[]::new));
CallSite callsite = InvokeDynamicSupport.bootstrapIntrinsic(MethodHandles.lookup(), methodName, methodType, cls.getName());
return (T) callsite.dynamicInvoker().invokeWithArguments(Arrays.stream(params).map(param -> param.val).collect(Collectors.toList()));
}
Aggregations