KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > detect > RedundantInterfaces


1 /*
2  * FindBugs - Find bugs in Java programs
3  * Copyright (C) 2005 Dave Brosius <dbrosius@users.sourceforge.net>
4  * Copyright (C) 2005 University of Maryland
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */

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 JavaDoc superClassName = obj.getSuperclassName();
44         if (superClassName.equals("java.lang.Object"))
45             return;
46         
47         String JavaDoc[] interfaceNames = obj.getInterfaceNames();
48         if ((interfaceNames == null) || (interfaceNames.length == 0))
49             return;
50
51         try {
52             JavaClass superObj = obj.getSuperClass();
53             SortedSet<String JavaDoc> redundantInfNames = new TreeSet<String JavaDoc>();
54
55             for (String JavaDoc 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 JavaDoc redundantInfName : redundantInfNames)
67                     bug.addClass(redundantInfName).describe("INTERFACE_TYPE");
68                     
69                 bugReporter.reportBug(bug);
70             }
71
72         } catch (ClassNotFoundException JavaDoc cnfe) {
73             bugReporter.reportMissingClass(cnfe);
74         }
75     }
76     
77     public void report() {
78     }
79 }
80
Popular Tags