mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
introduce constant/field: static/final should be set before actual add - anchor detection (IDEA-52969)
This commit is contained in:
+4
-3
@@ -205,7 +205,7 @@ public abstract class BaseExpressionToFieldHandler extends IntroduceHandlerBase
|
||||
}
|
||||
}
|
||||
PsiMember anchorMember = finalAnchorElement instanceof PsiMember ? (PsiMember)finalAnchorElement : null;
|
||||
|
||||
setModifiers(field, settings, settings.isDeclareStatic(), occurrences);
|
||||
if ((anchorMember instanceof PsiField) &&
|
||||
anchorMember.hasModifierProperty(PsiModifier.STATIC) == field.hasModifierProperty(PsiModifier.STATIC)) {
|
||||
field = (PsiField)destClass.addBefore(field, anchorMember);
|
||||
@@ -217,7 +217,9 @@ public abstract class BaseExpressionToFieldHandler extends IntroduceHandlerBase
|
||||
else {
|
||||
field = (PsiField)destClass.add(field);
|
||||
}
|
||||
setModifiers(field, settings, settings.isDeclareStatic(), occurrences);
|
||||
if (!settings.isIntroduceEnumConstant()) {
|
||||
VisibilityUtil.fixVisibility(occurrences, field, settings.getFieldVisibility());
|
||||
}
|
||||
PsiStatement assignStatement = null;
|
||||
PsiElement anchorElementHere = null;
|
||||
if (initializerPlace == InitializationPlace.IN_CURRENT_METHOD && initializer != null ||
|
||||
@@ -316,7 +318,6 @@ public abstract class BaseExpressionToFieldHandler extends IntroduceHandlerBase
|
||||
|
||||
public static void setModifiers(PsiField field, Settings settings, final boolean declareStatic, PsiExpression[] occurrences) {
|
||||
if (!settings.isIntroduceEnumConstant()) {
|
||||
VisibilityUtil.fixVisibility(occurrences, field, settings.getFieldVisibility());
|
||||
if (declareStatic) {
|
||||
PsiUtil.setModifierProperty(field, PsiModifier.STATIC, true);
|
||||
}
|
||||
|
||||
@@ -34,6 +34,7 @@ import com.intellij.refactoring.util.CommonRefactoringUtil;
|
||||
import com.intellij.refactoring.util.EnumConstantsUtil;
|
||||
import com.intellij.refactoring.util.RefactoringUtil;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.intellij.util.VisibilityUtil;
|
||||
import org.jetbrains.annotations.NonNls;
|
||||
|
||||
import static com.intellij.refactoring.introduceField.BaseExpressionToFieldHandler.InitializationPlace.IN_CONSTRUCTOR;
|
||||
@@ -134,7 +135,6 @@ public class LocalToFieldHandler {
|
||||
final String variableName = local.getName();
|
||||
final String fieldName = settings.getFieldName();
|
||||
final BaseExpressionToFieldHandler.InitializationPlace initializerPlace = settings.getInitializerPlace();
|
||||
@Modifier final String fieldVisibility = settings.getFieldVisibility();
|
||||
final PsiClass destinationClass = settings.getDestinationClass();
|
||||
boolean rebindNeeded = false;
|
||||
if (destinationClass != null) {
|
||||
@@ -161,6 +161,9 @@ public class LocalToFieldHandler {
|
||||
: createField(local, fieldName, initializerPlace == IN_FIELD_DECLARATION);
|
||||
field = (PsiField)aaClass.add(field);
|
||||
BaseExpressionToFieldHandler.setModifiers(field, settings, isStatic, occurences);
|
||||
if (!settings.isIntroduceEnumConstant()) {
|
||||
VisibilityUtil.fixVisibility(occurences, field, settings.getFieldVisibility());
|
||||
}
|
||||
|
||||
local.normalizeDeclaration();
|
||||
PsiDeclarationStatement declarationStatement = (PsiDeclarationStatement)local.getParent();
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
public class Test {
|
||||
public static final String FIRST = "<selection>pref</selection>";
|
||||
public static final String SECOND = "pref";
|
||||
}
|
||||
|
||||
class AnotherTest {}
|
||||
@@ -0,0 +1,8 @@
|
||||
public class Test {
|
||||
public static final String FIRST = AnotherTest.xxx;
|
||||
public static final String SECOND = AnotherTest.xxx;
|
||||
}
|
||||
|
||||
class AnotherTest {
|
||||
static final String xxx = "pref";
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
public class Test{
|
||||
public static final String FIRST = "<selection>pref</selection>.FIRST";
|
||||
public static final String SECOND = "pref.SECOND";
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
public class Test{
|
||||
public static final String xxx = "pref";
|
||||
public static final String FIRST = xxx + ".FIRST";
|
||||
public static final String SECOND = xxx + ".SECOND";
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import com.intellij.psi.PsiLocalVariable;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.testFramework.LightCodeInsightTestCase;
|
||||
import com.intellij.testFramework.TestDataPath;
|
||||
import com.intellij.util.VisibilityUtil;
|
||||
import junit.framework.Assert;
|
||||
import org.jetbrains.annotations.NonNls;
|
||||
|
||||
@@ -65,6 +66,27 @@ public class IntroduceConstantTest extends LightCodeInsightTestCase {
|
||||
checkResultByFile(BASE_PATH + getTestName(false) + "_after.java");
|
||||
}
|
||||
|
||||
public void testPartialStringLiteralAnchor() throws Exception {
|
||||
configureByFile(BASE_PATH + getTestName(false) + ".java");
|
||||
new MockIntroduceConstantHandler(null).invoke(getProject(), getEditor(), getFile(), null);
|
||||
checkResultByFile(BASE_PATH + getTestName(false) + "_after.java");
|
||||
}
|
||||
|
||||
public void testEscalateVisibility() throws Exception {
|
||||
configureByFile(BASE_PATH + getTestName(false) + ".java");
|
||||
final PsiClass[] classes = ((PsiJavaFile)getFile()).getClasses();
|
||||
Assert.assertTrue(classes.length == 2);
|
||||
final PsiClass targetClass = classes[1];
|
||||
Assert.assertNotNull(targetClass);
|
||||
new MockIntroduceConstantHandler(targetClass){
|
||||
@Override
|
||||
protected String getVisibility() {
|
||||
return VisibilityUtil.ESCALATE_VISIBILITY;
|
||||
}
|
||||
}.invoke(getProject(), getEditor(), getFile(), null);
|
||||
checkResultByFile(BASE_PATH + getTestName(false) + "_after.java");
|
||||
}
|
||||
|
||||
protected Sdk getProjectJDK() {
|
||||
return JavaSdkImpl.getMockJdk15("java 1.5");
|
||||
}
|
||||
|
||||
@@ -20,7 +20,11 @@ public class MockIntroduceConstantHandler extends IntroduceConstantHandler{
|
||||
protected Settings showRefactoringDialog(final Project project, final Editor editor, final PsiClass parentClass, final PsiExpression expr,
|
||||
final PsiType type, final PsiExpression[] occurences, final PsiElement anchorElement,
|
||||
final PsiElement anchorElementIfAll) {
|
||||
return new Settings("xxx", true, true, true, InitializationPlace.IN_FIELD_DECLARATION, PsiModifier.PUBLIC, null, null, false,
|
||||
return new Settings("xxx", true, true, true, InitializationPlace.IN_FIELD_DECLARATION, getVisibility(), null, null, false,
|
||||
myTargetClass != null ? myTargetClass : parentClass, false, false);
|
||||
}
|
||||
|
||||
protected String getVisibility() {
|
||||
return PsiModifier.PUBLIC;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user