Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inferred type issue with Java 1.8.0_65

So, I have the following code:

public class Tester {
    public static void doAssert(Object foo, Object bar) {
    }

    public static void doAssert(Object[] foo, Object[] bar) {
    }

    public static <T> T getValue(String name, Function<String, T> covert) {
        return null;
    }

    public static void main (String[] args) {
        doAssert(getValue("", Double::valueOf), null);
    }
}

If I compile this with javac v1.8.0_05, this works fine. Under 1.8.0_65, I get the following error (as reported with -Xdiags:verbose):

Tester.java:32: error: method doAssert in class Tester cannot be applied to given types;
            doAssert(null, getValue("", Double::valueOf));
                    ^
  required: Object[],Object[]
  found: <null>,Double
  reason: argument mismatch; inferred type does not conform to upper bound(s)
      inferred: Double
      upper bound(s): Object[],Object
1 error

This goes away if I explicitly cast the null argument to Double, or if I remove the Object[] overload of doAssert.

So...is this a regression in 1.8.0_65 or one of the other intervening releases, or was 1.8.0_05 overly permissive? And why can't javac figure out what it's supposed to do?

(re: the close vote - to me, it's non-obvious how the other Q&A is a duplicate; the linked questions don't appear to deal with method overloading issues, which is required to reproduce this issue.)

like image 388
Sbodd Avatar asked Nov 05 '15 19:11

Sbodd


1 Answers

In the update 20 changelog one of the features added was:

Java Compiler updated

You can see the amount of bugs related to javac and parameters inference here: http://www.oracle.com/technetwork/java/javase/2col/8u20-bugfixes-2257730.html

In some bugs (f.e: http://bugs.java.com/view_bug.do?bug_id=8046762) some new additions were reverted. Maybe this could be the cause for the javac behaviour in update 65 and explain it was working in update 5 too.

like image 197
Francisco Hernandez Avatar answered Nov 17 '22 19:11

Francisco Hernandez