change method return type: avoid changes in hierarchy when assignable (IDEA-209931)

GitOrigin-RevId: 43166bdd806d2a0b6ed2e72e6630c9cf53fba7b4
This commit is contained in:
Anna Kozlova
2019-04-28 19:25:11 +03:00
committed by intellij-monorepo-bot
parent 7f603ae6b1
commit d3928de54b
3 changed files with 81 additions and 18 deletions
@@ -54,7 +54,13 @@ public class MethodReturnTypeFix extends LocalQuickFixAndIntentionActionOnPsiEle
myReturnTypePointer = SmartTypePointerManager.getInstance(method.getProject()).createSmartTypePointer(returnType);
myFixWholeHierarchy = fixWholeHierarchy;
myName = method.getName();
myCanonicalText = returnType.getCanonicalText();
if (fixWholeHierarchy) {
PsiType type = getHierarchyAdjustedReturnType(method, returnType);
myCanonicalText = (type != null ? type : returnType).getCanonicalText();
}
else {
myCanonicalText = returnType.getCanonicalText();
}
}
@@ -192,27 +198,34 @@ public class MethodReturnTypeFix extends LocalQuickFixAndIntentionActionOnPsiEle
return editor;
}
private static PsiType getHierarchyAdjustedReturnType(final PsiMethod method, @NotNull PsiType returnType) {
for (PsiMethod superMethod : method.findDeepestSuperMethods()) {
PsiType superMethodReturnType = superMethod.getReturnType();
if (superMethodReturnType != null && superMethodReturnType.isAssignableFrom(returnType)) {
if (superMethodReturnType instanceof PsiClassType && returnType instanceof PsiPrimitiveType) {
return ((PsiPrimitiveType)returnType).getBoxedType(method);
}
return returnType;
}
}
return null;
}
@NotNull
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};
private List<PsiMethod> changeReturnType(final PsiMethod method, @NotNull PsiType returnType) {
PsiMethod[] methods = new PsiMethod[] {method};
if (myFixWholeHierarchy) {
PsiType type = getHierarchyAdjustedReturnType(method, returnType);
if (type != null) {
returnType = type;
}
else {
final PsiMethod[] superMethods = method.findDeepestSuperMethods();
if (superMethods.length > 0) {
methods = superMethods;
}
}
return methods;
}
// no - only base
return new PsiMethod[] {method};
}
@NotNull
private List<PsiMethod> changeReturnType(final PsiMethod method, @NotNull final PsiType returnType) {
final PsiMethod[] methods = getChangeRoots(method, returnType);
final MethodSignatureChangeVisitor methodSignatureChangeVisitor = new MethodSignatureChangeVisitor();
for (PsiMethod targetMethod : methods) {
@@ -0,0 +1,25 @@
// "Make 'b' return 'java.lang.Integer'" "true"
class MyClass {
interface BaseInterface {
Object b();
}
class BooleanImpl implements BaseInterface {
@Override
public Boolean b() {
return true;
}
}
class IntegerImpl implements BaseInterface {
@Override
public Integer b() {
return 1;
}
}
}
@@ -0,0 +1,25 @@
// "Make 'b' return 'java.lang.Integer'" "true"
class MyClass {
interface BaseInterface {
Object b();
}
class BooleanImpl implements BaseInterface {
@Override
public Boolean b() {
return true;
}
}
class IntegerImpl implements BaseInterface {
@Override
public String b() {
return <caret>1;
}
}
}