-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNearMatcher.java
87 lines (71 loc) · 2.38 KB
/
NearMatcher.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package cloneMatchingAlgorithms;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import database.Clone;
import database.Function;
import database.Functions;
import database.ToolsDB;
public class NearMatcher implements CloneMatcher {
public String toString() {
return "Near Match. Tolerence: " + this.tol;
}
private Connection conn;
private PreparedStatement stmt;
private long toolid;
private int tol;
public NearMatcher(int toolid, String init) throws IllegalArgumentException, SQLException, NumberFormatException {
this.toolid = toolid;
String [] options = init.split("\\s+");
if(options.length != 1)
throw new IllegalArgumentException("Should take 1 parameter.");
this.tol = Integer.parseInt(options[0]);
init();
}
private void init() throws SQLException {
String sql = "SELECT 1 FROM " + CloneMatcher.getTableName(this.toolid) + " where type1 = ? and name1 = ? and "
+ "startline1 >= ? and startline1 <= ? and "
+ "endline1 >= ? and endline1 <= ? and "
+ "type2 = ? and name2 = ? and "
+ "startline2 >= ? and startline2 <=? and "
+ "endline2 >= ? and endline2 <= ?";
this.conn = ToolsDB.getConnection();
this.stmt = conn.prepareStatement(sql);
}
@Override
public boolean isDetected(Clone clone) throws SQLException {
Clone alt = new Clone(clone.getFunction_id_two(), clone.getFunction_id_one());
return isDetected_helper(clone) || isDetected_helper(alt);
}
private boolean isDetected_helper(Clone clone) throws SQLException {
boolean retval = false;
Function f1 = Functions.get(clone.getFunction_id_one());
Function f2 = Functions.get(clone.getFunction_id_two());
stmt.setString(1, f1.getType());
stmt.setString(2, f1.getName());
stmt.setInt(3, f1.getStartline()-tol);
stmt.setInt(4, f1.getStartline()+tol);
stmt.setInt(5, f1.getEndline()-tol);
stmt.setInt(6, f1.getEndline()+tol);
stmt.setString(7, f2.getType());
stmt.setString(8, f2.getName());
stmt.setInt(9, f2.getStartline()-tol);
stmt.setInt(10, f2.getStartline()+tol);
stmt.setInt(11, f2.getEndline()-tol);
stmt.setInt(12, f2.getEndline()+tol);
ResultSet rs = stmt.executeQuery();
if(rs.next()) {
retval = true;
}
rs.close();
return retval;
}
@Override
public void close() throws SQLException {
stmt.close();
conn.close();
stmt = null;
conn = null;
}
}