mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Java8MapApiInspection: getOrDefault: precise type check for none expression
This commit is contained in:
+12
-7
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2000-2016 JetBrains s.r.o.
|
||||
* Copyright 2000-2017 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -143,7 +143,7 @@ public class Java8MapApiInspection extends BaseJavaBatchLocalInspectionTool {
|
||||
condition.register(holder, new ReplaceWithSingleMapOperation("putIfAbsent", getCall, putArgs[1], result));
|
||||
}
|
||||
if (mySuggestMapGetOrDefault && condition.isContainsKey() && ExpressionUtils.isSimpleExpression(noneExpression) &&
|
||||
!(getCall.getType() instanceof PsiCapturedWildcardType)) {
|
||||
condition.isMapValueType(noneExpression.getType())) {
|
||||
condition.register(holder, new ReplaceWithSingleMapOperation("getOrDefault", getCall, noneExpression, result));
|
||||
}
|
||||
}
|
||||
@@ -157,11 +157,10 @@ public class Java8MapApiInspection extends BaseJavaBatchLocalInspectionTool {
|
||||
value = ...
|
||||
}
|
||||
*/
|
||||
if (ExpressionUtils.isSimpleExpression(assignment.getRExpression()) &&
|
||||
condition.isValueReference(assignment.getLExpression()) &&
|
||||
!condition.isValueReference(assignment.getRExpression())) {
|
||||
condition
|
||||
.register(holder, ReplaceWithSingleMapOperation.fromIf("getOrDefault", condition, assignment.getRExpression()));
|
||||
PsiExpression rValue = assignment.getRExpression();
|
||||
if (ExpressionUtils.isSimpleExpression(rValue) && condition.isValueReference(assignment.getLExpression()) &&
|
||||
!condition.isValueReference(rValue) && condition.isMapValueType(rValue.getType())) {
|
||||
condition.register(holder, ReplaceWithSingleMapOperation.fromIf("getOrDefault", condition, rValue));
|
||||
}
|
||||
} else if (condition.isGetNull()) {
|
||||
/*
|
||||
@@ -560,5 +559,11 @@ public class Java8MapApiInspection extends BaseJavaBatchLocalInspectionTool {
|
||||
//noinspection DialogTitleCapitalization
|
||||
holder.registerProblem(getFullCondition(), QuickFixBundle.message("java.8.map.api.inspection.description", fix.myMethodName), fix);
|
||||
}
|
||||
|
||||
public boolean isMapValueType(PsiType type) {
|
||||
PsiType mapExpressionType = myMapExpression.getType();
|
||||
PsiType valueTypeParameter = PsiUtil.substituteTypeParameter(mapExpressionType, CommonClassNames.JAVA_UTIL_MAP, 1, false);
|
||||
return valueTypeParameter != null && valueTypeParameter.isAssignableFrom(type);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,7 @@ import java.util.Map;
|
||||
|
||||
public class Main {
|
||||
|
||||
public void testGetOrDefault(Map<String, String> map, String key) {
|
||||
public void testGetOrDefault(Map<String, Integer> map, String key) {
|
||||
Integer num = 123;
|
||||
System.out.println(num);
|
||||
num = map.getOrDefault(key, 0);
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
// "Replace with 'getOrDefault' method call" "true"
|
||||
import java.util.Map;
|
||||
|
||||
public class Main {
|
||||
|
||||
public void testGetOrDefault(Map<String, ? extends Number> map, String key) {
|
||||
System.out.println(map.getOrDefault(key, null));
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,7 @@ import java.util.Map;
|
||||
|
||||
public class Main {
|
||||
|
||||
public void testGetOrDefault(Map<String, String> map, String key) {
|
||||
public void testGetOrDefault(Map<String, Integer> map, String key) {
|
||||
Integer num = 123;
|
||||
System.out.println(num);
|
||||
num = map.get(key);
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
// "Replace with 'getOrDefault' method call" "true"
|
||||
import java.util.Map;
|
||||
|
||||
public class Main {
|
||||
|
||||
public void testGetOrDefault(Map<String, ? extends Number> map, String key) {
|
||||
System.out.println(map.<caret>containsKey(key) ? map.get(key) : null);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
// "Replace with 'getOrDefault' method call" "false"
|
||||
import java.util.Map;
|
||||
|
||||
public class Main {
|
||||
|
||||
public void testGetOrDefault(Map<String, Integer> map, String key) {
|
||||
System.out.println(map.<caret>containsKey(key) ? map.get(key) : 0.0);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user