mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-207874 Inspection to replace Map.keySet().contains(K) with Map.containsKey(K)
This commit is contained in:
+10
@@ -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");
|
||||
}
|
||||
}
|
||||
}
|
||||
+10
@@ -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");
|
||||
}
|
||||
}
|
||||
}
|
||||
+10
@@ -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");
|
||||
}
|
||||
}
|
||||
}
|
||||
+10
@@ -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");
|
||||
}
|
||||
}
|
||||
}
|
||||
+45
@@ -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() {
|
||||
|
||||
Reference in New Issue
Block a user