Adding SwitchDensity Rule
git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@1283 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
pmd
regress/test/net/sourceforge/pmd/rules/design
src/net/sourceforge/pmd/rules/design
test-data
@ -0,0 +1,55 @@
|
||||
/**
|
||||
* Created on Sep 4, 2002
|
||||
*/
|
||||
package test.net.sourceforge.pmd.rules.design;
|
||||
|
||||
import test.net.sourceforge.pmd.rules.RuleTst;
|
||||
|
||||
import net.sourceforge.pmd.Report;
|
||||
import net.sourceforge.pmd.rules.design.SwitchDensityRule;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* @author dpeugh
|
||||
*
|
||||
* This tests the new SwitchDensity rule to see if it really
|
||||
* does work.
|
||||
*/
|
||||
public class SwitchDensityTest extends RuleTst {
|
||||
|
||||
/**
|
||||
* Constructor for SwitchDensityTest.
|
||||
* @param arg0
|
||||
*/
|
||||
public SwitchDensityTest() {
|
||||
super();
|
||||
}
|
||||
|
||||
public SwitchDensityRule getIUT() {
|
||||
SwitchDensityRule RC = new SwitchDensityRule();
|
||||
RC.addProperty("minimum", "4");
|
||||
|
||||
return RC;
|
||||
}
|
||||
|
||||
public void testSD1() throws Throwable {
|
||||
runTest("SwitchDensity1.java", 1);
|
||||
}
|
||||
|
||||
public void testSD2() throws Throwable {
|
||||
runTest("SwitchDensity2.java", 0);
|
||||
}
|
||||
|
||||
public void testSD3() throws Throwable {
|
||||
runTest("SwitchDensity3.java", 0);
|
||||
}
|
||||
|
||||
public void runTest(String fileName, int expected )
|
||||
throws Throwable
|
||||
{
|
||||
SwitchDensityRule sdr = getIUT();
|
||||
Report report = process(fileName, sdr);
|
||||
assertEquals(expected, report.size());
|
||||
}
|
||||
}
|
@ -0,0 +1,93 @@
|
||||
/**
|
||||
* Created on Aug 29, 2002
|
||||
*/
|
||||
package net.sourceforge.pmd.rules.design;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import net.sourceforge.pmd.ast.ASTDoStatement;
|
||||
import net.sourceforge.pmd.ast.ASTStatement;
|
||||
import net.sourceforge.pmd.ast.ASTSwitchLabel;
|
||||
import net.sourceforge.pmd.ast.ASTSwitchStatement;
|
||||
import net.sourceforge.pmd.stat.DataPoint;
|
||||
import net.sourceforge.pmd.stat.StatisticalRule;
|
||||
|
||||
/**
|
||||
* @author dpeugh
|
||||
*
|
||||
* Switch Density - This is the number of statements over the
|
||||
* number of cases within a switch. The higher the value, the
|
||||
* more work each case is doing.
|
||||
*
|
||||
* Its my theory, that when the Switch Density is high, you should
|
||||
* start looking at Subclasses or State Pattern to alleviate the
|
||||
* problem.
|
||||
*/
|
||||
public class SwitchDensityRule extends StatisticalRule {
|
||||
private class SwitchDensity {
|
||||
private int labels = 0;
|
||||
private int stmts = 0;
|
||||
|
||||
public SwitchDensity() {}
|
||||
|
||||
public void addSwitchLabel() { labels++; }
|
||||
public void addStatement() { stmts++; }
|
||||
public void addStatements( int stmtCount ) { stmts += stmtCount; }
|
||||
public int getStatementCount() { return stmts; }
|
||||
|
||||
public double getDensity() {
|
||||
if (labels == 0) {
|
||||
System.err.println("Warning! Empty Switch!");
|
||||
return 0;
|
||||
}
|
||||
return 1.0 * (stmts / labels);
|
||||
}
|
||||
}
|
||||
|
||||
public SwitchDensityRule() { super(); }
|
||||
|
||||
public Object visit( ASTSwitchStatement node, Object data ) {
|
||||
SwitchDensity oldData = null;
|
||||
|
||||
if (data instanceof SwitchDensity) {
|
||||
oldData = (SwitchDensity) data;
|
||||
}
|
||||
|
||||
SwitchDensity density = new SwitchDensity();
|
||||
|
||||
node.childrenAccept( this, density );
|
||||
|
||||
DataPoint point = new DataPoint();
|
||||
point.setLineNumber( node.getBeginLine() );
|
||||
point.setScore( density.getDensity());
|
||||
point.setRule( this );
|
||||
point.setMessage( getMessage() );
|
||||
|
||||
addDataPoint( point );
|
||||
|
||||
if (data instanceof SwitchDensity) {
|
||||
((SwitchDensity) data).addStatements( density.getStatementCount() );
|
||||
}
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public Object visit( ASTStatement statement, Object data ) {
|
||||
if (data instanceof SwitchDensity) {
|
||||
((SwitchDensity) data).addStatement();
|
||||
}
|
||||
|
||||
statement.childrenAccept(this, data);
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
public Object visit( ASTSwitchLabel switchLabel, Object data ) {
|
||||
if (data instanceof SwitchDensity) {
|
||||
((SwitchDensity) data).addSwitchLabel();
|
||||
}
|
||||
|
||||
switchLabel.childrenAccept( this, data );
|
||||
return data;
|
||||
}
|
||||
}
|
15
pmd/test-data/SwitchDensity1.java
Normal file
15
pmd/test-data/SwitchDensity1.java
Normal file
@ -0,0 +1,15 @@
|
||||
// Switch Density = 5.0
|
||||
public class SwitchDensity1 {
|
||||
public void foo(int i) {
|
||||
switch (i) {
|
||||
case 0:
|
||||
{
|
||||
System.err.println("I am a fish.");
|
||||
System.err.println("I am a fish.");
|
||||
System.err.println("I am a fish.");
|
||||
System.err.println("I am a fish.");
|
||||
System.err.println("I am a fish.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
11
pmd/test-data/SwitchDensity2.java
Normal file
11
pmd/test-data/SwitchDensity2.java
Normal file
@ -0,0 +1,11 @@
|
||||
// Switch Density = 1.0
|
||||
public class SwitchDensity2 {
|
||||
public void foo(int i) {
|
||||
switch (i) {
|
||||
case 0:
|
||||
{
|
||||
System.err.println("I am a fish.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
19
pmd/test-data/SwitchDensity3.java
Normal file
19
pmd/test-data/SwitchDensity3.java
Normal file
@ -0,0 +1,19 @@
|
||||
// Switch Density = 1.0
|
||||
public class SwitchDensity3 {
|
||||
public void foo(int i) {
|
||||
switch (i) {
|
||||
case 0:
|
||||
case 1:
|
||||
case 2:
|
||||
case 3:
|
||||
case 4:
|
||||
{
|
||||
System.err.println("I am a fish.");
|
||||
System.err.println("I am a fish.");
|
||||
System.err.println("I am a fish.");
|
||||
System.err.println("I am a fish.");
|
||||
System.err.println("I am a fish.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user