name clash: toString(List<String>) and toString(List<List<String>>)

XMLWordPrintable

    • generic
    • generic
    • Not verified

      ADDITIONAL SYSTEM INFORMATION :
      OS Name: Microsoft Windows 10 Pro
      OS Version: 10.0.19042 N/A Build 19042

      java -version
      openjdk version "15.0.1" 2020-10-20
      OpenJDK Runtime Environment (build 15.0.1+9-18)
      OpenJDK 64-Bit Server VM (build 15.0.1+9-18, mixed mode, sharing)

      A DESCRIPTION OF THE PROBLEM :
      Compiling attached code, ErasureBugV1.java results in a compile time error:
      javac ErasureBugV1.java
      ErasureBugV1.java:33: error: name clash: toString(List<String>) and toString(List<List<String>>) have the same erasure
              public static String toString(java.util.List<String> list) {
                                   ^
      1 error

      Compiling attached code, ErasureBugV2.java results in a successful compile and runs as expected.


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      This issue is reproducible on any platform, using any OS, using any version of java.
      There are two versions of this code ErasureBugV1.java and ErasureBugV2.java. Cut and Paste each version into a file of the same name and compile each:

      javac ErasureBugV1.java (error is generated)
      javac ErasureBugV2.java (compiles with no error, as expected)

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      ErasureBugV1.java and ErasureBugV2.java should both compile and generate the same output when run.
      ACTUAL -
       Instead, ErasureBugV2.java compiles and runs as expected while ErasureBugV1.java fails to compile and returns with the following compile message:

      javac ErasureBugV1.java
      ErasureBug.java:33: error: name clash: toString(List<String>) and toString(List<List<String>>) have the same erasure
      public static String toString(java.util.List<String> list) {
                           ^
      1 error

      ---------- BEGIN SOURCE ----------
      //
      // Save the following in a file called: ErasureBugV1.java
      // There is another chunk of source below that should be saved in another file called
      // ErasureBugV2.java.
      // They are two separate source files. MAKE SURE ErasureBugV2.java CONTAINS THE
      // TWO HELPER CLASSES: List and Table
      //

      public class ErasureBugV1 {

              public static void main(String[] args) {
                      java.util.List<java.util.List<String>> table = new java.util.ArrayList<>();
                      java.util.List<String> list = new java.util.ArrayList<String>();
                      list.add("a");
                      list.add("b");
                      list.add("c");
                      table.add(list);
                      list = new java.util.ArrayList<String>();
                      list.add("d");
                      list.add("e");
                      list.add("f");
                      table.add(list);
                      System.err.println(toString(table));
              }

              public static String toString(java.util.List<java.util.List<String>> table) {
                      StringBuilder sb = new StringBuilder();
                      java.util.List<String> l;
                      int i = 0;
                      if (i < table.size()) {
                              sb.append(toString(table.get(i)));
                              for (++i; i < table.size(); ++i) {
                                      sb.append("\n");
                                      sb.append(toString(table.get(i)));
                              }
                      }
                      return sb.toString();
              }

              public static String toString(java.util.List<String> list) {
                      StringBuilder sb = new StringBuilder();
                      int i = 0;
                      if (i < list.size()) {
                              sb.append(list.get(i));
                              for (++i; i < list.size(); ++i) {
                                      sb.append(",");
                                      sb.append(list.get(i));
                              }
                      }
                      return sb.toString();
              }

      }

      //
      // Save the following in a file called: ErasureBugV2.java
      //
      public class ErasureBugV2 {

              public static void main(String[] args) {
                      Table table = new Table();
                      List list = new List();
                      list.add("a");
                      list.add("b");
                      list.add("c");
                      table.add(list);
                      list = new List();
                      list.add("d");
                      list.add("e");
                      list.add("f");
                      table.add(list);
                      System.err.println(toString(table));
              }

              public static String toString(Table table) {
                      StringBuilder sb = new StringBuilder();
                      List l;
                      int i = 0;
                      if (i < table.size()) {
                              sb.append(toString(table.get(i)));
                              for (++i; i < table.size(); ++i) {
                                      sb.append("\n");
                                      sb.append(toString(table.get(i)));
                              }
                      }
                      return sb.toString();
              }

              public static String toString(List list) {
                      StringBuilder sb = new StringBuilder();
                      int i = 0;
                      if (i < list.size()) {
                              sb.append(list.get(i));
                              for (++i; i < list.size(); ++i) {
                                      sb.append(",");
                                      sb.append(list.get(i));
                              }
                      }
                      return sb.toString();
              }

      }

      class List extends java.util.ArrayList<String> {
      }

      class Table extends java.util.ArrayList<List> {
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      ErasureBugV2.java is the workaround for the error generated by compiling ErasureBugV1.java.

      My observation is java.util.List<java.util.List<Stirng>> and java.util.List<String> are two distinct types. I don't believe there should be an erasure error since they are distinct types. ErasureBugV2.java accomplishes the same goal yet appears to be faking out the compiler, the workaround. There should be no need to provide this workaround.

      FREQUENCY : always


        1. ErasureBugV1.java
          2 kB
          Anupam Dev
        2. ErasureBugV2.java
          2 kB
          Anupam Dev

            Assignee:
            Anupam Dev
            Reporter:
            Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: