[Fb-contrib-commit] fb-contrib/src/com/mebigfatguy/fbcontrib/detect ClassEnvy.java,1.8,1.9
Brought to you by:
dbrosius
|
From: Dave B. <dbr...@us...> - 2005-10-14 06:34:44
|
Update of /cvsroot/fb-contrib/fb-contrib/src/com/mebigfatguy/fbcontrib/detect In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv11534/src/com/mebigfatguy/fbcontrib/detect Modified Files: ClassEnvy.java Log Message: better envy calculations, and make the min count configurable Index: ClassEnvy.java =================================================================== RCS file: /cvsroot/fb-contrib/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/ClassEnvy.java,v retrieving revision 1.8 retrieving revision 1.9 diff -u -d -r1.8 -r1.9 --- ClassEnvy.java 12 Oct 2005 05:25:45 -0000 1.8 +++ ClassEnvy.java 14 Oct 2005 06:34:35 -0000 1.9 @@ -18,9 +18,10 @@ */ package com.mebigfatguy.fbcontrib.detect; +import java.util.Arrays; +import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; -import java.util.Iterator; import java.util.Map; import java.util.Set; @@ -41,6 +42,7 @@ public class ClassEnvy extends BytecodeScanningDetector implements StatelessDetector { private static final String ENVY_PERCENT_PROPERTY = "fb-contrib.ce.percent"; + private static final String ENVY_MIN_PROPERTY = "fb-contrib.ce.min"; private static final Set<String> ignorableInterfaces = new HashSet<String>(); static { ignorableInterfaces.add("java.io.Serializable"); @@ -57,6 +59,7 @@ private String methodName; private boolean methodIsStatic; private double envyPercent = 0.90; + private int envyMin = 5; public ClassEnvy(final BugReporter bugReporter) { this.bugReporter = bugReporter; @@ -69,6 +72,9 @@ //Stick with original } } + Integer min = Integer.getInteger("ENVY_MIN_PROPERTY"); + if (min != null) + envyMin = min.intValue(); } @Override @@ -97,35 +103,34 @@ return; super.visitCode(obj); - - String bestEnvy = null; - double bestPercent = envyPercent; - Iterator<Map.Entry<String,Integer>> it = clsAccessCount.entrySet().iterator(); - while (it.hasNext()) { - Map.Entry<String,Integer> entry = it.next(); - if (generalPurpose(entry.getKey())) - continue; - - Integer mc = entry.getValue(); - if (mc.intValue() < 3) - continue; + + if (clsAccessCount.size() > 0) { + Map.Entry<String, Integer>[]envies = clsAccessCount.entrySet().toArray(new Map.Entry[clsAccessCount.size()]); + Arrays.sort(envies, new Comparator<Map.Entry<String, Integer>>() + { + public int compare(Map.Entry<String, Integer> entry1, Map.Entry<String, Integer> entry2) { + return entry2.getValue().intValue() - entry1.getValue().intValue(); + } + }); - double percent = ((double)mc.intValue()) / ((double)(mc.intValue() + thisClsAccessCount)); - if (percent > bestPercent) { - bestPercent = percent; - bestEnvy = entry.getKey(); - } - } - - if (bestPercent > envyPercent) { - if (implementsCommonInterface(bestEnvy)) + Map.Entry<String, Integer> bestEnvyEntry = envies[0]; + int bestEnvyCount = bestEnvyEntry.getValue().intValue(); + if (bestEnvyCount < envyMin) return; + String bestEnvy = bestEnvyEntry.getKey(); - bugReporter.reportBug( new BugInstance( this, "CE_CLASS_ENVY", NORMAL_PRIORITY) - .addClass(this) - .addMethod(this) - .addSourceLineRange(this, 0, obj.getCode().length-1) - .addString(bestEnvy)); + double bestPercent = ((double)bestEnvyCount) / ((double) (bestEnvyCount + thisClsAccessCount)); + + if (bestPercent > envyPercent) { + if (implementsCommonInterface(bestEnvy)) + return; + + bugReporter.reportBug( new BugInstance( this, "CE_CLASS_ENVY", NORMAL_PRIORITY) + .addClass(this) + .addMethod(this) + .addSourceLineRange(this, 0, obj.getCode().length-1) + .addString(bestEnvy)); + } } } |