java.lang.invoke.LambdaMetafactory Java Examples
The following examples show how to use
java.lang.invoke.LambdaMetafactory.
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 openjdk-jdk8u with GNU General Public License v2.0 | 6 votes |
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 |
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-jdk9 with GNU General Public License v2.0 | 6 votes |
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: LambdaReceiverBridge.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 6 votes |
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, LambdaReceiverBridge.class); MethodType A = mt(int.class, LambdaReceiver_A.class); MethodType mti = mt(IA.class); CallSite cs = LambdaMetafactory.altMetafactory(l, "m", mti,X,h,X, LambdaMetafactory.FLAG_BRIDGES, 1, A); 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 #5
Source File: LambdaReceiverBridge.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
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, LambdaReceiverBridge.class); MethodType A = mt(int.class, LambdaReceiver_A.class); MethodType mti = mt(IA.class); CallSite cs = LambdaMetafactory.altMetafactory(l, "m", mti,X,h,X, LambdaMetafactory.FLAG_BRIDGES, 1, A); 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 #6
Source File: LambdaReceiverBridge.java From jdk8u60 with GNU General Public License v2.0 | 6 votes |
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, LambdaReceiverBridge.class); MethodType A = mt(int.class, LambdaReceiver_A.class); MethodType mti = mt(IA.class); CallSite cs = LambdaMetafactory.altMetafactory(l, "m", mti,X,h,X, LambdaMetafactory.FLAG_BRIDGES, 1, A); 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 #7
Source File: LambdaReceiverBridge.java From native-obfuscator with GNU General Public License v3.0 | 6 votes |
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, LambdaReceiverBridge.class); MethodType A = mt(int.class, LambdaReceiver_A.class); MethodType mti = mt(IA.class); CallSite cs = LambdaMetafactory.altMetafactory(l, "m", mti,X,h,X, LambdaMetafactory.FLAG_BRIDGES, 1, A); 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 #8
Source File: LambdaReceiver.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 6 votes |
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 #9
Source File: LambdaReceiverBridge.java From TencentKona-8 with GNU General Public License v2.0 | 6 votes |
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, LambdaReceiverBridge.class); MethodType A = mt(int.class, LambdaReceiver_A.class); MethodType mti = mt(IA.class); CallSite cs = LambdaMetafactory.altMetafactory(l, "m", mti,X,h,X, LambdaMetafactory.FLAG_BRIDGES, 1, A); 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 #10
Source File: LambdaToMethodBridgeBuilder.java From aws-sdk-java-v2 with Apache License 2.0 | 6 votes |
public T build() { try { MethodHandle targetMethodHandle = targetMethod.map( m -> invokeSafely(() -> LOOKUP.unreflect(m)), c -> invokeSafely(() -> LOOKUP.unreflectConstructor(c))); return lambdaType.cast( LambdaMetafactory.metafactory(LOOKUP, lambdaMethodName, MethodType.methodType(lambdaType), MethodType.methodType(postEraseLambdaReturnType, postEraseLambdaParameters), targetMethodHandle, MethodType.methodType(preEraseLambdaReturnType, preEraseLambdaParameters)) .getTarget() .invoke()); } catch (Throwable e) { throw new IllegalArgumentException("Failed to generate method handle.", e); } }
Example #11
Source File: LambdaReceiver.java From TencentKona-8 with GNU General Public License v2.0 | 6 votes |
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 #12
Source File: LambdaReceiver.java From jdk8u-jdk with GNU General Public License v2.0 | 6 votes |
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 #13
Source File: LambdaCapture.java From presto with Apache License 2.0 | 6 votes |
public static CallSite lambdaCapture( MethodHandles.Lookup callerLookup, String name, MethodType type, MethodType samMethodType, MethodHandle implMethod, MethodType instantiatedMethodType) { try { // delegate to metafactory, we may choose to generate code ourselves in the future. return LambdaMetafactory.metafactory( callerLookup, name, type, samMethodType, implMethod, instantiatedMethodType); } catch (LambdaConversionException e) { throwIfUnchecked(e); throw new RuntimeException(e); } }
Example #14
Source File: LambdaReceiverBridge.java From jdk8u-jdk with GNU General Public License v2.0 | 6 votes |
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, LambdaReceiverBridge.class); MethodType A = mt(int.class, LambdaReceiver_A.class); MethodType mti = mt(IA.class); CallSite cs = LambdaMetafactory.altMetafactory(l, "m", mti,X,h,X, LambdaMetafactory.FLAG_BRIDGES, 1, A); 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 #15
Source File: LambdaReceiverBridge.java From openjdk-jdk8u with GNU General Public License v2.0 | 6 votes |
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, LambdaReceiverBridge.class); MethodType A = mt(int.class, LambdaReceiver_A.class); MethodType mti = mt(IA.class); CallSite cs = LambdaMetafactory.altMetafactory(l, "m", mti,X,h,X, LambdaMetafactory.FLAG_BRIDGES, 1, A); 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 #16
Source File: LambdaReceiverBridge.java From dragonwell8_jdk with GNU General Public License v2.0 | 6 votes |
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, LambdaReceiverBridge.class); MethodType A = mt(int.class, LambdaReceiver_A.class); MethodType mti = mt(IA.class); CallSite cs = LambdaMetafactory.altMetafactory(l, "m", mti,X,h,X, LambdaMetafactory.FLAG_BRIDGES, 1, A); 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 #17
Source File: LambdaMetafactoryUtils.java From servicecomb-java-chassis with Apache License 2.0 | 6 votes |
@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); } }
Example #18
Source File: LambdaReceiver.java From jdk8u60 with GNU General Public License v2.0 | 6 votes |
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 #19
Source File: LambdaReceiver.java From dragonwell8_jdk with GNU General Public License v2.0 | 6 votes |
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 #20
Source File: LambdaReturn.java From jdk8u-jdk with GNU General Public License v2.0 | 5 votes |
private static void amf(List<String> errs, MethodHandle h, MethodType mts, MethodType mtf, MethodType mtb, boolean shouldWork) { MethodType mti = mt(I.class); try { LambdaMetafactory.altMetafactory(l, "m", mti, mts,h,mtf, LambdaMetafactory.FLAG_BRIDGES, 1, mtb); } catch(LambdaConversionException e) { if (shouldWork) errs.add("Error: Should work h=" + h + " s=" + mts + " -- f=" + mtf + " / b=" + mtb + " got: " + e); return; } if (!shouldWork) errs.add("Error: Should fail h=" + h + " s=" + mts + " -- f=" + mtf + " / b=" + mtb); }
Example #21
Source File: SerializedLambdaTest.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 5 votes |
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 #22
Source File: SerializedLambdaTest.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 5 votes |
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 #23
Source File: SerializedLambdaTest.java From openjdk-jdk8u with GNU General Public License v2.0 | 5 votes |
public void testAltStdNonser() throws Throwable { MethodHandle fooMH = MethodHandles.lookup().findStatic(SerializedLambdaTest.class, "foo", predicateMT); // Alt metafactory, non-serializable target: not serializable CallSite cs = LambdaMetafactory.altMetafactory(MethodHandles.lookup(), "test", MethodType.methodType(Predicate.class), predicateMT, fooMH, stringPredicateMT, 0); assertNotSerial((Predicate<String>) cs.getTarget().invokeExact(), fooAsserter); }
Example #24
Source File: T8032697.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 5 votes |
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 #25
Source File: T8032711.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 5 votes |
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 #26
Source File: SerializedLambdaTest.java From jdk8u-jdk with GNU General Public License v2.0 | 5 votes |
public void testAltStdSer() throws Throwable { MethodHandle fooMH = MethodHandles.lookup().findStatic(SerializedLambdaTest.class, "foo", predicateMT); // Alt metafactory, serializable target, no FLAG_SERIALIZABLE: not serializable CallSite cs = LambdaMetafactory.altMetafactory(MethodHandles.lookup(), "test", MethodType.methodType(SerPredicate.class), predicateMT, fooMH, stringPredicateMT, 0); assertNotSerial((SerPredicate<String>) cs.getTarget().invokeExact(), fooAsserter); // Alt metafactory, serializable marker, no FLAG_SERIALIZABLE: not serializable cs = LambdaMetafactory.altMetafactory(MethodHandles.lookup(), "test", MethodType.methodType(Predicate.class), predicateMT, fooMH, stringPredicateMT, LambdaMetafactory.FLAG_MARKERS, 1, Serializable.class); assertNotSerial((Predicate<String>) cs.getTarget().invokeExact(), fooAsserter); }
Example #27
Source File: LambdaReturn.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 5 votes |
private static void amf(List<String> errs, MethodHandle h, MethodType mts, MethodType mtf, MethodType mtb, boolean shouldWork) { MethodType mti = mt(I.class); try { LambdaMetafactory.altMetafactory(l, "m", mti, mts,h,mtf, LambdaMetafactory.FLAG_BRIDGES, 1, mtb); } catch(LambdaConversionException e) { if (shouldWork) errs.add("Error: Should work h=" + h + " s=" + mts + " -- f=" + mtf + " / b=" + mtb + " got: " + e); return; } if (!shouldWork) errs.add("Error: Should fail h=" + h + " s=" + mts + " -- f=" + mtf + " / b=" + mtb); }
Example #28
Source File: LambdaMetafactoryUtils.java From servicecomb-java-chassis with Apache License 2.0 | 5 votes |
@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 #29
Source File: SerializedLambdaTest.java From openjdk-jdk8u with GNU General Public License v2.0 | 5 votes |
public void testAltStdSer() throws Throwable { MethodHandle fooMH = MethodHandles.lookup().findStatic(SerializedLambdaTest.class, "foo", predicateMT); // Alt metafactory, serializable target, no FLAG_SERIALIZABLE: not serializable CallSite cs = LambdaMetafactory.altMetafactory(MethodHandles.lookup(), "test", MethodType.methodType(SerPredicate.class), predicateMT, fooMH, stringPredicateMT, 0); assertNotSerial((SerPredicate<String>) cs.getTarget().invokeExact(), fooAsserter); // Alt metafactory, serializable marker, no FLAG_SERIALIZABLE: not serializable cs = LambdaMetafactory.altMetafactory(MethodHandles.lookup(), "test", MethodType.methodType(Predicate.class), predicateMT, fooMH, stringPredicateMT, LambdaMetafactory.FLAG_MARKERS, 1, Serializable.class); assertNotSerial((Predicate<String>) cs.getTarget().invokeExact(), fooAsserter); }
Example #30
Source File: T8032704.java From openjdk-jdk8u with GNU General Public License v2.0 | 5 votes |
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; }