}
mapFromNewToOldBug.clear();
matchedOldBugs.clear();
BugCollection resultCollection = newCollection.createEmptyCollectionWithMetadata();
// Previous sequence number
long lastSequence = origCollection.getSequenceNumber();
// The AppVersion history is retained from the orig collection,
// adding an entry for the sequence/timestamp of the current state
// of the orig collection.
resultCollection.clearAppVersions();
for (Iterator<AppVersion> i = origCollection.appVersionIterator(); i.hasNext();) {
AppVersion appVersion = i.next();
resultCollection.addAppVersion((AppVersion) appVersion.clone());
}
AppVersion origCollectionVersion = origCollection.getCurrentAppVersion();
AppVersion origCollectionVersionClone = new AppVersion(lastSequence);
origCollectionVersionClone.setTimestamp(origCollectionVersion.getTimestamp());
origCollectionVersionClone.setReleaseName(origCollectionVersion.getReleaseName());
origCollectionVersionClone.setNumClasses(origCollection.getProjectStats().getNumClasses());
origCollectionVersionClone.setCodeSize(origCollection.getProjectStats().getCodeSize());
resultCollection.addAppVersion(origCollectionVersionClone);
// We assign a sequence number to the new collection as one greater than
// the original collection.
long currentSequence = origCollection.getSequenceNumber() + 1;
resultCollection.setSequenceNumber(currentSequence);
// int oldBugs = 0;
matchBugs(origCollection, newCollection);
if (sloppyMatch) {
matchBugs(new SloppyBugComparator(), origCollection, newCollection);
}
// int newlyDeadBugs = 0;
// int persistantBugs = 0;
// int addedBugs = 0;
// int addedInNewCode = 0;
// int deadBugInDeadCode = 0;
HashSet<String> analyzedSourceFiles = sourceFilesInCollection(newCollection);
// Copy unmatched bugs
if (copyDeadBugs || incrementalAnalysis) {
for (BugInstance bug : origCollection.getCollection()) {
if (!matchedOldBugs.containsKey(bug)) {
if (bug.isDead()) {
// oldBugs++;
BugInstance newBug = (BugInstance) bug.clone();
resultCollection.add(newBug, false);
} else {
// newlyDeadBugs++;
BugInstance newBug = (BugInstance) bug.clone();
ClassAnnotation classBugFoundIn = bug.getPrimaryClass();
String className = classBugFoundIn.getClassName();
String sourceFile = classBugFoundIn.getSourceFileName();
boolean fixed = sourceFile != null && analyzedSourceFiles.contains(sourceFile)
|| newCollection.getProjectStats().getClassStats(className) != null;
if (fixed) {
if (!copyDeadBugs) {
continue;
}
newBug.setRemovedByChangeOfPersistingClass(true);
newBug.setLastVersion(lastSequence);
} else {
// deadBugInDeadCode++;
if (!incrementalAnalysis) {
newBug.setLastVersion(lastSequence);
}
}
if (newBug.isDead() && newBug.getFirstVersion() > newBug.getLastVersion()) {
throw new IllegalStateException("Illegal Version range: " + newBug.getFirstVersion() + ".."
+ newBug.getLastVersion());
}
resultCollection.add(newBug, false);
}
}
}
}
// Copy matched bugs
for (BugInstance bug : newCollection.getCollection()) {
BugInstance newBug = (BugInstance) bug.clone();
if (mapFromNewToOldBug.containsKey(bug)) {
BugInstance origWarning = mapFromNewToOldBug.get(bug);
mergeBugHistory(origWarning, newBug);
// handle getAnnotationText()/setAnnotationText() and
// designation key
BugDesignation designation = newBug.getUserDesignation();
if (designation != null) {
designation.merge(origWarning.getUserDesignation());
}
else {
newBug.setUserDesignation(origWarning.getUserDesignation()); // clone??
}
// persistantBugs++;
} else {
newBug.setFirstVersion(lastSequence + 1);
// addedBugs++;
ClassAnnotation classBugFoundIn = bug.getPrimaryClass();
String className = classBugFoundIn.getClassName();
if (origCollection.getProjectStats().getClassStats(className) != null) {
newBug.setIntroducedByChangeOfExistingClass(true);
// System.out.println("added bug to existing code " +
// newBug.getUniqueId() + " : " + newBug.getAbbrev() + " in
// " + classBugFoundIn);
} else {
// addedInNewCode++;
}
}
if (newBug.isDead()) {
throw new IllegalStateException("Illegal Version range: " + newBug.getFirstVersion() + ".."
+ newBug.getLastVersion());
}
int oldSize = resultCollection.getCollection().size();
resultCollection.add(newBug, false);
int newSize = resultCollection.getCollection().size();
if (newSize != oldSize + 1) {
System.out.println("Failed to add bug" + newBug.getMessage());
}
}
/*