-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New error-prone rule: PreferImmutableStreamExCollections (#1670)
A new error-prone rule `PreferImmutableStreamExCollections` converts the StreamEx `toMap()` -> `toImmutableMap()`, `toImmutableList()` and `toImmutableSet()`
- Loading branch information
Showing
8 changed files
with
226 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
...ne/src/main/java/com/palantir/baseline/errorprone/PreferImmutableStreamExCollections.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/* | ||
* (c) Copyright 2019 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.fixes.SuggestedFix; | ||
import com.google.errorprone.fixes.SuggestedFixes; | ||
import com.google.errorprone.matchers.Description; | ||
import com.google.errorprone.matchers.Matcher; | ||
import com.google.errorprone.matchers.method.MethodMatchers; | ||
import com.sun.source.tree.ExpressionTree; | ||
import com.sun.source.tree.MethodInvocationTree; | ||
import java.util.Collections; | ||
|
||
@AutoService(BugChecker.class) | ||
@BugPattern( | ||
name = "PreferImmutableStreamExCollections", | ||
link = "https://github.com/palantir/gradle-baseline#baseline-error-prone-checks", | ||
linkType = BugPattern.LinkType.CUSTOM, | ||
severity = SeverityLevel.WARNING, | ||
summary = "Prefer immutable/unmodifable collections wherever possible because they are inherently threadsafe " | ||
+ "and easier to reason about when passed between different functions." | ||
+ " If you really want a mutable output then explicitly suppress this check.") | ||
public final class PreferImmutableStreamExCollections extends BugChecker | ||
implements BugChecker.MethodInvocationTreeMatcher { | ||
|
||
private static final long serialVersionUID = 1L; | ||
|
||
private static final Matcher<ExpressionTree> STREAMEX_TO_MAP = MethodMatchers.instanceMethod() | ||
.onExactClass("one.util.streamex.EntryStream") | ||
.named("toMap") | ||
.withParameters(Collections.emptyList()); | ||
|
||
private static final Matcher<ExpressionTree> STREAMEX_TO_SET = MethodMatchers.instanceMethod() | ||
.onDescendantOf("one.util.streamex.AbstractStreamEx") | ||
.named("toSet") | ||
.withParameters(Collections.emptyList()); | ||
|
||
private static final Matcher<ExpressionTree> STREAMEX_TO_LIST = MethodMatchers.instanceMethod() | ||
.onDescendantOf("one.util.streamex.AbstractStreamEx") | ||
.named("toList") | ||
.withParameters(Collections.emptyList()); | ||
|
||
private static final Matcher<ExpressionTree> STREAMEX_COLLECT = MethodMatchers.instanceMethod() | ||
.onDescendantOf("one.util.streamex.AbstractStreamEx") | ||
.named("collect"); | ||
|
||
private static final Matcher<ExpressionTree> COLLECT_TO_SET = MethodMatchers.staticMethod() | ||
.onClass("java.util.stream.Collectors") | ||
.named("toSet") | ||
.withParameters(Collections.emptyList()); | ||
|
||
private static final Matcher<ExpressionTree> COLLECT_TO_LIST = MethodMatchers.staticMethod() | ||
.onClass("java.util.stream.Collectors") | ||
.named("toList") | ||
.withParameters(Collections.emptyList()); | ||
|
||
@Override | ||
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) { | ||
if (STREAMEX_TO_MAP.matches(tree, state)) { | ||
return buildDescription(tree) | ||
.addFix(SuggestedFixes.renameMethodInvocation(tree, "toImmutableMap", state)) | ||
.build(); | ||
} | ||
|
||
if (STREAMEX_TO_SET.matches(tree, state)) { | ||
return buildDescription(tree) | ||
.addFix(SuggestedFixes.renameMethodInvocation(tree, "toImmutableSet", state)) | ||
.build(); | ||
} | ||
|
||
if (STREAMEX_TO_LIST.matches(tree, state)) { | ||
return buildDescription(tree) | ||
.addFix(SuggestedFixes.renameMethodInvocation(tree, "toImmutableList", state)) | ||
.build(); | ||
} | ||
|
||
if (STREAMEX_COLLECT.matches(tree, state) && tree.getArguments().size() == 1) { | ||
ExpressionTree argument = tree.getArguments().get(0); | ||
|
||
if (COLLECT_TO_SET.matches(argument, state)) { | ||
return buildDescription(tree) | ||
.addFix(SuggestedFix.builder() | ||
.delete(argument) | ||
.merge(SuggestedFixes.renameMethodInvocation(tree, "toImmutableSet", state)) | ||
.build()) | ||
.build(); | ||
} | ||
|
||
if (COLLECT_TO_LIST.matches(argument, state)) { | ||
return buildDescription(tree) | ||
.addFix(SuggestedFix.builder() | ||
.delete(argument) | ||
.merge(SuggestedFixes.renameMethodInvocation(tree, "toImmutableList", state)) | ||
.build()) | ||
.build(); | ||
} | ||
} | ||
|
||
return Description.NO_MATCH; | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
...rc/test/java/com/palantir/baseline/errorprone/PreferImmutableStreamExCollectionsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* (c) Copyright 2021 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.BugCheckerRefactoringTestHelper; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class PreferImmutableStreamExCollectionsTest { | ||
|
||
@Test | ||
void toMap() { | ||
fix().addInputLines( | ||
"Test.java", | ||
"import one.util.streamex.EntryStream;", | ||
"import java.util.Map;", | ||
"public class Test {", | ||
" Map<String, String> map = EntryStream.of(Map.of(\"hello\", \"world\")).toMap();", | ||
" EntryStream<String, String> entryStream = EntryStream.of(Map.of(\"hello\", \"world\"));", | ||
" Map<String, String> entryStream2 = entryStream.toMap();", | ||
"}") | ||
.addOutputLines( | ||
"Test.java", | ||
"import one.util.streamex.EntryStream;", | ||
"import java.util.Map;", | ||
"public class Test {", | ||
" Map<String, String> map = EntryStream.of(Map.of(\"hello\", \"world\")).toImmutableMap();", | ||
" EntryStream<String, String> entryStream = EntryStream.of(Map.of(\"hello\", \"world\"));", | ||
" Map<String, String> entryStream2 = entryStream.toImmutableMap();", | ||
"}") | ||
.doTest(BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH); | ||
} | ||
|
||
@Test | ||
void toSet() { | ||
fix().addInputLines( | ||
"Test.java", | ||
"import one.util.streamex.StreamEx;", | ||
"import java.util.stream.Collectors;", | ||
"import java.util.Set;", | ||
"public class Test {", | ||
" Set<String> s = StreamEx.of(\"Hello\").toSet();", | ||
" Set<String> s2 = StreamEx.of(\"Hello\").collect(Collectors.toSet());", | ||
"}") | ||
.addOutputLines( | ||
"Test.java", | ||
"import one.util.streamex.StreamEx;", | ||
"import java.util.stream.Collectors;", | ||
"import java.util.Set;", | ||
"public class Test {", | ||
" Set<String> s = StreamEx.of(\"Hello\").toImmutableSet();", | ||
" Set<String> s2 = StreamEx.of(\"Hello\").toImmutableSet();", | ||
"}") | ||
.doTest(BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH); | ||
} | ||
|
||
@Test | ||
void toList() { | ||
fix().addInputLines( | ||
"Test.java", | ||
"import one.util.streamex.StreamEx;", | ||
"import java.util.List;", | ||
"import static java.util.stream.Collectors.toList;", | ||
"public class Test {", | ||
" List<String> s = StreamEx.of(\"Hello\").toList();", | ||
" List<String> s2 = StreamEx.of(\"Hello\").collect(toList());", | ||
"}") | ||
.addOutputLines( | ||
"Test.java", | ||
"import one.util.streamex.StreamEx;", | ||
"import java.util.List;", | ||
"import static java.util.stream.Collectors.toList;", | ||
"public class Test {", | ||
" List<String> s = StreamEx.of(\"Hello\").toImmutableList();", | ||
" List<String> s2 = StreamEx.of(\"Hello\").toImmutableList();", | ||
"}") | ||
.doTest(BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH); | ||
} | ||
|
||
private RefactoringValidator fix() { | ||
return RefactoringValidator.of(new PreferImmutableStreamExCollections(), getClass()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
type: improvement | ||
improvement: | ||
description: A new error-prone rule `PreferImmutableStreamExCollections` converts the StreamEx `toMap()` -> `toImmutableMap()`, `toImmutableList()` and `toImmutableSet()` | ||
links: | ||
- https://github.com/palantir/gradle-baseline/pull/1670 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters