mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
add fixes to change expected type when inference fails due to it (IDEA-162846)
This commit is contained in:
+32
@@ -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) {
|
||||
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
// "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");
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
// "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"));
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
// "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);
|
||||
}
|
||||
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
// "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");
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
// "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"));
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
// "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);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user