Skip to content

Commit

Permalink
Handle final variables in instanceof patterns
Browse files Browse the repository at this point in the history
Fixes #588

PiperOrigin-RevId: 367902205
  • Loading branch information
cushon authored and google-java-format Team committed Apr 12, 2021
1 parent 2396d08 commit fa01637
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
Expand Up @@ -27,6 +27,7 @@
import com.sun.source.tree.ClassTree;
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.InstanceOfTree;
import com.sun.source.tree.ModifiersTree;
import com.sun.source.tree.SwitchExpressionTree;
import com.sun.source.tree.Tree;
import com.sun.source.tree.VariableTree;
Expand Down Expand Up @@ -56,12 +57,13 @@ public Void visitBindingPattern(BindingPatternTree node, Void unused) {
try {
VariableTree variableTree =
(VariableTree) BindingPatternTree.class.getMethod("getVariable").invoke(node);
visitBindingPattern(variableTree.getType(), variableTree.getName());
visitBindingPattern(
variableTree.getModifiers(), variableTree.getType(), variableTree.getName());
} catch (ReflectiveOperationException e1) {
try {
Tree type = (Tree) BindingPatternTree.class.getMethod("getType").invoke(node);
Name name = (Name) BindingPatternTree.class.getMethod("getName").invoke(node);
visitBindingPattern(type, name);
visitBindingPattern(/* modifiers= */ null, type, name);
} catch (ReflectiveOperationException e2) {
e2.addSuppressed(e1);
throw new LinkageError(e2.getMessage(), e2);
Expand All @@ -70,7 +72,10 @@ public Void visitBindingPattern(BindingPatternTree node, Void unused) {
return null;
}

private void visitBindingPattern(Tree type, Name name) {
private void visitBindingPattern(ModifiersTree modifiers, Tree type, Name name) {
if (modifiers != null) {
builder.addAll(visitModifiers(modifiers, Direction.HORIZONTAL, Optional.empty()));
}
scan(type, null);
builder.breakOp(" ");
visit(name);
Expand Down
Expand Up @@ -50,6 +50,8 @@ public class FormatterIntegrationTest {
private static final ImmutableSet<String> JAVA14_TESTS =
ImmutableSet.of("I477", "Records", "RSLs", "Var", "ExpressionSwitch");

private static final ImmutableSet<String> JAVA16_TESTS = ImmutableSet.of("I588");

@Parameters(name = "{index}: {0}")
public static Iterable<Object[]> data() throws IOException {
Path testDataPath = Paths.get("com/google/googlejavaformat/java/testdata");
Expand All @@ -76,7 +78,7 @@ public static Iterable<Object[]> data() throws IOException {
case "output":
outputs.put(baseName, contents);
break;
default:
default: // fall out
}
}
}
Expand All @@ -90,6 +92,9 @@ public static Iterable<Object[]> data() throws IOException {
if (JAVA14_TESTS.contains(fileName) && getMajor() < 14) {
continue;
}
if (JAVA16_TESTS.contains(fileName) && getMajor() < 16) {
continue;
}
testInputs.add(new Object[] {fileName, input, expectedOutput});
}
return testInputs;
Expand Down
@@ -0,0 +1,8 @@
class T {
int f(Object x) {
if (x instanceof final Integer i) {
return i;
}
return -1;
}
}
@@ -0,0 +1,8 @@
class T {
int f(Object x) {
if (x instanceof final Integer i) {
return i;
}
return -1;
}
}

0 comments on commit fa01637

Please sign in to comment.