extract method: when context was changed ensure that static method is not qualified with this

This commit is contained in:
Anna.Kozlova
2016-04-13 21:11:02 +02:00
parent 1d7c5fbe69
commit 1a9c4c2f19
4 changed files with 65 additions and 1 deletions
@@ -730,6 +730,14 @@ public class ExtractMethodProcessor implements MatchProvider {
}
}
@TestOnly
public void testTargetClass(PsiClass targetClass) {
if (targetClass != null) {
myTargetClass = targetClass;
myNeedChangeContext = true;
}
}
@TestOnly
public void testPrepare(PsiType returnType, boolean makeStatic) throws PrepareFailedException{
if (makeStatic) {
@@ -1018,7 +1026,7 @@ public class ExtractMethodProcessor implements MatchProvider {
ChangeContextUtil.decodeContextInfo(myExtractedMethod, myTargetClass, RefactoringChangeUtil.createThisExpression(myManager, null));
if (myMethodCall.resolveMethod() != myExtractedMethod) {
final PsiReferenceExpression methodExpression = myMethodCall.getMethodExpression();
methodExpression.setQualifierExpression(RefactoringChangeUtil.createThisExpression(myManager, myTargetClass));
RefactoringChangeUtil.qualifyReference(methodExpression, myExtractedMethod, PsiUtil.getEnclosingStaticElement(methodExpression, myTargetClass) != null ? myTargetClass : null);
}
}
}
@@ -0,0 +1,11 @@
class A {
public void newMethod() {}
}
class B {
static class C extends A {
{
<selection>System.out.println();</selection>
}
}
}
@@ -0,0 +1,15 @@
class A {
public void newMethod() {}
}
class B {
static class C extends A {
{
B.newMethod();
}
}
private void newMethod() {
System.out.println();
}
}
@@ -26,6 +26,7 @@ import com.intellij.psi.codeStyle.CodeStyleSettings;
import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
import com.intellij.psi.codeStyle.CommonCodeStyleSettings;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.refactoring.extractMethod.ExtractMethodHandler;
import com.intellij.refactoring.extractMethod.ExtractMethodProcessor;
import com.intellij.refactoring.extractMethod.PrepareFailedException;
@@ -776,6 +777,18 @@ public class ExtractMethodTest extends LightCodeInsightTestCase {
doTest();
}
public void testQualifyWhenConflictingNamePresent() throws Exception {
final CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(getProject());
settings.ELSE_ON_NEW_LINE = true;
settings.CATCH_ON_NEW_LINE = myCatchOnNewLine;
configureByFile(BASE_PATH + getTestName(false) + ".java");
final PsiClass psiClass = PsiTreeUtil.getParentOfType(getFile().findElementAt(getEditor().getSelectionModel().getLeadSelectionOffset()), PsiClass.class);
assertNotNull(psiClass);
boolean success = performExtractMethod(true, true, getEditor(), getFile(), getProject(), false, null, false, null, psiClass.getContainingClass());
assertTrue(success);
checkResultByFile(BASE_PATH + getTestName(false) + "_after.java");
}
private void doTestDisabledParam() throws PrepareFailedException {
final CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(getProject());
settings.ELSE_ON_NEW_LINE = true;
@@ -875,6 +888,22 @@ public class ExtractMethodTest extends LightCodeInsightTestCase {
String newNameOfFirstParam,
int... disabledParams)
throws PrepareFailedException, IncorrectOperationException {
return performExtractMethod(doRefactor, replaceAllDuplicates, editor, file, project, extractChainedConstructor, returnType, makeStatic,
newNameOfFirstParam, null, disabledParams);
}
public static boolean performExtractMethod(boolean doRefactor,
boolean replaceAllDuplicates,
Editor editor,
PsiFile file,
Project project,
final boolean extractChainedConstructor,
PsiType returnType,
boolean makeStatic,
String newNameOfFirstParam,
PsiClass targetClass,
int... disabledParams)
throws PrepareFailedException, IncorrectOperationException {
int startOffset = editor.getSelectionModel().getSelectionStart();
int endOffset = editor.getSelectionModel().getSelectionEnd();
@@ -904,6 +933,7 @@ public class ExtractMethodTest extends LightCodeInsightTestCase {
}
if (doRefactor) {
processor.testTargetClass(targetClass);
processor.testPrepare(returnType, makeStatic);
processor.testNullness();
if (disabledParams != null) {