From fe06f311af3888d05c95bc8624f70240da162234 Mon Sep 17 00:00:00 2001 From: Andrew Duffy Date: Mon, 8 Jun 2020 15:15:13 -0400 Subject: [PATCH] Add inline/meta-annotation style collision errorprone --- .../errorprone/ImmutablesStyleCollision.java | 22 ++++++++-------- .../ImmutablesStyleCollisionTest.java | 25 +++++++++++++++++-- 2 files changed, 34 insertions(+), 13 deletions(-) diff --git a/baseline-error-prone/src/main/java/com/palantir/baseline/errorprone/ImmutablesStyleCollision.java b/baseline-error-prone/src/main/java/com/palantir/baseline/errorprone/ImmutablesStyleCollision.java index 17eedbcf42..3a295a1c18 100644 --- a/baseline-error-prone/src/main/java/com/palantir/baseline/errorprone/ImmutablesStyleCollision.java +++ b/baseline-error-prone/src/main/java/com/palantir/baseline/errorprone/ImmutablesStyleCollision.java @@ -2,34 +2,34 @@ import com.google.auto.service.AutoService; import com.google.errorprone.BugPattern; +import com.google.errorprone.BugPattern.LinkType; import com.google.errorprone.VisitorState; import com.google.errorprone.bugpatterns.BugChecker; import com.google.errorprone.matchers.ChildMultiMatcher; import com.google.errorprone.matchers.Description; import com.google.errorprone.matchers.Matcher; import com.google.errorprone.matchers.Matchers; -import com.sun.source.tree.AnnotatedTypeTree; +import com.sun.source.tree.ClassTree; import org.immutables.value.Value; @AutoService(BugChecker.class) @BugPattern( name = "ImmutablesStyleCollision", + linkType = LinkType.CUSTOM, link = "https://github.com/palantir/gradle-baseline#baseline-error-prone-checks", severity = BugPattern.SeverityLevel.ERROR, summary = "Immutables @Value.Style inline annotation should not be present alongside a Style " + "meta-annotation, as there is no Style merging. You should either modify the " - + "meta-annotation, add the meta-annotation's fields to your inline @Value.Style declaration.") -public final class ImmutablesStyleCollision extends BugChecker implements BugChecker.AnnotatedTypeTreeMatcher { - private static final Matcher INLINE_STYLE_ANNOTATION = Matchers.hasAnnotation(Value.Style.class); - private static final Matcher STYLE_META_ANNOTATION = Matchers.annotations( - ChildMultiMatcher.MatchType.AT_LEAST_ONE, - Matchers.allOf( - Matchers.not(Matchers.isSameType(Value.Style.class)), Matchers.isSubtypeOf(Value.Style.class))); - private static final Matcher MATCHER = - Matchers.allOf(INLINE_STYLE_ANNOTATION, STYLE_META_ANNOTATION); + + "meta-annotation, or add the meta-annotation's fields to your inline @Value.Style " + + "declaration.") +public final class ImmutablesStyleCollision extends BugChecker implements BugChecker.ClassTreeMatcher { + private static final Matcher INLINE_STYLE_ANNOTATION = Matchers.hasAnnotation(Value.Style.class); + private static final Matcher STYLE_META_ANNOTATION = + Matchers.annotations(ChildMultiMatcher.MatchType.AT_LEAST_ONE, Matchers.hasAnnotation(Value.Style.class)); + private static final Matcher MATCHER = Matchers.allOf(INLINE_STYLE_ANNOTATION, STYLE_META_ANNOTATION); @Override - public Description matchAnnotatedType(AnnotatedTypeTree tree, VisitorState state) { + public Description matchClass(ClassTree tree, VisitorState state) { if (MATCHER.matches(tree, state)) { return buildDescription(tree) .setMessage("Immutable type cannot have both inline @Value.Style and meta-annotation applied") diff --git a/baseline-error-prone/src/test/java/com/palantir/baseline/errorprone/ImmutablesStyleCollisionTest.java b/baseline-error-prone/src/test/java/com/palantir/baseline/errorprone/ImmutablesStyleCollisionTest.java index 7b3e8173e8..1fbda6986f 100644 --- a/baseline-error-prone/src/test/java/com/palantir/baseline/errorprone/ImmutablesStyleCollisionTest.java +++ b/baseline-error-prone/src/test/java/com/palantir/baseline/errorprone/ImmutablesStyleCollisionTest.java @@ -13,12 +13,33 @@ public void testPass() { + "public interface Person {" + " String name();" + "}"; - helper().addSourceLines("Pass.java", sourceCode).doTest(); + helper().addSourceLines("Person.java", sourceCode).doTest(); } @Test public void testFail() { - // + helper().addSourceLines( + "MyMetaAnnotation.java", + "import org.immutables.value.Value;", + "import java.lang.annotation.ElementType;", + "import java.lang.annotation.Retention;", + "import java.lang.annotation.RetentionPolicy;", + "import java.lang.annotation.Target;", + "import org.immutables.value.Value;", + "@Target({ElementType.PACKAGE, ElementType.TYPE})\n", + "@Retention(RetentionPolicy.CLASS)\n", + "@Value.Style(visibility = Value.Style.ImplementationVisibility.PUBLIC)\n", + "public @interface MyMetaAnnotation {}") + .addSourceLines( + "Person.java", + "import org.immutables.value.Value;", + "@MyMetaAnnotation", + "@Value.Style(with = \"with\")", + "// BUG: Diagnostic contains: Immutable type cannot have both inline", + "public interface Person {", + " String name();", + "}") + .doTest(); } private CompilationTestHelper helper() {