do not introduce field before this/super call (IDEA-56011)

This commit is contained in:
anna
2010-11-22 22:18:55 +03:00
parent 5e69316b8c
commit 9d03d64288
4 changed files with 41 additions and 12 deletions

View File

@@ -134,6 +134,7 @@ public abstract class BaseExpressionToFieldHandler extends IntroduceHandlerBase
}
PsiElement tempAnchorElement = RefactoringUtil.getParentExpressionAnchorElement(selectedExpr);
if (IntroduceVariableBase.checkAnchorBeforeThisOrSuper(project, editor, tempAnchorElement, getRefactoringName(), getHelpID())) return false;
final Settings settings =
showRefactoringDialog(project, editor, myParentClass, selectedExpr, tempType,

View File

@@ -396,18 +396,7 @@ public abstract class IntroduceVariableBase extends IntroduceHandlerBase impleme
if (anchorStatement == null) {
return parentStatementNotFound(project, editor);
}
if (anchorStatement instanceof PsiExpressionStatement) {
PsiExpression enclosingExpr = ((PsiExpressionStatement)anchorStatement).getExpression();
if (enclosingExpr instanceof PsiMethodCallExpression) {
PsiMethod method = ((PsiMethodCallExpression)enclosingExpr).resolveMethod();
if (method != null && method.isConstructor()) {
//This is either 'this' or 'super', both must be the first in the respective contructor
String message = RefactoringBundle.getCannotRefactorMessage(RefactoringBundle.message("invalid.expression.context"));
showErrorMessage(project, editor, message);
return false;
}
}
}
if (checkAnchorBeforeThisOrSuper(project, editor, anchorStatement, REFACTORING_NAME, HelpID.INTRODUCE_VARIABLE)) return false;
final PsiElement tempContainer = anchorStatement.getParent();
@@ -908,6 +897,26 @@ public abstract class IntroduceVariableBase extends IntroduceHandlerBase impleme
return createFinals == null ? CodeStyleSettingsManager.getSettings(project).GENERATE_FINAL_LOCALS : createFinals.booleanValue();
}
public static boolean checkAnchorBeforeThisOrSuper(final Project project,
final Editor editor,
final PsiElement tempAnchorElement,
final String refactoringName,
final String helpID) {
if (tempAnchorElement instanceof PsiExpressionStatement) {
PsiExpression enclosingExpr = ((PsiExpressionStatement)tempAnchorElement).getExpression();
if (enclosingExpr instanceof PsiMethodCallExpression) {
PsiMethod method = ((PsiMethodCallExpression)enclosingExpr).resolveMethod();
if (method != null && method.isConstructor()) {
//This is either 'this' or 'super', both must be the first in the respective contructor
String message = RefactoringBundle.getCannotRefactorMessage(RefactoringBundle.message("invalid.expression.context"));
CommonRefactoringUtil.showErrorHint(project, editor, message, refactoringName, helpID);
return true;
}
}
}
return false;
}
public interface Validator {
boolean isOK(IntroduceVariableSettings dialog);
}

View File

@@ -0,0 +1,6 @@
class Test {
Test(String s, String s1) {}
Test(String s) {
this(<selection>s</selection>, s);
}
}

View File

@@ -6,6 +6,7 @@ import com.intellij.openapi.projectRoots.impl.JavaSdkImpl;
import com.intellij.psi.PsiPrimitiveType;
import com.intellij.psi.PsiType;
import com.intellij.refactoring.introduceField.BaseExpressionToFieldHandler;
import com.intellij.refactoring.util.CommonRefactoringUtil;
import com.intellij.testFramework.LightCodeInsightTestCase;
/**
@@ -91,4 +92,16 @@ public class IntroduceFieldInSameClassTest extends LightCodeInsightTestCase {
}.invoke(getProject(), myEditor, myFile, null);
checkResultByFile("/refactoring/introduceField/afterForcedFieldType.java");
}
public void testRejectIntroduceFieldFromExprInThisCall() throws Exception {
configureByFile("/refactoring/introduceField/beforeRejectIntroduceFieldFromExprInThisCall.java");
try {
performRefactoring(BaseExpressionToFieldHandler.InitializationPlace.IN_FIELD_DECLARATION, false);
fail("Should not proceed");
}
catch (CommonRefactoringUtil.RefactoringErrorHintException e) {
assertEquals("Cannot perform refactoring.\n" +
"Invalid expression context.", e.getMessage());
}
}
}