extract: don't reject super types based on calling of final methods - they could be called on inheritors without problems (IDEA-149121)

This commit is contained in:
Anna Kozlova
2015-12-28 10:27:00 +01:00
parent 05b90b10a3
commit 6fe7ef3c12
4 changed files with 39 additions and 3 deletions
@@ -1230,9 +1230,7 @@ public class ExpectedTypesProvider {
final PsiClassType type =
substitutor == null ? facade.getElementFactory().createType(aClass) : facade.getElementFactory().createType(aClass, substitutor);
if (method.hasModifierProperty(PsiModifier.STATIC) ||
method.hasModifierProperty(PsiModifier.FINAL) ||
method.hasModifierProperty(PsiModifier.PRIVATE)) {
if (method.hasModifierProperty(PsiModifier.STATIC) || method.hasModifierProperty(PsiModifier.PRIVATE)) {
types.add(createInfoImpl(type, ExpectedTypeInfo.TYPE_STRICTLY, type, TailType.DOT));
} else if (method.findSuperMethods().length == 0) {
types.add(createInfoImpl(type, ExpectedTypeInfo.TYPE_OR_SUBTYPE, type, TailType.DOT));
@@ -0,0 +1,10 @@
class A {
public final void foo() {}
}
class B extends A {}
class C {
void f(B b) {
B v = b;
v.foo();
}
}
@@ -0,0 +1,9 @@
class A {
public final void foo() {}
}
class B extends A {}
class C {
void f(B b) {
<selection>b</selection>.foo();
}
}
@@ -521,6 +521,25 @@ public class IntroduceVariableTest extends LightCodeInsightTestCase {
});
}
public void testTypesHierarchyBasedOnCalledMethod() {
doTest(new MockIntroduceVariableHandler("v", true, false, false, "B") {
@Override
public IntroduceVariableSettings getSettings(Project project, Editor editor,
PsiExpression expr, PsiExpression[] occurrences,
TypeSelectorManagerImpl typeSelectorManager,
boolean declareFinalIfAll,
boolean anyAssignmentLHS,
InputValidator validator,
PsiElement anchor, final OccurrencesChooser.ReplaceChoice replaceChoice) {
final PsiType[] types = typeSelectorManager.getTypesForAll();
assertTrue(types[0].getPresentableText(), types[0].getPresentableText().equals("B"));
assertTrue(types[1].getPresentableText(), types[1].getPresentableText().equals("A"));
return super.getSettings(project, editor, expr, occurrences, typeSelectorManager, declareFinalIfAll, anyAssignmentLHS,
validator, anchor, replaceChoice);
}
});
}
private void doTest(IntroduceVariableBase testMe) {
String baseName = "/refactoring/introduceVariable/" + getTestName(false);
configureByFile(baseName + ".java");