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

String template in lambda, reporting "processor type cannot be a raw type"

XMLWordPrintable

    • generic
    • generic

      ADDITIONAL SYSTEM INFORMATION :
      macOS x64
      openjdk version "21.0.1" 2023-10-17
      OpenJDK Runtime Environment (build 21.0.1+12-29)
      OpenJDK 64-Bit Server VM (build 21.0.1+12-29, mixed mode, sharing)

      A DESCRIPTION OF THE PROBLEM :
      I am using string template processor to convert a sql into a prepared statement.
      I made some encapsulation to form a nice dsl, the code looks like this:

      ```
      jdbc.transaction(conn -> {
          var name = "a";
          var age = 100;
          conn.prepare()."""
              insert into "test_user" ("name", "age") values (\{name}, \{age})"""
              .execute();
      });
      ```

      The error occurred here: "conn.prepare(". The javac says: "processor type cannot be a raw type"

      There are two overloading methods with name `transaction`:
      ```
      void transaction(Consumer<ConnectionW> f);
      <T> T transaction(Function<ConnectionW, T> f);
      ```

      Here are the workarounds I've tried.

      1. javac compiles without any problem if I remove the second overloading method.
      2. javac also compiles if I change "jdbc.transaction(conn ->" into "jdbc.transaction((ConnectionW conn) ->"
      3. javac also compiles if I change "jdbc.transaction(conn ->" into "jdbc.transaction((Consumer<ConnectionW>) conn ->"
      4. javac also compiles if I add a standalone variable "SQL sql = conn.prepare()" and then `sql."xxx"`
      5. But javac won't compile if I use "var sql = conn.prepare()" and then `sql."xxx"`

      Detailed information including a reproducing code snippet can be found below.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      The buggy code:

      Bad00Main.java

      ```
      import java.util.function.*;

      class JDBC {
        void transaction(Consumer<ConnectionW> f) {
          f.accept(new ConnectionW());
        }
        <T> T transaction(Function<ConnectionW, T> f) {
          return f.apply(new ConnectionW());
        }
      }

      class ConnectionW {
        SQL prepare() {
          return new SQL();
        }
      }

      class SQL implements StringTemplate.Processor<PreparedStatementW, RuntimeException> {
        public PreparedStatementW process(StringTemplate template) throws RuntimeException {
          System.out.println(template.fragments());
          return new PreparedStatementW();
        }
      }

      class PreparedStatementW {
        void execute() {
          System.out.println("Here I am!");
        }
      }

      void main() {
        var jdbc = new JDBC();
        jdbc.transaction(conn -> {
          var name = "a";
          var age = 100;
          conn.prepare()."""
            insert into "test_user" ("name", "age") values (\{name}, \{age})"""
            .execute();
        });
      }
      ```

      Javac command:

      ```
      javac --source 21 --enable-preview Bad00Main.java
      ```

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The code compiles without any problem, and print the following lines when executed:

      ```
      [insert into "test_user" ("name", "age") values (, , , )]
      Here I am!
      ```
      ACTUAL -
      Picked up JAVA_TOOL_OPTIONS: -Duser.language=en
      Bad00Main.java:36: error: processor type cannot be a raw type: prepare
          conn.prepare()."""
                      ^
      Note: Bad00Main.java uses preview features of Java SE 21.
      Note: Recompile with -Xlint:preview for details.
      1 error

      ---------- BEGIN SOURCE ----------
      https://gist.github.com/wkgcass/6388da63eb1ea858e21717171d8efa68

      Bad00Main is the code represented in this post.

      OK01~04Main and Bad05Main corresponds to 1~5 described in the "Description" section in this post.
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      1~4 described in the "Description" section in this post.

      FREQUENCY : always


            jlaskey Jim Laskey
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: