1 19 20 package edu.umd.cs.findbugs.model; 21 22 import java.util.HashMap ; 23 import java.util.HashSet ; 24 import java.util.Iterator ; 25 import java.util.Locale ; 26 import java.util.Map ; 27 import java.util.Set ; 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 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 <String ,String > rewriteMap; 49 50 public MovedClassMap(BugCollection before, BugCollection after) { 51 this.before = before; 52 this.after = after; 53 this.rewriteMap = new HashMap <String ,String >(); 54 } 55 56 public MovedClassMap execute() { 57 Set <String > beforeClasses = buildClassSet(before); 58 Set <String > afterClasses = buildClassSet(after); 59 60 Set <String > removedClasses = new HashSet <String >(beforeClasses); 61 removedClasses.removeAll(afterClasses); 62 63 Set <String > addedClasses = new HashSet <String >(afterClasses); 64 addedClasses.removeAll(beforeClasses); 65 66 Map <String ,String > removedShortNameToFullNameMap = buildShortNameToFullNameMap(removedClasses); 67 68 for (String fullAddedName : addedClasses) { 71 72 75 String shortAddedName = getShortClassName(fullAddedName); 76 String 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 rewriteClassName(String className) { 91 String rewrittenClassName = rewriteMap.get(className); 92 if (rewrittenClassName != null) { 93 className = rewrittenClassName; 94 } 95 return className; 96 } 97 98 104 private Set <String > buildClassSet(BugCollection bugCollection) { 105 Set <String > classSet = new HashSet <String >(); 106 107 for (Iterator <BugInstance> i = bugCollection.iterator(); i.hasNext(); ) { 108 BugInstance warning = i.next(); 109 for (Iterator <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 127 private Map <String , String > buildShortNameToFullNameMap(Set <String > classSet) { 128 Map <String ,String > result = new HashMap <String ,String >(); 129 for (String className : classSet) { 130 String shortClassName = getShortClassName(className); 131 result.put(shortClassName, className); 132 } 133 return result; 134 } 135 136 142 private String getShortClassName(String 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 |