Skip to content

Fail to reformat by JDK17 for Pattern Matching for instanceof #768

Description

@bluebu

https://openjdk.java.net/jeps/394

idea version: 2021.3.2 (Ultimate Edition)

plugin version: 1.15.0.0

JDK version: Amazon corretto 17.0.2

the reformat doesn't work, if has the code below:

if (a instanceof String str) {
   System.out.println(str)
}

the reformat will work, if I delete any code with "Pattern Matching for instanceof" operator

Activity

  1. MatthewRyanRead commented on Jun 10, 2022

    @MatthewRyanRead

    Reproduced in 2022.1.2 with plugin 1.15.0.0 (formatter 1.15.0) on Temurin-17.0.2+8.

    https://github.com/spotify/fmt-maven-plugin and https://github.com/diffplug/spotless both appear to handle this construction perfectly, so it seems specific to the IntelliJ plugin rather than to the library generally.

  2. tbroyer commented on Jun 10, 2022

    @tbroyer
    Contributor

    Is this when running IDEA itself with JDK 17 ? or only using JDK 17 as the "project JDK" ?

    See #533

  3. MatthewRyanRead commented on Jun 11, 2022

    @MatthewRyanRead

    I was using 17 as the project JDK while using the default 11 runtime for the IDE.

    However, I can also reproduce this on the 2022.2 EAP version of IntelliJ, which runs on their bundled 17.0.3+7-469.3-jcef runtime at the moment.

  4. zxeoc commented on Jul 27, 2022

    @zxeoc

    In my case, it works fine with project JDK 17 & JBR 11. It fails after I update IDEA to 2022.2, which switches to JBR 17 by default.

  5. tbroyer commented on Jul 27, 2022

    @tbroyer
    Contributor

    IIRC, #533 is about using the appropriate JDK (JBR) for running the IDE (corresponding to your project JDK, i.e. you can't use JBR 11 to format Java 17 code), and #787 when using 2022.2.

  6. cushon commented on Aug 4, 2022

    @cushon
    Collaborator

    I think this is covered by the issues @tbroyer mentioned. The formatter itself can handle these constructs, but only when running on a JVM that supports them:

    java -jar google-java-format-1.15.0-all-deps.jar T.java
    class T {
      {
        if (a instanceof String str) {
          System.out.println(str);
        }
      }
    }
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions