inline superclass: don't change superconstructor if interface was inlined (IDEA-152481)

This commit is contained in:
Anna Kozlova
2016-03-03 15:16:22 +01:00
parent e836aa4980
commit 3cd6b26423
6 changed files with 56 additions and 30 deletions
@@ -167,43 +167,45 @@ public class InlineSuperClassRefactoringProcessor extends FixableUsagesRefactori
}
}
final PsiMethod[] superConstructors = mySuperClass.getConstructors();
for (PsiMethod constructor : targetClass.getConstructors()) {
final PsiCodeBlock constrBody = constructor.getBody();
LOG.assertTrue(constrBody != null);
final PsiStatement[] statements = constrBody.getStatements();
if (statements.length > 0) {
final PsiStatement firstConstrStatement = statements[0];
if (firstConstrStatement instanceof PsiExpressionStatement) {
final PsiExpression expression = ((PsiExpressionStatement)firstConstrStatement).getExpression();
if (expression instanceof PsiMethodCallExpression) {
final PsiReferenceExpression methodExpression = ((PsiMethodCallExpression)expression).getMethodExpression();
if (methodExpression.getText().equals(PsiKeyword.SUPER)) {
final PsiMethod superConstructor = ((PsiMethodCallExpression)expression).resolveMethod();
if (superConstructor != null && superConstructor.getBody() != null) {
usages.add(new InlineSuperCallUsageInfo((PsiMethodCallExpression)expression));
continue;
if (!mySuperClass.isInterface()) {
final PsiMethod[] superConstructors = mySuperClass.getConstructors();
for (PsiMethod constructor : targetClass.getConstructors()) {
final PsiCodeBlock constrBody = constructor.getBody();
LOG.assertTrue(constrBody != null);
final PsiStatement[] statements = constrBody.getStatements();
if (statements.length > 0) {
final PsiStatement firstConstrStatement = statements[0];
if (firstConstrStatement instanceof PsiExpressionStatement) {
final PsiExpression expression = ((PsiExpressionStatement)firstConstrStatement).getExpression();
if (expression instanceof PsiMethodCallExpression) {
final PsiReferenceExpression methodExpression = ((PsiMethodCallExpression)expression).getMethodExpression();
if (methodExpression.getText().equals(PsiKeyword.SUPER)) {
final PsiMethod superConstructor = ((PsiMethodCallExpression)expression).resolveMethod();
if (superConstructor != null && superConstructor.getBody() != null) {
usages.add(new InlineSuperCallUsageInfo((PsiMethodCallExpression)expression));
continue;
}
}
}
}
}
}
//insert implicit call to super
for (PsiMethod superConstructor : superConstructors) {
if (superConstructor.getParameterList().getParametersCount() == 0) {
final PsiExpression expression = JavaPsiFacade.getElementFactory(myProject).createExpressionFromText("super()", constructor);
usages.add(new InlineSuperCallUsageInfo((PsiMethodCallExpression)expression, constrBody));
//insert implicit call to super
for (PsiMethod superConstructor : superConstructors) {
if (superConstructor.getParameterList().getParametersCount() == 0) {
final PsiExpression expression = JavaPsiFacade.getElementFactory(myProject).createExpressionFromText("super()", constructor);
usages.add(new InlineSuperCallUsageInfo((PsiMethodCallExpression)expression, constrBody));
}
}
}
}
if (targetClass.getConstructors().length == 0) {
//copy default constructor
for (PsiMethod superConstructor : superConstructors) {
if (superConstructor.getParameterList().getParametersCount() == 0) {
usages.add(new CopyDefaultConstructorUsageInfo(targetClass, superConstructor));
break;
if (targetClass.getConstructors().length == 0) {
//copy default constructor
for (PsiMethod superConstructor : superConstructors) {
if (superConstructor.getParameterList().getParametersCount() == 0) {
usages.add(new CopyDefaultConstructorUsageInfo(targetClass, superConstructor));
break;
}
}
}
}
@@ -0,0 +1,6 @@
class A implements I {
A(int i) {
System.out.println(i);
}
}
@@ -0,0 +1,5 @@
abstract class Test extends A {
public Test(boolean mode) {
super(mode? 1: 0);
}
}
@@ -0,0 +1,7 @@
class A implements I {
A(int i) {
System.out.println(i);
}
}
interface Super {}
@@ -0,0 +1,5 @@
abstract class Test extends A implements Super {
public Test(boolean mode) {
super(mode? 1: 0);
}
}
@@ -70,6 +70,7 @@ public class InlineSuperClassTest extends MultiFileTestCase {
public void testInlineSuperclassExtendsList() { doTest(); }
public void testInterfaceHierarchyWithSubstitution() { doTest(); }
public void testTypeParameterBound() { doTest();}
public void testInlineInterfaceDoNotChangeConstructor() { doTest(); }
private void doTest() {
doTest(false, false);