Do not qualify this reference with null. if it refers to an anonymous class

Fixes EA-218789 - assert: JavaParserUtil.parseFragment

GitOrigin-RevId: 035aaf1efa43dc1f324be0942957bf24de65f50f
This commit is contained in:
Tagir Valeev
2019-11-26 04:35:01 +00:00
committed by intellij-monorepo-bot
parent de6bf1c185
commit b9e6d12fb6
3 changed files with 33 additions and 1 deletions
@@ -843,7 +843,12 @@ public class InlineMethodProcessor extends BaseRefactoringProcessor {
PsiClass parentClass = (PsiClass)parent;
final PsiClass containingClass = myMethod.getContainingClass();
if (containingClass != null && parentClass.isInheritor(containingClass, true)) {
qualifier = myFactory.createExpressionFromText(parentClass.getName() + ".this", null);
String name = parentClass.getName();
// We cannot have qualified this reference to an anonymous class, so we leave it unqualified
// this might produce incorrect code in extremely rare cases
// when we inline a superclass method in an anonymous class,
// and the method body contains a nested class that refers to the outer one
qualifier = myFactory.createExpressionFromText(name == null ? "this" : name + ".this", null);
}
else if (containingClass != null && parentClass.equals(containingClass)) {
qualifier = myFactory.createExpressionFromText("this", null);
@@ -0,0 +1,15 @@
public class Test {
static class X {
void foo() {
System.out.println(this);
}
}
void test() {
new X () {
void bar() {
<caret>foo();
}
};
}
}
@@ -0,0 +1,12 @@
public class Test {
static class X {
}
void test() {
new X () {
void bar() {
System.out.println(this);
}
};
}
}