Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle final variables in instanceof patterns #592

Merged
merged 1 commit into from Apr 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
}
}