19

I have the following classes in a file Sandbox.java:

package sandbox;

import java.util.Arrays;
import java.util.Collection;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;

public class Sandbox {

    public static void main(String[] args) throws Exception {
        ExecutorService executor = Executors.newSingleThreadExecutor();
        Collection<String> s = Arrays.asList(1,2,4,100).stream()
                .map(i -> CompletableFuture
                        .supplyAsync(() -> Wrapper.of(i), executor)
                        .thenApply(d -> d.get().toString())
                        )
                .map(CompletableFuture::join)
                .collect(Collectors.toList());

        executor.shutdown();

        System.out.println(s);
    }
}

class Wrapper<T> {
    T t;

    private Wrapper(T t) {
        this.t = t;
    }

    public T get() {
        return t;
    }

    public static <T> Wrapper<T> of (T t) {
        return new Wrapper<>(t);
    }
}

the compilation in Eclipse shows error in line 14 "Cannot infer type argument(s) for map(Function) ".

The same code compiles without problems using pure javac (JDK 1.8.0_121).

If I change the proper line into:

Collection<String> s = Arrays.asList(1,2,4,100).stream()
                .map(i -> CompletableFuture
                        .supplyAsync(() -> Wrapper.of(i), executor)
                        .<String>thenApply(d -> d.get().toString())
                        )
                .map(CompletableFuture::join)
                .collect(Collectors.toList());

then the code compiles without error in Eclipse.

Does anyone know why is there such a behaviour? Is it a bug?

I use Eclipse 4.6.2.20161208-0625 (it finds no updates at the moment).

Paul R
  • 208,748
  • 37
  • 389
  • 560
Przemysław Różycki
  • 809
  • 2
  • 10
  • 21
  • 1
    Are you using the same version of the java compiler with the same compliance flags via Eclipse? I sometimes find that Eclipse is using something unexpected, depending on how the project was setup. – Brick Feb 16 '17 at 21:53
  • 1
    [Related?](http://stackoverflow.com/q/40740223/1553851) – shmosel Feb 16 '17 at 21:55
  • Do you mean the source and target level? In both cases its 1.8. I use the same instance of the compiler configured in Eclipse, than I use with pure javac. So I assume it's a problem of JDT. – Przemysław Różycki Feb 16 '17 at 21:57
  • schmosel, yes, it's most likely the same problem. But unfortunately, there is no solution as well. – Przemysław Różycki Feb 16 '17 at 22:00
  • I know that there are some Java 1.8 releases which have this problem (either 1.8.0_25 or 1.8.0_45 - can't recall which). This code compiled and ran fine on 1.8.0_111. – Joe C Feb 16 '17 at 22:33
  • Joe C, as I mentioned, it compiles well on pure javac, but Eclipse shows the error, even if it use the same JDK. – Przemysław Różycki Feb 17 '17 at 05:33

4 Answers4

9

I have confirmed, that this is a bug: https://bugs.eclipse.org/bugs/show_bug.cgi?id=512486. It has been declared as resolved in 4.6.3. I'll confirm this when stable release is available.

Przemysław Różycki
  • 809
  • 2
  • 10
  • 21
3

I just checked with Eclipse IDE for Java Developers Version: Mars Release (4.5.0) Build id: 20150621-1200 and the code worked well for me. It may have been introduced in the 4.6 version.

Pallavi Sonal
  • 3,661
  • 1
  • 15
  • 19
2

I got this error when one of the parameters was of the wrong type. Correcting that made the error go away

Gayathri
  • 95
  • 1
  • 1
  • 11
0

I've recently installed STS 4.6.2 on Windows 10, and though the projects were linked to JDK 11 (project requirement) on my build path, the IDE itself couldn't set it up correctly, so I forced it, by changing the SpringToolSuite4.ini, by adding the -vm line with the java path, before the -vmargs line, like you see below:

-vm
C:\Program Files\Java\jdk-11.0.6\bin\javaw.exe
-vmargs
...

ps: I realized it may have happened because of some java path problem, out of the environment vars context, which I couldn't figure out how to solve on the case of STS, didn't happened on Eclipse though.

bzani
  • 487
  • 4
  • 5