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

Generic Type inference problem

XMLWordPrintable

    • generic
    • generic

      ADDITIONAL SYSTEM INFORMATION :
      Java 22, 23, 24

      A DESCRIPTION OF THE PROBLEM :
      See description inline. Problem: The line "line.isBlank()" does not work because the compiler reports:

      % java -version
      openjdk version "24-ea" 2025-03-18
      OpenJDK Runtime Environment (build 24-ea+6-619)
      OpenJDK 64-Bit Server VM (build 24-ea+6-619, mixed mode, sharing)
      % javac --release 24 --enable-preview J24Gatherer.java
      J24Gatherer.java:43: Fehler: Symbol nicht gefunden
                          if (line.isBlank()) { // does not work
                                  ^
        Symbol: Methode isBlank()
        Ort: Variable line von Typ String
        Dabei ist String eine Typvariable:
          String erweitert Object, deklariert in Methode <String>paraGroups()
      Hinweis: J24Gatherer.java verwendet Vorschaufeatures von Java SE 24.
      Hinweis: Wiederholen Sie die Kompilierung mit -Xlint:preview, um Details zu erhalten.
      1 Fehler

      The code compiles when commenting out (line 43)

      if (line.isBlank()) { // does not work

      and commenting out: (line 44)

      //if (line.toString().isBlank()) {

      At line 50 (para.add(line); everything works fine.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compile the code with "javac --release 24 --enable-preview J24Gatherer.java"

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The code must compile.
      ACTUAL -
      % java -version
      openjdk version "24-ea" 2025-03-18
      OpenJDK Runtime Environment (build 24-ea+6-619)
      OpenJDK 64-Bit Server VM (build 24-ea+6-619, mixed mode, sharing)
      % javac --release 24 --enable-preview J24Gatherer.java
      J24Gatherer.java:43: Fehler: Symbol nicht gefunden
                          if (line.isBlank()) { // does not work
                                  ^
        Symbol: Methode isBlank()
        Ort: Variable line von Typ String
        Dabei ist String eine Typvariable:
          String erweitert Object, deklariert in Methode <String>paraGroups()
      Hinweis: J24Gatherer.java verwendet Vorschaufeatures von Java SE 24.
      Hinweis: Wiederholen Sie die Kompilierung mit -Xlint:preview, um Details zu erhalten.
      1 Fehler


      ---------- BEGIN SOURCE ----------
      import java.util.ArrayList;
      import java.util.List;
      import java.util.stream.Gatherer;

      import static java.lang.System.out;
      import static java.util.List.copyOf;


      public class J24Gatherer {

          static String MULTILINE =
                  """
                  The quick brown fox.
                  Jumps over.
                  
                  The laszy dog.
                  """;

          public static void main(final String... args) throws Exception {
              // final var paragraphs = MULTILINE.lines().gather(paraGroups()).toList();
              final var paragraphs = MULTILINE.lines().gather(paraGroups()).toList();
              out.format("Paragraphs: %s%n", paragraphs);
          }
          
          static <String> Gatherer<String, List<String>, List<String>> paraGroups() {
              return Gatherer.ofSequential(

                      // The current paragraph holder
                      () -> new ArrayList<>(),
                      

                      Gatherer.Integrator.ofGreedy((para, line, downstream) -> {

                          // Looks like there is a compiler bug. "line" is not interpreted as String in line 44
                          // Also casting doesn't work. Only "" + line or line.toString(). Or it's a layer 8 problem.
                          // In line 50 everything is fine.

                          if (line.isBlank()) { // does not work
                          //if (line.toString().isBlank()) {
                              final var result = copyOf(para);
                              para.clear();
                              return downstream.push(result);
                          }

                          para.add(line);
                          return true;
                      }),


                      (para, downstream) -> {
                          if (!downstream.isRejecting() && !para.isEmpty()) {
                              downstream.push(copyOf(para));
                              para.clear();
                          }
                      }
              );
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Dedicated toString() call on input parameter.

      FREQUENCY : always


            vromero Vicente Arturo Romero Zaldivar
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: