google-java-format
google-java-format copied to clipboard
Google Java Format fails on files that use record patterns in switches (Java 19 preview)
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
I'm having the same issue with java 21 , with using the Idea plugin
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