Java Code Examples for java.lang.invoke.LambdaMetafactory#metafactory()

The following examples show how to use java.lang.invoke.LambdaMetafactory#metafactory() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LambdaReceiver.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Throwable {
    l = MethodHandles.lookup();
    h = l.findVirtual(LambdaReceiver_A.class, "f", mt(int.class));
    MethodType X = mt(int.class, LambdaReceiver.class);
    MethodType A = mt(int.class, LambdaReceiver_A.class);
    MethodType mti = mt(IA.class);
    CallSite cs = LambdaMetafactory.metafactory(l, "m", mti,A,h,X);
    IA p = (IA)cs.dynamicInvoker().invoke();
    LambdaReceiver_A lra = new LambdaReceiver_A();
    try {
        p.m(lra);
    } catch (ClassCastException cce) {
        return;
    }
    throw new AssertionError("CCE expected");
}
 
Example 2
Source File: LambdaReceiver.java    From native-obfuscator with GNU General Public License v3.0 6 votes vote down vote up
public static void main(String[] args) throws Throwable {
    l = MethodHandles.lookup();
    h = l.findVirtual(LambdaReceiver_A.class, "f", mt(int.class));
    MethodType X = mt(int.class, LambdaReceiver.class);
    MethodType A = mt(int.class, LambdaReceiver_A.class);
    MethodType mti = mt(IA.class);
    CallSite cs = LambdaMetafactory.metafactory(l, "m", mti,A,h,X);
    IA p = (IA)cs.dynamicInvoker().invoke();
    LambdaReceiver_A lra = new LambdaReceiver_A();
    try {
        p.m(lra);
    } catch (ClassCastException cce) {
        return;
    }
    throw new AssertionError("CCE expected");
}
 
Example 3
Source File: LambdaReceiver.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Throwable {
    l = MethodHandles.lookup();
    h = l.findVirtual(LambdaReceiver_A.class, "f", mt(int.class));
    MethodType X = mt(int.class, LambdaReceiver.class);
    MethodType A = mt(int.class, LambdaReceiver_A.class);
    MethodType mti = mt(IA.class);
    CallSite cs = LambdaMetafactory.metafactory(l, "m", mti,A,h,X);
    IA p = (IA)cs.dynamicInvoker().invoke();
    LambdaReceiver_A lra = new LambdaReceiver_A();
    try {
        p.m(lra);
    } catch (ClassCastException cce) {
        return;
    }
    throw new AssertionError("CCE expected");
}
 
Example 4
Source File: T8032711.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
private static boolean mf(Class<?> k) {
    try {
        LambdaMetafactory.metafactory(l, "m",
            mt(I.class),mt(k),h,mt(void.class));
    } catch(LambdaConversionException e) {
        return true;
    }
    return false;
}
 
Example 5
Source File: SerializedLambdaTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void testDirectStdNonser() throws Throwable {
    MethodHandle fooMH = MethodHandles.lookup().findStatic(SerializedLambdaTest.class, "foo", predicateMT);

    // Standard metafactory, non-serializable target: not serializable
    CallSite cs = LambdaMetafactory.metafactory(MethodHandles.lookup(),
                                                "test", MethodType.methodType(Predicate.class),
                                                predicateMT, fooMH, stringPredicateMT);
    Predicate<String> p = (Predicate<String>) cs.getTarget().invokeExact();
    assertNotSerial(p, fooAsserter);
}
 
Example 6
Source File: T8032704.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
private static boolean mf(MethodHandles.Lookup l) {
    try {
        LambdaMetafactory.metafactory(l, "close",
            mt(Closeable.class),mt(void.class),h,mt(void.class));
    } catch(LambdaConversionException e) {
        return true;
    }
    return false;
}
 
Example 7
Source File: T8032697.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private static boolean mf(MethodType mti, MethodType mtf) {
    try {
        LambdaMetafactory.metafactory(l, "m", mti,mtf,h,mtf);
    } catch(LambdaConversionException e) {
        return true;
    }
    return false;
}
 
Example 8
Source File: SerializedLambdaTest.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public void testDirectStdSer() throws Throwable {
    MethodHandle fooMH = MethodHandles.lookup().findStatic(SerializedLambdaTest.class, "foo", predicateMT);

    // Standard metafactory, serializable target: not serializable
    CallSite cs = LambdaMetafactory.metafactory(MethodHandles.lookup(),
                                                "test", MethodType.methodType(SerPredicate.class),
                                                predicateMT, fooMH, stringPredicateMT);
    assertNotSerial((SerPredicate<String>) cs.getTarget().invokeExact(), fooAsserter);
}
 
Example 9
Source File: T8032711.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private static boolean mf(Class<?> k) {
    try {
        LambdaMetafactory.metafactory(l, "m",
            mt(I.class),mt(k),h,mt(void.class));
    } catch(LambdaConversionException e) {
        return true;
    }
    return false;
}
 
Example 10
Source File: LambdaMetafactoryUtils.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
@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);
  }
}
 
