KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > model > MovedClassMap


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.model;
21
22 import java.util.HashMap JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.Locale JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.Set JavaDoc;
28
29 import edu.umd.cs.findbugs.BugAnnotation;
30 import edu.umd.cs.findbugs.BugCollection;
31 import edu.umd.cs.findbugs.BugInstance;
32 import edu.umd.cs.findbugs.ClassAnnotation;
33 import edu.umd.cs.findbugs.SystemProperties;
34
35 /**
36  * Build a map of added class names to removed class names.
37  * Serves as a ClassNameRewriter that can match up renamed classes
38  * in two BugCollections.
39  *
40  * @author David Hovemeyer
41  */

42 public class MovedClassMap implements ClassNameRewriter {
43     
44     private static final boolean DEBUG = SystemProperties.getBoolean("movedClasses.debug");
45     
46     private BugCollection before;
47     private BugCollection after;
48     private Map JavaDoc<String JavaDoc,String JavaDoc> rewriteMap;
49     
50     public MovedClassMap(BugCollection before, BugCollection after) {
51          this.before = before;
52          this.after = after;
53          this.rewriteMap = new HashMap JavaDoc<String JavaDoc,String JavaDoc>();
54     }
55     
56     public MovedClassMap execute() {
57         Set JavaDoc<String JavaDoc> beforeClasses = buildClassSet(before);
58         Set JavaDoc<String JavaDoc> afterClasses = buildClassSet(after);
59         
60         Set JavaDoc<String JavaDoc> removedClasses = new HashSet JavaDoc<String JavaDoc>(beforeClasses);
61         removedClasses.removeAll(afterClasses);
62         
63         Set JavaDoc<String JavaDoc> addedClasses = new HashSet JavaDoc<String JavaDoc>(afterClasses);
64         addedClasses.removeAll(beforeClasses);
65         
66         Map JavaDoc<String JavaDoc,String JavaDoc> removedShortNameToFullNameMap = buildShortNameToFullNameMap(removedClasses);
67         
68         // Map names of added classes to names of removed classes if
69
// they have the same short name.
70
for (String JavaDoc fullAddedName : addedClasses) {
71             
72             // FIXME: could use a similarity metric to match added and removed
73
// classes. Instead, we just match based on the short class name.
74

75             String JavaDoc shortAddedName = getShortClassName(fullAddedName);
76             String JavaDoc fullRemovedName = removedShortNameToFullNameMap.get(shortAddedName);
77             if (fullRemovedName != null) {
78                 if (DEBUG) System.err.println(fullAddedName + " --> " + fullRemovedName);
79                 rewriteMap.put(fullAddedName, fullRemovedName);
80             }
81             
82         }
83         
84         return this;
85     }
86     
87     public boolean isEmpty() {
88         return rewriteMap.isEmpty();
89     }
90     public String JavaDoc rewriteClassName(String JavaDoc className) {
91         String JavaDoc rewrittenClassName = rewriteMap.get(className);
92         if (rewrittenClassName != null) {
93             className = rewrittenClassName;
94         }
95         return className;
96     }
97
98     /**
99      * Find set of classes referenced in given BugCollection.
100      *
101      * @param bugCollection
102      * @return set of classes referenced in the BugCollection
103      */

104     private Set JavaDoc<String JavaDoc> buildClassSet(BugCollection bugCollection) {
105         Set JavaDoc<String JavaDoc> classSet = new HashSet JavaDoc<String JavaDoc>();
106         
107         for (Iterator JavaDoc<BugInstance> i = bugCollection.iterator(); i.hasNext(); ) {
108             BugInstance warning = i.next();
109             for (Iterator JavaDoc<BugAnnotation> j = warning.annotationIterator(); j.hasNext();) {
110                 BugAnnotation annotation = j.next();
111                 if (!(annotation instanceof ClassAnnotation))
112                     continue;
113                 classSet.add(((ClassAnnotation)annotation).getClassName());
114             }
115         }
116         
117         return classSet;
118     }
119
120     /**
121      * Build a map of short class names (without package) to full
122      * class names.
123      *
124      * @param classSet set of fully-qualified class names
125      * @return map of short class names to fully-qualified class names
126      */

127     private Map JavaDoc<String JavaDoc, String JavaDoc> buildShortNameToFullNameMap(Set JavaDoc<String JavaDoc> classSet) {
128         Map JavaDoc<String JavaDoc,String JavaDoc> result = new HashMap JavaDoc<String JavaDoc,String JavaDoc>();
129         for (String JavaDoc className : classSet) {
130             String JavaDoc shortClassName = getShortClassName(className);
131             result.put(shortClassName, className);
132         }
133         return result;
134     }
135
136     /**
137      * Get a short class name (no package part).
138      *
139      * @param className a class name
140      * @return short class name
141      */

142     private String JavaDoc getShortClassName(String JavaDoc className) {
143         int lastDot = className.lastIndexOf('.');
144         if (lastDot >= 0) {
145             className = className.substring(lastDot + 1);
146         }
147         return className.toLowerCase(Locale.US).replace('+', '$');
148     }
149     
150     
151 }
152
Popular Tags