Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8365676

javac incorrectly allows calling static method via type variable

XMLWordPrintable

    • generic
    • generic

      ADDITIONAL SYSTEM INFORMATION :
      wsl2-ubuntu 22.04
      Linux 5.15.167.4-microsoft-standard-WSL2 #1 SMP Tue Nov 5 00:21:55 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux

      javac 21.0.8

      java version "21.0.8" 2025-07-15 LTS
      Java(TM) SE Runtime Environment (build 21.0.8+12-LTS-250)
      Java HotSpot(TM) 64-Bit Server VM (build 21.0.8+12-LTS-250, mixed mode, sharing)

      A DESCRIPTION OF THE PROBLEM :
      Javac (tested on JDK 21 and 8, 11, 17) incorrectly accepts code that attempts to call a static method defined in an interface via a type parameter T, where T is a generic type variable bounded by that interface.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compling following code using javac in JDK21.
      ```
      javac Test.java
      ```

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The compliation should fail and report such error message:
      ```
      1. ERROR in Test.java (at line 18)
              T.nonExistentStaticMethod();
                ^^^^^^^^^^^^^^^^^^^^^^^
      The method nonExistentStaticMethod() is undefined for the type T
      ```
      ACTUAL -
      The compliation is successful.

      ---------- BEGIN SOURCE ----------
      public class Test {
          public static void main(String[] args) {
              TemplateClass<MyType> instance = new TemplateClass<>(new MyType());
          }
      }

      interface StaticMemberProvider {
          static void nonExistentStaticMethod() {
              throw new UnsupportedOperationException("This method should not be called");
          }
      }

      class MyType implements StaticMemberProvider {
      }

      class TemplateClass<T extends StaticMemberProvider> {
          public TemplateClass(T member) {
              T.nonExistentStaticMethod();
          }
      }
      ---------- END SOURCE ----------

            liach Chen Liang
            webbuggrp Webbug Group
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: