IDEA-207874 Inspection to replace Map.keySet().contains(K) with Map.containsKey(K)

This commit is contained in:
Tagir Valeev
2019-03-12 13:54:25 +07:00
parent 669f4ee67a
commit 77b6a04c45
5 changed files with 85 additions and 0 deletions

View File

@@ -0,0 +1,10 @@
// "Replace with 'Map.containsKey()'" "true"
import java.util.Map;
class Test {
void test(Map<String, String> map, String key) {
if(map.containsKey(key)) {
System.out.println("contains");
}
}
}

View File

@@ -0,0 +1,10 @@
// "Replace with 'Map.containsValue()'" "true"
import java.util.Map;
class Test {
void test(Map<String, String> map, String key) {
if(map.containsValue(key)) {
System.out.println("contains");
}
}
}

View File

@@ -0,0 +1,10 @@
// "Replace with 'Map.containsKey()'" "true"
import java.util.Map;
class Test {
void test(Map<String, String> map, String key) {
if(map.keySet().cont<caret>ains(key)) {
System.out.println("contains");
}
}
}

View File

@@ -0,0 +1,10 @@
// "Replace with 'Map.containsValue()'" "true"
import java.util.Map;
class Test {
void test(Map<String, String> map, String key) {
if(map.values().cont<caret>ains(key)) {
System.out.println("contains");
}
}
}

View File

@@ -60,12 +60,15 @@ public class RedundantCollectionOperationInspection extends AbstractBaseJavaLoca
private static final CallMatcher LIST_SORT = instanceCall(CommonClassNames.JAVA_UTIL_LIST, "sort").parameterTypes(
CommonClassNames.JAVA_UTIL_COMPARATOR);
private static final CallMatcher ITERABLE_ITERATOR = instanceCall(CommonClassNames.JAVA_LANG_ITERABLE, "iterator").parameterCount(0);
private static final CallMatcher MAP_KEY_SET = instanceCall(CommonClassNames.JAVA_UTIL_MAP, "keySet").parameterCount(0);
private static final CallMatcher MAP_VALUES = instanceCall(CommonClassNames.JAVA_UTIL_MAP, "values").parameterCount(0);
private static final CallMapper<RedundantCollectionOperationHandler> HANDLERS =
new CallMapper<RedundantCollectionOperationHandler>()
.register(TO_ARRAY, AsListToArrayHandler::handler)
.register(CONTAINS_ALL, ContainsAllSingletonHandler::handler)
.register(CONTAINS, SingletonContainsHandler::handler)
.register(CONTAINS, MapKeySetContainsHandler::handler)
.register(anyOf(CONTAINS, CONTAINS_KEY), ContainsBeforeAddRemoveHandler::handler)
.register(REMOVE_BY_INDEX, RedundantIndexOfHandler::handler)
.register(AS_LIST, RedundantAsListForIterationHandler::handler)
@@ -403,6 +406,48 @@ public class RedundantCollectionOperationInspection extends AbstractBaseJavaLoca
}
}
private static class MapKeySetContainsHandler implements RedundantCollectionOperationHandler {
private final String myReplacementMethod;
private MapKeySetContainsHandler(String method) {
myReplacementMethod = method;
}
@NotNull
@Override
public String getReplacement() {
return "Map." + myReplacementMethod + "()";
}
@Override
public void performFix(@NotNull Project project, @NotNull PsiMethodCallExpression call) {
PsiMethodCallExpression qualifierCall = MethodCallUtils.getQualifierMethodCall(call);
if (qualifierCall == null) return;
PsiExpression mapExpression = qualifierCall.getMethodExpression().getQualifierExpression();
if (mapExpression == null) return;
CommentTracker ct = new CommentTracker();
ct.replaceAndRestoreComments(qualifierCall, mapExpression);
ExpressionUtils.bindCallTo(call, myReplacementMethod);
}
static RedundantCollectionOperationHandler handler(PsiMethodCallExpression call) {
PsiMethodCallExpression qualifierCall = MethodCallUtils.getQualifierMethodCall(call);
String replacementMethod;
if (MAP_KEY_SET.test(qualifierCall)) {
replacementMethod = "containsKey";
}
else if (MAP_VALUES.test(qualifierCall)) {
replacementMethod = "containsValue";
}
else {
return null;
}
PsiExpression mapExpression = qualifierCall.getMethodExpression().getQualifierExpression();
if (mapExpression == null) return null;
return new MapKeySetContainsHandler(replacementMethod);
}
}
private static class RedundantAsListForIterationHandler implements RedundantCollectionOperationHandler {
@Override
public String getProblemName() {