KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > SloppyBugComparator


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;
21
22 import java.io.Serializable JavaDoc;
23
24 import edu.umd.cs.findbugs.model.ClassNameRewriter;
25 import edu.umd.cs.findbugs.model.ClassNameRewriterUtil;
26 import edu.umd.cs.findbugs.model.IdentityClassNameRewriter;
27
28 /**
29  * Very sloppy bug comparator: if the warnings are of the same type,
30  * and in the same class/method/field, assume they are the same.
31  *
32  * @author David Hovemeyer
33  */

34 public class SloppyBugComparator implements WarningComparator {
35     
36     private static final boolean DEBUG = SystemProperties.getBoolean("sloppyComparator.debug");
37     
38     private static final long serialVersionUID = 1L;
39     
40     private ClassNameRewriter classNameRewriter = IdentityClassNameRewriter.instance();
41     
42     /**
43      * Constructor.
44      */

45     public SloppyBugComparator() {
46     }
47     
48     public void setClassNameRewriter(ClassNameRewriter classNameRewriter) {
49         this.classNameRewriter = classNameRewriter;
50     }
51     
52     private int compareNullElements(Object JavaDoc lhs, Object JavaDoc rhs) {
53         if (lhs == null && rhs == null)
54             return 0;
55         else
56             return (lhs == null) ? -1 : 1;
57     }
58
59     /**
60      * Compare class annotations.
61      *
62      * @param lhs left hand class annotation
63      * @param rhs right hand class annotation
64      * @return comparison of the class annotations
65      */

66     private int compareClassesAllowingNull(ClassAnnotation lhs, ClassAnnotation rhs) {
67         if (lhs == null || rhs == null) {
68             return compareNullElements(lhs, rhs);
69         }
70         
71         String JavaDoc lhsClassName = classNameRewriter.rewriteClassName(lhs.getClassName());
72         String JavaDoc rhsClassName = classNameRewriter.rewriteClassName(rhs.getClassName());
73         
74         if (DEBUG) System.err.println("Comparing " + lhsClassName + " and " + rhsClassName);
75         
76         int cmp = lhsClassName.compareTo(rhsClassName);
77         if (DEBUG) System.err.println("\t==> " + cmp);
78         return cmp;
79     }
80     
81     private int compareMethodsAllowingNull(MethodAnnotation lhs, MethodAnnotation rhs) {
82         if (lhs == null || rhs == null) {
83             return compareNullElements(lhs, rhs);
84         }
85
86         lhs = convertMethod(lhs);
87         rhs = convertMethod(rhs);
88         
89         return lhs.compareTo(rhs);
90     }
91     
92     private int compareFieldsAllowingNull(FieldAnnotation lhs, FieldAnnotation rhs) {
93         if (lhs == null || rhs == null) {
94             return compareNullElements(lhs, rhs);
95         }
96
97         lhs = convertField(lhs);
98         rhs = convertField(rhs);
99         
100         if (DEBUG) System.err.println("Compare fields: " + lhs + " and " + rhs);
101         
102         return lhs.compareTo(rhs);
103     }
104
105     private MethodAnnotation convertMethod(MethodAnnotation methodAnnotation) {
106         return ClassNameRewriterUtil.convertMethodAnnotation(classNameRewriter, methodAnnotation);
107     }
108     
109     private FieldAnnotation convertField(FieldAnnotation fieldAnnotation) {
110         return ClassNameRewriterUtil.convertFieldAnnotation(classNameRewriter, fieldAnnotation);
111     }
112
113     /* (non-Javadoc)
114      * @see edu.umd.cs.findbugs.WarningComparator#compare(edu.umd.cs.findbugs.BugInstance, edu.umd.cs.findbugs.BugInstance)
115      */

116     public int compare(BugInstance lhs, BugInstance rhs) {
117         
118         int cmp;
119         
120         // Bug abbrevs must match
121
BugPattern lhsPattern = lhs.getBugPattern();
122         BugPattern rhsPattern = rhs.getBugPattern();
123         String JavaDoc lhsAbbrev, rhsAbbrev;
124         if (lhsPattern == null || rhsPattern == null) {
125             lhsAbbrev = getAbbrevFromBugType(lhs.getType());
126             rhsAbbrev = getAbbrevFromBugType(rhs.getType());
127         } else {
128             lhsAbbrev = lhsPattern.getAbbrev();
129             rhsAbbrev = rhsPattern.getAbbrev();
130         }
131         cmp = lhsAbbrev.compareTo(rhsAbbrev);
132         if (cmp != 0) {
133             if (DEBUG) System.err.println("bug abbrevs do not match");
134             return cmp;
135         }
136         
137         // Primary class must match
138
cmp = compareClassesAllowingNull(lhs.getPrimaryClass(), rhs.getPrimaryClass());
139         if (cmp != 0)
140             return cmp;
141         
142         // Primary method must match (if any)
143
cmp = compareMethodsAllowingNull(lhs.getPrimaryMethod(), rhs.getPrimaryMethod());
144         if (cmp != 0) {
145             if (DEBUG) System.err.println("primary methods do not match");
146             return cmp;
147         }
148         
149         // Primary field must match (if any)
150
cmp = compareFieldsAllowingNull(lhs.getPrimaryField(), rhs.getPrimaryField());
151         if (cmp != 0) {
152             if (DEBUG) System.err.println("primary fields do not match");
153             return cmp;
154         }
155         
156         // Assume they're the same
157
return 0;
158     }
159
160     /**
161      * @param type
162      * @return
163      */

164     private static String JavaDoc getAbbrevFromBugType(String JavaDoc type) {
165         int bar = type.indexOf('_');
166         return (bar >= 0) ? type.substring(0, bar) : "";
167     }
168 }
169
Popular Tags