IDEA-97353 (Method can be variable arity method shall have an option to be ignored on methods implementing/overriding another method)

This commit is contained in:
Bas Leijdekkers
2013-03-08 11:31:32 +01:00
parent 02ee0fece5
commit 19803e7436
5 changed files with 42 additions and 34 deletions
@@ -1226,7 +1226,7 @@ object.notify.replace.quickfix=Replace with 'notifyAll()'
safe.lock.problem.descriptor=''{0}'' should be locked in front of a ''try'' block and unlocked in the corresponding ''finally'' block #loc
synchronized.method.problem.descriptor=Method ''{0}()'' declared <code>#ref</code> #loc
synchronized.method.include.option=Include native methods
synchronized.method.ignore.synchronized.super.option=Ignore overrides synchronized methods
synchronized.method.ignore.synchronized.super.option=Ignore methods overriding a synchronized method
synchronized.method.move.quickfix=Move synchronization into method
thread.run.replace.quickfix=Replace with 'start()'
volatile.field.problem.descriptor=Volatile field <code>#ref</code> of type ''{0}'' #loc
@@ -1897,6 +1897,7 @@ arrays.hash.code.quickfix=Replace with 'Arrays.hashCode()'
method.can.be.variable.arity.method.display.name=Method can be variable arity method
method.can.be.variable.arity.method.problem.descriptor=<code>#ref()</code> can be converted to variable arity method #loc
method.can.be.variable.arity.method.ignore.byte.short.option=<html>Ignore methods with a last parameter of type byte[] or short[]</html>
method.can.be.variable.arity.method.ignore.overriding.methods=Ignore methods overriding a super method
convert.to.variable.arity.method.quickfix=Convert to variable arity method
mismatched.string.builder.query.update.display.name=Mismatched query and update of StringBuilder
mismatched.string.builder.updated.problem.descriptor=Contents of {0} <code>#ref</code> are updated, but never queried #loc
@@ -1,5 +1,5 @@
/*
* Copyright 2011 Bas Leijdekkers
* Copyright 2011-2013 Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,11 +16,11 @@
package com.siyeh.ig.migration;
import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.codeInspection.ui.SingleCheckboxOptionsPanel;
import com.intellij.codeInspection.ui.MultipleCheckboxOptionsPanel;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.search.searches.SuperMethodsSearch;
import com.intellij.psi.util.PsiUtil;
import com.intellij.util.IncorrectOperationException;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
@@ -36,26 +36,30 @@ public class MethodCanBeVariableArityMethodInspection extends BaseInspection {
@SuppressWarnings({"PublicField"})
public boolean ignoreByteAndShortArrayParameters = false;
@SuppressWarnings("PublicField")
public boolean ignoreOverridingMethods = false;
@Nls
@NotNull
@Override
public String getDisplayName() {
return InspectionGadgetsBundle.message(
"method.can.be.variable.arity.method.display.name");
return InspectionGadgetsBundle.message("method.can.be.variable.arity.method.display.name");
}
@NotNull
@Override
protected String buildErrorString(Object... infos) {
return InspectionGadgetsBundle.message(
"method.can.be.variable.arity.method.problem.descriptor");
return InspectionGadgetsBundle.message("method.can.be.variable.arity.method.problem.descriptor");
}
@Override
public JComponent createOptionsPanel() {
return new SingleCheckboxOptionsPanel(InspectionGadgetsBundle.message(
"method.can.be.variable.arity.method.ignore.byte.short.option"),
this, "ignoreByteAndShortArrayParameters");
final MultipleCheckboxOptionsPanel panel = new MultipleCheckboxOptionsPanel(this);
panel.addCheckbox(InspectionGadgetsBundle.message("method.can.be.variable.arity.method.ignore.byte.short.option"),
"ignoreByteAndShortArrayParameters");
panel.addCheckbox(InspectionGadgetsBundle.message("method.can.be.variable.arity.method.ignore.overriding.methods"),
"ignoreOverridingMethods");
return panel;
}
@Override
@@ -63,19 +67,16 @@ public class MethodCanBeVariableArityMethodInspection extends BaseInspection {
return new MethodCanBeVariableArityMethodFix();
}
private static class MethodCanBeVariableArityMethodFix
extends InspectionGadgetsFix {
private static class MethodCanBeVariableArityMethodFix extends InspectionGadgetsFix {
@NotNull
@Override
public String getName() {
return InspectionGadgetsBundle.message(
"convert.to.variable.arity.method.quickfix");
return InspectionGadgetsBundle.message("convert.to.variable.arity.method.quickfix");
}
@Override
protected void doFix(Project project, ProblemDescriptor descriptor)
throws IncorrectOperationException {
protected void doFix(Project project, ProblemDescriptor descriptor) {
final PsiElement element = descriptor.getPsiElement();
final PsiElement parent = element.getParent();
if (!(parent instanceof PsiMethod)) {
@@ -87,19 +88,15 @@ public class MethodCanBeVariableArityMethodInspection extends BaseInspection {
return;
}
final PsiParameter[] parameters = parameterList.getParameters();
final PsiParameter lastParameter =
parameters[parameters.length - 1];
final PsiParameter lastParameter = parameters[parameters.length - 1];
final PsiType type = lastParameter.getType();
if (!(type instanceof PsiArrayType)) {
return;
}
final PsiArrayType arrayType = (PsiArrayType)type;
final PsiType componentType = arrayType.getComponentType();
final PsiElementFactory factory =
JavaPsiFacade.getElementFactory(project);
final PsiTypeElement newTypeElement =
factory.createTypeElementFromText(
componentType.getCanonicalText() + "...", method);
final PsiElementFactory factory = JavaPsiFacade.getElementFactory(project);
final PsiTypeElement newTypeElement = factory.createTypeElementFromText(componentType.getCanonicalText() + "...", method);
final PsiTypeElement typeElement = lastParameter.getTypeElement();
if (typeElement != null) {
typeElement.replace(newTypeElement);
@@ -112,8 +109,7 @@ public class MethodCanBeVariableArityMethodInspection extends BaseInspection {
return new MethodCanBeVariableArityMethodVisitor();
}
private class MethodCanBeVariableArityMethodVisitor
extends BaseInspectionVisitor {
private class MethodCanBeVariableArityMethodVisitor extends BaseInspectionVisitor {
@Override
public void visitMethod(PsiMethod method) {
@@ -126,8 +122,7 @@ public class MethodCanBeVariableArityMethodInspection extends BaseInspection {
return;
}
final PsiParameter[] parameters = parameterList.getParameters();
final PsiParameter lastParameter =
parameters[parameters.length - 1];
final PsiParameter lastParameter = parameters[parameters.length - 1];
final PsiType type = lastParameter.getType();
if (!(type instanceof PsiArrayType)) {
return;
@@ -142,14 +137,16 @@ public class MethodCanBeVariableArityMethodInspection extends BaseInspection {
return;
}
if (ignoreByteAndShortArrayParameters) {
if (PsiType.BYTE.equals(componentType) ||
PsiType.SHORT.equals(componentType)) {
if (PsiType.BYTE.equals(componentType) || PsiType.SHORT.equals(componentType)) {
return;
}
}
if (LibraryUtil.isOverrideOfLibraryMethod(method)) {
return;
}
if (ignoreOverridingMethods && SuperMethodsSearch.search(method, null, true, false).findFirst() != null) {
return;
}
registerMethodError(method);
}
}
@@ -7,6 +7,10 @@ This inspection only reports if the project or module is configured to use a
language level of 5.0 or higher.
<!-- tooltip end -->
<p>
Use the first checkbox below to not offer to convert byte[] or short[] parameters to vararg.
<p>
Use the second checkbox below to ignore methods overriding a method in a super class.
<p>
<small>New in 10.5, Powered by InspectionGadgets</small>
</body>
</html>
@@ -17,4 +17,11 @@ abstract class MyInputStream extends Reader {
public int read(char[] cbuf) throws IOException {
return super.read(cbuf);
}
}
class Sub extends MethodCanBeVariableArity {
@Override
public void convertMe(String[] ss) {
super.convertMe(ss);
}
}
@@ -5,10 +5,9 @@ import com.siyeh.ig.IGInspectionTestCase;
public class MethodCanBeVariableArityMethodInspectionTest extends IGInspectionTestCase {
public void test() throws Exception {
final MethodCanBeVariableArityMethodInspection tool =
new MethodCanBeVariableArityMethodInspection();
final MethodCanBeVariableArityMethodInspection tool = new MethodCanBeVariableArityMethodInspection();
tool.ignoreByteAndShortArrayParameters = true;
doTest("com/siyeh/igtest/migration/method_can_be_variable_arity_method",
tool);
tool.ignoreOverridingMethods = true;
doTest("com/siyeh/igtest/migration/method_can_be_variable_arity_method", tool);
}
}