Inline inner: assignments to local vars and params in constructor body fixed (IDEA-39285)

This commit is contained in:
anna
2010-02-01 17:13:32 +03:00
parent f86a7f7f87
commit 3e37ba3466
4 changed files with 45 additions and 11 deletions
@@ -183,7 +183,7 @@ class InlineToAnonymousConstructorProcessor {
ProcessingContext context = new ProcessingContext();
if (ourAssignmentPattern.accepts(stmt, context)) {
PsiAssignmentExpression expression = context.get(ourAssignmentKey);
if (!processAssignmentInConstructor(expression)) {
if (processAssignmentInConstructor(expression)) {
initializerBlock.addBefore(replaceParameterReferences(stmt, null, false), initializerBlock.getRBrace());
}
}
@@ -205,7 +205,7 @@ class InlineToAnonymousConstructorProcessor {
if (expression.getLExpression() instanceof PsiReferenceExpression) {
PsiReferenceExpression lExpr = (PsiReferenceExpression) expression.getLExpression();
final PsiExpression rExpr = expression.getRExpression();
if (rExpr == null) return true;
if (rExpr == null) return false;
final PsiElement psiElement = lExpr.resolve();
if (psiElement instanceof PsiField) {
PsiField field = (PsiField) psiElement;
@@ -217,25 +217,20 @@ class InlineToAnonymousConstructorProcessor {
}
catch (IncorrectOperationException e) {
LOG.error(e);
return true;
return false;
}
if (!localVarRefs.isEmpty()) {
return false;
return true;
}
myFieldInitializers.put(field.getName(), initializer);
}
}
else if (psiElement instanceof PsiVariable) {
try {
replaceParameterReferences(rExpr.copy(), new ArrayList<PsiReferenceExpression>(), false);
}
catch (IncorrectOperationException e) {
LOG.error(e);
}
return true;
}
}
return true;
return false;
}
public static boolean isConstant(final PsiExpression expr) {
@@ -0,0 +1,20 @@
public class WithCtor {
private int myInt = 8;
private String myString = "Sashya";
public WithCtor() {
}
public WithCtor(int anInt, String string) {
int i = 2;
i = 3;
myInt = i + anInt;
int j = 4;
j = 5;
myString = string.substring(j);
}
}
class Usage {
private WithCtor wc1 = new With<caret>Ctor(17, "Sa");
}
@@ -0,0 +1,15 @@
class Usage {
private Object wc1 = new Object() {
private int myInt = 8;
private String myString = "Sashya";
{
int i = 2;
i = 3;
myInt = i + 17;
int j = 4;
j = 5;
myString = "Sa".substring(j);
}
};
}
@@ -218,6 +218,10 @@ public class InlineToAnonymousClassTest extends LightCodeInsightTestCase {
doTest(false, true);
}
public void testMultipleAssignments() throws Exception {
doTest(false, true);
}
public void testNoInlineAbstract() throws Exception {
doTestNoInline("Abstract classes cannot be inlined");
}