Skip to content

Commit

Permalink
Exclude classes annotated with lombok's @DaTa from DirectReturn bug…
Browse files Browse the repository at this point in the history
… checker

While here, add a new Matcher that determines whether a class is annotated with lombok's @DaTa.
  • Loading branch information
mohamedsamehsalah authored and rickie committed Aug 11, 2023
1 parent 6a11b59 commit f29d74f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import static com.google.errorprone.matchers.Matchers.staticMethod;
import static com.google.errorprone.matchers.Matchers.toType;
import static tech.picnic.errorprone.bugpatterns.util.Documentation.BUG_PATTERNS_BASE_URL;
import static tech.picnic.errorprone.bugpatterns.util.MoreMatchers.isClassAnnotatedWithLombokData;

import com.google.auto.service.AutoService;
import com.google.common.collect.Streams;
Expand All @@ -25,6 +26,7 @@
import com.google.errorprone.util.ASTHelpers;
import com.sun.source.tree.AssignmentTree;
import com.sun.source.tree.BlockTree;
import com.sun.source.tree.ClassTree;
import com.sun.source.tree.ExpressionStatementTree;
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.IdentifierTree;
Expand Down Expand Up @@ -60,14 +62,18 @@ public final class DirectReturn extends BugChecker implements BlockTreeMatcher {
allOf(
not(toType(MethodInvocationTree.class, argument(0, isSameType(Class.class.getName())))),
staticMethod().onClass("org.mockito.Mockito").namedAnyOf("mock", "spy"));
private static final Matcher<ClassTree> IS_CLASS_ANNOTATED_WITH_LOMBOK_DATA =
isClassAnnotatedWithLombokData();

/** Instantiates a new {@link DirectReturn} instance. */
public DirectReturn() {}

@Override
public Description matchBlock(BlockTree tree, VisitorState state) {
List<? extends StatementTree> statements = tree.getStatements();
if (statements.size() < 2) {
ClassTree enclosingNode = ASTHelpers.findEnclosingNode(state.getPath(), ClassTree.class);
if (statements.size() < 2
|| IS_CLASS_ANNOTATED_WITH_LOMBOK_DATA.matches(enclosingNode, state)) {
return Description.NO_MATCH;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import com.google.errorprone.predicates.TypePredicate;
import com.google.errorprone.util.ASTHelpers;
import com.sun.source.tree.AnnotationTree;
import com.sun.source.tree.ClassTree;
import com.sun.tools.javac.code.Symbol;
import lombok.Data;

/**
* A collection of general-purpose {@link Matcher}s.
Expand All @@ -32,6 +34,21 @@ public static <T extends AnnotationTree> Matcher<T> hasMetaAnnotation(String ann
};
}

/**
* Returns a {@link Matcher} that determines whether a given {@link ClassTree} is annotated with
* {@link Data}.
*
* @param <T> The type of tree to match against.
* @return A {@link Matcher} that matches trees with {@link Data} annotation.
*/
public static <T extends ClassTree> Matcher<T> isClassAnnotatedWithLombokData() {
TypePredicate typePredicate = hasAnnotation("lombok.Data");
return (tree, state) -> {
Symbol sym = ASTHelpers.getSymbol(tree);
return sym != null && typePredicate.apply(sym.type, state);
};
}

// XXX: Consider moving to a `MoreTypePredicates` utility class.
private static TypePredicate hasAnnotation(String annotationClassName) {
return (type, state) -> ASTHelpers.hasAnnotation(type.tsym, annotationClassName, state);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,4 +223,26 @@ void replacement() {
"}")
.doTest(TestMode.TEXT_MATCH);
}

@Test
void excludeClassesAnnotatedWithLombokData() {
BugCheckerRefactoringTestHelper.newInstance(DirectReturn.class, getClass())
.addInputLines(
"A.java",
"import lombok.Data;",
"",
"@Data",
"public class A {",
" private String field;",
"}")
.addOutputLines(
"A.java",
"import lombok.Data;",
"",
"@Data",
"public class A {",
" private String field;",
"}")
.doTest(TestMode.TEXT_MATCH);
}
}

0 comments on commit f29d74f

Please sign in to comment.