Skip to content

Commit

Permalink
Revert failing test, change main code
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan202 committed Nov 22, 2022
1 parent 1299c5a commit 0e7276b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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.
*
* <p>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 <T> 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 <T extends Tree> Matcher<T> hasMetaAnnotation(String annotationType) {
public static <T extends AnnotationTree> Matcher<T> hasMetaAnnotation(String annotationType) {
TypePredicate typePredicate = hasAnnotation(annotationType);
return (tree, state) -> {
Symbol sym = ASTHelpers.getSymbol(tree);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;",
Expand All @@ -40,56 +36,27 @@ void hasMetaAnnotation() {
" @TestTemplate",
" void negative4() {}",
"",
" @InheritedWithoutMetaAnnotation",
" void negative5() {}",
"",
" // BUG: Diagnostic contains:",
" @ParameterizedTest",
" void positive1() {}",
"",
" // 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<Tree> DELEGATE =
private static final Matcher<AnnotationTree> 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;
}
}
}

0 comments on commit 0e7276b

Please sign in to comment.