Search in sources :

Example 6 with CallSite

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);
    }
}
Also used : MethodHandles(java.lang.invoke.MethodHandles) Consumer(java.util.function.Consumer) BiConsumer(java.util.function.BiConsumer) CallSite(java.lang.invoke.CallSite) MethodHandle(java.lang.invoke.MethodHandle)

Example 7 with CallSite

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);
    }
}
Also used : MethodHandles(java.lang.invoke.MethodHandles) CallSite(java.lang.invoke.CallSite) Supplier(java.util.function.Supplier) MethodHandle(java.lang.invoke.MethodHandle)

Example 8 with CallSite

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);
    }
}
Also used : MethodHandles(java.lang.invoke.MethodHandles) CallSite(java.lang.invoke.CallSite) BiConsumer(java.util.function.BiConsumer) MethodHandle(java.lang.invoke.MethodHandle)

Example 9 with CallSite

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);
    }
}
Also used : MethodType(java.lang.invoke.MethodType) CallSite(java.lang.invoke.CallSite) Method(java.lang.reflect.Method) MethodHandle(java.lang.invoke.MethodHandle)

Example 10 with CallSite

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);
}
Also used : CallSite(java.lang.invoke.CallSite) MethodHandle(java.lang.invoke.MethodHandle)

Aggregations

CallSite (java.lang.invoke.CallSite)18 MethodHandle (java.lang.invoke.MethodHandle)15 MethodType (java.lang.invoke.MethodType)6 MethodHandles (java.lang.invoke.MethodHandles)5 LambdaReceiver_A (LambdaReceiver_anotherpkg.LambdaReceiver_A)2 Method (java.lang.reflect.Method)2 BiConsumer (java.util.function.BiConsumer)2 BiPredicate (java.util.function.BiPredicate)2 Predicate (java.util.function.Predicate)2 Supplier (java.util.function.Supplier)2 Method (org.elasticsearch.painless.Definition.Method)2 SystemClock (android.os.SystemClock)1 AndroidJUnit4 (androidx.test.ext.junit.runners.AndroidJUnit4)1 Truth.assertThat (com.google.common.truth.Truth.assertThat)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileDescriptor (java.io.FileDescriptor)1 PrintStream (java.io.PrintStream)1 Serializable (java.io.Serializable)1 Field (java.lang.reflect.Field)1 Socket (java.net.Socket)1