1 19 20 package edu.umd.cs.findbugs; 21 22 import java.io.Serializable ; 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 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 45 public SloppyBugComparator() { 46 } 47 48 public void setClassNameRewriter(ClassNameRewriter classNameRewriter) { 49 this.classNameRewriter = classNameRewriter; 50 } 51 52 private int compareNullElements(Object lhs, Object rhs) { 53 if (lhs == null && rhs == null) 54 return 0; 55 else 56 return (lhs == null) ? -1 : 1; 57 } 58 59 66 private int compareClassesAllowingNull(ClassAnnotation lhs, ClassAnnotation rhs) { 67 if (lhs == null || rhs == null) { 68 return compareNullElements(lhs, rhs); 69 } 70 71 String lhsClassName = classNameRewriter.rewriteClassName(lhs.getClassName()); 72 String 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 116 public int compare(BugInstance lhs, BugInstance rhs) { 117 118 int cmp; 119 120 BugPattern lhsPattern = lhs.getBugPattern(); 122 BugPattern rhsPattern = rhs.getBugPattern(); 123 String 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 cmp = compareClassesAllowingNull(lhs.getPrimaryClass(), rhs.getPrimaryClass()); 139 if (cmp != 0) 140 return cmp; 141 142 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 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 return 0; 158 } 159 160 164 private static String getAbbrevFromBugType(String type) { 165 int bar = type.indexOf('_'); 166 return (bar >= 0) ? type.substring(0, bar) : ""; 167 } 168 } 169 | Popular Tags |