diff --git a/core/src/main/java/com/google/googlejavaformat/java/java21/Java21InputAstVisitor.java b/core/src/main/java/com/google/googlejavaformat/java/java21/Java21InputAstVisitor.java index 8ab04e665..2192a32a9 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/java21/Java21InputAstVisitor.java +++ b/core/src/main/java/com/google/googlejavaformat/java/java21/Java21InputAstVisitor.java @@ -24,6 +24,8 @@ import com.sun.source.tree.PatternCaseLabelTree; import com.sun.source.tree.PatternTree; import com.sun.source.tree.StringTemplateTree; +import com.sun.source.tree.Tree; +import com.sun.tools.javac.tree.JCTree; import javax.lang.model.element.Name; /** @@ -107,4 +109,20 @@ protected void variableName(Name name) { visit(name); } } + + @Override + public Void scan(Tree tree, Void unused) { + // Pre-visit AST for preview features, since com.sun.source.tree.AnyPattern can't be + // accessed directly without --enable-preview. + if (tree instanceof JCTree.JCAnyPattern) { + visitJcAnyPattern((JCTree.JCAnyPattern) tree); + return null; + } else { + return super.scan(tree, unused); + } + } + + private void visitJcAnyPattern(JCTree.JCAnyPattern unused) { + token("_"); + } } diff --git a/core/src/test/java/com/google/googlejavaformat/java/FormatterIntegrationTest.java b/core/src/test/java/com/google/googlejavaformat/java/FormatterIntegrationTest.java index 26493b022..15a6e75f9 100644 --- a/core/src/test/java/com/google/googlejavaformat/java/FormatterIntegrationTest.java +++ b/core/src/test/java/com/google/googlejavaformat/java/FormatterIntegrationTest.java @@ -62,7 +62,8 @@ public class FormatterIntegrationTest { "Unnamed", "I981", "StringTemplate", - "I1020") + "I1020", + "I1037") .build(); @Parameters(name = "{index}: {0}") diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/I1037.input b/core/src/test/resources/com/google/googlejavaformat/java/testdata/I1037.input new file mode 100644 index 000000000..ed39459dd --- /dev/null +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/I1037.input @@ -0,0 +1,11 @@ +public sealed interface A { + record AA(Long a) implements A {} + + static Long mySwitch(A a) { + switch (a) { + case AA(_) -> { + return 1L; + } + } + } +} diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/I1037.output b/core/src/test/resources/com/google/googlejavaformat/java/testdata/I1037.output new file mode 100644 index 000000000..ed39459dd --- /dev/null +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/I1037.output @@ -0,0 +1,11 @@ +public sealed interface A { + record AA(Long a) implements A {} + + static Long mySwitch(A a) { + switch (a) { + case AA(_) -> { + return 1L; + } + } + } +}