Skip to content
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
2 changes: 2 additions & 0 deletions baseline-error-prone/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ dependencies {
testRuntimeOnly 'com.fasterxml.jackson.module:jackson-module-afterburner'
// for ForbidJavax
testRuntimeOnly 'com.palantir.conjure.java.runtime:conjure-java-annotations'
// for PreferCommonAnnotations
testRuntimeOnly 'org.jetbrains:annotations'

annotationProcessor 'com.google.auto.service:auto-service'
compileOnly 'org.immutables:value::annotations'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* (c) Copyright 2023 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.SeverityLevel;
import com.google.errorprone.VisitorState;
import com.google.errorprone.bugpatterns.BugChecker;
import com.google.errorprone.bugpatterns.BugChecker.ImportTreeMatcher;
import com.google.errorprone.fixes.SuggestedFix;
import com.google.errorprone.matchers.Description;
import com.google.errorprone.util.ASTHelpers;
import com.sun.source.tree.ImportTree;
import com.sun.tools.javac.code.Type;
import java.util.Map;
import java.util.Objects;

/**
* Checker that recommends using the common version of an annotation.
*
* Examples:
* - Guava's version of {@code @VisibleForTesting} over other copies.
*/
@AutoService(BugChecker.class)
@BugPattern(
summary = "Prefer the common version of annotations over other copies.",
severity = SeverityLevel.SUGGESTION)
public final class PreferCommonAnnotations extends BugChecker implements ImportTreeMatcher {

/** ClassName -> preferred import. */
private static final Map<String, String> PREFERRED_IMPORTS =
Map.of("VisibleForTesting", "com.google.common.annotations.VisibleForTesting");

@Override
public Description matchImport(ImportTree tree, VisitorState state) {
Type importType = ASTHelpers.getType(tree.getQualifiedIdentifier());
for (String affectedClassName : PREFERRED_IMPORTS.keySet()) {
String preferredType = PREFERRED_IMPORTS.get(affectedClassName);
if (importType != null
&& importType.toString().endsWith(affectedClassName)
&& !Objects.equals(importType.toString(), preferredType)) {
SuggestedFix fix = SuggestedFix.builder()
.removeImport(importType.toString())
.addImport(preferredType)
.build();
return describeMatch(tree, fix);
}
}
return Description.NO_MATCH;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* (c) Copyright 2023 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.jupiter.api.Test;

public final class PreferCommonAnnotationsTest {
@Test
public void desired_import_remains_unchanged() {
fix().addInputLines(
"Client.java",
"package com.google.frobber;",
"import com.google.common.annotations.VisibleForTesting;",
"public final class Client {",
" @VisibleForTesting",
" public int getValue() {",
" return 42;",
" }",
"}")
.expectUnchanged()
.doTest();
}

@Test
public void other_import_is_replaced() {
fix().addInputLines(
"Client.java",
"package com.google.frobber;",
"import org.jetbrains.annotations.VisibleForTesting;",
"public final class Client {",
" @VisibleForTesting",
" public int getValue() {",
" return 42;",
" }",
"}")
.addOutputLines(
"Client.java",
"package com.google.frobber;",
"import com.google.common.annotations.VisibleForTesting;",
"public final class Client {",
" @VisibleForTesting",
" public int getValue() {",
" return 42;",
" }",
"}")
.doTest();
}

@Test
public void fully_qualified_annotations_are_not_currently_rewritten() {
CompilationTestHelper.newInstance(PreferCommonAnnotations.class, getClass())
.addSourceLines(
"Client.java",
"package com.google.frobber;",
"public final class Client {",
// NOTE: fully-qualified annotations are not currently re-written
" @org.jetbrains.annotations.VisibleForTesting",
Comment on lines +71 to +72
Copy link
Contributor

Choose a reason for hiding this comment

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

seems reasonable to allow fully qualified as method to explicitly use different type

" public int getValue() {",
" return 42;",
" }",
"}")
.doTest();
}

@Test
public void wildcard_import_annotations_are_not_currently_rewritten() {
CompilationTestHelper.newInstance(PreferCommonAnnotations.class, getClass())
.addSourceLines(
"Client.java",
"package com.google.frobber;",
"import org.jetbrains.annotations.*;",
"public final class Client {",
// NOTE: wildcard-imported annotations are not currently re-written
" @VisibleForTesting",
Comment on lines +86 to +89
Copy link
Contributor

Choose a reason for hiding this comment

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

this seems ok for now given we block wildcard imports

" public int getValue() {",
" return 42;",
" }",
"}")
.doTest();
}

private RefactoringValidator fix() {
return RefactoringValidator.of(PreferCommonAnnotations.class, getClass());
}
}
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-2505.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: feature
feature:
description: Prefer common versions of annotations over other copies
links:
- https://github.com/palantir/gradle-baseline/pull/2505