IPP: new "Replace with 'Arrays.asList()'" intention (IDEA-167803)

This commit is contained in:
Bas Leijdekkers
2017-05-19 19:12:56 +02:00
parent 2c1cbcf6a7
commit 34c2f5ca92
7 changed files with 146 additions and 0 deletions
@@ -516,5 +516,10 @@
<bundleName>com.siyeh.IntentionPowerPackBundle</bundleName>
<categoryKey>intention.category.other</categoryKey>
</intentionAction>
<intentionAction>
<className>com.siyeh.ipp.collections.ReplaceWithArraysAsListIntention</className>
<bundleName>com.siyeh.IntentionPowerPackBundle</bundleName>
<categoryKey>intention.category.other</categoryKey>
</intentionAction>
</extensions>
</idea-plugin>
@@ -158,6 +158,8 @@ objects.require.non.null.intention.family.name=Use 'java.util.Objects.requireNon
postfix.prefix.intention.family.name=Flip prefix/postfix expression
replace.conditional.with.boolean.expression.intention.family.name=Replace conditional expression with boolean expression
replace.conditional.with.boolean.expression.intention.name=Replace '?:' with boolean expression
replace.with.arrays.as.list.intention.name=Replace with 'Arrays.asList()'
replace.with.arrays.as.list.intention.family.name=Replace with 'Arrays.asList()'
#hand made
demorgans.intention.name1=Replace '\\&\\&' with '||'
@@ -0,0 +1,60 @@
/*
* Copyright 2000-2017 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.siyeh.ipp.collections;
import com.intellij.psi.*;
import com.siyeh.ig.PsiReplacementUtil;
import com.siyeh.ipp.base.Intention;
import com.siyeh.ipp.base.PsiElementPredicate;
import org.jetbrains.annotations.NotNull;
/**
* @author Bas Leijdekkers
*/
public class ReplaceWithArraysAsListIntention extends Intention {
@NotNull
@Override
protected PsiElementPredicate getElementPredicate() {
return e -> {
if (!(e instanceof PsiMethodCallExpression)) {
return false;
}
final PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression)e;
final PsiMethod method = methodCallExpression.resolveMethod();
if (method == null) {
return false;
}
final String name = method.getName();
if (!name.equals("emptyList") && !name.equals("singletonList")) {
return false;
}
final PsiClass aClass = method.getContainingClass();
if (aClass == null) {
return false;
}
final String qualifiedName = aClass.getQualifiedName();
return qualifiedName != null && qualifiedName.equals("java.util.Collections");
};
}
@Override
protected void processIntention(@NotNull PsiElement element) {
final PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression)element;
final PsiExpressionList argumentList = methodCallExpression.getArgumentList();
PsiReplacementUtil.replaceExpressionAndShorten(methodCallExpression, "java.util.Arrays.asList" + argumentList.getText());
}
}
@@ -0,0 +1,7 @@
import java.util.*;
public class X {
List<String> f() {
return Arrays.asList();
}
}
@@ -0,0 +1,7 @@
import java.util.*;
public class X {
List<String> f() {
return <spot>Collections.emptyList()</spot>;
}
}
@@ -0,0 +1,5 @@
<html>
<body>
Replaces a <b>Collections.emptyList()</b> or <b>Collections.singletonList()</b> call with a call to <b>Arrays.asList()</b>.
</body>
</html>
@@ -0,0 +1,60 @@
/*
* Copyright 2000-2017 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.siyeh.ipp.collections;
import com.siyeh.ipp.IPPTestCase;
/**
* @author Bas Leijdekkers
*/
public class ReplaceWithArraysAsListIntentionTest extends IPPTestCase {
public void testBrokenCode() {
doTest(
"import java.util.*;" +
"class X {" +
" List<String> f() {" +
" return Collections.emptyList(\"text\"/*_Replace with 'Arrays.asList()'*/);" +
" }" +
"}",
"import java.util.*;" +
"class X {" +
" List<String> f() {" +
" return Arrays.asList(\"text\");" +
" }" +
"}"
);
}
public void testReplaceSingletonList() {
doTest(
"import java.util.*;" +
"class X {" +
" List<String> f() {" +
" return Collections.singletonList(\"text\"/*_Replace with 'Arrays.asList()'*/);" +
" }" +
"}",
"import java.util.*;" +
"class X {" +
" List<String> f() {" +
" return Arrays.asList(\"text\");" +
" }" +
"}"
);
}
}