Static methods of generic classes are treated differently

Ethan McCue ethan at mccue.dev
Sun Apr 30 12:40:29 UTC 2023


I've run into either a corner case in type resolution or a compiler bug and
I'm not sure which.

In an expression context, if a static method is referenced from a generic
class then it won't properly target overloads that require primitive
wrapper unboxing.

package org.example;

import java.time.Month;
import java.util.function.Supplier;


interface A {
    static int f() {
        return 1;
    }
}

interface B<T> {
    static int f() {
        return 2;
    }
}

final class C {
    static int f() {
        return 3;
    }
}

final class D<T> {
    static int f() {
        return 4;
    }
}


public class Main {
    static void h(Month b) {}
    static void h(int b) {}

    static <T> T g(Supplier<? extends T> d) {
        return d.get();
    }


    public static void main(String[] args) {
        // Compiles
        h(
                g(A::f)
        );

        // Doesn't compile
        h(
                g(B::f)
        );

        // Compiles
        var r1 = g(B::f);
        h(r1);

        // Compiles
        h(
                g(C::f)
        );

        // Doesn't compile
        h(
                g(D::f)
        );

        // Compiles
        var r2 = g(D::f);
        h(r2);
    }
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/compiler-dev/attachments/20230430/c5ed271f/attachment-0001.htm>


More information about the compiler-dev mailing list