Skip to content

Commit bb4470f

Browse files
cushongoogle-java-format Team
authored and
google-java-format Team
committed
Handle final variables in instanceof patterns
Fixes #588 PiperOrigin-RevId: 367902205
1 parent 2396d08 commit bb4470f

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed

core/src/main/java/com/google/googlejavaformat/java/java14/Java14InputAstVisitor.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import com.sun.source.tree.ClassTree;
2828
import com.sun.source.tree.ExpressionTree;
2929
import com.sun.source.tree.InstanceOfTree;
30+
import com.sun.source.tree.ModifiersTree;
3031
import com.sun.source.tree.SwitchExpressionTree;
3132
import com.sun.source.tree.Tree;
3233
import com.sun.source.tree.VariableTree;
@@ -56,12 +57,13 @@ public Void visitBindingPattern(BindingPatternTree node, Void unused) {
5657
try {
5758
VariableTree variableTree =
5859
(VariableTree) BindingPatternTree.class.getMethod("getVariable").invoke(node);
59-
visitBindingPattern(variableTree.getType(), variableTree.getName());
60+
visitBindingPattern(
61+
variableTree.getModifiers(), variableTree.getType(), variableTree.getName());
6062
} catch (ReflectiveOperationException e1) {
6163
try {
6264
Tree type = (Tree) BindingPatternTree.class.getMethod("getType").invoke(node);
6365
Name name = (Name) BindingPatternTree.class.getMethod("getName").invoke(node);
64-
visitBindingPattern(type, name);
66+
visitBindingPattern(/* modifiers= */ null, type, name);
6567
} catch (ReflectiveOperationException e2) {
6668
e2.addSuppressed(e1);
6769
throw new LinkageError(e2.getMessage(), e2);
@@ -70,7 +72,10 @@ public Void visitBindingPattern(BindingPatternTree node, Void unused) {
7072
return null;
7173
}
7274

73-
private void visitBindingPattern(Tree type, Name name) {
75+
private void visitBindingPattern(ModifiersTree modifiers, Tree type, Name name) {
76+
if (modifiers != null) {
77+
builder.addAll(visitModifiers(modifiers, Direction.HORIZONTAL, Optional.empty()));
78+
}
7479
scan(type, null);
7580
builder.breakOp(" ");
7681
visit(name);

core/src/test/java/com/google/googlejavaformat/java/FormatterIntegrationTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ public class FormatterIntegrationTest {
5050
private static final ImmutableSet<String> JAVA14_TESTS =
5151
ImmutableSet.of("I477", "Records", "RSLs", "Var", "ExpressionSwitch");
5252

53+
private static final ImmutableSet<String> JAVA16_TESTS = ImmutableSet.of("I588");
54+
5355
@Parameters(name = "{index}: {0}")
5456
public static Iterable<Object[]> data() throws IOException {
5557
Path testDataPath = Paths.get("com/google/googlejavaformat/java/testdata");
@@ -76,7 +78,7 @@ public static Iterable<Object[]> data() throws IOException {
7678
case "output":
7779
outputs.put(baseName, contents);
7880
break;
79-
default:
81+
default: // fall out
8082
}
8183
}
8284
}
@@ -90,6 +92,9 @@ public static Iterable<Object[]> data() throws IOException {
9092
if (JAVA14_TESTS.contains(fileName) && getMajor() < 14) {
9193
continue;
9294
}
95+
if (JAVA16_TESTS.contains(fileName) && getMajor() < 16) {
96+
continue;
97+
}
9398
testInputs.add(new Object[] {fileName, input, expectedOutput});
9499
}
95100
return testInputs;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class T {
2+
int f(Object x) {
3+
if (x instanceof final Integer i) {
4+
return i;
5+
}
6+
return -1;
7+
}
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class T {
2+
int f(Object x) {
3+
if (x instanceof final Integer i) {
4+
return i;
5+
}
6+
return -1;
7+
}
8+
}

0 commit comments

Comments
 (0)