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 3 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
3 changes: 3 additions & 0 deletions baseline-error-prone/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@ 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'
compileOnly 'org.immutables:value::annotations'
}

// Incorrectly identifies tests as junit4 usage
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* (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 buildDescription(tree)
.setMessage("Immutable type cannot have both inline @Value.Style and meta-annotation applied")
.build();
a10y marked this conversation as resolved.
Show resolved Hide resolved
}
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: Immutable type cannot have both inline",
"public interface Person {",
" String name();",
"}")
.doTest();
}

private CompilationTestHelper helper() {
return CompilationTestHelper.newInstance(ImmutablesStyleCollision.class, getClass());
}
}