From 81ae180a4fe63faf1f6a26483eb7b90addaec4ac Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Tue, 29 Nov 2016 15:55:26 +0100 Subject: [PATCH] IG/IPP: make "Varargs method" inspection INFORMATION-level and remove intention --- .../src/META-INF/InspectionGadgets.xml | 2 +- .../VarargParameter.html | 5 +- .../src/META-INF/IntentionPowerPack.xml | 6 - .../siyeh/IntentionPowerPackBundle.properties | 4 - ...onvertVarargParameterToArrayIntention.java | 128 ------------------ ...onvertVarargParameterToArrayPredicate.java | 38 ------ .../after.java.template | 8 -- .../before.java.template | 8 -- .../description.html | 7 - 9 files changed, 5 insertions(+), 201 deletions(-) delete mode 100644 plugins/IntentionPowerPak/src/com/siyeh/ipp/varargs/ConvertVarargParameterToArrayIntention.java delete mode 100644 plugins/IntentionPowerPak/src/com/siyeh/ipp/varargs/ConvertVarargParameterToArrayPredicate.java delete mode 100644 plugins/IntentionPowerPak/src/intentionDescriptions/ConvertVarargParameterToArrayIntention/after.java.template delete mode 100644 plugins/IntentionPowerPak/src/intentionDescriptions/ConvertVarargParameterToArrayIntention/before.java.template delete mode 100644 plugins/IntentionPowerPak/src/intentionDescriptions/ConvertVarargParameterToArrayIntention/description.html diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/META-INF/InspectionGadgets.xml b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/META-INF/InspectionGadgets.xml index 8572cf3de092..5a8be077574e 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/META-INF/InspectionGadgets.xml +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/META-INF/InspectionGadgets.xml @@ -1378,7 +1378,7 @@ level="WARNING" implementationClass="com.siyeh.ig.jdk.ForeachStatementInspection"/> diff --git a/plugins/InspectionGadgets/src/inspectionDescriptions/VarargParameter.html b/plugins/InspectionGadgets/src/inspectionDescriptions/VarargParameter.html index 14fc3d02027b..8d130c162678 100644 --- a/plugins/InspectionGadgets/src/inspectionDescriptions/VarargParameter.html +++ b/plugins/InspectionGadgets/src/inspectionDescriptions/VarargParameter.html @@ -1,7 +1,10 @@ -Reports methods taking variable numbers of parameters. +Reports methods taking a variable number of arguments, also known as varargs methods. Such methods are not supported under Java 1.4 or earlier JVMs. +The quickfix of this inspection replaces a variable argument +parameter with the equivalent array parameter. Relevant arguments in calls to +the method are wrapped in an array initializer expression.

