Skip to content

Commit

Permalink
Avoid Tree#toString call
Browse files Browse the repository at this point in the history
  • Loading branch information
hisener committed Jan 2, 2024
1 parent 0c02c70 commit 67aa36d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import com.google.errorprone.util.ASTHelpers;
import com.sun.source.tree.ClassTree;
import com.sun.source.tree.CompilationUnitTree;
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.IdentifierTree;
import com.sun.source.tree.ImportTree;
import com.sun.source.tree.MemberSelectTree;
Expand Down Expand Up @@ -85,11 +86,11 @@ public final class UnnecessarilyFullyQualified extends BugChecker
* org.immutables.value.Value} and using {@code @Value.Immutable} is more favorable than importing
* {@code org.immutables.value.Value.Immutable} and using {@code @Immutable}.
*/
private final ImmutableSet<String> exemptedTypes;
private final ImmutableSet<String> exemptedEnclosingTypes;

@Inject
UnnecessarilyFullyQualified(ErrorProneFlags errorProneFlags) {
this.exemptedTypes = errorProneFlags.getSetOrEmpty("BadImport:BadEnclosingTypes");
this.exemptedEnclosingTypes = errorProneFlags.getSetOrEmpty("BadImport:BadEnclosingTypes");
}

@Override
Expand Down Expand Up @@ -159,12 +160,8 @@ private void handle(TreePath path) {
if (!isFullyQualified(tree)) {
return;
}
if (exemptedTypes.contains(tree.getExpression().toString())
|| BadImport.BAD_NESTED_CLASSES.contains(tree.getIdentifier().toString())) {
if (tree.getExpression() instanceof MemberSelectTree
&& getSymbol(tree.getExpression()) instanceof ClassSymbol) {
handle(new TreePath(path, tree.getExpression()));
}
if (isBadNestedClass(tree) || isExemptedEnclosingType(tree)) {
handle(new TreePath(path, tree.getExpression()));
return;
}
Symbol symbol = getSymbol(tree);
Expand All @@ -182,6 +179,23 @@ && getSymbol(tree.getExpression()) instanceof ClassSymbol) {
treePaths.add(path);
}

private boolean isBadNestedClass(MemberSelectTree tree) {
return BadImport.BAD_NESTED_CLASSES.contains(tree.getIdentifier().toString());
}

private boolean isExemptedEnclosingType(MemberSelectTree tree) {
ExpressionTree expression = tree.getExpression();
if (!(expression instanceof MemberSelectTree)) {
return false;
}
Symbol symbol = getSymbol(expression);
if (!(symbol instanceof ClassSymbol)) {
return false;
}

return exemptedEnclosingTypes.contains(symbol.getQualifiedName().toString());
}

private boolean isFullyQualified(MemberSelectTree tree) {
AtomicBoolean isFullyQualified = new AtomicBoolean();
new SimpleTreeVisitor<Void, Void>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,32 @@ public void packageInfo() {
}

@Test
public void exemptedTypes() {
public void staticNestedClass() {
helper
.addInputLines(
"test/EnclosingType.java",
"package test;",
"",
"public final class EnclosingType {",
" public static final class StaticNestedClass {}",
"}")
.expectUnchanged()
.addInputLines(
"Test.java",
"interface Test {",
" test.EnclosingType.StaticNestedClass method();",
"}")
.addOutputLines(
"Test.java",
"import test.EnclosingType.StaticNestedClass;",
"interface Test {",
" StaticNestedClass method();",
"}")
.doTest();
}

@Test
public void exemptedEnclosingTypes() {
helper
.setArgs("-XepOpt:BadImport:BadEnclosingTypes=org.immutables.value.Value")
.addInputLines(
Expand Down Expand Up @@ -258,7 +283,7 @@ public void exemptedTypes() {
}

@Test
public void exemptedTypes_importWouldBeAmbiguous() {
public void exemptedEnclosingTypes_importWouldBeAmbiguous() {
helper
.setArgs("-XepOpt:BadImport:BadEnclosingTypes=org.immutables.value.Value")
.addInputLines(
Expand Down

0 comments on commit 67aa36d

Please sign in to comment.