Eclipse Luna and JDK8

25 June 2014, by: Jan Beernink

Today marked the launch of Eclipse Luna, which is the first version of Eclipse which ships with Java 8 support built in. As we like to stay on the cutting edge here at ZEEF, I decided to give this new version of Eclipse a try straight away.

Unfortunately, I ran into a bug in the Java 8 support fairly quickly. Type inference in Luna seems to fail when the return type of a method needs to be inferred from a lambda within a lambda that has been used as a method parameter. For example, the following code snippet compiles normally using javac, but fails in Eclipse Luna:

    Stream.of("test")
        .flatMap(s -> s.chars().mapToObj(i -> Character.valueOf((char)i)))
        .filter(Character::isLowerCase)
        .toArray();

Eclipse seems to infer that the return type of the “i -> Character.valueOf((char)i)” lambda is Object instead of Character, which leads to infer that the return type of flatMap() should be Stream<Object> instead of Stream<Character>. As the method reference used as the filter parameter, the compiler fails on this line as this method reference is not applicable for an argument of type Object.

Fortunately, it’s easy to work around this bug in Eclipse’s compiler. Any change to either the outer or inner lambda which makes the return type explicit allows Eclipse to correctly compile the code. Casting either the inner or the outer lambda to the correct type, assigning either to a variable or replacing either with a method reference allows Eclipse to correctly compile the code. For example, if you were to assign the inner lambda to a variable, you would end up with the following code:

    IntFunction<Character> characterFromInt = i -> Character.valueOf((char)i);
    Stream.of("test")
        .flatMap(s -> s.chars().mapToObj(characterFromInt))
        .filter(Character::isLowerCase)
        .toArray();

The need to for such a workaround is unfortunate as improved type inference is one of the major enhancements in Java 8. Hopefully a fix for this issue will be available soon.

3 comments to “Eclipse Luna and JDK8”

  1. Ricky Clarkson says:

    You really shouldn’t be making your code worse to make Eclipse work. Any Eclipse problem shouldn’t be a showstopper as you can still compile the code with javac, you just have to live with a couple of red lines.

  2. Eduardo says:

    If you expect it to be fixed, please raise an issue, the time you spent writing this blog post should be more than enough to raise an issue, and would be much more effectively in actually getting the problem known by the people who can fix it.

  3. Jan Beernink says:

    I agree that in general you shouldn’t change code to make it work in an IDE when it does in javac. Though having to manually compile code, deploy it to a Java EE server and then launch this server for each change can be quite a hassle(especially when you have been spoiled by JRebel 😉 ). In such cases it might be worth it to slightly adjust the code in order to prevent a drastic reduction in developer productivity.

    @Eduardo
    I actually visited the Eclipse bug tracker first before writing this post. My code snippet was originally meant as a minimal case to reproduce this bug, but fortunately someone else had already reported this issue.

Type your comment below:

Time limit is exhausted. Please reload CAPTCHA.

css.php best counter