introduce field: choose anchor corresponding to used fields without initializer (IDEA-97420)

This commit is contained in:
anna
2012-12-13 15:50:07 +01:00
parent 7cf3221fa3
commit 032a2a603a
7 changed files with 64 additions and 47 deletions
@@ -906,7 +906,6 @@ public abstract class BaseExpressionToFieldHandler extends IntroduceHandlerBase
super.visitReferenceExpression(expression);
final PsiElement resolve = expression.resolve();
if (resolve instanceof PsiField &&
((PsiField)resolve).hasModifierProperty(PsiModifier.FINAL) &&
PsiTreeUtil.isAncestor(parentClass, resolve, false) && ((PsiField)resolve).hasInitializer() &&
!PsiTreeUtil.isAncestor(initializer, resolve, false)) {
if (refConstantFields[0] == null || refConstantFields[0].getTextOffset() < resolve.getTextOffset()) {
@@ -16,10 +16,8 @@
package com.intellij.refactoring.introduceField;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiExpression;
import com.intellij.psi.PsiLocalVariable;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtil;
import com.intellij.refactoring.RefactoringBundle;
import com.intellij.refactoring.ui.TypeSelectorManager;
@@ -83,7 +81,27 @@ public abstract class IntroduceFieldCentralPanel {
myTypeSelectorManager = typeSelectorManager;
}
protected abstract boolean setEnabledInitializationPlaces(PsiElement initializerPart, PsiElement initializer);
protected boolean setEnabledInitializationPlaces(PsiElement initializerPart, PsiElement initializer) {
if (initializerPart instanceof PsiReferenceExpression) {
PsiReferenceExpression refExpr = (PsiReferenceExpression)initializerPart;
if (refExpr.getQualifierExpression() == null) {
PsiElement refElement = refExpr.resolve();
if (refElement == null ||
(refElement instanceof PsiLocalVariable ||
refElement instanceof PsiParameter ||
(refElement instanceof PsiField && !((PsiField)refElement).hasInitializer())) &&
!PsiTreeUtil.isAncestor(initializer, refElement, true)) {
return updateInitializationPlaceModel();
}
}
}
PsiElement[] children = initializerPart.getChildren();
for (PsiElement child : children) {
if (!setEnabledInitializationPlaces(child, initializer)) return false;
}
return true;
}
public abstract BaseExpressionToFieldHandler.InitializationPlace getInitializerPlace();
protected abstract void initializeInitializerPlace(PsiExpression initializerExpression,
BaseExpressionToFieldHandler.InitializationPlace ourLastInitializerPlace);
@@ -257,4 +275,6 @@ public abstract class IntroduceFieldCentralPanel {
ourLastCbFinalState = myCbFinal.isSelected();
}
}
protected abstract boolean updateInitializationPlaceModel();
}
@@ -194,27 +194,13 @@ public class IntroduceFieldDialogPanel extends IntroduceFieldCentralPanel {
return mainPanel;
}
protected boolean setEnabledInitializationPlaces(PsiElement initializerPart, PsiElement initializer) {
if (initializerPart instanceof PsiReferenceExpression) {
PsiReferenceExpression refExpr = (PsiReferenceExpression) initializerPart;
if (refExpr.getQualifierExpression() == null) {
PsiElement refElement = refExpr.resolve();
if (refElement == null ||
(refElement instanceof PsiLocalVariable || refElement instanceof PsiParameter) &&
!PsiTreeUtil.isAncestor(initializer, refElement, true)) {
myRbInFieldDeclaration.setEnabled(false);
myRbInConstructor.setEnabled(false);
if (myRbInSetUp != null) myRbInSetUp.setEnabled(false);
enableFinal(false);
return false;
}
}
}
PsiElement[] children = initializerPart.getChildren();
for (PsiElement child : children) {
if (!setEnabledInitializationPlaces(child, initializer)) return false;
}
return true;
@Override
protected boolean updateInitializationPlaceModel() {
myRbInFieldDeclaration.setEnabled(false);
myRbInConstructor.setEnabled(false);
if (myRbInSetUp != null) myRbInSetUp.setEnabled(false);
enableFinal(false);
return false;
}
public void setInitializeInFieldDeclaration() {
@@ -186,26 +186,12 @@ public class IntroduceFieldPopupPanel extends IntroduceFieldCentralPanel {
return null;
}
protected boolean setEnabledInitializationPlaces(PsiElement initializerPart, PsiElement initializer) {
if (initializerPart instanceof PsiReferenceExpression) {
PsiReferenceExpression refExpr = (PsiReferenceExpression) initializerPart;
if (refExpr.getQualifierExpression() == null) {
PsiElement refElement = refExpr.resolve();
if (refElement == null ||
(refElement instanceof PsiLocalVariable || refElement instanceof PsiParameter) &&
!PsiTreeUtil.isAncestor(initializer, refElement, true)) {
myInitialisersPlaceModel.removeElement(BaseExpressionToFieldHandler.InitializationPlace.IN_FIELD_DECLARATION);
myInitialisersPlaceModel.removeElement(BaseExpressionToFieldHandler.InitializationPlace.IN_CONSTRUCTOR);
myInitialisersPlaceModel.removeElement(BaseExpressionToFieldHandler.InitializationPlace.IN_SETUP_METHOD);
return false;
}
}
}
PsiElement[] children = initializerPart.getChildren();
for (PsiElement child : children) {
if (!setEnabledInitializationPlaces(child, initializer)) return false;
}
return true;
@Override
protected boolean updateInitializationPlaceModel() {
myInitialisersPlaceModel.removeElement(BaseExpressionToFieldHandler.InitializationPlace.IN_FIELD_DECLARATION);
myInitialisersPlaceModel.removeElement(BaseExpressionToFieldHandler.InitializationPlace.IN_CONSTRUCTOR);
myInitialisersPlaceModel.removeElement(BaseExpressionToFieldHandler.InitializationPlace.IN_SETUP_METHOD);
return false;
}
public void setInitializeInFieldDeclaration() {
@@ -0,0 +1,8 @@
import java.util.*;
class ExtractFieldCompilationFailureTest {
private ArrayList aelements = new ArrayList();
public void test() {
aelements.it<caret>erator();
}
}
@@ -0,0 +1,9 @@
import java.util.*;
class ExtractFieldCompilationFailureTest {
private ArrayList aelements = new ArrayList();
private Iterator iterator;
public void test() {
iterator = aelements.iterator();
}
}
@@ -41,6 +41,15 @@ public class InplaceIntroduceFieldTest extends AbstractJavaInplaceIntroduceTest
});
}
public void testAnchor1() throws Exception {
doTest(new Pass<AbstractInplaceIntroducer>() {
@Override
public void pass(AbstractInplaceIntroducer inplaceIntroduceFieldPopup) {
}
});
}
public void testBeforeAssignment() throws Exception {
doTest(new Pass<AbstractInplaceIntroducer>() {