I have this code:
Arrays.asList(1L, 2L, 3L, 10L, 20L, 30L, 100L)
.stream()
.map(Bytes::fromMegaBytes) // Function<Long, Bytes>
.map(FileUtils::generateTempFileRunEx) // Function<Bytes, Path>
.flatMap(path -> parameters.getAllocatedCredits() // Map<Object, Integer>
.keySet()
.stream()
.map(group -> Pair.of(group, path))
)
.collect(Collectors.toList())
.forEach(item -> { // item should be Pair<Object,Path>
System.out.println(item.getKey() + ": " + item.getValue());
// The method getKey() is undefined for the type Object
// The method getValue() is undefined for the type Object
});
I know that Javac or ECJ (in that case) can go wrong in guessing the type in case of method type parameters, and in those case we are forced to tell the compiler which type:
.flatMap(path -> parameters.getAllocatedCredits()
.keySet()
.stream()
.<Pair<Object,Path>> map(group -> Pair.of(group, path))
Why in that particular case ECJ does not guess correctly the type while it seems a simple case ?
Edit: updated my answer after testing on javac (using maven) and seen that it works.
Edit (2): refactoring the code to this works:
.flatMap(path -> {final Stream<Pair<Object, Path>> w = parameters.getAllocatedCredits()
.keySet()
.stream()
.map(group -> Pair.of(group, path));
return w;
}
Note: Pair
is taken from commons-lang, and implements Map.Entry
.
To close this loop:
The reported bug in ecj, was found to be a transitive duplicate of bug 444891. The latter implemented a change in JLS, which was proposed after Java 8 GA and set to resolved on 2015-02-16. Based on the JLS change, the ecj bug could be fixed and was released for Eclipse 4.5 (Mars).