mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-85579 (Inspections > Method metrics > Method with more then three negations: Add ignore assert)
This commit is contained in:
@@ -1278,6 +1278,7 @@ parameters.per.method.problem.descriptor=<code>#ref()</code> has too many parame
|
||||
parameters.per.constructor.problem.descriptor=<code>#ref()</code> has too many parameters (num parameters = {0}) #loc
|
||||
parameter.limit.option=Parameter limit:
|
||||
three.negations.per.method.ignore.option=Ignore negations in 'equals()' methods
|
||||
three.negations.per.method.ignore.assert.option=Ignore negations in 'assert' statements
|
||||
three.negations.per.method.problem.descriptor=<code>#ref</code> contains {0} negations #loc
|
||||
thrown.exceptions.per.method.problem.descriptor=<code>#ref</code> has too many exceptions declared (num exceptions = {0}) #loc
|
||||
thrown.exceptions.per.method.limit.option=Exceptions thrown limit:
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2003-2007 Dave Griffith
|
||||
* Copyright 2003-2012 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.
|
||||
@@ -21,8 +21,13 @@ import org.jetbrains.annotations.NotNull;
|
||||
|
||||
class NegationCountVisitor extends JavaRecursiveElementVisitor {
|
||||
|
||||
private final boolean myIgnoreInAssertStatements;
|
||||
private int m_count = 0;
|
||||
|
||||
public NegationCountVisitor(boolean ignoreInAssertStatements) {
|
||||
myIgnoreInAssertStatements = ignoreInAssertStatements;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitBinaryExpression(@NotNull PsiBinaryExpression expression) {
|
||||
super.visitBinaryExpression(expression);
|
||||
@@ -45,6 +50,15 @@ class NegationCountVisitor extends JavaRecursiveElementVisitor {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitAssertStatement(PsiAssertStatement statement) {
|
||||
final int count = m_count;
|
||||
super.visitAssertStatement(statement);
|
||||
if (myIgnoreInAssertStatements) {
|
||||
m_count = count;
|
||||
}
|
||||
}
|
||||
|
||||
public int getCount() {
|
||||
return m_count;
|
||||
}
|
||||
|
||||
+15
-7
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2003-2007 Dave Griffith, Bas Leijdekkers
|
||||
* Copyright 2003-2012 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.
|
||||
@@ -15,12 +15,12 @@
|
||||
*/
|
||||
package com.siyeh.ig.methodmetrics;
|
||||
|
||||
import com.intellij.codeInspection.ui.MultipleCheckboxOptionsPanel;
|
||||
import com.intellij.psi.PsiMethod;
|
||||
import com.siyeh.InspectionGadgetsBundle;
|
||||
import com.siyeh.ig.BaseInspection;
|
||||
import com.siyeh.ig.BaseInspectionVisitor;
|
||||
import com.siyeh.ig.psiutils.MethodUtils;
|
||||
import com.intellij.codeInspection.ui.SingleCheckboxOptionsPanel;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import javax.swing.*;
|
||||
@@ -32,24 +32,31 @@ public class ThreeNegationsPerMethodInspection extends BaseInspection {
|
||||
*/
|
||||
public boolean m_ignoreInEquals = true;
|
||||
|
||||
@SuppressWarnings("UnusedDeclaration")
|
||||
public boolean ignoreInAssert = false;
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public String getID() {
|
||||
return "MethodWithMoreThanThreeNegations";
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public String getDisplayName() {
|
||||
return InspectionGadgetsBundle.message(
|
||||
"three.negations.per.method.display.name");
|
||||
}
|
||||
|
||||
@Override
|
||||
public JComponent createOptionsPanel() {
|
||||
return new SingleCheckboxOptionsPanel(
|
||||
InspectionGadgetsBundle.message(
|
||||
"three.negations.per.method.ignore.option"),
|
||||
this, "m_ignoreInEquals");
|
||||
final MultipleCheckboxOptionsPanel panel = new MultipleCheckboxOptionsPanel(this);
|
||||
panel.addCheckbox(InspectionGadgetsBundle.message("three.negations.per.method.ignore.option"), "m_ignoreInEquals");
|
||||
panel.addCheckbox(InspectionGadgetsBundle.message("three.negations.per.method.ignore.assert.option"), "ignoreInAssert");
|
||||
return panel;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public String buildErrorString(Object... infos) {
|
||||
final Integer negationCount = (Integer)infos[0];
|
||||
@@ -57,6 +64,7 @@ public class ThreeNegationsPerMethodInspection extends BaseInspection {
|
||||
"three.negations.per.method.problem.descriptor", negationCount);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BaseInspectionVisitor buildVisitor() {
|
||||
return new ThreeNegationsPerMethodVisitor();
|
||||
}
|
||||
@@ -69,7 +77,7 @@ public class ThreeNegationsPerMethodInspection extends BaseInspection {
|
||||
if (method.getNameIdentifier() == null) {
|
||||
return;
|
||||
}
|
||||
final NegationCountVisitor visitor = new NegationCountVisitor();
|
||||
final NegationCountVisitor visitor = new NegationCountVisitor(ignoreInAssert);
|
||||
method.accept(visitor);
|
||||
final int negationCount = visitor.getCount();
|
||||
if (negationCount <= 3) {
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.siyeh.igtest.metrics;
|
||||
package com.siyeh.igtest.methodmetrics;
|
||||
|
||||
public class CyclomaticComplexityInspection
|
||||
{
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.siyeh.igtest.metrics;
|
||||
package com.siyeh.igtest.methodmetrics;
|
||||
|
||||
public class FiveParametersPerMethodInspection
|
||||
{
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.siyeh.igtest.metrics;
|
||||
package com.siyeh.igtest.methodmetrics;
|
||||
|
||||
public class MultipleReturnPointsInspection
|
||||
{
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.siyeh.igtest.metrics;
|
||||
package com.siyeh.igtest.methodmetrics;
|
||||
|
||||
public class NestingDepthInspection
|
||||
{
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.siyeh.igtest.metrics;
|
||||
package com.siyeh.igtest.methodmetrics;
|
||||
|
||||
public class NonCommentSourceStatementsInspection
|
||||
{
|
||||
+81
@@ -0,0 +1,81 @@
|
||||
package com.siyeh.igtest.methodmetrics.three_negations_per_method;
|
||||
|
||||
public class ThreeNegationsPerMethod
|
||||
{
|
||||
int foo, bar, baz;
|
||||
|
||||
public void okayMethod()
|
||||
{
|
||||
if(!!!true)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public void badMethod()
|
||||
{
|
||||
if(!!!!true)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public void badMethod2()
|
||||
{
|
||||
if(!!!true && 3 !=4)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean equals(final Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
|
||||
final ThreeNegationsPerMethod threeNegationsPerMethod = (ThreeNegationsPerMethod) o;
|
||||
|
||||
if (bar != threeNegationsPerMethod.bar) return false;
|
||||
if (baz != threeNegationsPerMethod.baz) return false;
|
||||
if (foo != threeNegationsPerMethod.foo) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
int result;
|
||||
result = foo;
|
||||
result = 29 * result + bar;
|
||||
result = 29 * result + baz;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
class User {
|
||||
public User getUser(final String username, final String password )
|
||||
{
|
||||
if (!isUsernameValid(username))
|
||||
{
|
||||
throw new IllegalArgumentException("Invalid username!");
|
||||
}
|
||||
|
||||
if (!isPasswordValid(password))
|
||||
{
|
||||
throw new IllegalArgumentException("Invalid password!");
|
||||
}
|
||||
|
||||
assert (username != null) && (password != null);
|
||||
|
||||
return searchLoginTable(username.toLowerCase(), password.toCharArray());
|
||||
}
|
||||
|
||||
private User searchLoginTable(String s, char[] chars) {
|
||||
return null;
|
||||
}
|
||||
|
||||
private boolean isPasswordValid(String password) {
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean isUsernameValid(String username) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<problems>
|
||||
<problem>
|
||||
<file>ThreeNegationsPerMethod.java</file>
|
||||
<line>23</line>
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Method with more than three negations</problem_class>
|
||||
<description><code>badMethod2</code> contains 4 negations #loc</description>
|
||||
</problem>
|
||||
|
||||
<problem>
|
||||
<file>ThreeNegationsPerMethod.java</file>
|
||||
<line>15</line>
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Method with more than three negations</problem_class>
|
||||
<description><code>badMethod</code> contains 4 negations #loc</description>
|
||||
</problem>
|
||||
</problems>
|
||||
-51
@@ -1,51 +0,0 @@
|
||||
package com.siyeh.igtest.metrics;
|
||||
|
||||
public class ThreeNegationsPerMethodInspection
|
||||
{
|
||||
int foo, bar, baz;
|
||||
|
||||
public void okayMethod()
|
||||
{
|
||||
if(!!!true)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public void badMethod()
|
||||
{
|
||||
if(!!!!true)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public void badMethod2()
|
||||
{
|
||||
if(!!!true && 3 !=4)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean equals(final Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
|
||||
final ThreeNegationsPerMethodInspection threeNegationsPerMethodInspection = (ThreeNegationsPerMethodInspection) o;
|
||||
|
||||
if (bar != threeNegationsPerMethodInspection.bar) return false;
|
||||
if (baz != threeNegationsPerMethodInspection.baz) return false;
|
||||
if (foo != threeNegationsPerMethodInspection.foo) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
int result;
|
||||
result = foo;
|
||||
result = 29 * result + bar;
|
||||
result = 29 * result + baz;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.siyeh.ig.methodmetrics;
|
||||
|
||||
import com.siyeh.ig.IGInspectionTestCase;
|
||||
|
||||
public class ThreeNegationsPerMethodInspectionTest extends IGInspectionTestCase {
|
||||
|
||||
public void test() throws Exception {
|
||||
final ThreeNegationsPerMethodInspection tool = new ThreeNegationsPerMethodInspection();
|
||||
tool.m_ignoreInEquals = true;
|
||||
tool.ignoreInAssert = true;
|
||||
doTest("com/siyeh/igtest/methodmetrics/three_negations_per_method", tool);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user