Search in sources :

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

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

Example 18 with CallSite

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()));
}
Also used : PrintStream(java.io.PrintStream) Socket(java.net.Socket) Arrays(java.util.Arrays) InvokeDynamicSupport(org.robolectric.internal.bytecode.InvokeDynamicSupport) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ShadowLooper(org.robolectric.shadows.ShadowLooper) ClassParameter(org.robolectric.util.ReflectionHelpers.ClassParameter) MethodHandles(java.lang.invoke.MethodHandles) RunWith(org.junit.runner.RunWith) SystemClock(android.os.SystemClock) Test(org.junit.Test) LooperMode(org.robolectric.annotation.LooperMode) Truth.assertThat(com.google.common.truth.Truth.assertThat) Field(java.lang.reflect.Field) AndroidJUnit4(androidx.test.ext.junit.runners.AndroidJUnit4) Collectors(java.util.stream.Collectors) MethodType(java.lang.invoke.MethodType) CallSite(java.lang.invoke.CallSite) ShadowSystemClock(org.robolectric.shadows.ShadowSystemClock) FileDescriptor(java.io.FileDescriptor) Duration(java.time.Duration) Pattern(java.util.regex.Pattern) MethodType(java.lang.invoke.MethodType) CallSite(java.lang.invoke.CallSite)

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