From 34c2f5ca926f1aacaecdf4b1a1c88304b89020a7 Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Fri, 19 May 2017 18:06:02 +0200 Subject: [PATCH] IPP: new "Replace with 'Arrays.asList()'" intention (IDEA-167803) --- .../src/META-INF/IntentionPowerPack.xml | 5 ++ .../siyeh/IntentionPowerPackBundle.properties | 2 + .../ReplaceWithArraysAsListIntention.java | 60 +++++++++++++++++++ .../after.java.template | 7 +++ .../before.java.template | 7 +++ .../description.html | 5 ++ .../ReplaceWithArraysAsListIntentionTest.java | 60 +++++++++++++++++++ 7 files changed, 146 insertions(+) create mode 100644 plugins/IntentionPowerPak/src/com/siyeh/ipp/collections/ReplaceWithArraysAsListIntention.java create mode 100644 plugins/IntentionPowerPak/src/intentionDescriptions/ReplaceWithArraysAsListIntention/after.java.template create mode 100644 plugins/IntentionPowerPak/src/intentionDescriptions/ReplaceWithArraysAsListIntention/before.java.template create mode 100644 plugins/IntentionPowerPak/src/intentionDescriptions/ReplaceWithArraysAsListIntention/description.html create mode 100644 plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/collections/ReplaceWithArraysAsListIntentionTest.java diff --git a/plugins/IntentionPowerPak/src/META-INF/IntentionPowerPack.xml b/plugins/IntentionPowerPak/src/META-INF/IntentionPowerPack.xml index 2918ec52f9b3..ef1df657424b 100644 --- a/plugins/IntentionPowerPak/src/META-INF/IntentionPowerPack.xml +++ b/plugins/IntentionPowerPak/src/META-INF/IntentionPowerPack.xml @@ -516,5 +516,10 @@ com.siyeh.IntentionPowerPackBundle intention.category.other + + com.siyeh.ipp.collections.ReplaceWithArraysAsListIntention + com.siyeh.IntentionPowerPackBundle + intention.category.other + diff --git a/plugins/IntentionPowerPak/src/com/siyeh/IntentionPowerPackBundle.properties b/plugins/IntentionPowerPak/src/com/siyeh/IntentionPowerPackBundle.properties index 29f54d3b2858..d075103467c2 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/IntentionPowerPackBundle.properties +++ b/plugins/IntentionPowerPak/src/com/siyeh/IntentionPowerPackBundle.properties @@ -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 '||' diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/collections/ReplaceWithArraysAsListIntention.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/collections/ReplaceWithArraysAsListIntention.java new file mode 100644 index 000000000000..f0cf55ea4c39 --- /dev/null +++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/collections/ReplaceWithArraysAsListIntention.java @@ -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()); + } +} diff --git a/plugins/IntentionPowerPak/src/intentionDescriptions/ReplaceWithArraysAsListIntention/after.java.template b/plugins/IntentionPowerPak/src/intentionDescriptions/ReplaceWithArraysAsListIntention/after.java.template new file mode 100644 index 000000000000..f7cef35cdc94 --- /dev/null +++ b/plugins/IntentionPowerPak/src/intentionDescriptions/ReplaceWithArraysAsListIntention/after.java.template @@ -0,0 +1,7 @@ +import java.util.*; + +public class X { + List f() { + return Arrays.asList(); + } +} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/src/intentionDescriptions/ReplaceWithArraysAsListIntention/before.java.template b/plugins/IntentionPowerPak/src/intentionDescriptions/ReplaceWithArraysAsListIntention/before.java.template new file mode 100644 index 000000000000..96e3785f05f1 --- /dev/null +++ b/plugins/IntentionPowerPak/src/intentionDescriptions/ReplaceWithArraysAsListIntention/before.java.template @@ -0,0 +1,7 @@ +import java.util.*; + +public class X { + List f() { + return Collections.emptyList(); + } +} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/src/intentionDescriptions/ReplaceWithArraysAsListIntention/description.html b/plugins/IntentionPowerPak/src/intentionDescriptions/ReplaceWithArraysAsListIntention/description.html new file mode 100644 index 000000000000..1a284606d35c --- /dev/null +++ b/plugins/IntentionPowerPak/src/intentionDescriptions/ReplaceWithArraysAsListIntention/description.html @@ -0,0 +1,5 @@ + + +Replaces a Collections.emptyList() or Collections.singletonList() call with a call to Arrays.asList(). + + \ No newline at end of file diff --git a/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/collections/ReplaceWithArraysAsListIntentionTest.java b/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/collections/ReplaceWithArraysAsListIntentionTest.java new file mode 100644 index 000000000000..2b4bf2f57e85 --- /dev/null +++ b/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/collections/ReplaceWithArraysAsListIntentionTest.java @@ -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 f() {" + + " return Collections.emptyList(\"text\"/*_Replace with 'Arrays.asList()'*/);" + + " }" + + "}", + + "import java.util.*;" + + "class X {" + + " List f() {" + + " return Arrays.asList(\"text\");" + + " }" + + "}" + ); + } + + public void testReplaceSingletonList() { + doTest( + "import java.util.*;" + + "class X {" + + " List f() {" + + " return Collections.singletonList(\"text\"/*_Replace with 'Arrays.asList()'*/);" + + " }" + + "}", + + "import java.util.*;" + + "class X {" + + " List f() {" + + " return Arrays.asList(\"text\");" + + " }" + + "}" + ); + } +}