mirror of
https://github.com/jlengrand/error-prone-support.git
synced 2026-03-10 08:11:25 +00:00
Compare commits
15 Commits
junie-init
...
rossendrij
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0d98178fc8 | ||
|
|
a1b92ef92a | ||
|
|
857e912061 | ||
|
|
f87b916329 | ||
|
|
3b15c3bf7d | ||
|
|
a3c828fe94 | ||
|
|
0cbcdb8c8b | ||
|
|
7548278b91 | ||
|
|
5b2a391523 | ||
|
|
6f98868171 | ||
|
|
0ad09268e0 | ||
|
|
7f246785cf | ||
|
|
7dab0eb562 | ||
|
|
fa004e74b4 | ||
|
|
e9fcfc7624 |
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user