mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
qualify conflicting fields name (IDEA-123969)
This commit is contained in:
+4
-10
@@ -60,10 +60,7 @@ import com.intellij.refactoring.RefactoringBundle;
|
||||
import com.intellij.refactoring.introduce.inplace.AbstractInplaceIntroducer;
|
||||
import com.intellij.refactoring.introduceVariable.IntroduceVariableBase;
|
||||
import com.intellij.refactoring.rename.RenameJavaVariableProcessor;
|
||||
import com.intellij.refactoring.util.CommonRefactoringUtil;
|
||||
import com.intellij.refactoring.util.EnumConstantsUtil;
|
||||
import com.intellij.refactoring.util.RefactoringChangeUtil;
|
||||
import com.intellij.refactoring.util.RefactoringUtil;
|
||||
import com.intellij.refactoring.util.*;
|
||||
import com.intellij.refactoring.util.occurrences.OccurrenceManager;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.intellij.util.VisibilityUtil;
|
||||
@@ -715,9 +712,8 @@ public abstract class BaseExpressionToFieldHandler extends IntroduceHandlerBase
|
||||
|
||||
if (!CommonRefactoringUtil.checkReadOnlyStatus(myProject, destClass.getContainingFile())) return;
|
||||
|
||||
if (initializer != null) {
|
||||
ChangeContextUtil.encodeContextInfo(initializer, true);
|
||||
}
|
||||
ChangeContextUtil.encodeContextInfo(destClass, true);
|
||||
|
||||
myField = mySettings.isIntroduceEnumConstant() ? EnumConstantsUtil.createEnumConstant(destClass, myFieldName, initializer) :
|
||||
createField(myFieldName, myType, initializer, initializerPlace == InitializationPlace.IN_FIELD_DECLARATION && initializer != null,
|
||||
myParentClass);
|
||||
@@ -847,9 +843,7 @@ public abstract class BaseExpressionToFieldHandler extends IntroduceHandlerBase
|
||||
}
|
||||
}
|
||||
|
||||
if (initializer != null) {
|
||||
ChangeContextUtil.clearContextInfo(initializer);
|
||||
}
|
||||
ChangeContextUtil.decodeContextInfo(destClass, destClass, null);
|
||||
}
|
||||
catch (IncorrectOperationException e) {
|
||||
LOG.error(e);
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package com.intellij.refactoring.introduceField;
|
||||
|
||||
import com.intellij.codeInsight.ChangeContextUtil;
|
||||
import com.intellij.codeInsight.CodeInsightUtil;
|
||||
import com.intellij.codeInsight.TestFrameworks;
|
||||
import com.intellij.codeInsight.navigation.NavigationUtil;
|
||||
@@ -282,6 +283,7 @@ public abstract class LocalToFieldHandler {
|
||||
|
||||
public void run() {
|
||||
try {
|
||||
ChangeContextUtil.encodeContextInfo(myDestinationClass, true);
|
||||
final boolean rebindNeeded2 = !myVariableName.equals(myFieldName) || myRebindNeeded;
|
||||
final PsiReference[] refs;
|
||||
if (rebindNeeded2) {
|
||||
@@ -342,6 +344,7 @@ public abstract class LocalToFieldHandler {
|
||||
}
|
||||
}
|
||||
//RefactoringUtil.renameVariableReferences(local, pPrefix + fieldName, GlobalSearchScope.projectScope(myProject));
|
||||
ChangeContextUtil.decodeContextInfo(myDestinationClass, myDestinationClass, null);
|
||||
}
|
||||
}
|
||||
catch (IncorrectOperationException e) {
|
||||
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
class A {
|
||||
private final int aField = 1;
|
||||
|
||||
class B {
|
||||
|
||||
public final int aField = 2;
|
||||
|
||||
int method() {
|
||||
return A.this.aField + aField;
|
||||
}
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
class A {
|
||||
private final int aField = 1;
|
||||
|
||||
class B {
|
||||
|
||||
public final int aField = 2;
|
||||
|
||||
int method() {
|
||||
return A.this.aField + aField;
|
||||
}
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
class A {
|
||||
private final int aField = 1;
|
||||
|
||||
class B {
|
||||
|
||||
int method() {
|
||||
return aField + <selection>2</selection>;
|
||||
}
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
class A {
|
||||
private final int aField = 1;
|
||||
|
||||
class B {
|
||||
|
||||
int method() {
|
||||
int <selection>i</selection> = 2;
|
||||
return aField + i;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,9 @@
|
||||
package com.intellij.refactoring;
|
||||
|
||||
import com.intellij.JavaTestUtil;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.PsiClass;
|
||||
import com.intellij.psi.PsiExpression;
|
||||
import com.intellij.psi.PsiPrimitiveType;
|
||||
import com.intellij.psi.PsiType;
|
||||
import com.intellij.refactoring.introduceField.BaseExpressionToFieldHandler;
|
||||
@@ -27,6 +29,33 @@ public class IntroduceFieldInSameClassTest extends LightCodeInsightTestCase {
|
||||
checkResultByFile("/refactoring/introduceField/after1.java");
|
||||
}
|
||||
|
||||
public void testConflictingFieldInContainingClass () throws Exception {
|
||||
configureByFile("/refactoring/introduceField/beforeConflictingFieldInContainingClass.java");
|
||||
new MockIntroduceFieldHandler(BaseExpressionToFieldHandler.InitializationPlace.IN_FIELD_DECLARATION, false){
|
||||
@Override
|
||||
protected String getNewName(Project project, PsiExpression expr, PsiType type) {
|
||||
return "aField";
|
||||
}
|
||||
}.invoke(getProject(), myEditor, myFile, null);
|
||||
checkResultByFile("/refactoring/introduceField/afterConflictingFieldInContainingClass.java");
|
||||
}
|
||||
|
||||
public void testConflictingFieldInContainingClassLocal () throws Exception {
|
||||
configureByFile("/refactoring/introduceField/beforeConflictingFieldInContainingClassLocal.java");
|
||||
new MockIntroduceFieldHandler(BaseExpressionToFieldHandler.InitializationPlace.IN_FIELD_DECLARATION, false){
|
||||
@Override
|
||||
protected String getNewName(Project project, PsiExpression expr, PsiType type) {
|
||||
return "aField";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int getChosenClassIndex(List<PsiClass> classes) {
|
||||
return 0;
|
||||
}
|
||||
}.invoke(getProject(), myEditor, myFile, null);
|
||||
checkResultByFile("/refactoring/introduceField/afterConflictingFieldInContainingClassLocal.java");
|
||||
}
|
||||
|
||||
public void testInElseClause() throws Exception {
|
||||
configureByFile("/refactoring/introduceField/beforeElseClause.java");
|
||||
performRefactoring(BaseExpressionToFieldHandler.InitializationPlace.IN_CURRENT_METHOD, true);
|
||||
|
||||
@@ -23,13 +23,18 @@ public class MockIntroduceFieldHandler extends IntroduceFieldHandler {
|
||||
@Override
|
||||
protected Settings showRefactoringDialog(Project project, Editor editor, PsiClass parentClass, PsiExpression expr, PsiType type,
|
||||
PsiExpression[] occurrences, PsiElement anchorElement, PsiElement anchorElementIfAll) {
|
||||
SuggestedNameInfo name = JavaCodeStyleManager.getInstance(project).suggestVariableName(VariableKind.FIELD, null, expr, type);
|
||||
return new Settings(name.names[0], expr, occurrences, true, myDeclareStatic, true, myInitializationPlace,
|
||||
final String fieldName = getNewName(project, expr, type);
|
||||
return new Settings(fieldName, expr, occurrences, true, myDeclareStatic, true, myInitializationPlace,
|
||||
PsiModifier.PUBLIC,
|
||||
null,
|
||||
getFieldType(type), true, (TargetDestination)null, false, false);
|
||||
}
|
||||
|
||||
protected String getNewName(Project project, PsiExpression expr, PsiType type) {
|
||||
SuggestedNameInfo name = JavaCodeStyleManager.getInstance(project).suggestVariableName(VariableKind.FIELD, null, expr, type);
|
||||
return name.names[0];
|
||||
}
|
||||
|
||||
protected PsiType getFieldType(PsiType type) {
|
||||
return type;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user