JDK-7115209 : Compiler regression with ambiguous varargs methods
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 7
  • Priority: P3
  • Status: Closed
  • Resolution: Not an Issue
  • OS: windows_xp,windows_7
  • CPU: x86
  • Submitted: 2011-11-23
  • Updated: 2012-03-20
  • Resolved: 2011-11-24
Related Reports
Duplicate :  
Description
FULL PRODUCT VERSION :
java version "1.7.0_01"
Java(TM) SE Runtime Environment (build 1.7.0_01-b08)
Java HotSpot(TM) 64-Bit Server VM (build 21.1-b02, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows [Version 6.1.7601]

A DESCRIPTION OF THE PROBLEM :
Code that was working in Java 6 does not compile with Java 7 anymore.

As the result we can't move on with adopting Java 7 for existing projects.

REGRESSION.  Last worked in version 6u29

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Copy provide source code into file B.java and try to compile it using:
>javac B.java
Compilation should fail.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Compilation succeeds and program prints "boolean wins!" to the console.
ACTUAL -
Compilation fails.

ERROR MESSAGES/STACK TRACES THAT OCCUR :
B.java:13: error: reference to m1 is ambiguous, both method m1(boolean,Object...) in A and method m1(Object...) in A match
                a.m1(true, "A", new Integer(100), null);
                 ^
1 error

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
class A {
	void m1(boolean flag, Object...args) {
	  System.out.println("boolean wins!");
	}
	void m1(Object...args) {
	  System.out.println("DEFAULT");
	}
}

public class B {
	public static void main(String[] args) {
		A a = new A();
		a.m1(true, "A", new Integer(100), null);
	}
}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
As workaround it is possible to change definition of first m1 method so that it uses Boolean instead of boolean in which case program compiles and runs as expected.

Comments
EVALUATION This is not a defect - the compiler is behaving in compliance with JLS7. - Is (boolean,Object[]) more specific than (Object[]) ? No, because the boolean formal parameter cannot accept an Object. - Is (Object[]) more specific than (boolean,Object[]) ? No, because the Object[] formal parameter cannot accept a boolean (note: boxing is not allowed here!). This is listed in the JDK 7 release notes - see following URL (section named "Changes in Most Specific Varargs Method Selection"): http://www.oracle.com/technetwork/java/javase/compatibility-417013.html#jdk7
2011-11-24