[Fb-contrib-commit] fb-contrib/src/com/mebigfatguy/fbcontrib/detect ClassEnvy.java,1.7,1.8
Brought to you by:
dbrosius
|
From: Dave B. <dbr...@us...> - 2005-10-12 05:25:56
|
Update of /cvsroot/fb-contrib/fb-contrib/src/com/mebigfatguy/fbcontrib/detect In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv1721/src/com/mebigfatguy/fbcontrib/detect Modified Files: ClassEnvy.java Log Message: if the envy class implements a well known java.* interfaces (besides the bogus ones) then don't report it. Index: ClassEnvy.java =================================================================== RCS file: /cvsroot/fb-contrib/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/ClassEnvy.java,v retrieving revision 1.7 retrieving revision 1.8 diff -u -d -r1.7 -r1.8 --- ClassEnvy.java 10 Oct 2005 07:05:01 -0000 1.7 +++ ClassEnvy.java 12 Oct 2005 05:25:45 -0000 1.8 @@ -19,8 +19,10 @@ package com.mebigfatguy.fbcontrib.detect; import java.util.HashMap; +import java.util.HashSet; import java.util.Iterator; import java.util.Map; +import java.util.Set; import org.apache.bcel.Repository; import org.apache.bcel.classfile.Code; @@ -38,7 +40,14 @@ public class ClassEnvy extends BytecodeScanningDetector implements StatelessDetector { - private static final String ENVY_PERCENT_PROPERTY = "fb-contrib.ce.percentt"; + private static final String ENVY_PERCENT_PROPERTY = "fb-contrib.ce.percent"; + private static final Set<String> ignorableInterfaces = new HashSet<String>(); + static { + ignorableInterfaces.add("java.io.Serializable"); + ignorableInterfaces.add("java.lang.Cloneable"); + ignorableInterfaces.add("java.lang.Comparable"); + }; + private BugReporter bugReporter; private OpcodeStack stack; private String packageName; @@ -109,6 +118,9 @@ } if (bestPercent > envyPercent) { + if (implementsCommonInterface(bestEnvy)) + return; + bugReporter.reportBug( new BugInstance( this, "CE_CLASS_ENVY", NORMAL_PRIORITY) .addClass(this) .addMethod(this) @@ -147,6 +159,26 @@ } } + private boolean implementsCommonInterface(String name) { + try { + JavaClass cls = Repository.lookupClass(name); + JavaClass[] infs = cls.getAllInterfaces(); + + for (JavaClass inf : infs) { + String infName = inf.getClassName(); + if (ignorableInterfaces.contains(infName)) + continue; + if (infName.startsWith("java.")) + return true; + } + return false; + + } catch (ClassNotFoundException cnfe) { + bugReporter.reportMissingClass(cnfe); + return true; + } + } + private boolean countClassAccess(final int classAtStackIndex) { String calledClass = null; |