Commit 412d071d authored by Anna.Kozlova's avatar Anna.Kozlova
Browse files

add fixes to change expected type when inference fails due to it (IDEA-162846)

parent f3259b84
Showing with 104 additions and 0 deletions
+104 -0
......@@ -390,6 +390,7 @@ public class HighlightMethodUtil {
if (highlightInfo != null) {
registerMethodCallIntentions(highlightInfo, methodCall, list, resolveHelper);
registerMethodReturnFixAction(highlightInfo, (MethodCandidateInfo)resolveResult, methodCall);
registerTargetTypeFixesBasedOnApplicabilityInference(methodCall, (MethodCandidateInfo)resolveResult, (PsiMethod)resolved, highlightInfo);
}
}
}
......@@ -430,6 +431,7 @@ public class HighlightMethodUtil {
if (highlightInfo != null) {
registerMethodCallIntentions(highlightInfo, methodCall, list, resolveHelper);
registerMethodReturnFixAction(highlightInfo, candidateInfo, methodCall);
registerTargetTypeFixesBasedOnApplicabilityInference(methodCall, candidateInfo, resolvedMethod, highlightInfo);
}
}
else {
......@@ -472,6 +474,36 @@ public class HighlightMethodUtil {
return highlightInfo;
}
private static void registerTargetTypeFixesBasedOnApplicabilityInference(@NotNull PsiMethodCallExpression methodCall,
MethodCandidateInfo resolveResult,
PsiMethod resolved,
HighlightInfo highlightInfo) {
PsiElement parent = PsiUtil.skipParenthesizedExprUp(methodCall.getParent());
PsiVariable variable = null;
if (parent instanceof PsiVariable) {
variable = (PsiVariable)parent;
}
else if (parent instanceof PsiAssignmentExpression) {
PsiExpression lExpression = ((PsiAssignmentExpression)parent).getLExpression();
if (lExpression instanceof PsiReferenceExpression) {
PsiElement resolve = ((PsiReferenceExpression)lExpression).resolve();
if (resolve instanceof PsiVariable) {
variable = (PsiVariable)resolve;
}
}
}
if (variable != null) {
PsiType rType = methodCall.getType();
if (rType != null && !variable.getType().isAssignableFrom(rType)) {
PsiType expectedTypeByApplicabilityConstraints = resolveResult.getSubstitutor(false).substitute(resolved.getReturnType());
if (expectedTypeByApplicabilityConstraints != null && !expectedTypeByApplicabilityConstraints.equals(rType)) {
HighlightUtil.registerChangeVariableTypeFixes(variable, expectedTypeByApplicabilityConstraints, methodCall, highlightInfo);
}
}
}
}
/* see also PsiReferenceExpressionImpl.hasValidQualifier() */
@Nullable
private static String checkStaticInterfaceMethodCallQualifier(PsiReferenceExpression ref, PsiElement scope, PsiClass containingClass) {
......
// "Change variable 'foo' type to 'java.util.List<java.lang.String>'" "true"
import java.util.Arrays;
import java.util.List;
class MyClass {
void bar() {
List<String> foo = Arrays.asList("a");
}
}
\ No newline at end of file
// "Change variable 'foo' type to 'java.util.List<java.lang.String>'" "true"
import java.util.Arrays;
import java.util.List;
class MyClass {
void bar() {
List<String> foo;
foo = (Arrays.asList("a"));
}
}
\ No newline at end of file
// "Change variable 'foo' type to 'java.lang.String'" "true"
import java.util.List;
import java.util.Set;
class MyClass {
public static void sort(final List<String> list) {
String foo = (findStart(list));
}
private static <V> V findStart(List<V> result) {
return result.get(0);
}
}
\ No newline at end of file
// "Change variable 'foo' type to 'java.util.List<java.lang.String>'" "true"
import java.util.Arrays;
import java.util.List;
class MyClass {
void bar() {
String[] foo = Arrays.asList(<caret>"a");
}
}
\ No newline at end of file
// "Change variable 'foo' type to 'java.util.List<java.lang.String>'" "true"
import java.util.Arrays;
import java.util.List;
class MyClass {
void bar() {
String[] foo;
foo = (Arrays.asList(<caret>"a"));
}
}
\ No newline at end of file
// "Change variable 'foo' type to 'java.lang.String'" "true"
import java.util.List;
import java.util.Set;
class MyClass {
public static void sort(final List<String> list) {
Set<String> foo = (findStart(<caret>list));
}
private static <V> V findStart(List<V> result) {
return result.get(0);
}
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment