inline to anonymous: wrap code with code block when needed (IDEA-86007)

This commit is contained in:
Anna Kozlova
2012-05-18 15:42:44 +04:00
parent 8aa66c1911
commit 56311ae33e
4 changed files with 71 additions and 3 deletions
@@ -57,11 +57,11 @@ class InlineToAnonymousConstructorProcessor {
psiElement().withText(PsiKeyword.THIS)));
private final PsiClass myClass;
private final PsiNewExpression myNewExpression;
private PsiNewExpression myNewExpression;
private final PsiType mySuperType;
private final Map<String, PsiExpression> myFieldInitializers = new HashMap<String, PsiExpression>();
private final Map<PsiParameter, PsiVariable> myLocalsForParameters = new HashMap<PsiParameter, PsiVariable>();
private final PsiStatement myNewStatement;
private PsiStatement myNewStatement;
private final PsiElementFactory myElementFactory;
private PsiMethod myConstructor;
private PsiExpressionList myConstructorArguments;
@@ -263,7 +263,23 @@ class InlineToAnonymousConstructorProcessor {
final PsiDeclarationStatement declaration = myElementFactory.createVariableDeclarationStatement(localName, type, initializer);
PsiVariable variable = (PsiVariable)declaration.getDeclaredElements()[0];
PsiUtil.setModifierProperty(variable, PsiModifier.FINAL, true);
myNewStatement.getParent().addBefore(declaration, myNewStatement);
final PsiElement parent = myNewStatement.getParent();
if (parent instanceof PsiCodeBlock) {
variable = (PsiVariable)((PsiDeclarationStatement)parent.addBefore(declaration, myNewStatement)).getDeclaredElements()[0];
}
else {
final int offsetInStatement = myNewExpression.getTextRange().getStartOffset() - myNewStatement.getTextRange().getStartOffset();
final PsiBlockStatement blockStatement = (PsiBlockStatement)myElementFactory.createStatementFromText("{}", null);
PsiCodeBlock block = blockStatement.getCodeBlock();
block.add(declaration);
block.add(myNewStatement);
block = ((PsiBlockStatement)myNewStatement.replace(blockStatement)).getCodeBlock();
variable = (PsiVariable)((PsiDeclarationStatement)block.getStatements()[0]).getDeclaredElements()[0];
myNewStatement = block.getStatements()[1];
myNewExpression = PsiTreeUtil.getParentOfType(myNewStatement.findElementAt(offsetInStatement), PsiNewExpression.class);
}
return variable;
}
catch (IncorrectOperationException e) {
@@ -0,0 +1,26 @@
public class Demo {
static class MyParent {
private final String value;
MyParent(String value) {
this.value = value;
}
}
static class MyC<caret>hild extends MyParent {
MyChild(String value) {
super(value);
}
}
public static void main(String[] args) {
String value = "something";
final MyParent p;
if (true)
p = new MyChild(value);
else
p = new MyParent("value");
}
}
@@ -0,0 +1,22 @@
public class Demo {
static class MyParent {
private final String value;
MyParent(String value) {
this.value = value;
}
}
public static void main(String[] args) {
String value = "something";
final MyParent p;
if (true) {
final String value1 = value;
p = new MyParent(value1);
}
else
p = new MyParent("value");
}
}
@@ -226,6 +226,10 @@ public class InlineToAnonymousClassTest extends LightRefactoringTestCase {
doTest(false, true);
}
public void testBraces() throws Exception {
doTest(false, false);
}
public void testNoInlineAbstract() throws Exception {
doTestNoInline("Abstract classes cannot be inlined");
}