1

I've discovered a problem compiling a class that happens only in maven, not inside Eclipse.

The code is the following:

@Test
public void compliationFailOnMaven() {

    Optional<List<String>> list = getDummyList();

    List<Integer> hascodes = list.orElse(Collections.EMPTY_LIST).stream().map(value -> value.hashCode()).collect(toList());

    assertThat(hascodes).isNotNull();
}

private Optional<List<String>> getDummyList() {

    return Optional.ofNullable(new ArrayList<String>(0));
}

If you insert this code into a maven project and try to execute it using mvn clean test it fails due a compilation problem:

[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /java-tests/general-test/src/test/java/com/java8/stream/StreamTest.java:[113,125] incompatible types: java.lang.Object cannot be converted to java.util.List<java.lang.Integer>
[INFO] 1 error
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.844 s
[INFO] Finished at: 2015-11-19T18:07:36+01:00
[INFO] Final Memory: 26M/277M
[INFO] ------------------------------------------------------------------------

However, if you import this project into eclipse, it can compile and execute it without any problem.

The problem is the use of generics. If I replace this line:

List<Integer> hascodes = list.orElse(Collections.EMPTY_LIST).stream().map(value -> value.hashCode()).collect(toList());

with this one:

List<Integer> hascodes = list.orElse(new ArrayList<String>(0)).stream().map(value -> value.hashCode()).collect(toList());

Everything works in both environments: eclipse and maven.

Does anybody know why it's happening this?

Why are they producing different results?

I only have one JVM installed, so both are using the same Java version

11
  • 5
  • Eclipse has a small bug here, I don't think this should compile.
    – Tunaki
    Nov 19, 2015 at 17:22
  • @Tunaki My intuition here is that Eclipse actually has the correct (or my expected) behavior, which is an unchecked conversion from List to List<String> when passing EMPTY_LIST to orElse. But it doesn't really matter, because the problem here is that OP shouldn't be trying to use EMPTY_LIST in the first place.
    – Radiodef
    Nov 19, 2015 at 17:40
  • @Radiodef: No, the error is correct. The return type of the raw signature of collect is Object not List. If it were List, you could perform an unchecked conversion to List<String>, but it isn’t.
    – Holger
    Nov 19, 2015 at 17:50
  • The question is: Why are they producing different results? I only have one JVM installed, so both are using the same Java version
    – jfcorugedo
    Nov 20, 2015 at 10:35

1 Answer 1

1

The fundamental reason why different behavior can be observed in some situations is: Eclipse uses its own compiler "ECJ", which has been explained before. Maven, by contrast, normally delegates to javac - although it can be told to use ECJ, too, if you want to be sure maven builds show the same result as what you see in Eclipse.

Of course both compilers should obey to JLS and thus show the same behavior, but in reality due to the complexity of JLS any real world compiler has bugs, resulting in deviations between compilers.


For this given difference in particular, I don't see why javac should come to different results depending on the argument to orElse(). Note the signature:

public final class Optional<T> {
    ....
    public T orElse(T other)
    ....
}

Note, that only the class is generic, not the method. Since T is List<String> (from the declaration of list), orElse also returns a List<String>, no type inference involved at this stage. Since we know that the tail should compile fine once we have a List<String>, there's no reason any compiler should report an error.

The only problem is in passing the raw List as an argument but that's fully handled by the unchecked warning at this location.

I'm surprised to see javac rejecting a program due to problems with raw types, because most trouble in this area results from https://bugs.openjdk.java.net/browse/JDK-8026527 which however speaks of admitting programs that shouldn't be.

Edit: For completeness: I assumed the following imports:

import java.util.*;
import static java.util.stream.Collectors.toList;

We need a definition of toList to be fully sure what happens.

1
  • Yes, I agree with you. I don't know why the compiler gives different behaviour. I guess maven and eclipse are using different compilers or different options in the compiler. That's the reason of my question.
    – jfcorugedo
    Nov 28, 2015 at 23:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.