Add found bugs to database.
authorChris Jaekl <chris@ringo.jaekl.net>
Sun, 27 Sep 2015 03:11:04 +0000 (12:11 +0900)
committerChris Jaekl <chris@ringo.jaekl.net>
Sun, 27 Sep 2015 03:11:04 +0000 (12:11 +0900)
go.sh
prod/net/jaekl/cfb/db/CfbSchema.java
prod/net/jaekl/cfb/db/driver/DbDriver.java
prod/net/jaekl/cfb/store/DbStore.java
prod/net/jaekl/cfb/store/Location.java
prod/net/jaekl/cfb/xml/BugInstance.java

diff --git a/go.sh b/go.sh
index a1b104b0acf6bd4c8856255d73e010e3b8b2293b..a1a24064794136dcbe69ee736a882c918742b11d 100755 (executable)
--- a/go.sh
+++ b/go.sh
@@ -5,4 +5,4 @@ find "${CFB_ROOT}/prod" -name "*.java" | xargs javac -g -classpath ${CFB_ROOT}/p
 cp -r ${CFB_ROOT}/prod/* ${CFB_ROOT}/bin/
 find "${CFB_ROOT}/prod" -name '*.class' -exec rm {} \;
 echo Launching...
-java -Djsse.enableSNIExtension=false net.jaekl.cfb.CFB $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10} ${11} ${12} ${13} ${14} ${15} ${16} ${17} ${18} ${19} ${20}
+java -ea -Djsse.enableSNIExtension=false net.jaekl.cfb.CFB $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10} ${11} ${12} ${13} ${14} ${15} ${16} ${17} ${18} ${19} ${20}
index 33c1b9e1c8ceb1baff08b20fa8964c2368175051..6125393a3331eb8b6f04ec2366143fc3aa99cc2f 100644 (file)
@@ -49,8 +49,7 @@ public class CfbSchema extends Schema {
                                        // Description of each possible bug
                                        { "BUG" },
                                        { BUGID, INTEGER, -1, NOT_NULL },
-                                       { TYPE, VARCHAR, 80, NOT_NULL }, 
-                                       { "CATEGORYID", INTEGER, -1, NOT_NULL }
+                                       { TYPE, VARCHAR, 80, NOT_NULL }
                                };
        private static final Object[][] CATEGORIES_DEFN = 
                                {
@@ -65,6 +64,7 @@ public class CfbSchema extends Schema {
                                        { "FOUND" },
                                        { FOUNDID, INTEGER, -1, NOT_NULL },
                                        { BUGID, INTEGER, -1, NOT_NULL },
+                                       { CATEGORYID, INTEGER, -1, NOT_NULL },
                                        { FIRSTLOCID, INTEGER, -1, NOT_NULL },
                                        { SECONDLOCID, INTEGER, -1, NULL },
                                        { THIRDLOCID, INTEGER, -1, NULL }
index ecfe2a7eae6bd48e4eef3d0160e0380a09a3274d..f85db06ca3345f66f7e9732f8fa3c75db1e2ca18 100644 (file)
@@ -8,7 +8,9 @@ import java.sql.Connection;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
+import java.sql.Timestamp;
 import java.util.ArrayList;
+import java.util.Date;
 import java.util.List;
 
 import net.jaekl.cfb.db.Column;
@@ -125,7 +127,15 @@ public abstract class DbDriver {
                                assert(data.length == table.getNumColumns());
                                
                                for (int col = 0; col < data.length; ++col) {
-                                       ps.setObject(col + 1, data[col]);
+                                       Object obj = data[col];
+                                       if (obj instanceof java.util.Date) {
+                                               Date date = (Date)obj;
+                                               Timestamp ts = new Timestamp(date.getTime());
+                                               ps.setTimestamp(col + 1, ts);
+                                       }
+                                       else {
+                                               ps.setObject(col + 1, data[col]);
+                                       }
                                        pendingValues++;
                                }
                                ps.addBatch();
index 7591c88d5ef8633ee4cfeafbd3a94b10070528a1..2238568c001bec9d9207419eb36806055a8d0abd 100644 (file)
@@ -2,6 +2,7 @@ package net.jaekl.cfb.store;
 
 import java.sql.Connection;
 import java.sql.SQLException;
+import java.util.ArrayList;
 import java.util.List;
 
 import net.jaekl.cfb.analyze.Analysis;
@@ -13,7 +14,9 @@ import net.jaekl.cfb.db.Row;
 import net.jaekl.cfb.db.Table;
 import net.jaekl.cfb.db.TypeMismatchException;
 import net.jaekl.cfb.db.driver.DbDriver;
+import net.jaekl.cfb.xml.BugClass;
 import net.jaekl.cfb.xml.BugInstance;
+import net.jaekl.cfb.xml.BugMethod;
 import net.jaekl.cfb.xml.SourceLine;
 import net.jaekl.cfb.xml.messages.MessageCollection;
 
@@ -28,7 +31,7 @@ public class DbStore {
                m_msgColl = msgColl;
        }
        
-       public boolean put(Analysis analysis) throws SQLException {
+       public boolean put(Analysis analysis) throws SQLException, TypeMismatchException {
                if (null == analysis) {
                        return false;
                }
@@ -59,31 +62,58 @@ public class DbStore {
                int row = 0;
                for (BugInstance bug : bugs)
                {
-                       long foundId = m_driver.nextVal(m_con, CfbSchema.FOUND_SEQ);
-                       long bugId = m_msgColl.getPattern(bug.getType()).getId();
-                       // values[row] = { foundId, bugId, firstLocId, secondLocId, thirdLocId }; 
+                       Long foundId = Long.valueOf(m_driver.nextVal(m_con, CfbSchema.FOUND_SEQ));
+                       Long bugId = Long.valueOf(m_msgColl.getPattern(bug.getType()).getId());
+                       Long categoryId = Long.valueOf(m_msgColl.getCategory(bug.getCategory()).getId());
+                       Location[] locs = computeLocations(bug);
+                       Location firstLoc  = (locs.length > 0) ? locs[0] : null;
+                       Location secondLoc = (locs.length > 1) ? locs[1] : null;
+                       Location thirdLoc  = (locs.length > 2) ? locs[2] : null;
+                       
+                       values[row][0] = foundId;
+                       values[row][1] = bugId;
+                       values[row][2] = categoryId;
+                       values[row][3] = getLocId(firstLoc);
+                       values[row][4] = getLocId(secondLoc);
+                       values[row][5] = getLocId(thirdLoc);
                        row++;
                }
                
-               return true;
+               count = m_driver.insert(m_con, CfbSchema.FOUND, values);
+               return (bugs.size() == count);
        }
        
        Location[] computeLocations(BugInstance bug)
        {
-               throw new UnsupportedOperationException("UNIMPLEMENTED");
+               ArrayList<Location> locs = new ArrayList<Location>();
+               
+               for (SourceLine line : bug.getLines()) {
+                       locs.add(new Location(line));
+               }
+               for (BugMethod method : bug.getMethods()) {
+                       locs.add(new Location(method));
+               }
+               for (BugClass clazz : bug.getClasses()) {
+                       locs.add(new Location(clazz));
+               }
+               
+               return locs.toArray(new Location[locs.size()]);
        }
        
-       long getLocId(SourceLine loc) throws SQLException, TypeMismatchException 
+       Long getLocId(Location loc) throws SQLException, TypeMismatchException 
        {
-               long locId = findLocId(loc);
-               if (locId >= 0) {
+               if (null == loc) {
+                       return null;
+               }
+               Long locId = findLocId(loc);
+               if (null != locId) {
                        return locId;
                }
 
                return storeLoc(loc);
        }
        
-       long findLocId(SourceLine loc) throws SQLException, TypeMismatchException
+       Long findLocId(Location loc) throws SQLException, TypeMismatchException
        {
                Column[] columns = { CfbSchema.LOCATIONS.getColumn(CfbSchema.LOCID) };
                Table[] tables = { CfbSchema.LOCATIONS };
@@ -92,6 +122,10 @@ public class DbStore {
                                                               loc.getClassName(),
                                                               Operation.EQUAL 
                                                             ),
+                                               new Condition( CfbSchema.LOCATIONS.getColumn(CfbSchema.METHODNAME),
+                                                                      loc.getMethodName(),
+                                                                      Operation.EQUAL
+                                                                    ),
                                                new Condition( CfbSchema.LOCATIONS.getColumn(CfbSchema.STARTLINE),
                                                                       loc.getStart(),
                                                                       Operation.EQUAL
@@ -108,21 +142,22 @@ public class DbStore {
                        return rows.get(0).getLong(0);
                }
                
-               return (-1);    // not found
+               return null;    // not found
        }
        
-       long storeLoc(SourceLine loc) throws SQLException
+       Long storeLoc(Location loc) throws SQLException
        {
                long locId = m_driver.nextVal(m_con, CfbSchema.LOC_SEQ);
                
                Object[][] values = { { 
-                                                       locId,
+                                                       Long.valueOf(locId),
                                                        loc.getClassName(),
+                                                       loc.getMethodName(),
                                                        Long.valueOf(loc.getStart()),
                                                        Long.valueOf(loc.getEnd())
                                                } };
                m_driver.insert(m_con, CfbSchema.LOCATIONS, values);
                
-               return locId;
+               return Long.valueOf(locId);
        }
 }
index 208c3db3dd10d8740e532ae9c89af39715499130..2cd91c80f70e5a81c5f5df4e8b00f97b55cd954b 100644 (file)
@@ -28,6 +28,11 @@ public class Location {
                m_className = bugClass.getClassName();
        }
        
+       public String getClassName() { return m_className; }
+       public String getMethodName() { return m_methodName; }
+       public int getStart() { return m_startLine; }
+       public int getEnd() { return m_endLine; }
+       
        private void init(SourceLine[] sourceLines) 
        {
                if (sourceLines.length > 0) {
index ee5bac0474aa3a24e38dcbfae3c8f7b7fb1f1db9..4c78f8d5606d62ed6ab991e3248236801ac94ab5 100644 (file)
@@ -2,8 +2,8 @@ package net.jaekl.cfb.xml;
 
 import java.io.PrintWriter;
 import java.util.ArrayList;
-
-import javax.tools.JavaFileManager.Location;
+import java.util.Collections;
+import java.util.List;
 
 import org.xml.sax.Attributes;
 
@@ -19,8 +19,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;
@@ -30,14 +32,18 @@ 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); }
        
        @Override
        public void endContents(String uri, String localName, String qName, String chars) 
@@ -84,6 +90,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 
@@ -93,6 +100,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);
                }