From 0e7276bdf4d5e98d3dbe5c0e3568a3944ad18084 Mon Sep 17 00:00:00 2001 From: Stephan Schroevers Date: Tue, 22 Nov 2022 08:18:17 +0100 Subject: [PATCH] Revert failing test, change main code --- .../bugpatterns/util/MoreMatchers.java | 13 +++---- .../bugpatterns/util/MoreMatchersTest.java | 37 +------------------ 2 files changed, 7 insertions(+), 43 deletions(-) diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/util/MoreMatchers.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/util/MoreMatchers.java index 7e457cdf48..b62e761db0 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/util/MoreMatchers.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/util/MoreMatchers.java @@ -4,7 +4,7 @@ import com.google.errorprone.matchers.Matchers; import com.google.errorprone.predicates.TypePredicate; import com.google.errorprone.util.ASTHelpers; -import com.sun.source.tree.Tree; +import com.sun.source.tree.AnnotationTree; import com.sun.tools.javac.code.Symbol; /** @@ -16,18 +16,15 @@ public final class MoreMatchers { private MoreMatchers() {} /** - * Returns a {@link Matcher} that determines whether a given tree has a meta annotation of the - * specified type. - * - *

This includes annotations inherited from superclasses due to {@link - * java.lang.annotation.Inherited}. + * Returns a {@link Matcher} that determines whether a given {@link AnnotationTree} has a + * meta-annotation of the specified type. * * @param The type of tree to match against. * @param annotationType The binary type name of the annotation (e.g. * "org.jspecify.annotations.Nullable", or "some.package.OuterClassName$InnerClassName") - * @return A {@link Matcher} that matches trees with the specified meta annotation. + * @return A {@link Matcher} that matches trees with the specified meta-annotation. */ - public static Matcher hasMetaAnnotation(String annotationType) { + public static Matcher hasMetaAnnotation(String annotationType) { TypePredicate typePredicate = hasAnnotation(annotationType); return (tree, state) -> { Symbol sym = ASTHelpers.getSymbol(tree); diff --git a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/util/MoreMatchersTest.java b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/util/MoreMatchersTest.java index 93030bcc32..842fb57b6b 100644 --- a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/util/MoreMatchersTest.java +++ b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/util/MoreMatchersTest.java @@ -7,12 +7,9 @@ import com.google.errorprone.VisitorState; import com.google.errorprone.bugpatterns.BugChecker; import com.google.errorprone.bugpatterns.BugChecker.AnnotationTreeMatcher; -import com.google.errorprone.bugpatterns.BugChecker.MethodTreeMatcher; import com.google.errorprone.matchers.Description; import com.google.errorprone.matchers.Matcher; import com.sun.source.tree.AnnotationTree; -import com.sun.source.tree.MethodTree; -import com.sun.source.tree.Tree; import org.junit.jupiter.api.Test; final class MoreMatchersTest { @@ -21,7 +18,6 @@ void hasMetaAnnotation() { CompilationTestHelper.newInstance(TestMatcher.class, getClass()) .addSourceLines( "A.java", - "import java.lang.annotation.Inherited;", "import org.junit.jupiter.api.AfterAll;", "import org.junit.jupiter.api.RepeatedTest;", "import org.junit.jupiter.api.Test;", @@ -40,9 +36,6 @@ void hasMetaAnnotation() { " @TestTemplate", " void negative4() {}", "", - " @InheritedWithoutMetaAnnotation", - " void negative5() {}", - "", " // BUG: Diagnostic contains:", " @ParameterizedTest", " void positive1() {}", @@ -50,46 +43,20 @@ void hasMetaAnnotation() { " // BUG: Diagnostic contains:", " @RepeatedTest(2)", " void positive2() {}", - "", - " // BUG: Diagnostic contains:", - " @InheritedWithMetaAnnotation", - " void positive3() {}", - "", - " @Inherited", - " @interface InheritedWithoutMetaAnnotation {}", - "", - " @Inherited", - " @TestTemplate", - " @interface InheritedWithMetaAnnotation {}", - "", - " class B extends A {", - " @Override", - " void negative5() {}", - "", - " @Override", - " // BUG: Diagnostic contains:", - " void positive3() {}", - " }", "}") .doTest(); } /** A {@link BugChecker} that delegates to {@link MoreMatchers#hasMetaAnnotation(String)} . */ @BugPattern(summary = "Interacts with `MoreMatchers` for testing purposes", severity = ERROR) - public static final class TestMatcher extends BugChecker - implements AnnotationTreeMatcher, MethodTreeMatcher { + public static final class TestMatcher extends BugChecker implements AnnotationTreeMatcher { private static final long serialVersionUID = 1L; - private static final Matcher DELEGATE = + private static final Matcher DELEGATE = MoreMatchers.hasMetaAnnotation("org.junit.jupiter.api.TestTemplate"); @Override public Description matchAnnotation(AnnotationTree tree, VisitorState state) { return DELEGATE.matches(tree, state) ? describeMatch(tree) : Description.NO_MATCH; } - - @Override - public Description matchMethod(MethodTree tree, VisitorState state) { - return DELEGATE.matches(tree, state) ? describeMatch(tree) : Description.NO_MATCH; - } } }