0

I found a bug in my code which is similar to this gist: https://gist.github.com/rompetroll/667bf46ac0168a92497a

If I define a lambda like this directly in a .thenApply(Func) method, I will get the unreported exception compile error.

Does not work:

Optional o = Optional.of(nullable);

return myComplFuture
  .thenApply(o -> o.orElseThrow(() -> new RuntimeException("nah"));

This does work:

Optional o = Optional.of(nullable);
Function<Optional<String>, String> stringOrException = o -> o.orElseThrow(() -> new RuntimeException("nah");

return myComplFuture
  .thenApply(stringOrException);

So, please can anyone tell me, why the first does not compile but the latter does? Can also please anyone tell me how to mark this error in eclipse?

Things I tried: Window -> Preferences -> Java -> Compiler -> Errors/Warnings But I didn't find anything which would show me the compiler error in eclipse. In fact, eclipse does not show a red x next to this line. But everytime I run mvn test the compiler complains as shown in the gist.

Thanks in advance!

Benjamin Marwell
  • 1,173
  • 1
  • 13
  • 36
  • No repro on `jdk1.8.0_111`, but repro on `jdk1.8.0_11`, so it seems like it's a JDK bug that got fixed later (and since eclipse uses its own compiler it doesn't have the bug), you could add `maven-enforcer-plugin` so it warns you when you use a JDK version that is too old for your project – Ferrybig Jan 13 '17 at 09:10
  • I was testing on ibm-j9 which is build on jdk1.8.9u51. I'll look into it and report back. – Benjamin Marwell Jan 13 '17 at 09:29
  • @Ferrybig I think it is a little bit different, though, because I don't use a Typed Class containing an Exception. You may also add your comment as an answer, I'll happily accept it. It was a compiler issue. Only a dup if http://stackoverflow.com/questions/24986279/java-8-generics-exceptions-compile-time-error-when-using-a-lambda-expression is also solvable by upgrading javac (SDK). – Benjamin Marwell Jan 13 '17 at 12:46
  • Also, my question is: How Do I make eclipse show this kind of error? Thanks! – Benjamin Marwell Jan 13 '17 at 13:26
  • It's not possible to make eclipse show these, as its valid java code according to the grammar, just javac has a bug with the code in question (that got fixed later in newer versions). According to [How to configure Eclipse to compile using Oracle javac 1.7.0_09?](http://stackoverflow.com/a/14186301/1542723 ) eclipse uses its own compiler to provide proper syntax highlinthing and other things, and both sides have their unique bugs for certain kings of valid java syntax – Ferrybig Jan 13 '17 at 13:33

1 Answers1

0

Afaik, there is no way to let the Eclipse IDE use javac, but you can make Maven use the Eclipse compiler, to avoid inconsistencies between the editor highlighting and maven builds.

Regarding this specific problem, it seems to be a compiler bug, related to the type inference, that exists in all versions up to update 77, but not in update 92 and newer. So this specific problem can also be solved by updating the jdk.

Community
  • 1
  • 1
Holger
  • 285,553
  • 42
  • 434
  • 765