Java: Simplified handling the parentheses in the quick-fix Collection.toArray() (IDEA-163341)

This commit is contained in:
Pavel Dolgov
2017-01-13 12:42:51 +03:00
parent f351ba2ddb
commit 93e109126a
2 changed files with 4 additions and 15 deletions

View File

@@ -17,7 +17,6 @@ package com.intellij.codeInsight.daemon.impl.quickfix;
import com.intellij.codeInsight.daemon.QuickFixBundle;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
@@ -30,8 +29,6 @@ import org.jetbrains.annotations.NotNull;
* @author Pavel.Dolgov
*/
public class ConvertCollectionToArrayFix implements IntentionAction {
private static final Logger LOG = Logger.getInstance(ConvertCollectionToArrayFix.class);
private final PsiExpression myCollectionExpression;
private final String myNewArrayText;
@@ -64,17 +61,9 @@ public class ConvertCollectionToArrayFix implements IntentionAction {
@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
PsiElementFactory factory = JavaPsiFacade.getInstance(project).getElementFactory();
PsiMethodCallExpression toArrayCall =
(PsiMethodCallExpression)factory.createExpressionFromText("(a).toArray(" + myNewArrayText + ")", myCollectionExpression);
PsiParenthesizedExpression parenthesized = (PsiParenthesizedExpression)toArrayCall.getMethodExpression().getQualifierExpression();
LOG.assertTrue(parenthesized != null);
PsiExpression placeholder = parenthesized.getExpression();
LOG.assertTrue(placeholder != null);
placeholder.replace(myCollectionExpression);
if (!ParenthesesUtils.areParenthesesNeeded(parenthesized, false)) {
parenthesized.replace(myCollectionExpression);
}
myCollectionExpression.replace(toArrayCall);
String replacement = ParenthesesUtils.getText(myCollectionExpression, ParenthesesUtils.POSTFIX_PRECEDENCE) +
".toArray(" + myNewArrayText + ")";
myCollectionExpression.replace(factory.createExpressionFromText(replacement, myCollectionExpression));
}
@Override

View File

@@ -4,7 +4,7 @@ import java.util.*;
class Cast {
Integer[] foo() {
Iterable<Integer> c = Arrays.asList(1, 2);
Integer[] arr = ((Collection<Integer>)c).toArray(new Integer[0]);
Integer[] arr = ((Collection<Integer>) c).toArray(new Integer[0]);
return arr;
}
}