package edu.umd.cs.findbugs;
import edu.umd.cs.findbugs.model.ClassNameRewriter;
import edu.umd.cs.findbugs.model.ClassNameRewriterUtil;
import edu.umd.cs.findbugs.model.IdentityClassNameRewriter;
public class SloppyBugComparator implements WarningComparator {
private static final boolean DEBUG = SystemProperties.getBoolean("sloppyComparator.debug");
private ClassNameRewriter classNameRewriter = IdentityClassNameRewriter.instance();
public SloppyBugComparator() {
}
@Override
public void setClassNameRewriter(ClassNameRewriter classNameRewriter) {
this.classNameRewriter = classNameRewriter;
}
private int compareNullElements(Object lhs, Object rhs) {
if (lhs == null && rhs == null) {
return 0;
} else {
return (lhs == null) ? -1 : 1;
}
}
private int compareClassesAllowingNull(ClassAnnotation lhs, ClassAnnotation rhs) {
if (lhs == null || rhs == null) {
return compareNullElements(lhs, rhs);
}
String lhsClassName = classNameRewriter.rewriteClassName(lhs.getClassName());
String rhsClassName = classNameRewriter.rewriteClassName(rhs.getClassName());
if (DEBUG) {
System.err.println("Comparing " + lhsClassName + " and " + rhsClassName);
}
int cmp = lhsClassName.compareTo(rhsClassName);
if (DEBUG) {
System.err.println("\t==> " + cmp);
}
return cmp;
}
private int compareMethodsAllowingNull(MethodAnnotation lhs, MethodAnnotation rhs) {
if (lhs == null || rhs == null) {
return compareNullElements(lhs, rhs);
}
lhs = convertMethod(lhs);
rhs = convertMethod(rhs);
return lhs.compareTo(rhs);
}
private int compareFieldsAllowingNull(FieldAnnotation lhs, FieldAnnotation rhs) {
if (lhs == null || rhs == null) {
return compareNullElements(lhs, rhs);
}
lhs = convertField(lhs);
rhs = convertField(rhs);
if (DEBUG) {
System.err.println("Compare fields: " + lhs + " and " + rhs);
}
return lhs.compareTo(rhs);
}
private MethodAnnotation convertMethod(MethodAnnotation methodAnnotation) {
return ClassNameRewriterUtil.convertMethodAnnotation(classNameRewriter, methodAnnotation);
}
private FieldAnnotation convertField(FieldAnnotation fieldAnnotation) {
return ClassNameRewriterUtil.convertFieldAnnotation(classNameRewriter, fieldAnnotation);
}
@Override
public int compare(BugInstance lhs, BugInstance rhs) {
int cmp;
BugPattern lhsPattern = lhs.getBugPattern();
BugPattern rhsPattern = rhs.getBugPattern();
String lhsAbbrev, rhsAbbrev;
lhsAbbrev = lhsPattern.getAbbrev();
rhsAbbrev = rhsPattern.getAbbrev();
cmp = lhsAbbrev.compareTo(rhsAbbrev);
if (cmp != 0) {
if (DEBUG) {
System.err.println("bug abbrevs do not match");
}
return cmp;
}
cmp = compareClassesAllowingNull(lhs.getPrimaryClass(), rhs.getPrimaryClass());
if (cmp != 0) {
return cmp;
}
boolean havePrimaryMethods = lhs.getPrimaryMethod() != null && rhs.getPrimaryMethod() != null;
cmp = compareMethodsAllowingNull(lhs.getPrimaryMethod(), rhs.getPrimaryMethod());
if (cmp != 0) {
if (DEBUG) {
System.err.println("primary methods do not match");
}
return cmp;
}
if (!havePrimaryMethods) {
cmp = compareFieldsAllowingNull(lhs.getPrimaryField(), rhs.getPrimaryField());
if (cmp != 0) {
if (DEBUG) {
System.err.println("primary fields do not match");
}
return cmp;
}
}
return 0;
}
}