inline method: ensure this qualifier inside inheritor in some nested class (IDEA-194889)

This commit is contained in:
Anna.Kozlova
2018-08-09 11:36:31 +02:00
parent d9822e4d4f
commit 859d44032d
7 changed files with 48 additions and 4 deletions
@@ -951,7 +951,10 @@ public class InlineMethodProcessor extends BaseRefactoringProcessor {
if (parent instanceof PsiClass) {
PsiClass parentClass = (PsiClass)parent;
final PsiClass containingClass = myMethod.getContainingClass();
if (InheritanceUtil.isInheritorOrSelf(parentClass, containingClass, true)) {
if (containingClass != null && parentClass.isInheritor(containingClass, true)) {
qualifier = myFactory.createExpressionFromText(parentClass.getName() + ".this", null);
}
else if (containingClass != null && parentClass.equals(containingClass)) {
qualifier = myFactory.createExpressionFromText("this", null);
}
else {
@@ -167,6 +167,13 @@ public class ChangeContextUtil {
if (qualifier == null){
if (encodedQualifierClass != null && encodedQualifierClass.isValid()){
if (encodedQualifierClass.equals(thisClass) && thisAccessExpr != null && thisAccessExpr.isValid()){
if (thisAccessExpr instanceof PsiThisExpression) {
PsiJavaCodeReferenceElement thisAccessQualifier = ((PsiThisExpression)thisAccessExpr).getQualifier();
PsiElement resolve = thisAccessQualifier != null ? thisAccessQualifier.resolve() : null;
if (PsiTreeUtil.getParentOfType(thisExpr, PsiClass.class) == resolve) {
return thisExpr;
}
}
return thisExpr.replace(thisAccessExpr);
}
}
@@ -0,0 +1,18 @@
abstract class Test {
void g() {
new Object() {
void foo() {
Test.this.foo();
}
};
}
void foo() {
}
}
class Test2 extends Test {
{
<caret>g();
}
}
@@ -0,0 +1,15 @@
abstract class Test {
void foo() {
}
}
class Test2 extends Test {
{
new Object() {
void foo() {
Test2.this.foo();
}
};
}
}
@@ -2,6 +2,6 @@ class Test {
private final String field;
Test(){
this.field = "text";
Test.this.field = "text";
}
}
@@ -3,8 +3,8 @@ class Test {
protected final Object myBizz;
public Test() {
this.myBar = new Object() {
Test.this.myBar = new Object() {
};
this.myBizz = null;
Test.this.myBizz = null;
}
}
@@ -95,6 +95,7 @@ public class InlineMethodTest extends LightRefactoringTestCase {
public void testStaticFieldInitializer() { doTest(); }
public void testSCR22644() { doTest(); }
public void testChangeContextForThisInNestedClasses() { doTest(); }
public void testCallUnderIf() { doTest(); }