IDEA-93040

This commit is contained in:
Dmitry Batkovich
2014-05-28 16:05:55 +04:00
parent 622c1df75a
commit dff9280491
10 changed files with 211 additions and 1 deletions

View File

@@ -542,6 +542,7 @@ public class HighlightMethodUtil {
TextRange fixRange = getFixRange(elementToHighlight);
CastMethodArgumentFix.REGISTRAR.registerCastActions(candidates, methodCall, info, fixRange);
WrapArrayToArraysAsListFix.REGISTAR.registerCastActions(candidates, methodCall, info, fixRange);
PermuteArgumentsFix.registerFix(info, methodCall, candidates, fixRange);
WrapExpressionFix.registerWrapAction(candidates, list.getExpressions(), info);
registerChangeParameterClassFix(methodCall, list, info);
@@ -633,6 +634,7 @@ public class HighlightMethodUtil {
TextRange fixRange = getFixRange(elementToHighlight);
CastMethodArgumentFix.REGISTRAR.registerCastActions(candidates, methodCall, info, fixRange);
WrapArrayToArraysAsListFix.REGISTAR.registerCastActions(candidates, methodCall, info, fixRange);
PermuteArgumentsFix.registerFix(info, methodCall, candidates, fixRange);
WrapExpressionFix.registerWrapAction(candidates, list.getExpressions(), info);
registerChangeParameterClassFix(methodCall, list, info);
@@ -666,6 +668,7 @@ public class HighlightMethodUtil {
CastMethodArgumentFix.REGISTRAR.registerCastActions(methodCandidates, methodCall, highlightInfo, fixRange);
PermuteArgumentsFix.registerFix(highlightInfo, methodCall, methodCandidates, fixRange);
AddTypeArgumentsFix.REGISTRAR.registerCastActions(methodCandidates, methodCall, highlightInfo, fixRange);
WrapArrayToArraysAsListFix.REGISTAR.registerCastActions(methodCandidates, methodCall, highlightInfo, fixRange);
registerMethodAccessLevelIntentions(methodCandidates, methodCall, list, highlightInfo);
registerChangeMethodSignatureFromUsageIntentions(methodCandidates, list, highlightInfo, fixRange);
RemoveRedundantArgumentsFix.registerIntentions(methodCandidates, list, highlightInfo, fixRange);

View File

@@ -0,0 +1,94 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.codeInsight.daemon.impl.quickfix;
import com.intellij.codeInsight.daemon.QuickFixBundle;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
import com.intellij.psi.util.InheritanceUtil;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* @author Dmitry Batkovich
*/
public class WrapArrayToArraysAsListFix extends MethodArgumentFix {
public static final ArgumentFixerActionFactory REGISTAR = new MyFixerActionFactory();
protected WrapArrayToArraysAsListFix(final @NotNull PsiExpressionList list,
final int i,
final @NotNull PsiType toType,
final @NotNull ArgumentFixerActionFactory fixerActionFactory) {
super(list, i, toType, fixerActionFactory);
}
@NotNull
@Override
public String getText() {
if (myArgList.getExpressions().length == 1) {
return QuickFixBundle.message("wrap.array.to.arrays.as.list.single.parameter.text");
} else {
return QuickFixBundle.message("wrap.array.to.arrays.as.list.parameter.text", myIndex + 1);
}
}
public static class MyFixerActionFactory extends ArgumentFixerActionFactory {
@Nullable
@Override
protected PsiExpression getModifiedArgument(final PsiExpression expression, final PsiType toType) throws IncorrectOperationException {
final PsiType exprType = expression.getType();
if (!(exprType instanceof PsiArrayType && toType instanceof PsiClassType)) {
return null;
}
final PsiClass resolvedToType = ((PsiClassType)toType).resolve();
if (resolvedToType == null) {
return null;
}
final PsiClass javaUtilList = getJavaUtilList(expression);
if (javaUtilList == null || !InheritanceUtil.isInheritorOrSelf(javaUtilList, resolvedToType, true)) {
return null;
}
final PsiType[] parameters = ((PsiClassType)toType).getParameters();
final PsiType arrayComponentType = ((PsiArrayType)exprType).getComponentType();
if (!(parameters.length == 1 && parameters[0].equals(arrayComponentType))) {
return null;
}
final String rawNewExpression = String.format("java.util.Arrays.asList(%s)", expression.getText());
final Project project = expression.getProject();
final PsiExpression newExpression = JavaPsiFacade.getInstance(project).getElementFactory().createExpressionFromText(rawNewExpression, null);
return (PsiExpression)JavaCodeStyleManager.getInstance(project).shortenClassReferences(newExpression);
}
@Nullable
private static PsiClass getJavaUtilList(final PsiElement context) {
return JavaPsiFacade.getInstance(context.getProject()).findClass(CommonClassNames.JAVA_UTIL_LIST, context.getResolveScope());
}
@Override
public boolean areTypesConvertible(final PsiType exprType, final PsiType parameterType, final PsiElement context) {
return true;
}
@Override
public MethodArgumentFix createFix(final PsiExpressionList list, final int i, final PsiType toType) {
return new WrapArrayToArraysAsListFix(list, i, toType, this);
}
}
}

View File

@@ -0,0 +1,14 @@
// "Wrap 4th parameter using 'Arrays.asList'" "true"
import java.util.Arrays;
import java.util.List;
public class Test {
void list(int i, int j, int k, List<String> l, String s) {
}
void m(String[] a) {
list(1, 2, 3, Arrays.asList(a), "asd");
}
}

View File

@@ -0,0 +1,14 @@
// "Wrap using 'Arrays.asList'" "true"
import java.util.Arrays;
import java.util.List;
public class Test {
void list(List<String> l) {
}
void m(String[] a) {
list(Arrays.asList(a));
}
}

View File

@@ -0,0 +1,13 @@
// "Wrap 4th parameter using 'Arrays.asList'" "true"
import java.util.List;
public class Test {
void list(int i, int j, int k, List<String> l, String s) {
}
void m(String[] a) {
list(1, 2, 3, a<caret>, "asd");
}
}

View File

@@ -0,0 +1,13 @@
// "Wrap using 'Arrays.asList'" "false"
import java.util.LinkedList;
public class Test {
void list(LinkedList<String> l) {
}
void m(String[] a) {
list(a<caret>);
}
}

View File

@@ -0,0 +1,13 @@
// "Wrap using 'Arrays.asList'" "false"
import java.util.LinkedList;
public class Test {
void list(LinkedList<String> l) {
}
void m(Long[] a) {
list(a<caret>);
}
}

View File

@@ -0,0 +1,13 @@
// "Wrap using 'Arrays.asList'" "false"
import java.util.List;
public class Test {
void list(List<String> l) {
}
void m(String[] a) {
list(a<caret>);
}
}

View File

@@ -0,0 +1,30 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.codeInsight.daemon.quickFix;
/**
* @author Dmitry Batkovich
*/
public class WrapArrayToArraysAsListFixTest extends LightQuickFixParameterizedTestCase {
public void test() throws Exception {
doAllTests();
}
@Override
protected String getBasePath() {
return "/codeInsight/daemonCodeAnalyzer/quickFix/wrapArrayToArraysAsList";
}
}

View File

@@ -274,4 +274,7 @@ remove.redundant.arguments.family=Remove redundant arguments
replace.with.list.access.text=Replace with list access
add.qualifier=Add qualifier
add.qualifier.original.class.chooser.title=Original class
add.qualifier.original.class.chooser.title=Original class
wrap.array.to.arrays.as.list.parameter.text=Wrap {0, choice, 1#1st|2#2nd|3#3rd|4#{0,number}th} parameter using ''Arrays.asList''
wrap.array.to.arrays.as.list.single.parameter.text=Wrap using ''Arrays.asList''