google-java-format icon indicating copy to clipboard operation
google-java-format copied to clipboard

Google Java Format fails on files that use record patterns in switches (Java 19 preview)

Open josephearl opened this issue 3 years ago • 1 comments

Using a record pattern in a switch (Java 19 preview) e.g.:

int c = switch (r) {
  case Record(var a) -> a;
  case AnotherRecord(var b) -> b;
  default -> 0;
}

causes Google Java Format to fail with:

com.google.googlejavaformat.java.FormatterException: 29:35: error: did not generate token "("
        at com.google.googlejavaformat.java.Formatter.getFormatReplacements(Formatter.java:273)
        at com.google.googlejavaformat.java.Formatter.formatSource(Formatter.java:247)
        at com.google.googlejavaformat.java.Formatter.formatSource(Formatter.java:213)
        at [email protected]/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
        ... 124 more

josephearl avatar Dec 05 '22 09:12 josephearl

I'm having the same issue with java 21 , with using the Idea plugin

Abdelhady avatar Jan 17 '25 19:01 Abdelhady

This is supported now:

$ java -jar google-java-format-1.32.0-all-deps.jar R.java
class R {
  {
    int c =
        switch (r) {
          case Record(var a) -> a;
          case AnotherRecord(var b) -> b;
          default -> 0;
        };
  }
}

For the IntelliJ plugin, note that similar to https://github.com/google/google-java-format/issues/1278#issuecomment-3458051292, IntelliJ needs to be running on a JDK that's new enough to support the features

cushon avatar Oct 28 '25 19:10 cushon