mirror of
https://gitflic.ru/project/openide/openide.git
synced 2025-12-14 18:05:27 +07:00
do not introduce field before this/super call (IDEA-56011)
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
class Test {
|
||||
Test(String s, String s1) {}
|
||||
Test(String s) {
|
||||
this(<selection>s</selection>, s);
|
||||
}
|
||||
}
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user