1 20 package edu.umd.cs.findbugs.detect; 21 22 23 import edu.umd.cs.findbugs.*; 24 import edu.umd.cs.findbugs.ba.ClassContext; 25 import edu.umd.cs.findbugs.visitclass.PreorderVisitor; 26 import java.util.*; 27 import org.apache.bcel.Repository; 28 import org.apache.bcel.classfile.JavaClass; 29 30 public class RedundantInterfaces extends PreorderVisitor implements Detector, StatelessDetector 31 { 32 private BugReporter bugReporter; 33 34 public RedundantInterfaces(BugReporter bugReporter) { 35 this.bugReporter = bugReporter; 36 } 37 38 39 40 public void visitClassContext(ClassContext classContext) { 41 JavaClass obj = classContext.getJavaClass(); 42 43 String superClassName = obj.getSuperclassName(); 44 if (superClassName.equals("java.lang.Object")) 45 return; 46 47 String [] interfaceNames = obj.getInterfaceNames(); 48 if ((interfaceNames == null) || (interfaceNames.length == 0)) 49 return; 50 51 try { 52 JavaClass superObj = obj.getSuperClass(); 53 SortedSet<String > redundantInfNames = new TreeSet<String >(); 54 55 for (String interfaceName : interfaceNames) { 56 if (!"java/io/Serializable".equals(interfaceName)) { 57 JavaClass inf = Repository.lookupClass(interfaceName.replace('/', '.')); 58 if (superObj.instanceOf(inf)) 59 redundantInfNames.add(inf.getClassName()); 60 } 61 } 62 63 if (redundantInfNames.size() > 0) { 64 BugInstance bug = new BugInstance( this, "RI_REDUNDANT_INTERFACES", LOW_PRIORITY ) 65 .addClass(obj); 66 for (String redundantInfName : redundantInfNames) 67 bug.addClass(redundantInfName).describe("INTERFACE_TYPE"); 68 69 bugReporter.reportBug(bug); 70 } 71 72 } catch (ClassNotFoundException cnfe) { 73 bugReporter.reportMissingClass(cnfe); 74 } 75 } 76 77 public void report() { 78 } 79 } 80 | Popular Tags |