Add computation of deltas (differences between Analysis runs).
[cfb.git] / prod / net / jaekl / cfb / xml / BugInstance.java
index 4764c1ca5cef826a83bf0e6bf411b891370478c0..100251f9e9a37fce9235b30beb1492ebec24adc3 100644 (file)
@@ -2,9 +2,12 @@ package net.jaekl.cfb.xml;
 
 import java.io.PrintWriter;
 import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
 
 import org.xml.sax.Attributes;
 
+import net.jaekl.cfb.util.Util;
 import net.jaekl.qd.xml.MissingAttributeException;
 import net.jaekl.qd.xml.ParseResult;
 import net.jaekl.qd.xml.XmlParseException;
@@ -17,8 +20,10 @@ public class BugInstance extends ParseResult {
                                                 { BugMethod.TAG, BugMethod.class},
                                                 { LocalVariable.TAG, LocalVariable.class},
                                                 { SourceLine.TAG, SourceLine.class} };
+       static final String CATEGORY = "category";
        static final String TYPE = "type";
 
+       String m_category;
        String m_type;
        ArrayList<BugClass> m_classes;
        ArrayList<BugMethod> m_methods;
@@ -28,14 +33,19 @@ public class BugInstance extends ParseResult {
        public BugInstance() {
                super(TAG, INTERNAL, EXTERNAL);
                
-               m_type = null;
+               m_category = m_type = null;
                m_classes = new ArrayList<BugClass>();
                m_methods = new ArrayList<BugMethod>();
                m_locals = new ArrayList<LocalVariable>();
                m_lines = new ArrayList<SourceLine>();
        }
        
+       public String getCategory() { return m_category; }
        public String getType() { return m_type; }
+       public List<BugClass> getClasses() { return Collections.unmodifiableList(m_classes); }
+       public List<BugMethod> getMethods() { return Collections.unmodifiableList(m_methods); }
+       public List<SourceLine> getLines() { return Collections.unmodifiableList(m_lines); }
+       public List<LocalVariable> getVariables() { return Collections.unmodifiableList(m_locals); }
        
        @Override
        public void endContents(String uri, String localName, String qName, String chars) 
@@ -82,6 +92,7 @@ public class BugInstance extends ParseResult {
        public void handleMainAttributes(Attributes attr) throws MissingAttributeException
        {
                m_type = this.getRequiredAttr(TAG, attr, TYPE);
+               m_category = this.getRequiredAttr(TAG, attr, CATEGORY);
        }
 
        @Override 
@@ -91,6 +102,7 @@ public class BugInstance extends ParseResult {
                String margin = String.format("%" + indent + "s", "");
                
                pw.println(margin + TAG + " (" + m_type + ")");
+               pw.println(margin + CATEGORY + " (" + m_category + ")");
                for (BugClass bc : m_classes) {
                        bc.dump(pw, childIndent);
                }
@@ -104,4 +116,77 @@ public class BugInstance extends ParseResult {
                        sl.dump(pw, childIndent);
                }
        }
+       
+       // Note that this is a heuristic, "fuzzy", equals.
+       // Two BugInstances will be considered equal if:
+       //   - they refer to the same bug type
+       //   - they took place in the same class and method
+       //   - the variable names referred to (if any) match
+       // In particular, this equality test does not check 
+       // for line numbers being equal.  This is by design;
+       // we want to consider two bugs to be the "same bug" 
+       // even if other changes in the file have shifted 
+       // line numbers a bit.
+       @Override
+       public boolean equals(Object obj) 
+       {
+               if (null == obj) { 
+                       return false;
+               }
+               if (obj instanceof BugInstance) {
+                       BugInstance that = (BugInstance)obj;
+                       
+                       if (! Util.objsAreEqual(this.m_type, that.m_type)) {
+                               return false;
+                       }
+                       
+                       if (! Util.objsAreEqual(this.m_category, that.m_category)) {
+                               return false;
+                       }
+
+                       BugMethod thisMethod = this.getPrincipalMethod();
+                       BugMethod thatMethod = that.getPrincipalMethod();
+                       if (null == thisMethod) {
+                               if (null == thatMethod) {
+                                       return false;
+                               }
+                       }
+                       else {
+                               if (! Util.objsAreEqual(thisMethod.getClassName(), thatMethod.getClassName())) {
+                                       return false;
+                               }
+                               if (! Util.objsAreEqual(thisMethod.getMethodName(), thatMethod.getMethodName())) {
+                                       return false;
+                               }
+                       }
+                       
+                       if (! Util.objsAreEqual(this.getVariables(), that.getVariables())) {
+                               return false;
+                       }
+                       
+                       return true;
+               }
+               return false;
+       }
+       
+       @Override
+       public int hashCode() 
+       {
+               int code = Util.objHashCode(m_type)
+                                * Util.objHashCode(m_category)
+                                * Util.objHashCode(getPrincipalMethod())
+                                * Util.objHashCode(getVariables());
+               return code;
+       }
+       
+       // Get the "principal" method.
+       // This should be the place where the bug is reported.
+       BugMethod getPrincipalMethod()
+       {
+               List<BugMethod> bugMethods = getMethods();
+               if ((null == bugMethods) || (0 == bugMethods.size())) {
+                       return null;
+               }
+               return bugMethods.get(0);
+       }
 }