Handle 'any' patterns
Fixes https://github.com/google/google-java-format/issues/1037
PiperOrigin-RevId: 606394473
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 8ab04e6..2192a32 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 @@
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 26493b0..15a6e75 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 @@
"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 0000000..ed39459
--- /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 0000000..ed39459
--- /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;
+ }
+ }
+ }
+}
diff --git a/util/test-native.sh b/util/test-native.sh
deleted file mode 100755
index a8643ba..0000000
--- a/util/test-native.sh
+++ /dev/null
@@ -1,30 +0,0 @@
-#!/usr/bin/env bash
-# Copyright 2024 The Google Java Format Authors
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-
-set -euox pipefail
-
-time java -jar core/target/google-java-format-*-all-deps.jar || true
-
-status=-1
-chmod +x core/target/google-java-format
-if time core/target/google-java-format; then
- status=0
-else
- status=$?
-fi
-if [[ $status -ne 2 ]]; then
- echo "google-java-format_linux (native) without arguments should have printed usage help and exited with 2, but did not :("
- exit 1
-fi