IDEA-121234 IDEA can not update method return type

This commit is contained in:
Anna Kozlova
2014-03-14 20:43:58 +01:00
parent c7648e1a1c
commit 3704bfc631
6 changed files with 74 additions and 26 deletions
@@ -550,7 +550,7 @@ public class HighlightUtil extends HighlightUtilBase {
boolean isMethodVoid = returnType == null || PsiType.VOID.equals(returnType);
final PsiExpression returnValue = statement.getReturnValue();
if (returnValue != null) {
PsiType valueType = returnValue.getType();
PsiType valueType = RefactoringChangeUtil.getTypeByExpression(returnValue);
if (isMethodVoid) {
description = JavaErrorMessages.message("return.from.void.method");
errorResult =
@@ -108,7 +108,7 @@ public class MethodReturnTypeFix extends LocalQuickFixAndIntentionActionOnPsiEle
final PsiMethod myMethod = (PsiMethod)startElement;
if (!FileModificationService.getInstance().prepareFileForWrite(myMethod.getContainingFile())) return;
PsiType myReturnType = myReturnTypePointer.getType();
final PsiType myReturnType = myReturnTypePointer.getType();
if (myReturnType == null) return;
if (myFixWholeHierarchy) {
final PsiMethod superMethod = myMethod.findDeepestSuperMethod();
@@ -202,12 +202,17 @@ public class MethodReturnTypeFix extends LocalQuickFixAndIntentionActionOnPsiEle
}
@Nullable
private PsiMethod[] getChangeRoots(final PsiMethod method) {
private PsiMethod[] getChangeRoots(final PsiMethod method, @NotNull PsiType returnType) {
if (!myFixWholeHierarchy) return new PsiMethod[]{method};
final PsiMethod[] methods = method.findDeepestSuperMethods();
if (methods.length > 0) {
for (PsiMethod psiMethod : methods) {
if (returnType.equals(psiMethod.getReturnType())) {
return new PsiMethod[] {method};
}
}
return methods;
}
// no - only base
@@ -215,8 +220,8 @@ public class MethodReturnTypeFix extends LocalQuickFixAndIntentionActionOnPsiEle
}
@NotNull
private List<PsiMethod> changeReturnType(final PsiMethod method, final PsiType returnType) {
final PsiMethod[] methods = getChangeRoots(method);
private List<PsiMethod> changeReturnType(final PsiMethod method, @NotNull final PsiType returnType) {
final PsiMethod[] methods = getChangeRoots(method, returnType);
if (methods == null) {
// canceled
return Collections.emptyList();
@@ -444,26 +444,7 @@ public class RefactoringUtil {
}
private static PsiType getTypeByExpression(PsiExpression expr, final PsiElementFactory factory) {
PsiType type = expr.getType();
if (type == null) {
if (expr instanceof PsiArrayInitializerExpression) {
PsiExpression[] initializers = ((PsiArrayInitializerExpression)expr).getInitializers();
if (initializers.length > 0) {
PsiType initType = getTypeByExpression(initializers[0]);
if (initType == null) return null;
return initType.createArrayType();
}
}
if (expr instanceof PsiReferenceExpression && PsiUtil.isOnAssignmentLeftHand(expr)) {
return getTypeByExpression(((PsiAssignmentExpression)expr.getParent()).getRExpression());
}
return null;
}
PsiClass refClass = PsiUtil.resolveClassInType(type);
if (refClass instanceof PsiAnonymousClass) {
type = ((PsiAnonymousClass)refClass).getBaseClassType();
}
PsiType type = RefactoringChangeUtil.getTypeByExpression(expr);
if (PsiType.NULL.equals(type)) {
ExpectedTypeInfo[] infos = ExpectedTypesProvider.getInstance(expr.getProject()).getExpectedTypes(expr, false);
if (infos.length == 1) {
@@ -474,7 +455,7 @@ public class RefactoringUtil {
}
}
return GenericsUtil.getVariableTypeByExpressionType(type);
return type;
}
public static boolean isAssignmentLHS(PsiElement element) {
@@ -20,6 +20,7 @@ import com.intellij.psi.*;
import com.intellij.psi.codeStyle.CodeStyleManager;
import com.intellij.psi.util.InheritanceUtil;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtil;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -44,6 +45,31 @@ public class RefactoringChangeUtil {
return PsiKeyword.SUPER.equals(name);
}
public static PsiType getTypeByExpression(PsiExpression expr) {
PsiType type = expr.getType();
if (type == null) {
if (expr instanceof PsiArrayInitializerExpression) {
PsiExpression[] initializers = ((PsiArrayInitializerExpression)expr).getInitializers();
if (initializers.length > 0) {
PsiType initType = getTypeByExpression(initializers[0]);
if (initType == null) return null;
return initType.createArrayType();
}
}
if (expr instanceof PsiReferenceExpression && PsiUtil.isOnAssignmentLeftHand(expr)) {
return getTypeByExpression(((PsiAssignmentExpression)expr.getParent()).getRExpression());
}
return null;
}
PsiClass refClass = PsiUtil.resolveClassInType(type);
if (refClass instanceof PsiAnonymousClass) {
type = ((PsiAnonymousClass)refClass).getBaseClassType();
}
return GenericsUtil.getVariableTypeByExpressionType(type);
}
public static PsiReferenceExpression qualifyReference(@NotNull PsiReferenceExpression referenceExpression,
@NotNull PsiMember member,
@Nullable final PsiClass qualifyingClass) throws IncorrectOperationException {
@@ -0,0 +1,18 @@
// "Make 'get' return 'Callable<java.lang.Integer>'" "true"
interface Gettable<T> {
Callable<Integer> get();
}
public class Issue<T> implements Gettable<T> {
public Callable<Integer> get() {
return new Callable<Integer>() {
public Integer call() {
return 0;
}
};
}
}
class Callable<T> {}
@@ -0,0 +1,18 @@
// "Make 'get' return 'Callable<java.lang.Integer>'" "true"
interface Gettable<T> {
Callable<Integer> get();
}
public class Issue<T> implements Gettable<T> {
public void get() {
return new Call<caret>able<Integer>() {
public Integer call() {
return 0;
}
};
}
}
class Callable<T> {}