mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
introduce field/constant: fix array initializer (IDEA-135785)
This commit is contained in:
+1
-2
@@ -27,7 +27,6 @@ import com.intellij.ide.util.DirectoryChooserUtil;
|
||||
import com.intellij.ide.util.PackageUtil;
|
||||
import com.intellij.ide.util.PsiClassListCellRenderer;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.application.Result;
|
||||
import com.intellij.openapi.command.WriteCommandAction;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
@@ -708,7 +707,7 @@ public abstract class BaseExpressionToFieldHandler extends IntroduceHandlerBase
|
||||
}
|
||||
|
||||
final SmartTypePointer type = SmartTypePointerManager.getInstance(myProject).createSmartTypePointer(myType);
|
||||
initializer = IntroduceVariableBase.simplifyVariableInitializer(initializer, myType);
|
||||
initializer = IntroduceVariableBase.simplifyVariableInitializer(initializer, myType, initializerPlace == InitializationPlace.IN_FIELD_DECLARATION);
|
||||
|
||||
final PsiMethod enclosingConstructor = getEnclosingConstructor(myParentClass, myAnchorElement);
|
||||
PsiClass destClass = mySettings.getDestinationClass() == null ? myParentClass : mySettings.getDestinationClass();
|
||||
|
||||
@@ -53,6 +53,23 @@ public class ElementToWorkOn {
|
||||
myLocalVariable = localVariable;
|
||||
myExpression = expr;
|
||||
}
|
||||
|
||||
public static ElementToWorkOn adjustElements(PsiExpression expr, PsiElement anchorElement) {
|
||||
PsiLocalVariable localVariable = null;
|
||||
if (anchorElement instanceof PsiLocalVariable) {
|
||||
localVariable = (PsiLocalVariable)anchorElement;
|
||||
}
|
||||
else if (expr instanceof PsiReferenceExpression) {
|
||||
PsiElement ref = ((PsiReferenceExpression)expr).resolve();
|
||||
if (ref instanceof PsiLocalVariable) {
|
||||
localVariable = (PsiLocalVariable)ref;
|
||||
}
|
||||
}
|
||||
else if (expr instanceof PsiArrayInitializerExpression && expr.getParent() instanceof PsiNewExpression) {
|
||||
expr = (PsiExpression)expr.getParent();
|
||||
}
|
||||
return new ElementToWorkOn(localVariable, expr);
|
||||
}
|
||||
|
||||
public PsiExpression getExpression() {
|
||||
return myExpression;
|
||||
@@ -156,6 +173,9 @@ public class ElementToWorkOn {
|
||||
localVar = PsiTreeUtil.getParentOfType(ident, PsiLocalVariable.class);
|
||||
}
|
||||
}
|
||||
else if (expr instanceof PsiArrayInitializerExpression && expr.getParent() instanceof PsiNewExpression) {
|
||||
expr = (PsiExpression)expr.getParent();
|
||||
}
|
||||
}
|
||||
|
||||
if (expr == null && localVar == null) {
|
||||
|
||||
+3
-10
@@ -101,16 +101,9 @@ public class IntroduceConstantHandler extends BaseExpressionToFieldHandler {
|
||||
PsiElement anchorElementIfAll) {
|
||||
final PsiMethod containingMethod = PsiTreeUtil.getParentOfType(expr != null ? expr : anchorElement, PsiMethod.class);
|
||||
|
||||
PsiLocalVariable localVariable = null;
|
||||
if (expr instanceof PsiReferenceExpression) {
|
||||
PsiElement ref = ((PsiReferenceExpression)expr).resolve();
|
||||
if (ref instanceof PsiLocalVariable) {
|
||||
localVariable = (PsiLocalVariable)ref;
|
||||
}
|
||||
}
|
||||
else if (anchorElement instanceof PsiLocalVariable) {
|
||||
localVariable = (PsiLocalVariable)anchorElement;
|
||||
}
|
||||
ElementToWorkOn elementToWorkOn = ElementToWorkOn.adjustElements(expr, anchorElement);
|
||||
PsiLocalVariable localVariable = elementToWorkOn.getLocalVariable();
|
||||
expr = elementToWorkOn.getExpression();
|
||||
|
||||
String enteredName = null;
|
||||
boolean replaceAllOccurrences = true;
|
||||
|
||||
+3
-10
@@ -74,16 +74,9 @@ public class IntroduceFieldHandler extends BaseExpressionToFieldHandler {
|
||||
PsiExpression[] occurrences, PsiElement anchorElement, PsiElement anchorElementIfAll) {
|
||||
final AbstractInplaceIntroducer activeIntroducer = AbstractInplaceIntroducer.getActiveIntroducer(editor);
|
||||
|
||||
PsiLocalVariable localVariable = null;
|
||||
if (anchorElement instanceof PsiLocalVariable) {
|
||||
localVariable = (PsiLocalVariable)anchorElement;
|
||||
}
|
||||
else if (expr instanceof PsiReferenceExpression) {
|
||||
PsiElement ref = ((PsiReferenceExpression)expr).resolve();
|
||||
if (ref instanceof PsiLocalVariable) {
|
||||
localVariable = (PsiLocalVariable)ref;
|
||||
}
|
||||
}
|
||||
ElementToWorkOn elementToWorkOn = ElementToWorkOn.adjustElements(expr, anchorElement);
|
||||
PsiLocalVariable localVariable = elementToWorkOn.getLocalVariable();
|
||||
expr = elementToWorkOn.getExpression();
|
||||
|
||||
String enteredName = null;
|
||||
boolean replaceAll = false;
|
||||
|
||||
+10
-2
@@ -1040,7 +1040,13 @@ public abstract class IntroduceVariableBase extends IntroduceHandlerBase {
|
||||
}
|
||||
|
||||
public static PsiExpression simplifyVariableInitializer(final PsiExpression initializer,
|
||||
final PsiType expectedType) {
|
||||
final PsiType expectedType) {
|
||||
return simplifyVariableInitializer(initializer, expectedType, true);
|
||||
}
|
||||
|
||||
public static PsiExpression simplifyVariableInitializer(final PsiExpression initializer,
|
||||
final PsiType expectedType,
|
||||
final boolean inDeclaration) {
|
||||
|
||||
if (initializer instanceof PsiTypeCastExpression) {
|
||||
PsiExpression operand = ((PsiTypeCastExpression)initializer).getOperand();
|
||||
@@ -1054,7 +1060,9 @@ public abstract class IntroduceVariableBase extends IntroduceHandlerBase {
|
||||
else if (initializer instanceof PsiNewExpression) {
|
||||
final PsiNewExpression newExpression = (PsiNewExpression)initializer;
|
||||
if (newExpression.getArrayInitializer() != null) {
|
||||
return newExpression.getArrayInitializer();
|
||||
if (inDeclaration) {
|
||||
return newExpression.getArrayInitializer();
|
||||
}
|
||||
}
|
||||
else {
|
||||
final PsiExpression tryToDetectDiamondNewExpr = ((PsiVariable)JavaPsiFacade.getElementFactory(initializer.getProject())
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
class Test {
|
||||
{
|
||||
String[] str = new String[] {"a"<caret>, "b", "c"};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
class Test {
|
||||
|
||||
private String[] str;
|
||||
|
||||
{
|
||||
this.str = new String[]{"a", "b", "c"};
|
||||
String[] str = this.str;
|
||||
}
|
||||
}
|
||||
@@ -37,6 +37,15 @@ public class InplaceIntroduceFieldTest extends AbstractJavaInplaceIntroduceTest
|
||||
});
|
||||
}
|
||||
|
||||
public void testArrayInitializer() {
|
||||
|
||||
doTest(new Pass<AbstractInplaceIntroducer>() {
|
||||
@Override
|
||||
public void pass(AbstractInplaceIntroducer inplaceIntroduceFieldPopup) {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void testAnchor1() {
|
||||
|
||||
doTest(new Pass<AbstractInplaceIntroducer>() {
|
||||
|
||||
Reference in New Issue
Block a user