KickJava   Java API By Example, From Geeks To Geeks.

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


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

19
20 package edu.umd.cs.findbugs.detect;
21
22 import org.apache.bcel.classfile.JavaClass;
23 import org.apache.bcel.classfile.Method;
24
25 import edu.umd.cs.findbugs.BugInstance;
26 import edu.umd.cs.findbugs.BugReporter;
27 import edu.umd.cs.findbugs.Detector;
28 import edu.umd.cs.findbugs.ba.AnalysisContext;
29 import edu.umd.cs.findbugs.ba.ClassContext;
30 import edu.umd.cs.findbugs.ba.XFactory;
31 import edu.umd.cs.findbugs.ba.XMethod;
32 import edu.umd.cs.findbugs.ba.npe.ParameterNullnessProperty;
33 import edu.umd.cs.findbugs.ba.npe.ParameterNullnessPropertyDatabase;
34
35 /**
36  * Find equals(Object) methods that unconditionally dereference the parameter,
37  * rather than returning false if it's null.
38  *
39  * @author David Hovemeyer
40  */

41 public class FindBadEqualsImplementation implements Detector {
42     
43     private BugReporter bugReporter;
44     private ParameterNullnessPropertyDatabase database;
45     private boolean checkedDatabase;
46     
47     public FindBadEqualsImplementation(BugReporter bugReporter) {
48         this.bugReporter = bugReporter;
49     }
50
51     /* (non-Javadoc)
52      * @see edu.umd.cs.findbugs.Detector#visitClassContext(edu.umd.cs.findbugs.ba.ClassContext)
53      */

54     public void visitClassContext(ClassContext classContext) {
55         if (!checkedDatabase) {
56             database = AnalysisContext.currentAnalysisContext().getUnconditionalDerefParamDatabase();
57             checkedDatabase = true;
58         }
59         
60         if (database == null)
61             return;
62         
63         JavaClass javaClass = classContext.getJavaClass();
64         Method[] methodList = javaClass.getMethods();
65         for (Method method : methodList) {
66             if (!isEqualsMethod(method))
67                 continue;
68
69             XMethod xmethod = XFactory.createXMethod(javaClass, method);
70             ParameterNullnessProperty property = database.getProperty(xmethod);
71             if (property == null)
72                 continue;
73
74             if (property.isNonNull(0)) {
75                 BugInstance warning = new BugInstance("NP_DOES_NOT_HANDLE_NULL", NORMAL_PRIORITY)
76                         .addClassAndMethod(javaClass, method);
77                 bugReporter.reportBug(warning);
78             }
79         }
80     }
81
82     private boolean isEqualsMethod(Method method) {
83         return method.getName().equals("equals")
84             && method.getSignature().equals("(Ljava/lang/Object;)Z")
85             && !method.isStatic();
86     }
87
88     /* (non-Javadoc)
89      * @see edu.umd.cs.findbugs.Detector#report()
90      */

91     public void report() {
92         // TODO Auto-generated method stub
93

94     }
95
96     /**
97      * @param args
98      */

99     public static void main(String JavaDoc[] args) {
100         // TODO Auto-generated method stub
101

102     }
103
104 }
105
Popular Tags