Skip to content

Commit

Permalink
Add inline/meta-annotation style collision errorprone
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Duffy committed Jun 8, 2020
1 parent 2ebe8d0 commit 3b3c97c
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,35 +1,50 @@
/*
* (c) Copyright 2020 Palantir Technologies Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.palantir.baseline.errorprone;

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<AnnotatedTypeTree> INLINE_STYLE_ANNOTATION = Matchers.hasAnnotation(Value.Style.class);
private static final Matcher<AnnotatedTypeTree> 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<AnnotatedTypeTree> 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<ClassTree> INLINE_STYLE_ANNOTATION = Matchers.hasAnnotation(Value.Style.class);
private static final Matcher<ClassTree> STYLE_META_ANNOTATION =
Matchers.annotations(ChildMultiMatcher.MatchType.AT_LEAST_ONE, Matchers.hasAnnotation(Value.Style.class));
private static final Matcher<ClassTree> 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")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* (c) Copyright 2020 Palantir Technologies Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.palantir.baseline.errorprone;

import com.google.errorprone.CompilationTestHelper;
Expand All @@ -13,12 +28,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() {
Expand Down

0 comments on commit 3b3c97c

Please sign in to comment.