Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add errorprone check for immutables style collisions #1396

Merged
merged 8 commits into from
Jun 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ Safe Logging can be found at [github.com/palantir/safe-logging](https://github.c
- `VisibleForTestingPackagePrivate`: `@VisibleForTesting` members should be package-private.
- `OptionalFlatMapOfNullable`: Optional.map functions may return null to safely produce an empty result.
- `ExtendsErrorOrThrowable`: Avoid extending Error (or subclasses of it) or Throwable directly.
- `ImmutablesStyleCollision`: Prevent unintentionally voiding immutables Style meta-annotations through the introduction of inline style annotations.

### Programmatic Application

Expand Down
3 changes: 3 additions & 0 deletions baseline-error-prone/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ apply from: "${rootDir}/gradle/publish-jar.gradle"
dependencies {
compile 'com.google.errorprone:error_prone_core'
compile 'org.mockito:mockito-errorprone'
compile 'org.immutables:value::annotations'

testCompile gradleApi()
testCompile 'com.palantir.tokens:auth-tokens'
Expand All @@ -21,10 +22,12 @@ dependencies {
testCompile 'org.assertj:assertj-core'
testCompile 'org.jooq:jooq'
testCompile 'com.palantir.tritium:tritium-registry'
testCompileOnly 'org.immutables:value::annotations'
testImplementation 'org.junit.jupiter:junit-jupiter'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-migrationsupport'

annotationProcessor 'com.google.auto.service:auto-service'
annotationProcessor 'org.immutables:value'
compileOnly 'com.google.auto.service:auto-service'
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* (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.ClassTree;
import org.immutables.value.Value;

@AutoService(BugChecker.class)
@BugPattern(
name = "ImmutablesStyleCollision",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please document this alongside the other baseline error-prone checks in the readme!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll also need to add it to the list of errorprone rules we always apply

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added it to the readme but where is the list @ferozco ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that's just for automatic refactors, but this can't be auto-fixed. severity = BugPattern.SeverityLevel.ERROR, should be sufficient.

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, 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 matchClass(ClassTree tree, VisitorState state) {
if (MATCHER.matches(tree, state)) {
return describeMatch(tree);
}
return Description.NO_MATCH;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* (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;
import org.junit.Test;

public class ImmutablesStyleCollisionTest {

@Test
public void testPass() {
helper().addSourceLines(
"Person.java",
"import org.immutables.value.Value;",
"public interface Person {",
" String name();",
"}")
.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: Immutables @Value.Style inline annotation should not be present",
"public interface Person {",
" String name();",
"}")
.doTest();
}

private CompilationTestHelper helper() {
return CompilationTestHelper.newInstance(ImmutablesStyleCollision.class, getClass());
}
}
6 changes: 6 additions & 0 deletions changelog/@unreleased/pr-1396.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type: improvement
improvement:
description: Add errorprone check to avoid accidentally undoing immutables Style
meta-annotations.
links:
- https://github.com/palantir/gradle-baseline/pull/1396