Skip to content

Commit 58978e6

Browse files
cushongoogle-java-format Team
authored andcommitted
Fix handling of case patterns in unused import removal
#684 PiperOrigin-RevId: 411136606
1 parent c874114 commit 58978e6

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

core/src/main/java/com/google/googlejavaformat/java/RemoveUnusedImports.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import com.google.googlejavaformat.Newlines;
3333
import com.sun.source.doctree.DocCommentTree;
3434
import com.sun.source.doctree.ReferenceTree;
35+
import com.sun.source.tree.CaseTree;
3536
import com.sun.source.tree.IdentifierTree;
3637
import com.sun.source.tree.ImportTree;
3738
import com.sun.source.tree.Tree;
@@ -55,8 +56,10 @@
5556
import com.sun.tools.javac.util.Options;
5657
import java.io.IOError;
5758
import java.io.IOException;
59+
import java.lang.reflect.Method;
5860
import java.net.URI;
5961
import java.util.LinkedHashSet;
62+
import java.util.List;
6063
import java.util.Map;
6164
import java.util.Set;
6265
import javax.tools.Diagnostic;
@@ -115,6 +118,31 @@ public Void visitIdentifier(IdentifierTree tree, Void unused) {
115118
return null;
116119
}
117120

121+
// TODO(cushon): remove this override when pattern matching in switch is no longer a preview
122+
// feature, and TreePathScanner visits CaseTree#getLabels instead of CaseTree#getExpressions
123+
@SuppressWarnings("unchecked") // reflection
124+
@Override
125+
public Void visitCase(CaseTree tree, Void unused) {
126+
if (CASE_TREE_GET_LABELS != null) {
127+
try {
128+
scan((List<? extends Tree>) CASE_TREE_GET_LABELS.invoke(tree), null);
129+
} catch (ReflectiveOperationException e) {
130+
throw new LinkageError(e.getMessage(), e);
131+
}
132+
}
133+
return super.visitCase(tree, null);
134+
}
135+
136+
private static final Method CASE_TREE_GET_LABELS = caseTreeGetLabels();
137+
138+
private static Method caseTreeGetLabels() {
139+
try {
140+
return CaseTree.class.getMethod("getLabels");
141+
} catch (NoSuchMethodException e) {
142+
return null;
143+
}
144+
}
145+
118146
@Override
119147
public Void scan(Tree tree, Void unused) {
120148
if (tree == null) {
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2021 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
15+
package com.google.googlejavaformat.java;
16+
17+
import static com.google.common.truth.Truth.assertThat;
18+
import static com.google.googlejavaformat.java.RemoveUnusedImports.removeUnusedImports;
19+
import static org.junit.Assume.assumeTrue;
20+
21+
import com.google.common.base.Joiner;
22+
import org.junit.Test;
23+
import org.junit.runner.RunWith;
24+
import org.junit.runners.JUnit4;
25+
26+
/** Tests that unused import removal doesn't remove types used in case labels. */
27+
@RunWith(JUnit4.class)
28+
public class RemoveUnusedImportsCaseLabelsTest {
29+
@Test
30+
public void preserveTypesInCaseLabels() throws FormatterException {
31+
assumeTrue(Runtime.version().feature() >= 17);
32+
String input =
33+
Joiner.on('\n')
34+
.join(
35+
"package example;",
36+
"import example.model.SealedInterface;",
37+
"import example.model.TypeA;",
38+
"import example.model.TypeB;",
39+
"public class Main {",
40+
" public void apply(SealedInterface sealedInterface) {",
41+
" switch(sealedInterface) {",
42+
" case TypeA a -> System.out.println(\"A!\");",
43+
" case TypeB b -> System.out.println(\"B!\");",
44+
" }",
45+
" }",
46+
"}");
47+
assertThat(removeUnusedImports(input)).isEqualTo(input);
48+
}
49+
}

0 commit comments

Comments
 (0)