diff --git a/java/java-impl/src/com/intellij/refactoring/introduceField/BaseExpressionToFieldHandler.java b/java/java-impl/src/com/intellij/refactoring/introduceField/BaseExpressionToFieldHandler.java index c46c15c4dda5..74c3bba54825 100644 --- a/java/java-impl/src/com/intellij/refactoring/introduceField/BaseExpressionToFieldHandler.java +++ b/java/java-impl/src/com/intellij/refactoring/introduceField/BaseExpressionToFieldHandler.java @@ -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, diff --git a/java/java-impl/src/com/intellij/refactoring/introduceVariable/IntroduceVariableBase.java b/java/java-impl/src/com/intellij/refactoring/introduceVariable/IntroduceVariableBase.java index 829079e73d52..30cf33133658 100644 --- a/java/java-impl/src/com/intellij/refactoring/introduceVariable/IntroduceVariableBase.java +++ b/java/java-impl/src/com/intellij/refactoring/introduceVariable/IntroduceVariableBase.java @@ -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); } diff --git a/java/java-tests/testData/refactoring/introduceField/beforeRejectIntroduceFieldFromExprInThisCall.java b/java/java-tests/testData/refactoring/introduceField/beforeRejectIntroduceFieldFromExprInThisCall.java new file mode 100644 index 000000000000..4056e8a03b3f --- /dev/null +++ b/java/java-tests/testData/refactoring/introduceField/beforeRejectIntroduceFieldFromExprInThisCall.java @@ -0,0 +1,6 @@ +class Test { + Test(String s, String s1) {} + Test(String s) { + this(s, s); + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/refactoring/IntroduceFieldInSameClassTest.java b/java/java-tests/testSrc/com/intellij/refactoring/IntroduceFieldInSameClassTest.java index de6dd6214841..80a17f4aa744 100644 --- a/java/java-tests/testSrc/com/intellij/refactoring/IntroduceFieldInSameClassTest.java +++ b/java/java-tests/testSrc/com/intellij/refactoring/IntroduceFieldInSameClassTest.java @@ -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()); + } + } } \ No newline at end of file