KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * FindBugs - Find bugs in Java programs
3  * Copyright (C) 2004,2005 Dave Brosius <dbrosius@users.sourceforge.net>
4  * Copyright (C) 2004,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
21 package edu.umd.cs.findbugs.detect;
22
23 import java.util.HashMap JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import org.apache.bcel.classfile.JavaClass;
27 import org.apache.bcel.classfile.Method;
28
29 import edu.umd.cs.findbugs.BugInstance;
30 import edu.umd.cs.findbugs.BugReporter;
31 import edu.umd.cs.findbugs.BytecodeScanningDetector;
32 import edu.umd.cs.findbugs.StatelessDetector;
33
34 public class BadlyOverriddenAdapter extends BytecodeScanningDetector {
35     private BugReporter bugReporter;
36     private boolean isAdapter;
37     private Map JavaDoc<String JavaDoc, String JavaDoc> methodMap;
38     private Map JavaDoc<String JavaDoc, BugInstance> badOverrideMap;
39
40     public BadlyOverriddenAdapter(BugReporter bugReporter) {
41         this.bugReporter = bugReporter;
42         methodMap = new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
43         badOverrideMap = new HashMap JavaDoc<String JavaDoc,BugInstance>();
44     }
45     
46
47
48     @Override JavaDoc
49          public void visit(JavaClass obj) {
50         try {
51             methodMap.clear();
52             badOverrideMap.clear();
53             JavaClass superClass = obj.getSuperClass();
54             if (superClass == null) return;
55             String JavaDoc packageName = superClass.getPackageName();
56             String JavaDoc className = superClass.getClassName();
57             
58             //A more generic way to add Adapters would be nice here
59
isAdapter = ((className.endsWith("Adapter")) && (packageName.equals("java.awt.event") || packageName.equals("javax.swing.event")))
60                       ||((className.equals("DefaultHandler") && (packageName.equals("org.xml.sax.helpers"))));
61             if (isAdapter) {
62                 Method[] methods = superClass.getMethods();
63                 for (Method method1 : methods) {
64                     methodMap.put(method1.getName(), method1.getSignature());
65                 }
66             }
67         } catch (ClassNotFoundException JavaDoc cnfe) {
68             bugReporter.reportMissingClass(cnfe);
69         }
70     }
71     
72     @Override JavaDoc
73          public void visitAfter(JavaClass obj) {
74         for (BugInstance bi : badOverrideMap.values()) {
75             if (bi != null)
76                 bugReporter.reportBug(bi);
77         }
78     }
79
80     @Override JavaDoc
81          public void visit(Method obj) {
82         if (isAdapter) {
83             String JavaDoc methodName = obj.getName();
84             String JavaDoc signature = methodMap.get(methodName);
85             if (!methodName.equals("<init>") && signature != null) {
86                 if (!signature.equals(obj.getSignature())) {
87                     if (!badOverrideMap.keySet().contains(methodName)) {
88                         badOverrideMap.put(methodName, new BugInstance(this, "BOA_BADLY_OVERRIDDEN_ADAPTER", NORMAL_PRIORITY)
89                                 .addClassAndMethod(this)
90                                 .addSourceLine(this));
91                     }
92                 }
93                 else {
94                     badOverrideMap.put(methodName, null);
95                 }
96             }
97         }
98     }
99 }
100
101 // vim:ts=4
102
Popular Tags