Skip to content

Commit

Permalink
Handle 'any' patterns
Browse files Browse the repository at this point in the history
Fixes #1037

PiperOrigin-RevId: 606394473
  • Loading branch information
cushon authored and google-java-format Team committed Feb 13, 2024
1 parent 1ae69f9 commit ad089da
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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("_");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ public class FormatterIntegrationTest {
"Unnamed",
"I981",
"StringTemplate",
"I1020")
"I1020",
"I1037")
.build();

@Parameters(name = "{index}: {0}")
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
}
30 changes: 0 additions & 30 deletions util/test-native.sh

This file was deleted.

0 comments on commit ad089da

Please sign in to comment.