Compare commits

...

15 Commits

Author SHA1 Message Date
Rick Ossendrijver
0d98178fc8 Add a first version of the check for incorrect method signature 2021-03-22 16:51:00 +01:00
Stephan Schroevers
a1b92ef92a Suggestions 2021-02-27 12:47:44 +01:00
Rick Ossendrijver
857e912061 Change severity and tag 2021-02-27 12:32:05 +01:00
Rick Ossendrijver
f87b916329 Remove trailing comments 2021-02-27 12:32:05 +01:00
Rick Ossendrijver
3b15c3bf7d Improve the method by using getKind and ::cast 2021-02-27 12:32:05 +01:00
Rick Ossendrijver
a3c828fe94 Format and turn back on refaster checks 2021-02-27 12:32:05 +01:00
Rick Ossendrijver
0cbcdb8c8b Refactor names and the test class names 2021-02-27 12:32:05 +01:00
Rick Ossendrijver
7548278b91 Remove wildcart import 2021-02-27 12:32:05 +01:00
Rick Ossendrijver
5b2a391523 Remove unused imports 2021-02-27 12:32:05 +01:00
Rick Ossendrijver
6f98868171 Add test cases and improve the comments 2021-02-27 12:32:05 +01:00
Rick Ossendrijver
0ad09268e0 Make the description work and fix the test 2021-02-27 12:32:05 +01:00
Rick Ossendrijver
7f246785cf Update some comments 2021-02-27 12:32:05 +01:00
Rick Ossendrijver
7dab0eb562 Add comment for the next time 2021-02-27 12:32:05 +01:00
Rick Ossendrijver
fa004e74b4 Add retrieval of methods and try some multimatcher things 2021-02-27 12:32:05 +01:00
Rick Ossendrijver
e9fcfc7624 Add file and test for missing refaster annotations 2021-02-27 12:32:05 +01:00
3 changed files with 133 additions and 2 deletions

View File

@@ -0,0 +1,53 @@
package tech.picnic.errorprone.bugpatterns;
import static com.google.errorprone.matchers.ChildMultiMatcher.MatchType.AT_LEAST_ONE;
import static com.google.errorprone.matchers.Matchers.annotations;
import static com.google.errorprone.matchers.Matchers.anyOf;
import static com.google.errorprone.matchers.Matchers.isType;
import com.google.auto.service.AutoService;
import com.google.errorprone.BugPattern;
import com.google.errorprone.BugPattern.LinkType;
import com.google.errorprone.BugPattern.SeverityLevel;
import com.google.errorprone.BugPattern.StandardTags;
import com.google.errorprone.VisitorState;
import com.google.errorprone.bugpatterns.BugChecker;
import com.google.errorprone.bugpatterns.BugChecker.ClassTreeMatcher;
import com.google.errorprone.matchers.Description;
import com.google.errorprone.matchers.MultiMatcher;
import com.sun.source.tree.AnnotationTree;
import com.sun.source.tree.ClassTree;
import com.sun.source.tree.MethodTree;
import com.sun.source.tree.Tree;
/** A {@link BugChecker} that flags likely missing Refaster annotations. */
@AutoService(BugChecker.class)
@BugPattern(
name = "MissingRefasterAnnotation",
summary = "The Refaster template contains a method without any Refaster annotations",
linkType = LinkType.NONE,
severity = SeverityLevel.WARNING,
tags = StandardTags.LIKELY_ERROR)
public final class MissingRefasterAnnotationCheck extends BugChecker implements ClassTreeMatcher {
private static final long serialVersionUID = 1L;
private static final MultiMatcher<Tree, AnnotationTree> HAS_REFASTER_ANNOTATION =
annotations(
AT_LEAST_ONE,
anyOf(
isType("com.google.errorprone.refaster.annotation.Placeholder"),
isType("com.google.errorprone.refaster.annotation.BeforeTemplate"),
isType("com.google.errorprone.refaster.annotation.AfterTemplate")));
@Override
public Description matchClass(ClassTree tree, VisitorState state) {
long methodTypes =
tree.getMembers().stream()
.filter(member -> member.getKind() == Tree.Kind.METHOD)
.map(MethodTree.class::cast)
.map(method -> HAS_REFASTER_ANNOTATION.matches(method, state))
.distinct()
.count();
return methodTypes < 2 ? Description.NO_MATCH : buildDescription(tree).build();
}
}

View File

@@ -67,8 +67,8 @@ final class ImmutableListTemplates {
}
@AfterTemplate
ImmutableList<T> after(T element) {
return ImmutableList.of(element);
ImmutableList<T> after(T secondName) {
return ImmutableList.of(secondName);
}
}

View File

@@ -0,0 +1,78 @@
package tech.picnic.errorprone.bugpatterns;
import com.google.common.base.Predicates;
import com.google.errorprone.CompilationTestHelper;
import org.junit.jupiter.api.Test;
public final class MissingRefasterAnnotationCheckTest {
private final CompilationTestHelper compilationTestHelper =
CompilationTestHelper.newInstance(MissingRefasterAnnotationCheck.class, getClass())
.expectErrorMessage(
"X",
Predicates.containsPattern(
"The Refaster template contains a method without any Refaster annotations"));
@Test
public void testIdentification() {
compilationTestHelper
.addSourceLines(
"A.java",
"import com.google.errorprone.refaster.annotation.AfterTemplate;",
"import com.google.errorprone.refaster.annotation.AlsoNegation;",
"import com.google.errorprone.refaster.annotation.BeforeTemplate;",
"import java.util.Map;",
"",
"class A {",
" // BUG: Diagnostic matches: X",
" static final class MethodLacksBeforeTemplateAnnotation {",
" @BeforeTemplate",
" boolean before1(String string) {",
" return string.equals(\"\");",
" }",
"",
" // @BeforeTemplate is missing",
" boolean before2(String string) {",
" return string.length() == 0;",
" }",
"",
" @AfterTemplate",
" @AlsoNegation",
" boolean after(String string) {",
" return string.isEmpty();",
" }",
" }",
"",
" // BUG: Diagnostic matches: X",
" static final class MethodLacksAfterTemplateAnnotation {",
" @BeforeTemplate",
" boolean before(String string) {",
" return string.equals(\"\");",
" }",
"",
" // @AfterTemplate is missing",
" boolean after(String string) {",
" return string.isEmpty();",
" }",
" }",
"",
" // BUG: Diagnostic matches: X",
" abstract class MethodLacksPlaceholderAnnotation<K, V> {",
" // @Placeholder is missing",
" abstract V function(K key);",
"",
" @BeforeTemplate",
" void before(Map<K, V> map, K key) {",
" if (!map.containsKey(key)) {",
" map.put(key, function(key));",
" }",
" }",
"",
" @AfterTemplate",
" void after(Map<K, V> map, K key) {",
" map.computeIfAbsent(key, k -> function(k));",
" }",
" }",
"}")
.doTest();
}
}