Skip to content

Commit

Permalink
Drop type split "hack"
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan202 committed Nov 13, 2023
1 parent 73cd856 commit b752e72
Showing 1 changed file with 11 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState

if (LIST_COLLECTOR.matches(tree, state)) {
return suggestToCollectionAlternatives(
tree, "com.google.common.collect.ImmutableList.toImmutableList", "ArrayList", state);
tree,
"com.google.common.collect.ImmutableList.toImmutableList",
"java.util.ArrayList",
state);
}

if (MAP_COLLECTOR.matches(tree, state)) {
Expand All @@ -67,26 +70,28 @@ public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState

if (SET_COLLECTOR.matches(tree, state)) {
return suggestToCollectionAlternatives(
tree, "com.google.common.collect.ImmutableSet.toImmutableSet", "HashSet", state);
tree,
"com.google.common.collect.ImmutableSet.toImmutableSet",
"java.util.HashSet",
state);
}

return Description.NO_MATCH;
}

private Description suggestToCollectionAlternatives(
MethodInvocationTree tree,
String fullyQualifiedImmutableReplacement,
String immutableReplacement,
String mutableReplacement,
VisitorState state) {
SuggestedFix.Builder mutableFix = SuggestedFix.builder();
String toCollectionSelect =
SuggestedFixes.qualifyStaticImport(
"java.util.stream.Collectors.toCollection", mutableFix, state);
String mutableCollection =
SuggestedFixes.qualifyType(state, mutableFix, "java.util." + mutableReplacement);
String mutableCollection = SuggestedFixes.qualifyType(state, mutableFix, mutableReplacement);

return buildDescription(tree)
.addFix(replaceMethodInvocation(tree, fullyQualifiedImmutableReplacement, state))
.addFix(replaceMethodInvocation(tree, immutableReplacement, state))
.addFix(
mutableFix
.replace(tree, String.format("%s(%s::new)", toCollectionSelect, mutableCollection))
Expand Down

0 comments on commit b752e72

Please sign in to comment.