import java.io.PrintWriter;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.xml.sax.Attributes;
+import net.jaekl.cfb.store.Location;
import net.jaekl.cfb.util.Util;
import net.jaekl.qd.xml.MissingAttributeException;
import net.jaekl.qd.xml.ParseResult;
static final String TAG = "BugInstance";
static final String[] INTERNAL = { };
- static final Object[][] EXTERNAL = { { BugClass.TAG, BugClass.class},
- { BugMethod.TAG, BugMethod.class},
- { LocalVariable.TAG, LocalVariable.class},
- { SourceLine.TAG, SourceLine.class} };
+ static final Object[][] EXTERNAL = { { BugClass.TAG, BugClass.class },
+ { BugMethod.TAG, BugMethod.class },
+ { Field.TAG, Field.class },
+ { LocalVariable.TAG, LocalVariable.class },
+ { SourceLine.TAG, SourceLine.class } };
static final String CATEGORY = "category";
static final String TYPE = "type";
+ Long m_id;
String m_category;
String m_type;
ArrayList<BugClass> m_classes;
ArrayList<BugMethod> m_methods;
- ArrayList<LocalVariable> m_locals;
+ ArrayList<Variable> m_vars;
ArrayList<SourceLine> m_lines;
+ ArrayList<Location> m_locations;
public BugInstance() {
super(TAG, INTERNAL, EXTERNAL);
+ m_id = null;
m_category = m_type = null;
m_classes = new ArrayList<BugClass>();
m_methods = new ArrayList<BugMethod>();
- m_locals = new ArrayList<LocalVariable>();
+ m_vars = new ArrayList<Variable>();
m_lines = new ArrayList<SourceLine>();
+ m_locations = new ArrayList<Location>();
+ }
+
+ public BugInstance(Long id,
+ String category,
+ String type,
+ Location[] locations,
+ Variable[] variables)
+ {
+ super(TAG, INTERNAL, EXTERNAL);
+
+ m_id = id;
+ m_category = category;
+ m_type = type;
+
+ m_classes = new ArrayList<BugClass>();
+ m_methods = new ArrayList<BugMethod>();
+ m_lines = new ArrayList<SourceLine>();
+
+ m_locations = new ArrayList<Location>(Arrays.asList(locations));
+ m_vars = new ArrayList<Variable>(Arrays.asList(variables));
}
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); }
+ public List<Variable> getVariables() { return Collections.unmodifiableList(m_vars); }
+ public List<Location> getLocations() { return Collections.unmodifiableList(m_locations); }
@Override
public void endContents(String uri, String localName, String qName, String chars)
throws XmlParseException
{
- // no-op
+ // no operation
+ }
+
+ @Override
+ public void complete()
+ {
+ computeLocations();
}
@Override
m_methods.add((BugMethod)pr);
}
}
+ else if (Field.TAG.equals(localName)) {
+ ParseResult[] collected = collectParsedChildren(Field.class);
+ for (ParseResult pr : collected) {
+ assert(pr instanceof Field);
+ m_vars.add((Field)pr);
+ }
+ }
else if (LocalVariable.TAG.equals(localName)) {
ParseResult[] collected = collectParsedChildren(LocalVariable.class);
for (ParseResult pr : collected) {
assert(pr instanceof LocalVariable);
- m_locals.add((LocalVariable)pr);
+ m_vars.add((LocalVariable)pr);
}
}
else if (SourceLine.TAG.equals(localName)) {
for (BugMethod bm : m_methods) {
bm.dump(pw, childIndent);
}
- for (LocalVariable lv : m_locals) {
- lv.dump(pw, childIndent);
+ for (Variable var : m_vars) {
+ pw.println(margin + " " + var.getDescription());
}
for (SourceLine sl : m_lines) {
sl.dump(pw, childIndent);
}
+ for (Location loc : m_locations) {
+ if (null != loc) {
+ loc.dump(pw, childIndent);
+ }
+ }
}
// Note that this is a heuristic, "fuzzy", equals.
return false;
}
- BugMethod thisMethod = this.getPrincipalMethod();
- BugMethod thatMethod = that.getPrincipalMethod();
- if (null == thisMethod) {
- if (null == thatMethod) {
+ Location thisLoc = this.getPrincipalLocation();
+ Location thatLoc = that.getPrincipalLocation();
+ if (null == thisLoc) {
+ if (null == thatLoc) {
return false;
}
}
else {
- if (! Util.objsAreEqual(thisMethod.getClassName(), thatMethod.getClassName())) {
+ if (! thisLoc.fuzzyEquals(thatLoc)) {
return false;
}
- if (! Util.objsAreEqual(thisMethod.getMethodName(), thatMethod.getMethodName())) {
- return false;
- }
- }
-
- if (! Util.objsAreEqual(this.getVariables(), that.getVariables())) {
- return false;
}
return true;
public int hashCode()
{
int code = Util.objHashCode(m_type)
- * Util.objHashCode(m_category)
- * Util.objHashCode(getPrincipalMethod())
- * Util.objHashCode(getVariables());
+ ^ Util.objHashCode(m_category)
+ ^ Util.objHashCode(getPrincipalLocation());
return code;
}
- // Get the "principal" method.
+ // Get the "principal" Location.
// This should be the place where the bug is reported.
- BugMethod getPrincipalMethod()
+ Location getPrincipalLocation()
{
- List<BugMethod> bugMethods = getMethods();
- if ((null == bugMethods) || (0 == bugMethods.size())) {
+ if (null == m_locations) {
return null;
}
- return bugMethods.get(0);
+
+ for (int idx = 0; idx < m_locations.size(); ++idx) {
+ Location loc = m_locations.get(idx);
+ if (Location.METHOD_CALLED.equals(loc.getMethodRole())) {
+ // METHOD_CALLED locations describe the method that is being called,
+ // but the bug is located in the caller, not in the callee.
+ // Thus, ignore this information about the callee.
+ continue;
+ }
+ return loc;
+ }
+
+ return null;
+ }
+
+ private void computeLocations()
+ {
+ assert(null != m_classes);
+ assert(null != m_methods);
+ assert(null != m_lines);
+
+ m_locations.clear();
+
+ /*
+ Somewhat unfortunate special case.
+ The primary "location" for a bug instance is split between tags.
+ Most bugs have a pattern like this:
+ <BugInstance>
+ ...
+ <Method>
+ <SourceLine .../>
+ </Method>
+ ...
+ <SourceLine .../>
+ </BugInstance>
+
+ The primary location for a bug is given by the <Method> with no role attribute,
+ but the <SourceLine/> inside that method describes the whole range of lines
+ covered by that Method, not the spot where the bug is located--that is given
+ by the <SourceLine/> that is a direct child fo the <BugInstance/>.
+ */
+
+ BugMethod primaryMethod = null;
+ SourceLine primaryLine = null;
+
+ for (BugMethod method : m_methods) {
+ if (null != method.getRole()) {
+ primaryMethod = method;
+ break;
+ }
+ }
+ if (m_lines.size() > 0) {
+ primaryLine = m_lines.get(0);
+ }
+
+ if ((null != primaryMethod) && (null != primaryLine)) {
+ m_locations.add(new Location(primaryMethod, primaryLine));
+ }
+
+ for (BugMethod method : m_methods) {
+ if (primaryMethod != method) {
+ m_locations.add(new Location(method));
+ }
+ }
+ for (SourceLine line : m_lines) {
+ if (primaryLine != line) {
+ m_locations.add(new Location(line));
+ }
+ }
+ for (BugClass clazz : m_classes) {
+ m_locations.add(new Location(clazz));
+ }
}
}