JDK-8289277 : A static method with varargs is not correctly recognized from a static context.
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 17
  • Priority: P3
  • Status: New
  • Resolution: Unresolved
  • OS: generic
  • CPU: generic
  • Submitted: 2022-06-27
  • Updated: 2022-06-28
Description
ADDITIONAL SYSTEM INFORMATION :
I'm using Linux but I'm pretty sure the person who pointed out the issue uses Windows.

A DESCRIPTION OF THE PROBLEM :
When there are static and instance methods with the same name in the same class, static with varargs and instance with args from the same type as the varargs, compiler mistakes the call to the static method from a static context for a call to the non-static method in the case when the parameters are the same number expected by the instance method and fails to compile the class. This same scenario workes on Java 11 and 8. Trying to compile the sample source when calling the problematic method through the class name doesn't work on either version. This is a clear sign for the compiler which method to chose, which might be another issue.

REGRESSION : Last worked in version 11.0.15

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile the provided source code below.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The static method with int varargs should have been chosen over the instance method with 2 int params and the class should have compiled.
ACTUAL -
Hippo.java:15: error: non-static method hippo(int,int) cannot be referenced from a static context
        hippo(1, 5);
        ^
1 error


---------- BEGIN SOURCE ----------
public class Hippo {
    private static void hippo(short num1, short num2) {
        System.out.println("shorts");
    }
    private static void hippo(int... nums) {
        System.out.println("varargs");
    }
    private void hippo(long num1, long num2) {
        System.out.println("longs");
    }
    private void hippo(int num1, int num2) {
        System.out.println("nums");
    }
    public static void main(String... args) {
        hippo(1, 5);
       // Hippo.hippo(1, 5); doesn't work on either version.
    } }
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Passing the parameters as an array works including when the class name is used to invoke the static method on 8, 11 and 17, but this defeats the purpose of varargs.
Adding or removing a parameter also causes the compiler to chose the static method.

FREQUENCY : always



Comments
The issue is reproducible in JDK 17 and above. Hippo.java:15: error: non-static method hippo(int,int) cannot be referenced from a static context hippo(1, 5); ^ 1 error Using Hippo.hippo(1, 5); doesn't work on either of the versions.
28-06-2022

Observations: JDK 8: pass JDK 11: pass JDK 16: pass JDK 17: compilation fails
28-06-2022