IG/IPP: make "Varargs method" inspection INFORMATION-level and remove intention

This commit is contained in:
Bas Leijdekkers
2016-11-29 20:18:52 +01:00
parent 4a26c44907
commit 81ae180a4f
9 changed files with 5 additions and 201 deletions
@@ -1378,7 +1378,7 @@
level="WARNING" implementationClass="com.siyeh.ig.jdk.ForeachStatementInspection"/>
<localInspection groupPath="Java" language="JAVA" suppressId="VariableArgumentMethod" shortName="VarargParameter" bundle="com.siyeh.InspectionGadgetsBundle"
key="variable.argument.method.display.name" groupBundle="messages.InspectionsBundle"
groupKey="group.names.java.language.level.issues" enabledByDefault="false" level="WARNING"
groupKey="group.names.java.language.level.issues" enabledByDefault="true" level="INFORMATION"
implementationClass="com.siyeh.ig.jdk.VarargParameterInspection"/>
<!--group.names.junit.issues-->
@@ -1,7 +1,10 @@
<html>
<body>
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.
<!-- tooltip end -->
<p>
@@ -193,12 +193,6 @@
<categoryKey>intention.category.declaration</categoryKey>
</intentionAction>
<intentionAction>
<className>com.siyeh.ipp.varargs.ConvertVarargParameterToArrayIntention</className>
<bundleName>com.siyeh.IntentionPowerPackBundle</bundleName>
<categoryKey>intention.category.declaration</categoryKey>
</intentionAction>
<intentionAction>
<className>com.siyeh.ipp.varargs.WrapVarargArgumentsWithExplicitArrayIntention</className>
<bundleName>com.siyeh.IntentionPowerPackBundle</bundleName>
@@ -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
@@ -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<PsiReference> 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);
}
}
@@ -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();
}
}
@@ -1,8 +0,0 @@
public class X {
void f(int i, String[] strings) {
}
void g(String string) {
f(1, new String[]{string});
}
}
@@ -1,8 +0,0 @@
public class X {
void f(<spot>int i, String... strings</spot>) {
}
void g(String string) {
f(1, string);
}
}
@@ -1,7 +0,0 @@
<html>
<body>
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.
</body>
</html>