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

Check eclipse collection api factory #2772

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions baseline-error-prone/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ dependencies {
testRuntimeOnly 'com.palantir.conjure.java.runtime:conjure-java-annotations'
// for PreferCommonAnnotations
testRuntimeOnly 'org.jetbrains:annotations'
// for eclipse-collections classloading bug
testRuntimeOnly 'org.eclipse.collections:eclipse-collections'

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,59 @@
/*
* (c) Copyright 2024 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.VisitorState;
import com.google.errorprone.bugpatterns.BugChecker;
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;

@AutoService(BugChecker.class)
@BugPattern(
summary = "Import eclipse-collections uses api factory instead of impl factory. This could result in"
+ "classloading deadlocks as explained here: https://github.com/palantir/atlasdb/pull/7073",
severity = BugPattern.SeverityLevel.ERROR,
documentSuppression = false)
public final class EclipseCollectionsApiFactoryUsage extends BugChecker implements BugChecker.ImportTreeMatcher {
private static final String BAD_ECLIPSE_COLLECTIONS_USAGE_SUBSTRING = "org.eclipse.collections.api.factory.";
Copy link
Author

Choose a reason for hiding this comment

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

I can't find an errorprone rule that deals with libraries we don't like. I suppose because that is pretty opinionated.

Anyway, decided against making this check too generic (supporting other bad imports and fixes) since there would be a lot of abstraction written for one case (so far) to be able to support various ways of fixing issues.


public EclipseCollectionsApiFactoryUsage() {}

@Override
public Description matchImport(ImportTree tree, VisitorState state) {
Type importType = ASTHelpers.getType(tree.getQualifiedIdentifier());

if (importType == null) {
return Description.NO_MATCH;
}

String importName = importType.toString();
if (importName.contains(BAD_ECLIPSE_COLLECTIONS_USAGE_SUBSTRING)) {
String fixedImport = importName.replace(".api.", ".impl.");
SuggestedFix fix = SuggestedFix.builder()
.removeImport(importName)
.addImport(fixedImport)
.build();
return describeMatch(tree, fix);
}
return Description.NO_MATCH;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* (c) Copyright 2024 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 org.junit.jupiter.api.Test;

public class EclipseCollectionsApiFactoryUsageTest {
@Test
public void desired_import_remains_unchanged() {
fix().addInputLines(
"Client.java",
"package com.google.frobber;",
"import org.eclipse.collections.impl.factory.primitive.LongLists;",
"public final class Client {",
" public int getOrder() {",
" return 66;",
" }",
"}")
.expectUnchanged()
.doTest();
}

@Test
public void bad_import_changes() {
fix().addInputLines(
"Client.java",
"package com.google.frobber;",
"import org.eclipse.collections.api.factory.primitive.LongLists;",
"public final class Client {",
" public int getOrder() {",
" return 66;",
" }",
"}")
.addOutputLines(
"Client.java",
"package com.google.frobber;",
"import org.eclipse.collections.impl.factory.primitive.LongLists;",
"public final class Client {",
" public int getOrder() {",
" return 66;",
" }",
"}")
.doTest();
}

private RefactoringValidator fix() {
return RefactoringValidator.of(EclipseCollectionsApiFactoryUsage.class, getClass());
}
}
1 change: 1 addition & 0 deletions versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ net.ltgt.gradle:gradle-errorprone-plugin = 3.1.0
one.util:streamex = 0.8.2
org.apache.commons:commons-lang3 = 3.14.0
org.assertj:assertj-core = 3.25.3
org.eclipse.collections:* = 11.1.0
org.hamcrest:hamcrest-core = 2.2
org.junit.jupiter:* = 5.10.2
org.junit.vintage:* = 5.10.2
Expand Down