0

EDIT: As of IntelliJ version 2017.2 this issue has been fixed.


Java version: 1.8.0_131

IntelliJ IDEA version: 2017.1.4

I have this class:

public class MethodReferenceWithArguments {

    static <T, U> T createWith(Function<? super U, ? extends T> methodRef, U arg) {
        return methodRef.apply(arg);
    }

    public static void main(String[] args) {

        Map<String, String> map = createWith(
                TreeMap::new,
                Comparator.<String>reverseOrder());

        map.put("aaa", "ONE");
        map.put("zzz", "TWO");

        System.out.println(map);
    }
}

With javac, it compiles fine and works fine. With IntelliJ, it compiles fine and works fine, however the TreeMap::new constructor reference is underlined with red and Bad return type in method reference: cannot convert java.util.TreeMap<K, V> to T message is displayed on hover tooltip.

Does anybody know how to disable this misleading and annoying error message without turning off important, relevant error messages? Is this a bug? If yes, where can I report it?

fps
  • 33,623
  • 8
  • 55
  • 110
  • 1
    You should report this to them, here https://youtrack.jetbrains.com/issues/IDEA Suggested Title "Good code red: constructor reference can not be inferred". – Ryan Leach Jul 03 '17 at 04:02
  • 1
    that's *definitely* something to report to IDEA since it compiles just fine (without warnings) with JDK 8-131, 9-157 and latest eclipse - these are the ones I've tried. – Eugene Jul 03 '17 at 04:14
  • @Ryan Reported [here](https://youtrack.jetbrains.com/issue/IDEA-175280) – fps Jul 03 '17 at 04:48
  • @Eugene Thanks for checking other versions of java and eclipse. Here's the [submitted bug](https://youtrack.jetbrains.com/issue/IDEA-175280) – fps Jul 03 '17 at 04:50
  • 2
    it was fixed in intellij idea 2017.2+ – holi-java Jul 03 '17 at 09:36
  • @holi-java Glad to know, thanks – fps Jul 03 '17 at 11:36

1 Answers1

2

This is caused by Intellij can't infer the TreeMap<K,V> generic type, You can explicitly set the K, V type for resolving this warning, like:

    Map<String, String> map = createWith(
            TreeMap<String, String>::new,
            Comparator.<String>reverseOrder());
chengpohi
  • 14,064
  • 1
  • 24
  • 42
  • Thanks, I didn't know that method references allowed explicit generic type parameters. – fps Jul 03 '17 at 04:45