public void testReturnVoidDef() { assumeFalse("JDK is JDK 9", Constants.JRE_IS_MINIMUM_JAVA9); Exception expected = expectScriptThrows(LambdaConversionException.class, () -> { exec("StringBuilder b = new StringBuilder(); def l = [1, 2]; l.stream().mapToLong(b::setLength);"); }); assertThat(expected.getMessage(), containsString("Type mismatch for lambda expected return: void is not convertible to long")); expected = expectScriptThrows(LambdaConversionException.class, () -> { exec("def b = new StringBuilder(); def l = [1, 2]; l.stream().mapToLong(b::setLength);"); }); assertThat(expected.getMessage(), containsString("Type mismatch for lambda expected return: void is not convertible to long")); expected = expectScriptThrows(LambdaConversionException.class, () -> { exec("def b = new StringBuilder(); List l = [1, 2]; l.stream().mapToLong(b::setLength);"); }); assertThat(expected.getMessage(), containsString("Type mismatch for lambda expected return: void is not convertible to long")); }
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); }
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; }
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; }
private static boolean mf(MethodType mti, MethodType mtf) { try { LambdaMetafactory.metafactory(l, "m", mti,mtf,h,mtf); } catch(LambdaConversionException e) { return true; } return false; }
private static CallSite createCallSite(String signatureName, MethodHandles.Lookup lookup, MethodHandle methodHandle, MethodType instantiatedMethodType, MethodType signature, Class<?> interfaceClass) throws LambdaConversionException { return LambdaMetafactory.metafactory( lookup, signatureName, MethodType.methodType(interfaceClass), signature, methodHandle, instantiatedMethodType); }
private void buildFactory(String name, MethodHandles.Lookup caller, MethodType fact, MethodType getter) throws LambdaConversionException, IllegalAccessException { CallSite site = LambdaMetafactory.metafactory(caller, name, fact, getter, target, getter); factory = site.getTarget(); }