use of java.lang.invoke.CallSite in project LanternServer by LanternPowered.
the class LambdaFactory method createConsumer.
/**
* Creates a {@link BiConsumer} for the given {@link Method}. If the
* method is static, two parameters are expected. Of it's non static,
* only one parameter is expected which will be the second parameter
* in the {@link BiConsumer}, the first one is the target object.
*
* @param method The method
* @param <A> The parameter type
* @return The bi consumer
*/
public static <A> Consumer<A> createConsumer(Method method) {
if (Modifier.isStatic(method.getModifiers())) {
checkMethodArgument(method.getParameterCount() == 1, "The method \"%s\" doesn't have exactly one parameter, this must be the case for static methods.", method);
} else {
checkMethodArgument(method.getParameterCount() == 0, "The method \"%s\" doesn't have exactly zero parameters, this must be the case for non static methods.", method);
}
checkMethodArgument(method.getReturnType().equals(void.class), "The method \"%s\" return type must be void.", method);
try {
final MethodHandles.Lookup lookup = MethodHandleMagic.trustedLookup().in(method.getDeclaringClass());
final MethodHandle methodHandle = lookup.unreflect(method);
final CallSite callSite = LambdaMetafactory.metafactory(lookup, "accept", consumerMethodType, consumerAcceptMethodType, methodHandle, methodHandle.type());
// Create the bi consumer
return (Consumer<A>) callSite.getTarget().invokeExact();
} catch (Throwable e) {
throw new IllegalStateException("Something went wrong for \"" + formatMethod(method) + "\"", e);
}
}
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 class must have a empty constructor.
*
* @param objectType The object class
* @param <T> The object type
* @return The supplier
*/
public static <T> Supplier<T> createSupplier(Class<T> objectType) {
final int modifier = objectType.getModifiers();
checkState(!Modifier.isAbstract(modifier), "The object class \"%s\" cannot be abstract.", objectType.getName());
checkState(Modifier.isStatic(modifier) || objectType.getEnclosingClass() == null, "The object class \"%s\" cannot be inner and non static", objectType.getName());
final Constructor<?> constructor;
try {
constructor = objectType.getDeclaredConstructor();
} catch (NoSuchMethodException e) {
throw new IllegalArgumentException("The object class \"" + objectType.getName() + "\" doesn't have a empty constructor.");
}
try {
final MethodHandles.Lookup lookup = MethodHandleMagic.trustedLookup().in(objectType);
final MethodHandle methodHandle = lookup.unreflectConstructor(constructor);
// 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 \"" + objectType.getName() + "\"", e);
}
}
use of java.lang.invoke.CallSite in project LanternServer by LanternPowered.
the class LambdaFactory method createBiConsumer.
/**
* Creates a {@link BiConsumer} for the given {@link Method}. If the
* method is static, two parameters are expected. Of it's non static,
* only one parameter is expected which will be the second parameter
* in the {@link BiConsumer}, the first one is the target object.
*
* @param method The method
* @param <A> The first parameter type
* @param <B> The second parameter type
* @return The bi consumer
*/
public static <A, B> BiConsumer<A, B> createBiConsumer(Method method) {
if (Modifier.isStatic(method.getModifiers())) {
checkMethodArgument(method.getParameterCount() == 2, "The method \"%s\" doesn't have exactly two parameters, this must be the case for static methods.", method);
} else {
checkMethodArgument(method.getParameterCount() == 1, "The method \"%s\" doesn't have exactly one parameter, this must be the case for non static methods.", method);
}
checkMethodArgument(method.getReturnType().equals(void.class), "The method \"%s\" return type must be void.", method);
try {
final MethodHandles.Lookup lookup = MethodHandleMagic.trustedLookup().in(method.getDeclaringClass());
final MethodHandle methodHandle = lookup.unreflect(method);
final CallSite callSite = LambdaMetafactory.metafactory(lookup, "accept", biConsumerMethodType, biConsumerAcceptMethodType, methodHandle, methodHandle.type());
// Create the bi consumer
return (BiConsumer<A, B>) 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(Object instance, Method instanceMethod, Class<?> functionalIntfCls) {
try {
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);
}
}
use of java.lang.invoke.CallSite in project elasticsearch by elastic.
the class DefBootstrapTests method testTwoTypes.
public void testTwoTypes() throws Throwable {
CallSite site = DefBootstrap.bootstrap(MethodHandles.publicLookup(), "toString", MethodType.methodType(String.class, Object.class), 0, DefBootstrap.METHOD_CALL, "");
MethodHandle handle = site.dynamicInvoker();
assertDepthEquals(site, 0);
assertEquals("5", (String) handle.invokeExact((Object) 5));
assertDepthEquals(site, 1);
assertEquals("1.5", (String) handle.invokeExact((Object) 1.5f));
assertDepthEquals(site, 2);
// both these should be cached
assertEquals("6", (String) handle.invokeExact((Object) 6));
assertDepthEquals(site, 2);
assertEquals("2.5", (String) handle.invokeExact((Object) 2.5f));
assertDepthEquals(site, 2);
}
Aggregations