Skip to content

Commit

Permalink
Simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
Bastien Diederichs committed Oct 29, 2022
1 parent f65110f commit 005de9d
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,21 @@
import static com.google.errorprone.BugPattern.LinkType.CUSTOM;
import static com.google.errorprone.BugPattern.SeverityLevel.SUGGESTION;
import static com.google.errorprone.BugPattern.StandardTags.STYLE;
import static com.sun.source.tree.Tree.Kind.IDENTIFIER;
import static com.sun.source.tree.Tree.Kind.INSTANCE_OF;
import static com.sun.source.tree.Tree.Kind.LAMBDA_EXPRESSION;
import static com.sun.source.tree.Tree.Kind.MEMBER_SELECT;
import static com.sun.source.tree.Tree.Kind.METHOD_INVOCATION;
import static tech.picnic.errorprone.bugpatterns.util.Documentation.BUG_PATTERNS_BASE_URL;

import com.google.auto.service.AutoService;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.errorprone.BugPattern;
import com.google.errorprone.VisitorState;
import com.google.errorprone.bugpatterns.BugChecker;
import com.google.errorprone.bugpatterns.BugChecker.LambdaExpressionTreeMatcher;
import com.google.errorprone.fixes.SuggestedFix;
import com.google.errorprone.fixes.SuggestedFixes;
import com.google.errorprone.matchers.Description;
import com.sun.source.tree.IdentifierTree;
import com.sun.source.tree.InstanceOfTree;
import com.sun.source.tree.LambdaExpressionTree;
import com.sun.source.tree.MemberSelectTree;
import com.sun.source.tree.MethodInvocationTree;

/** A {@link BugChecker} that aligns usages of T.class::isInstance. */
@AutoService(BugChecker.class)
Expand All @@ -42,36 +35,13 @@ public IsInstanceUsage() {}

@Override
public Description matchLambdaExpression(LambdaExpressionTree tree, VisitorState state) {
if (ImmutableSet.of(LAMBDA_EXPRESSION, METHOD_INVOCATION).contains(tree.getKind())) {
if (INSTANCE_OF == tree.getBody().getKind()) {
return constructDescription(
tree, state, constructFix(tree, ((InstanceOfTree) tree.getBody()).getType()));
} else if (METHOD_INVOCATION == tree.getBody().getKind()) {
return treatMethodInvocation(tree, (MethodInvocationTree) tree.getBody(), state);
}
if (LAMBDA_EXPRESSION == tree.getKind() && INSTANCE_OF == tree.getBody().getKind()) {
return constructDescription(
tree, state, constructFix(tree, ((InstanceOfTree) tree.getBody()).getType()));
}
return Description.NO_MATCH;
}

private Description treatMethodInvocation(
LambdaExpressionTree tree, MethodInvocationTree methodInvocationTree, VisitorState state) {
if (MEMBER_SELECT != methodInvocationTree.getMethodSelect().getKind()
|| !((MemberSelectTree) methodInvocationTree.getMethodSelect())
.getIdentifier()
.contentEquals("isInstance")) {
return Description.NO_MATCH;
}
MemberSelectTree methodSelect = (MemberSelectTree) methodInvocationTree.getMethodSelect();
if (MEMBER_SELECT != methodSelect.getExpression().getKind()
|| IDENTIFIER
!= ((MemberSelectTree) methodSelect.getExpression()).getExpression().getKind()) {
return Description.NO_MATCH;
}
IdentifierTree identifierTree =
(IdentifierTree) ((MemberSelectTree) methodSelect.getExpression()).getExpression();
return constructDescription(tree, state, constructFix(tree, identifierTree.getName()));
}

private Description constructDescription(
LambdaExpressionTree tree, VisitorState state, SuggestedFix.Builder fixBuilder) {
SuggestedFix fix = fixBuilder.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ private static Optional<SuggestedFix.Builder> constructMethodRef(
}

// XXX: Replace nested `Optional` usage.
// XXX: Cover for deeper method invocation like `T.class.isInstance(i)`.
@SuppressWarnings("NestedOptionals")
private static Optional<SuggestedFix.Builder> constructMethodRef(
LambdaExpressionTree lambdaExpr, MethodInvocationTree subTree) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ void identification() {
" // BUG: Diagnostic contains:",
" Flux.just(1).filter(i -> i instanceof Integer);",
" // BUG: Diagnostic contains:",
" Flux.just(1).filter(i -> Integer.class.isInstance(i));",
" // BUG: Diagnostic contains:",
" Flux.just(1).onErrorResume(t -> t instanceof Exception, t -> Flux.empty());",
"",
" Flux.just(1).filter(Integer.class::isInstance);",
Expand All @@ -44,7 +42,6 @@ void replacement() {
"class A {",
" void m() {",
" Flux.just(1).filter(i -> i instanceof Integer);",
" Flux.just(1).filter(i -> Integer.class.isInstance(i));",
" }",
"}")
.addOutputLines(
Expand All @@ -54,7 +51,6 @@ void replacement() {
"class A {",
" void m() {",
" Flux.just(1).filter(Integer.class::isInstance);",
" Flux.just(1).filter(Integer.class::isInstance);",
" }",
"}")
.doTest(BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH);
Expand Down

0 comments on commit 005de9d

Please sign in to comment.