3

I'm trying to migrate some old applications that uses Java 8 libs to Java 11, but something strange happened with code that uses method references:

Error:(26, 46) java: incompatible types: cannot infer type-variable(s) R
    (argument mismatch; invalid method reference
      unexpected static method getId(com.mongodb.BasicDBObject) found in unbound lookup)

The scenario looks like this:

MyOldLib.java (on Java 8 project)

public class MyOldLib {
    public static String getId(BasicDBObject o) { return o.getString("id"); }
}

MyMigratingApplication.java (on Java 11 project)

public class MyMigratingApplication {
    public static void main(String[] args) {
        List<BasicDBObject> myList = getDataFromSomewhere();
        List<String> myIdList = myList.stream().map(MyOldLib::getId).collect(Collectors.toList());

    //...
    }
}

Using the method reference as was done doesn't work, but if I use a lambda or encapsulate the getId method instead, it does work:

Using lambda:

public class MyMigratingApplication {
    public static void main(String[] args) {
        List<BasicDBObject> myList = getDataFromSomewhere();
        List<String> myIdList = myList.stream().map(data -> MyOldLib.getId(data)).collect(Collectors.toList());

    //...
    }
}

Encapsulating:

public class MyMigratingApplication {
    static String getId(BasicDBObject data) {
        return MyOldLib.getId(data)
    }

public static void main(String[] args) {
        List<BasicDBObject> myList = getDataFromSomewhere();
        List<String> myIdList = myList.stream().map(MyMigratingApplication::getId).collect(Collectors.toList());
    //...
    }
}

Why is this happening? There's any solution that minimizes the code refactor needs?

Some aditional info:

1) The MongoDB version is the same on both projects, 3.6.4.

2) This happens when I try to run the applicatin on IntelliJ IDE.

3) I'm using Maven as dependency manager.

Augusto Dias
  • 169
  • 1
  • 9

0 Answers0