initialize in setup: add field initialization instead of variable declaration (IDEA-62451)

This commit is contained in:
anna
2010-12-07 18:16:39 +03:00
parent 19ccd895cb
commit cb7ccebc23
4 changed files with 40 additions and 1 deletions

View File

@@ -26,6 +26,7 @@ import com.intellij.psi.*;
import com.intellij.psi.codeStyle.CodeStyleManager;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.search.searches.ReferencesSearch;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.refactoring.HelpID;
import com.intellij.refactoring.RefactoringBundle;
import com.intellij.refactoring.util.CommonRefactoringUtil;
@@ -231,7 +232,11 @@ public abstract class LocalToFieldHandler {
PsiStatement assignment = createAssignment(local, field.getName(), factory);
final PsiCodeBlock body = inClass.getBody();
assert body != null;
if (PsiTreeUtil.isAncestor(body, local, false)) {
body.addBefore(assignment, PsiTreeUtil.getParentOfType(local, PsiStatement.class));
} else {
body.add(assignment);
}
local.delete();
}

View File

@@ -0,0 +1,16 @@
import junit.framework.TestCase;
public class T extends TestCase {
private String i;
public void setUp() throws Exception {
i = getName();
myName = " second " + i;
}
public void test() throws Exception {
}
private String getName() {
return null;
}
}

View File

@@ -0,0 +1,14 @@
import junit.framework.TestCase;
public class T extends TestCase {
public void setUp() throws Exception {
String na<caret>me = getName();
myName = " second " + name;
}
public void test() throws Exception {
}
private String getName() {
return null;
}
}

View File

@@ -78,6 +78,10 @@ public class IntroduceFieldWitSetUpInitializationTest extends CodeInsightTestCas
doTest();
}
public void testOrderInSetup() throws Exception {
doTest();
}
private void doTest() throws Exception {
configureByFile("/refactoring/introduceField/before" + getTestName(false) + ".java");