• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pmd / pmd / 558

28 May 2026 07:27AM UTC coverage: 79.05% (+0.001%) from 79.049%
558

push

github

adangel
[java] UnnecessaryBooleanAssertion: Use InvocationMatcher to find assertions (#6712)

18977 of 24933 branches covered (76.11%)

Branch coverage included in aggregate %.

5 of 5 new or added lines in 1 file covered. (100.0%)

11 existing lines in 4 files now uncovered.

41273 of 51285 relevant lines covered (80.48%)

0.81 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.63
/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/SwitchDensityRule.java
1
/*
2
 * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3
 */
4

5
package net.sourceforge.pmd.lang.java.rule.design;
6

7
import static net.sourceforge.pmd.properties.NumericConstraints.positive;
8

9
import net.sourceforge.pmd.lang.java.ast.ASTStatement;
10
import net.sourceforge.pmd.lang.java.ast.ASTSwitchBranch;
11
import net.sourceforge.pmd.lang.java.ast.ASTSwitchExpression;
12
import net.sourceforge.pmd.lang.java.ast.ASTSwitchLike;
13
import net.sourceforge.pmd.lang.java.ast.ASTSwitchStatement;
14
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRulechainRule;
15
import net.sourceforge.pmd.lang.rule.internal.CommonPropertyDescriptors;
16
import net.sourceforge.pmd.properties.PropertyDescriptor;
17
import net.sourceforge.pmd.reporting.RuleContext;
18

19
/**
20
 * Switch Density - This is the number of statements over the number of
21
 * cases within a switch. The higher the value, the more work each case
22
 * is doing.
23
 *
24
 * <p>It's my theory, that when the Switch Density is high, you should start
25
 * looking at Subclasses or State Pattern to alleviate the problem.</p>
26
 *
27
 * @author David Dixon-Peugh
28
 * @author Clément Fournier
29
 */
30
public class SwitchDensityRule extends AbstractJavaRulechainRule {
31

32
    private static final PropertyDescriptor<Integer> REPORT_LEVEL =
1✔
33
            CommonPropertyDescriptors.reportLevelProperty()
1✔
34
                    .desc("Threshold at or above which a switch statement or expression is reported")
1✔
35
                    .require(positive())
1✔
36
                    .defaultValue(10)
1✔
37
                    .build();
1✔
38

39
    public SwitchDensityRule() {
40
        super(ASTSwitchStatement.class, ASTSwitchExpression.class);
1✔
41
        definePropertyDescriptor(REPORT_LEVEL);
1✔
42
    }
1✔
43

44
    @Override
45
    public Object visit(ASTSwitchStatement node, Object data) {
46
        RuleContext ctx = (RuleContext) data;
1✔
47

48
        visitSwitchLike(node, ctx);
1✔
49

50
        return null;
1✔
51
    }
52

53
    @Override
54
    public Object visit(ASTSwitchExpression node, Object data) {
55
        RuleContext ctx = (RuleContext) data;
1✔
56

57
        visitSwitchLike(node, ctx);
1✔
58

59
        return null;
1✔
60
    }
61

62
    /**
63
     * @deprecated since 7.25.0. This method should have never been public.
64
     */
65
    @Deprecated
66
    public Void visitSwitchLike(ASTSwitchLike node, Object data) {
UNCOV
67
        RuleContext ctx = (RuleContext) data;
×
68

UNCOV
69
        visitSwitchLike(node, ctx);
×
70

UNCOV
71
        return null;
×
72
    }
73

74
    private void visitSwitchLike(ASTSwitchLike node, RuleContext ctx) {
75
        // note: this does not cross find boundaries.
76
        int stmtCount = node.descendants(ASTStatement.class).count();
1✔
77
        int labelCount = node.getBranches()
1✔
78
                .map(ASTSwitchBranch::getLabel)
1✔
79
                .sumBy(label -> label.isDefault() || label.isPatternLabel() ? 1 : label.getExprList().count());
1✔
80

81
        // note: if labelCount is zero, double division will produce +Infinity or NaN, not ArithmeticException
82
        double density = stmtCount / (double) labelCount;
1✔
83
        if (density >= getProperty(REPORT_LEVEL)) {
1✔
84
            ctx.addViolation(node);
1✔
85
        }
86
    }
1✔
87
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc