IDEA-114160 (Inspection "Class with too many methods" may ignore overridden methods)

This commit is contained in:
Bas Leijdekkers
2013-10-01 12:31:45 +02:00
parent eb1101f06d
commit ce60cb6ee0
12 changed files with 104 additions and 125 deletions
@@ -662,7 +662,6 @@ thrown.exceptions.per.method.display.name=Method with too many exceptions declar
public.static.array.field.display.name='public static' array field
await.not.in.loop.display.name='await()' not in loop
method.names.differ.only.by.case.display.name=Method names differing only by case
method.names.differ.only.by.case.ignore.override.option=Ignore if method is override of super method
unsecure.random.number.generation.display.name=Insecure random number generation
parameters.per.method.display.name=Method with too many parameters
parameters.per.constructor.display.name=Constructor with too many parameters
@@ -1791,7 +1790,7 @@ ignore.single.field.static.imports.option=Ignore single &field static imports
ignore.single.method.static.imports.option=Ignore single &method static imports
ignore.methods.with.boolean.return.type.option=Ignore methods with &Boolean return type
ignore.boolean.methods.in.an.interface.option=Ignore boolean methods in an @&interface
ignore.methods.overriding.super.method=Ignore methods &overriding a super method
ignore.methods.overriding.super.method=Ignore methods &overriding/implementing a super method
ignored.io.resource.types=Ignored I/O resource types
choose.io.resource.type.to.ignore=Choose I/O resource type to ignore
ignore.accesses.from.the.same.class=Ignore accesses from the same class
@@ -1900,7 +1899,6 @@ 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 2003-2010 Dave Griffith, Bas Leijdekkers
* Copyright 2003-2013 Dave Griffith, Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -23,6 +23,7 @@ import com.intellij.util.ui.UIUtil;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.psiutils.MethodUtils;
import org.jetbrains.annotations.NotNull;
import javax.swing.*;
@@ -38,6 +39,9 @@ public class MethodCountInspection extends BaseInspection {
@SuppressWarnings({"PublicField"})
public boolean ignoreGettersAndSetters = false;
@SuppressWarnings("PublicField")
public boolean ignoreOverridingMethods = false;
@Override
@NotNull
public String getID() {
@@ -53,8 +57,7 @@ public class MethodCountInspection extends BaseInspection {
@Override
public JComponent createOptionsPanel() {
final JComponent panel = new JPanel(new GridBagLayout());
final Component label = new JLabel(
InspectionGadgetsBundle.message("method.count.limit.option"));
final Component label = new JLabel(InspectionGadgetsBundle.message("method.count.limit.option"));
final JFormattedTextField valueField = prepareNumberEditor("m_limit");
final GridBagConstraints constraints = new GridBagConstraints();
@@ -68,18 +71,23 @@ public class MethodCountInspection extends BaseInspection {
constraints.insets.right = 0;
panel.add(valueField, constraints);
final CheckBox gettersSettersCheckBox = new CheckBox(
InspectionGadgetsBundle.message(
"method.count.ignore.getters.setters.option"),
final CheckBox gettersSettersCheckBox = new CheckBox(InspectionGadgetsBundle.message("method.count.ignore.getters.setters.option"),
this, "ignoreGettersAndSetters");
constraints.gridx = 0;
constraints.gridy = 1;
constraints.weighty = 1.0;
constraints.gridwidth = 2;
constraints.anchor = GridBagConstraints.NORTHWEST;
constraints.anchor = GridBagConstraints.WEST;
panel.add(gettersSettersCheckBox, constraints);
final CheckBox overridingMethodCheckBox =
new CheckBox(InspectionGadgetsBundle.message("ignore.methods.overriding.super.method"), this, "ignoreOverridingMethods");
constraints.weighty = 1.0;
constraints.gridy = 2;
constraints.anchor = GridBagConstraints.NORTHWEST;
panel.add(overridingMethodCheckBox, constraints);
return panel;
}
@@ -88,8 +96,7 @@ public class MethodCountInspection extends BaseInspection {
@NotNull
public String buildErrorString(Object... infos) {
final Integer count = (Integer)infos[0];
return InspectionGadgetsBundle.message(
"too.many.methods.problem.descriptor", count);
return InspectionGadgetsBundle.message("too.many.methods.problem.descriptor", count);
}
@Override
@@ -101,7 +108,6 @@ public class MethodCountInspection extends BaseInspection {
@Override
public void visitClass(@NotNull PsiClass aClass) {
// note: no call to super
final int methodCount = calculateTotalMethodCount(aClass);
if (methodCount <= m_limit) {
return;
@@ -117,8 +123,12 @@ public class MethodCountInspection extends BaseInspection {
continue;
}
if (ignoreGettersAndSetters) {
if (PropertyUtil.isSimpleGetter(method) ||
PropertyUtil.isSimpleSetter(method)) {
if (PropertyUtil.isSimpleGetter(method) || PropertyUtil.isSimpleSetter(method)) {
continue;
}
}
if (ignoreOverridingMethods) {
if (MethodUtils.hasSuper(method)) {
continue;
}
}
@@ -19,13 +19,13 @@ import com.intellij.codeInspection.ProblemDescriptor;
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.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.InspectionGadgetsFix;
import com.siyeh.ig.psiutils.LibraryUtil;
import com.siyeh.ig.psiutils.MethodUtils;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
@@ -57,7 +57,7 @@ public class MethodCanBeVariableArityMethodInspection extends BaseInspection {
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"),
panel.addCheckbox(InspectionGadgetsBundle.message("ignore.methods.overriding.super.method"),
"ignoreOverridingMethods");
return panel;
}
@@ -149,7 +149,7 @@ public class MethodCanBeVariableArityMethodInspection extends BaseInspection {
if (LibraryUtil.isOverrideOfLibraryMethod(method)) {
return;
}
if (ignoreOverridingMethods && SuperMethodsSearch.search(method, null, true, false).findFirst() != null) {
if (ignoreOverridingMethods && MethodUtils.hasSuper(method)) {
return;
}
registerMethodError(method);
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2012 Dave Griffith, Bas Leijdekkers
* Copyright 2003-2013 Dave Griffith, Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -54,7 +54,7 @@ public class MethodNamesDifferOnlyByCaseInspection extends BaseInspection {
@Override
public JComponent createOptionsPanel() {
return new SingleCheckboxOptionsPanel(InspectionGadgetsBundle.message("method.names.differ.only.by.case.ignore.override.option"),
return new SingleCheckboxOptionsPanel(InspectionGadgetsBundle.message("ignore.methods.overriding.super.method"),
this, "ignoreIfMethodIsOverride");
}
@@ -9,7 +9,7 @@ language level of 5.0 or higher.
<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.
Use the second checkbox below to ignore methods overriding or implementing a method from a superclass.
<p>
<small>New in 10.5</small>
</body>
@@ -7,7 +7,9 @@ refactored into multiple smaller classes.
<p>
Use the field below to specify the maximum number of methods a class is allowed to have.
<p>
Use the checkbox below to specify that simple getters and setters should be ignored in the method count.
Use the first checkbox below to ignore simple getters and setters in the method count.
<p>
Use the second checkbox below to ignore methods overriding or implementing a method from a superclass
<p>
</body>
@@ -3,8 +3,7 @@
Reports on cases where multiple methods of a class have names which differ only by
case. Such method names may be very confusing.
<!-- tooltip end -->
<p>Use the checkbox below to have this inspection ignore methods which are overrides or implementations of
super methods.
<p>Use the checkbox below to ignore methods overriding or implementing a method from a superclass.
<p>
</body>
@@ -1,16 +0,0 @@
package com.siyeh.igtest.classmetrics;
public class ConstructorCountInspection
{
public ConstructorCountInspection(int i){}
public ConstructorCountInspection(boolean ch){}
public ConstructorCountInspection(char b){}
public ConstructorCountInspection(float d){}
public ConstructorCountInspection(double f){}
public ConstructorCountInspection(long s){}
}
@@ -1,84 +0,0 @@
package com.siyeh.igtest.classmetrics;
public class MethodCountInspection
{
public void aaa(){}
public void bbb(){}
public void ccc(){}
public void ddd(){}
public void eee(){}
public void fff(){}
public void ggg(){}
public void hhh(){}
public void iii(){}
public void jjj(){}
public void kkk(){}
public void lll(){}
public void mmm(){}
public void nnn(){}
public void ooo(){}
public void ppp(){}
public void qqq(){}
public void rrr(){}
public void sss(){}
public void ttt(){}
public void aaa2(){}
public void bbb2(){}
public void ccc2(){}
public void ddd2(){}
public void eee2(){}
public void fff2(){}
public void ggg2(){}
public void hhh2(){}
public void iii2(){}
public void jjj2(){}
public void kkk2(){}
public void lll2(){}
public void mmm2(){}
public void nnn2(){}
public void ooo2(){}
public void ppp2(){}
public void qqq2(){}
public void rrr2(){}
public void sss2(){}
public void ttt2(){}
}
@@ -0,0 +1,48 @@
package com.siyeh.igtest.classmetrics.method_count;
public class MethodCount {
public void one() {}
public void two() {}
public void three() {}
public void four() {}
public void five() {}
public void six() {}
}
class NotTooMany {
public void one() {}
public void two() {}
public void three() {}
public void four() {}
public void five() {}
}
class SuperMethods {
public void one() {}
public void two() {}
public void three() {}
@Override
public int hashCode() {
return super.hashCode();
}
@Override
public boolean equals(Object obj) {
return super.equals(obj);
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
@Override
public String toString() {
return super.toString();
}
@Override
protected void finalize() throws Throwable {
super.finalize();
}
}
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>MethodCount.java</file>
<line>3</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Class with too many methods</problem_class>
<description>&lt;code&gt;MethodCount&lt;/code&gt; has too many methods (method count = 6) #loc</description>
</problem>
</problems>
@@ -0,0 +1,13 @@
package com.siyeh.ig.classmetrics;
import com.siyeh.ig.IGInspectionTestCase;
public class MethodCountInspectionTest extends IGInspectionTestCase {
public void test() throws Exception {
final MethodCountInspection tool = new MethodCountInspection();
tool.m_limit = 5;
tool.ignoreOverridingMethods = true;
doTest("com/siyeh/igtest/classmetrics/method_count", tool);
}
}