diff --git a/plugins/IntentionPowerPak/src/META-INF/IntentionPowerPack.xml b/plugins/IntentionPowerPak/src/META-INF/IntentionPowerPack.xml index 71e3753cc96a..9eaf239a1e07 100644 --- a/plugins/IntentionPowerPak/src/META-INF/IntentionPowerPack.xml +++ b/plugins/IntentionPowerPak/src/META-INF/IntentionPowerPack.xml @@ -193,12 +193,6 @@ intention.category.declaration - - com.siyeh.ipp.varargs.ConvertVarargParameterToArrayIntention - com.siyeh.IntentionPowerPackBundle - intention.category.declaration - - com.siyeh.ipp.varargs.WrapVarargArgumentsWithExplicitArrayIntention com.siyeh.IntentionPowerPackBundle diff --git a/plugins/IntentionPowerPak/src/com/siyeh/IntentionPowerPackBundle.properties b/plugins/IntentionPowerPak/src/com/siyeh/IntentionPowerPackBundle.properties index fdbbc70eaad6..f71af0584e7b 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/IntentionPowerPackBundle.properties +++ b/plugins/IntentionPowerPak/src/com/siyeh/IntentionPowerPackBundle.properties @@ -106,10 +106,6 @@ convert.interface.to.class.intention.family.name=Convert Interface to Class replace.on.demand.import.intention.name=Replace with single class imports replace.on.demand.import.intention.family.name=Replace On Demand Import with Single Class Imports swap.method.call.arguments.intention.family.name=Swap Method Call Arguments -make.method.varargs.intention.name=Convert to variable argument method -make.method.varargs.intention.family.name=Convert Method to Variable Argument Method -convert.vararg.parameter.to.array.intention.name=Convert variable argument parameter to array -convert.vararg.parameter.to.array.intention.family.name=Convert Variable Argument Parameter to Array Parameter replace.operator.assignment.with.assignment.intention.family.name=Replace Operator Assignment with Assignment convert.catch.to.throws.intention.name=Replace 'catch' section with 'throws' declaration convert.catch.to.throws.intention.family.name=Replace Catch Section with Throws Declaration diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/varargs/ConvertVarargParameterToArrayIntention.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/varargs/ConvertVarargParameterToArrayIntention.java deleted file mode 100644 index f6f35af01f90..000000000000 --- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/varargs/ConvertVarargParameterToArrayIntention.java +++ /dev/null @@ -1,128 +0,0 @@ -/* - * Copyright 2006-2011 Bas Leijdekkers - * - * 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.varargs; - -import com.intellij.openapi.project.Project; -import com.intellij.psi.*; -import com.intellij.psi.codeStyle.CodeStyleManager; -import com.intellij.psi.codeStyle.JavaCodeStyleManager; -import com.intellij.psi.search.searches.ReferencesSearch; -import com.intellij.util.IncorrectOperationException; -import com.intellij.util.Query; -import com.siyeh.ipp.base.Intention; -import com.siyeh.ipp.base.PsiElementPredicate; -import org.jetbrains.annotations.NonNls; -import org.jetbrains.annotations.NotNull; - -public class ConvertVarargParameterToArrayIntention extends Intention { - - @NotNull - protected PsiElementPredicate getElementPredicate() { - return new ConvertVarargParameterToArrayPredicate(); - } - - protected void processIntention(@NotNull PsiElement element) - throws IncorrectOperationException { - final PsiParameterList parameterList = (PsiParameterList)element; - convertVarargsToArray(parameterList); - } - - private static void convertVarargsToArray(PsiParameterList parameterList) - throws IncorrectOperationException { - final PsiParameter[] parameters = parameterList.getParameters(); - if (parameters.length == 0) { - return; - } - final PsiParameter lastParameter = parameters[parameters.length - 1]; - if (lastParameter == null || !lastParameter.isVarArgs()) { - return; - } - final PsiEllipsisType type = - (PsiEllipsisType)lastParameter.getType(); - final PsiMethod method = (PsiMethod)parameterList.getParent(); - final Query query = ReferencesSearch.search(method); - final PsiType componentType = type.getComponentType(); - final String typeText = componentType.getCanonicalText(); - final int parameterIndex = - parameterList.getParameterIndex(lastParameter); - for (PsiReference reference : query) { - final PsiElement referenceElement = reference.getElement(); - if (!(referenceElement instanceof PsiReferenceExpression)) { - continue; - } - final PsiReferenceExpression referenceExpression = - (PsiReferenceExpression)referenceElement; - final PsiMethodCallExpression methodCallExpression = - (PsiMethodCallExpression)referenceExpression.getParent(); - modifyCall(methodCallExpression, typeText, parameterIndex); - } - final PsiType arrayType = type.toArrayType(); - final Project project = lastParameter.getProject(); - final PsiElementFactory factory = - JavaPsiFacade.getElementFactory(project); - final PsiTypeElement newTypeElement = - factory.createTypeElement(arrayType); - final PsiTypeElement typeElement = - lastParameter.getTypeElement(); - typeElement.replace(newTypeElement); - } - - public static void modifyCall(PsiMethodCallExpression methodCallExpression, - String arrayTypeText, - int indexOfFirstVarargArgument) - throws IncorrectOperationException { - final PsiExpressionList argumentList = - methodCallExpression.getArgumentList(); - final PsiExpression[] arguments = argumentList.getExpressions(); - @NonNls final StringBuilder builder = new StringBuilder("new "); - builder.append(arrayTypeText); - builder.append("[]{"); - if (arguments.length > indexOfFirstVarargArgument) { - final PsiExpression firstArgument = - arguments[indexOfFirstVarargArgument]; - final String firstArgumentText = firstArgument.getText(); - builder.append(firstArgumentText); - for (int i = indexOfFirstVarargArgument + 1; - i < arguments.length; i++) { - builder.append(','); - builder.append(arguments[i].getText()); - } - } - builder.append('}'); - final Project project = methodCallExpression.getProject(); - final PsiElementFactory factory = - JavaPsiFacade.getElementFactory(project); - final PsiExpression arrayExpression = - factory.createExpressionFromText(builder.toString(), - methodCallExpression); - if (arguments.length > indexOfFirstVarargArgument) { - final PsiExpression firstArgument = - arguments[indexOfFirstVarargArgument]; - argumentList.deleteChildRange(firstArgument, - arguments[arguments.length - 1]); - argumentList.add(arrayExpression); - } - else { - argumentList.add(arrayExpression); - } - final JavaCodeStyleManager javaCodeStyleManager = - JavaCodeStyleManager.getInstance(project); - javaCodeStyleManager.shortenClassReferences(argumentList); - final CodeStyleManager codeStyleManager = - CodeStyleManager.getInstance(project); - codeStyleManager.reformat(argumentList); - } -} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/varargs/ConvertVarargParameterToArrayPredicate.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/varargs/ConvertVarargParameterToArrayPredicate.java deleted file mode 100644 index 8c0b0007f6dc..000000000000 --- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/varargs/ConvertVarargParameterToArrayPredicate.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright 2006-2011 Bas Leijdekkers - * - * 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.varargs; - -import com.intellij.psi.PsiElement; -import com.intellij.psi.PsiMethod; -import com.intellij.psi.PsiParameterList; -import com.siyeh.ipp.base.PsiElementPredicate; -import org.jetbrains.annotations.NotNull; - -class ConvertVarargParameterToArrayPredicate - implements PsiElementPredicate { - - public boolean satisfiedBy(@NotNull PsiElement element) { - if (!(element instanceof PsiParameterList)) { - return false; - } - final PsiElement parent = element.getParent(); - if (!(parent instanceof PsiMethod)) { - return false; - } - final PsiMethod method = (PsiMethod)parent; - return method.isVarArgs(); - } -} diff --git a/plugins/IntentionPowerPak/src/intentionDescriptions/ConvertVarargParameterToArrayIntention/after.java.template b/plugins/IntentionPowerPak/src/intentionDescriptions/ConvertVarargParameterToArrayIntention/after.java.template deleted file mode 100644 index 967c4540ec70..000000000000 --- a/plugins/IntentionPowerPak/src/intentionDescriptions/ConvertVarargParameterToArrayIntention/after.java.template +++ /dev/null @@ -1,8 +0,0 @@ -public class X { - void f(int i, String[] strings) { - } - - void g(String string) { - f(1, new String[]{string}); - } -} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/src/intentionDescriptions/ConvertVarargParameterToArrayIntention/before.java.template b/plugins/IntentionPowerPak/src/intentionDescriptions/ConvertVarargParameterToArrayIntention/before.java.template deleted file mode 100644 index b9f1d2b0e53d..000000000000 --- a/plugins/IntentionPowerPak/src/intentionDescriptions/ConvertVarargParameterToArrayIntention/before.java.template +++ /dev/null @@ -1,8 +0,0 @@ -public class X { - void f(int i, String... strings) { - } - - void g(String string) { - f(1, string); - } -} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/src/intentionDescriptions/ConvertVarargParameterToArrayIntention/description.html b/plugins/IntentionPowerPak/src/intentionDescriptions/ConvertVarargParameterToArrayIntention/description.html deleted file mode 100644 index c3ad1b26c08d..000000000000 --- a/plugins/IntentionPowerPak/src/intentionDescriptions/ConvertVarargParameterToArrayIntention/description.html +++ /dev/null @@ -1,7 +0,0 @@ - - -This intention replaces a variable argument -parameter with the equivalent array parameter. Relevant arguments in calls to -this method are wrapped in an array initializer expression. - -