change method return type on call site (IDEA-125166)

This commit is contained in:
Anna Kozlova
2014-05-16 20:42:44 +04:00
parent e4bbd52fbe
commit 8d2b706f59
6 changed files with 57 additions and 7 deletions
@@ -688,7 +688,7 @@ public class GenericsHighlightUtil {
final PsiType parameterType = parameter.getType();
HighlightInfo highlightInfo = HighlightUtil.checkAssignability(parameterType, itemType, null, new TextRange(start, end), 0);
if (highlightInfo != null) {
HighlightUtil.registerChangeVariableTypeFixes(parameter, itemType, highlightInfo);
HighlightUtil.registerChangeVariableTypeFixes(parameter, itemType, expression, highlightInfo);
}
return highlightInfo;
}
@@ -1297,7 +1297,7 @@ public class GenericsHighlightUtil {
}
PsiSubstitutor substitutor = factory.createSubstitutor(map);
PsiType suggestedType = factory.createType(aClass, substitutor);
HighlightUtil.registerChangeVariableTypeFixes(variable, suggestedType, highlightInfo);
HighlightUtil.registerChangeVariableTypeFixes(variable, suggestedType, variable.getInitializer(), highlightInfo);
}
}
}
@@ -434,22 +434,23 @@ public class HighlightUtil extends HighlightUtilBase {
if (highlightInfo == null) {
return null;
}
registerChangeVariableTypeFixes(lExpr, rType, highlightInfo);
registerChangeVariableTypeFixes(lExpr, rType, rExpr, highlightInfo);
if (lType != null) {
registerChangeVariableTypeFixes(rExpr, lType, highlightInfo);
registerChangeVariableTypeFixes(rExpr, lType, lExpr, highlightInfo);
}
return highlightInfo;
}
private static void registerChangeVariableTypeFixes(@NotNull PsiExpression expression,
@NotNull PsiType type,
@Nullable PsiExpression lExpr,
@Nullable HighlightInfo highlightInfo) {
if (highlightInfo == null || !(expression instanceof PsiReferenceExpression)) return;
final PsiElement element = ((PsiReferenceExpression)expression).resolve();
if (element == null || !(element instanceof PsiVariable)) return;
registerChangeVariableTypeFixes((PsiVariable)element, type, highlightInfo);
registerChangeVariableTypeFixes((PsiVariable)element, type, lExpr, highlightInfo);
}
private static boolean isCastIntentionApplicable(@NotNull PsiExpression expression, @Nullable PsiType toType) {
@@ -478,7 +479,7 @@ public class HighlightUtil extends HighlightUtilBase {
int end = variable.getTextRange().getEndOffset();
HighlightInfo highlightInfo = checkAssignability(lType, rType, initializer, new TextRange(start, end), 0);
if (highlightInfo != null) {
registerChangeVariableTypeFixes(variable, rType, highlightInfo);
registerChangeVariableTypeFixes(variable, rType, variable.getInitializer(), highlightInfo);
}
return highlightInfo;
}
@@ -2660,10 +2661,19 @@ public class HighlightUtil extends HighlightUtilBase {
return info;
}
public static void registerChangeVariableTypeFixes(@NotNull PsiVariable parameter, PsiType itemType, @NotNull HighlightInfo highlightInfo) {
public static void registerChangeVariableTypeFixes(@NotNull PsiVariable parameter,
PsiType itemType,
@Nullable PsiExpression expr,
@NotNull HighlightInfo highlightInfo) {
for (IntentionAction action : getChangeVariableTypeFixes(parameter, itemType)) {
QuickFixAction.registerQuickFixAction(highlightInfo, action);
}
if (expr instanceof PsiMethodCallExpression) {
final PsiMethod method = ((PsiMethodCallExpression)expr).resolveMethod();
if (method != null) {
QuickFixAction.registerQuickFixAction(highlightInfo, QUICK_FIX_FACTORY.createMethodReturnFix(method, parameter.getType(), true));
}
}
}
@NotNull
@@ -0,0 +1,11 @@
// "Make 'bar' return 'java.lang.String'" "true"
public class Foo {
void foo() {
String s;
s = bar();
}
String bar() {
return null;
}
}
@@ -0,0 +1,10 @@
// "Make 'bar' return 'java.lang.String'" "true"
public class Foo {
void foo() {
String s = bar();
}
String bar() {
return null;
}
}
@@ -0,0 +1,10 @@
// "Make 'bar' return 'java.lang.String'" "true"
public class Foo {
void foo() {
String s;
<caret>s = bar();
}
void bar() {
}
}
@@ -0,0 +1,9 @@
// "Make 'bar' return 'java.lang.String'" "true"
public class Foo {
void foo() {
String <caret>s = bar();
}
void bar() {
}
}