Example 11
Source File: SerializedLambdaTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void testDirectStdSer() throws Throwable {
    MethodHandle fooMH = MethodHandles.lookup().findStatic(SerializedLambdaTest.class, "foo", predicateMT);

    // Standard metafactory, serializable target: not serializable
    CallSite cs = LambdaMetafactory.metafactory(MethodHandles.lookup(),
                                                "test", MethodType.methodType(SerPredicate.class),
                                                predicateMT, fooMH, stringPredicateMT);
    assertNotSerial((SerPredicate<String>) cs.getTarget().invokeExact(), fooAsserter);
}
 
Example 12
Source File: SerializedLambdaTest.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public void testDirectStdNonser() throws Throwable {
    MethodHandle fooMH = MethodHandles.lookup().findStatic(SerializedLambdaTest.class, "foo", predicateMT);

    // Standard metafactory, non-serializable target: not serializable
    CallSite cs = LambdaMetafactory.metafactory(MethodHandles.lookup(),
                                                "test", MethodType.methodType(Predicate.class),
                                                predicateMT, fooMH, stringPredicateMT);
    Predicate<String> p = (Predicate<String>) cs.getTarget().invokeExact();
    assertNotSerial(p, fooAsserter);
}
 
Example 13
Source File: T8032697.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private static boolean mf(MethodType mti, MethodType mtf) {
    try {
        LambdaMetafactory.metafactory(l, "m", mti,mtf,h,mtf);
    } catch(LambdaConversionException e) {
        return true;
    }
    return false;
}
 
Example 14
Source File: T8032704.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private static boolean mf(MethodHandles.Lookup l) {
    try {
        LambdaMetafactory.metafactory(l, "close",
            mt(Closeable.class),mt(void.class),h,mt(void.class));
    } catch(LambdaConversionException e) {
        return true;
    }
    return false;
}
 
Example 15
Source File: SerializedLambdaTest.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public void testDirectStdNonser() throws Throwable {
    MethodHandle fooMH = MethodHandles.lookup().findStatic(SerializedLambdaTest.class, "foo", predicateMT);

    // Standard metafactory, non-serializable target: not serializable
    CallSite cs = LambdaMetafactory.metafactory(MethodHandles.lookup(),
                                                "test", MethodType.methodType(Predicate.class),
                                                predicateMT, fooMH, stringPredicateMT);
    Predicate<String> p = (Predicate<String>) cs.getTarget().invokeExact();
    assertNotSerial(p, fooAsserter);
}
 
Example 16
Source File: SerializedLambdaTest.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public void testDirectStdSer() throws Throwable {
    MethodHandle fooMH = MethodHandles.lookup().findStatic(SerializedLambdaTest.class, "foo", predicateMT);

    // Standard metafactory, serializable target: not serializable
    CallSite cs = LambdaMetafactory.metafactory(MethodHandles.lookup(),
                                                "test", MethodType.methodType(SerPredicate.class),
                                                predicateMT, fooMH, stringPredicateMT);
    assertNotSerial((SerPredicate<String>) cs.getTarget().invokeExact(), fooAsserter);
}
 
Example 17
Source File: SerializedLambdaTest.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public void testDirectStdSer() throws Throwable {
    MethodHandle fooMH = MethodHandles.lookup().findStatic(SerializedLambdaTest.class, "foo", predicateMT);

    // Standard metafactory, serializable target: not serializable
    CallSite cs = LambdaMetafactory.metafactory(MethodHandles.lookup(),
                                                "test", MethodType.methodType(SerPredicate.class),
                                                predicateMT, fooMH, stringPredicateMT);
    assertNotSerial((SerPredicate<String>) cs.getTarget().invokeExact(), fooAsserter);
}
 
Example 18
Source File: T8032697.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static boolean mf(MethodType mti, MethodType mtf) {
    try {
        LambdaMetafactory.metafactory(l, "m", mti,mtf,h,mtf);
    } catch(LambdaConversionException e) {
        return true;
    }
    return false;
}
 
Example 19
Source File: SerializedLambdaTest.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public void testDirectStdNonser() throws Throwable {
    MethodHandle fooMH = MethodHandles.lookup().findStatic(SerializedLambdaTest.class, "foo", predicateMT);

    // Standard metafactory, non-serializable target: not serializable
    CallSite cs = LambdaMetafactory.metafactory(MethodHandles.lookup(),
                                                "test", MethodType.methodType(Predicate.class),
                                                predicateMT, fooMH, stringPredicateMT);
    Predicate<String> p = (Predicate<String>) cs.getTarget().invokeExact();
    assertNotSerial(p, fooAsserter);
}
 
Example 20
Source File: SerializedLambdaTest.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public void testDirectStdSer() throws Throwable {
    MethodHandle fooMH = MethodHandles.lookup().findStatic(SerializedLambdaTest.class, "foo", predicateMT);

    // Standard metafactory, serializable target: not serializable
    CallSite cs = LambdaMetafactory.metafactory(MethodHandles.lookup(),
                                                "test", MethodType.methodType(SerPredicate.class),
                                                predicateMT, fooMH, stringPredicateMT);
    assertNotSerial((SerPredicate<String>) cs.getTarget().invokeExact(), fooAsserter);
}