This commit is contained in:
Kui LIU
2017-09-19 21:41:53 +02:00
parent 72116994e4
commit c8137c23e2
51 changed files with 111530 additions and 631 deletions
@@ -28,6 +28,8 @@ public class Tokenizer {
tokens += astNodeType + " arrayInitializer ";
} else if ("LambdaExpression".equals(astNodeType)) {
tokens += astNodeType + " lambda ";
} else if ("NumberLiteral".equals(astNodeType)) {
tokens += astNodeType + " numberLiteral ";
} else {
tokens += astNodeType + " " + simpleTree.getLabel() + " ";
}
@@ -0,0 +1,121 @@
package edu.lu.uni.serval.FixPatternParser.violations;
public enum FieldViolation {
UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR,
ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD,
LI_LAZY_INIT_STATIC,
URF_UNREAD_FIELD,
MS_SHOULD_BE_FINAL,
URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD,
UWF_UNWRITTEN_FIELD,
MS_PKGPROTECT,
SS_SHOULD_BE_STATIC,
NP_UNWRITTEN_FIELD,
UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD,
SE_BAD_FIELD,
UUF_UNUSED_FIELD,
SE_BAD_FIELD_STORE,
MS_MUTABLE_ARRAY,
NP_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD,
}
enum ClassViolation{
SIC_INNER_SHOULD_BE_STATIC_ANON, //inner class. ClassInstanceCreation by overriding some methods.
SIC_INNER_SHOULD_BE_STATIC,
RI_REDUNDANT_INTERFACES,
SE_COMPARATOR_SHOULD_BE_SERIALIZABLE,
SE_NO_SERIALVERSIONID,
CI_CONFUSED_INHERITANCE,
SE_INNER_CLASS,
NM_CLASS_NOT_EXCEPTION,
}
enum MethodViolation {
NM_METHOD_NAMING_CONVENTION,
UPM_UNCALLED_PRIVATE_METHOD,
DE_MIGHT_IGNORE,
NM_CONFUSING,
CN_IMPLEMENTS_CLONE_BUT_NOT_CLONEABLE,
UC_USELESS_VOID_METHOD,
}
enum StmtViolation {
DLS_DEAD_LOCAL_STORE, //Assignment statement. false positives may be high.
//This instruction assigns a value to a local variable, but the value is not read or used in any subsequent instruction.
DM_BOXED_PRIMITIVE_FOR_PARSING,
DM_CONVERT_CASE,
DM_DEFAULT_ENCODING,
IS2_INCONSISTENT_SYNC,
NP_GUARANTEED_DEREF,
SIO_SUPERFLUOUS_INSTANCEOF,
NP_BOOLEAN_RETURN_NULL,
SF_SWITCH_FALLTHROUGH,
DP_DO_INSIDE_DO_PRIVILEGED,
DMI_INVOKING_TOSTRING_ON_ARRAY,
NP_NONNULL_PARAM_VIOLATION,
CN_IDIOM_NO_SUPER_CALL,
UL_UNRELEASED_LOCK_EXCEPTION_PATH,
RC_REF_COMPARISON_BAD_PRACTICE,
NP_ALWAYS_NULL,
NP_NULL_PARAM_DEREF,
GC_UNRELATED_TYPES,
DLS_DEAD_LOCAL_STORE_OF_NULL,
RpC_REPEATED_CONDITIONAL_TEST,
ODR_OPEN_DATABASE_RESOURCE_EXCEPTION_PATH,
DB_DUPLICATE_SWITCH_CLAUSES,
RV_RETURN_VALUE_IGNORED,
ES_COMPARING_PARAMETER_STRING_WITH_EQ,
RCN_REDUNDANT_NULLCHECK_OF_NULL_VALUE,
ICAST_IDIV_CAST_TO_DOUBLE,
BC_VACUOUS_INSTANCEOF,
EC_UNRELATED_TYPES,
ICAST_INTEGER_MULTIPLY_CAST_TO_LONG,
DM_STRING_TOSTRING,
DMI_HARDCODED_ABSOLUTE_FILENAME,
MS_MUTABLE_COLLECTION_PKGPROTECT,
NP_NULL_ON_SOME_PATH_EXCEPTION,
SQL_NONCONSTANT_STRING_PASSED_TO_EXECUTE,
EQ_COMPARETO_USE_OBJECT_EQUALS,
BX_UNBOXING_IMMEDIATELY_REBOXED,
HE_EQUALS_USE_HASHCODE,
DM_EXIT,
FE_FLOATING_POINT_EQUALITY,
RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE,
BC_UNCONFIRMED_CAST,
REC_CATCH_EXCEPTION,
BC_UNCONFIRMED_CAST_OF_RETURN_VALUE,
VA_FORMAT_STRING_USES_NEWLINE,
RV_RETURN_VALUE_IGNORED_BAD_PRACTICE,
EI_EXPOSE_REP2,
WMI_WRONG_MAP_ITERATOR,
EI_EXPOSE_REP,
NP_LOAD_OF_KNOWN_NULL_VALUE,
DM_NUMBER_CTOR,
SBSC_USE_STRINGBUFFER_CONCATENATION,
OS_OPEN_STREAM_EXCEPTION_PATH,
NP_NONNULL_RETURN_VIOLATION,
SF_SWITCH_NO_DEFAULT,
RV_RETURN_VALUE_IGNORED_NO_SIDE_EFFECT,
ODR_OPEN_DATABASE_RESOURCE,
PZLA_PREFER_ZERO_LENGTH_ARRAYS,
NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE,
UCF_USELESS_CONTROL_FLOW,
UC_USELESS_CONDITION,
NP_NULL_ON_SOME_PATH,
DM_FP_NUMBER_CTOR,
SQL_PREPARED_STATEMENT_GENERATED_FROM_NONCONSTANT_STRING,
OBL_UNSATISFIED_OBLIGATION_EXCEPTION_EDGE,
ES_COMPARING_STRINGS_WITH_EQ,
OS_OPEN_STREAM,
RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE,
NP_PARAMETER_MUST_BE_NONNULL_BUT_MARKED_AS_NULLABLE,
UC_USELESS_OBJECT,
OBL_UNSATISFIED_OBLIGATION,
}
@@ -7,13 +7,12 @@ import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.github.gumtreediff.tree.ITree;
import edu.lu.uni.serval.FixPatternParser.Tokenizer;
import edu.lu.uni.serval.config.Configuration;
import edu.lu.uni.serval.diffentry.DiffEntryHunk;
import edu.lu.uni.serval.diffentry.DiffEntryReader;
import edu.lu.uni.serval.gumtree.GumTreeGenerator;
import edu.lu.uni.serval.gumtree.GumTreeGenerator.GumTreeType;
import edu.lu.uni.serval.gumtree.regroup.HierarchicalActionSet;
@@ -29,8 +28,8 @@ import edu.lu.uni.serval.gumtree.regroup.SimplifyTree;
*/
public class FixedViolationHunkParser extends FixedViolationParser {
@SuppressWarnings("unused")
private static Logger log = LoggerFactory.getLogger(FixedViolationHunkParser.class);
private static final int THRESHOLD_LINE = 3;
public String testingInfo = "";
public int nullMappingGumTreeResult = 0;
@@ -38,6 +37,7 @@ public class FixedViolationHunkParser extends FixedViolationParser {
public int largeHunk = 0;
public int nullSourceCode = 0;
public int nullMatchedDiffEntry = 0;
public int testInfos = 0;
public FixedViolationHunkParser() {
}
@@ -48,66 +48,30 @@ public class FixedViolationHunkParser extends FixedViolationParser {
@Override
public void parseFixPatterns(File prevFile, File revFile, File diffentryFile) {
List<Violation> allViolations = readViolations(prevFile, revFile);
// GumTree results
// TODO remove the modification of variable names or not? FIXME
List<HierarchicalActionSet> actionSets = parseChangedSourceCodeWithGumTree2(prevFile, revFile); // only remove non-statement source code, eg. method declaration
List<Violation> violations = readViolations(revFile.getName());
List<HierarchicalActionSet> actionSets = parseChangedSourceCodeWithGumTree2(prevFile, revFile);
if (this.resultType != 0) {
String type = "";
switch (this.resultType) {
case 1:
if (this.resultType == 1) {
type = "#NullGumTreeResult:";
break;
case 2:
} else if (this.resultType == 2) {
type = "#NoSourceCodeChange:";
break;
case 3:
} else if (this.resultType == 3) {
type = "#NoStatementChange:";
break;
}
for (Violation v : violations) {
System.err.println(type + revFile.getName().replace("#", "/") + ":" + v.getStartLineNum() + ":" + v.getEndLineNum() + ":" + v.getAlarmType());
for (Violation v : allViolations) {
System.err.println(type + revFile.getName() + ":" + v.getStartLineNum() + ":" + v.getEndLineNum() + ":" + v.getViolationType());
}
return;
} else {
if (violations.size() == 0) {
this.resultType = 4;
return;
}
// List<DiffEntryHunk> diffentryHunks1 = new DiffEntryReader().readHunks2(diffentryFile);
// // Select hunks by positions of violations.
// for (Violation violation : violations) {
// int violationStartLineNum = violation.getStartLineNum();
// int violationEndLineNum = violation.getEndLineNum();
// for (int index = 0, hunkListSize = diffentryHunks1.size(); index < hunkListSize; index ++) {
// DiffEntryHunk hunk = diffentryHunks1.get(index);
// int startLine = hunk.getBugLineStartNum();
// int range = hunk.getBugRange();
// if (violationStartLineNum > startLine + range - 1) continue;
// if (violationEndLineNum < startLine) break;
//
// if (violation.getBugStartLineNum() == 0) {
// violation.setBugStartLineNum(startLine);
// violation.setFixStartLineNum(hunk.getFixLineStartNum());
// }
// violation.setBugEndLineNum(startLine + range - 1);
// violation.setFixEndLineNum(hunk.getFixLineStartNum() + hunk.getFixRange() - 1);
// violation.getHunks().add(hunk);
// }
// if (violation.getBugStartLineNum() == 0 && violation.getBugEndLineNum() == 0) {
// // This fixed violation cannot be matched with a DiffEntry, it is difficult to identify related source code change for it.
// nullMatchedDiffEntry ++;
// log.warn("#Null-DiffEntry: " + revFile.getName().replace("#", "/") + " : " +violation.getStartLineNum() + " : " +
// violation.getBugEndLineNum() + " : " + violation.getAlarmType());
// }
// }
List<DiffEntryHunk> diffentryHunks = new DiffEntryReader().readHunks2(diffentryFile);
// Identify DiffEntry hunks by positions of violations.
List<Violation> violations = identifyFixRangeHeuristically(allViolations, diffentryHunks, revFile);
//Filter out the modify actions, which are not in the DiffEntry hunks.
HunkActionFilter hunkFilter = new HunkActionFilter();
List<Violation> selectedViolations = hunkFilter.filterActionsByModifiedRange2(violations, actionSets, revFile, prevFile);
violations.removeAll(selectedViolations);
this.nullMappingGumTreeResult += violations.size();
this.nullMappingGumTreeResult += violations.size() - selectedViolations.size();
for (Violation violation : selectedViolations) {
List<HierarchicalActionSet> hunkActionSets = violation.getActionSets();
@@ -193,15 +157,16 @@ public class FixedViolationHunkParser extends FixedViolationParser {
}
}
if (fixStartLine == 0) {
if (fixStartLine == 0 && bugStartLine != 0) {
// pure delete actions.
this.pureDeletions ++;
System.err.println("#PureDeletion:" + revFile.getName().replace("#", "/") + ":" + violation.getStartLineNum()
+ ":" + violation.getEndLineNum() + ":" + violation.getAlarmType());
// continue;
System.err.println("#PureDeletion:" + revFile.getName() + ":" + violation.getStartLineNum()
+ ":" + violation.getEndLineNum() + ":" + violation.getViolationType());
// get the exact buggy code by violation's position. TODO later
}
for (HierarchicalActionSet hunkActionSet : hunkActionSets) {
// TODO simplify buggy tree with buggy code.
SimplifyTree abstractIdentifier = new SimplifyTree();
abstractIdentifier.abstractTree(hunkActionSet, bugEndPosition);
SimpleTree simpleT = hunkActionSet.getSimpleTree();
@@ -212,19 +177,17 @@ public class FixedViolationHunkParser extends FixedViolationParser {
}
// if (children.size() == 0) continue;
boolean isInsert = false;
boolean isPureInsert = false;
if (bugStartLine == 0) {
bugStartLine = violation.getStartLineNum();
bugEndLine = violation.getEndLineNum();
isInsert = true;
isPureInsert = true;
// continue;
}
if (bugEndLine - bugStartLine > Configuration.HUNK_SIZE || fixEndLine - fixStartLine > Configuration.HUNK_SIZE) {
if (fixStartLine != 0) {
this.largeHunk ++;
System.err.println("#LargeHunk:" + revFile.getName().replace("#", "/") + ":" + violation.getStartLineNum()
+ ":" + violation.getEndLineNum() + ":" + violation.getAlarmType());
}
this.largeHunk ++;
System.err.println("#LargeHunk:" + revFile.getName() + ":" + violation.getStartLineNum()
+ ":" + violation.getEndLineNum() + ":" + violation.getViolationType());
continue;
}
@@ -232,13 +195,11 @@ public class FixedViolationHunkParser extends FixedViolationParser {
simpleTree.setParent(null);
// Source Code of patches.
String patchSourceCode = getPatchSourceCode(prevFile, revFile, bugStartLine, bugEndLine, fixStartLine, fixEndLine, isInsert);
String patchSourceCode = getPatchSourceCode(prevFile, revFile, bugStartLine, bugEndLine, fixStartLine, fixEndLine, isPureInsert);
if ("".equals(patchSourceCode)) {
if (fixStartLine != 0) {
this.nullSourceCode ++;
System.err.println("#NullSourceCode:" + revFile.getName().replace("#", "/") + ":" + violation.getStartLineNum()
+ ":" + violation.getEndLineNum() + ":" + violation.getAlarmType());
}
this.nullSourceCode ++;
System.err.println("#NullSourceCode:" + revFile.getName() + ":" + violation.getStartLineNum()
+ ":" + violation.getEndLineNum() + ":" + violation.getViolationType());
continue;
}
@@ -257,23 +218,24 @@ public class FixedViolationHunkParser extends FixedViolationParser {
// this.nullMappingGumTreeResult ++;
// continue;
// }
String alarmType = violation.getAlarmType();
String alarmType = violation.getViolationType();
// String patchPosition = "\n" + revFile.getName() + "Position: " + violation.getStartLineNum() + " --> " + violation.getEndLineNum() + "\n@@ -" + bugStartLine + ", " + bugEndLine + " +" + fixStartLine + ", " + fixEndLine + "@@\n";
// String patchPosition = "\n" + "Position: " + violation.getStartLineNum() + " --> " + violation.getEndLineNum() + "\n@@ -" + bugStartLine + ", " + bugEndLine + " +" + fixStartLine + ", " + fixEndLine + "@@\n";
// String info = Configuration.PATCH_SIGNAL + "\nAlarm Type :" + violation.getAlarmType() + "\n" + patchPosition + patchSourceCode + "\nAST Diff###:\n" + getAstEditScripts(hunkActionSets, bugEndPosition, fixEndPosition) + "\n";
String patchPosition = "\n" + "Position: " + violation.getStartLineNum() + " --> " + violation.getEndLineNum() + "\n@@ -" + bugStartLine + ", " + bugEndLine + " +" + fixStartLine + ", " + fixEndLine + "@@\n";
String info = Configuration.PATCH_SIGNAL + "\nAlarm Type :" + violation.getAlarmType() + "\n" + patchSourceCode + "\n" + patchPosition + revFile.getName() + "\n";
String info = Configuration.PATCH_SIGNAL + "\nAlarm Type :" + violation.getViolationType() + "\n" + patchSourceCode + "\n" + patchPosition + revFile.getName() + "\n";
if (noUpdate(editScriptTokens)) {
if (!"SE_NO_SERIALVERSIONID".equals(alarmType)) {
if (containsFiledDeclaration(hunkActionSets)) {
// this.nullMappingGumTreeResult ++; //TODO
if (fixStartLine != 0) {
this.testingInfo += info + revFile.getName() + "\n";
this.testingInfo += "#TestingInfo: " + revFile.getName().replace("#", "/") + ":" + violation.getStartLineNum()
+ ":" + violation.getEndLineNum() + ":" + violation.getAlarmType();
}
this.testingInfo += info + revFile.getName() + "\n";
this.testingInfo += "#TestingInfo: " + revFile.getName() + ":" + violation.getStartLineNum()
+ ":" + violation.getEndLineNum() + ":" + violation.getViolationType();
System.err.println("#TestingInfo:" + revFile.getName() + ":" + violation.getStartLineNum()
+ ":" + violation.getEndLineNum() + ":" + violation.getViolationType());
this.testInfos ++;
continue;
}
}
@@ -292,11 +254,12 @@ public class FixedViolationHunkParser extends FixedViolationParser {
} else {
// this.nullMappingGumTreeResult ++; // TODO
if (fixStartLine != 0) {
this.testingInfo += info + revFile.getName() + "\n";
this.testingInfo += "#TestingInfo: " + revFile.getName().replace("#", "/") + ":" + violation.getStartLineNum()
+ ":" + violation.getEndLineNum() + ":" + violation.getAlarmType();
}
this.testingInfo += info + revFile.getName() + "\n";
this.testingInfo += "#TestingInfo: " + revFile.getName() + ":" + violation.getStartLineNum()
+ ":" + violation.getEndLineNum() + ":" + violation.getViolationType();
System.err.println("#TestingInfo:" + revFile.getName() + ":" + violation.getStartLineNum()
+ ":" + violation.getEndLineNum() + ":" + violation.getViolationType());
this.testInfos ++;
continue;
}
}
@@ -304,7 +267,7 @@ public class FixedViolationHunkParser extends FixedViolationParser {
this.patchesSourceCode += info;
this.sizes += size + "\n";
this.astEditScripts += astEditScripts + "\n";
this.alarmTypes += alarmType + "\n";
this.violationTypes += alarmType + "\n";
// this.buggyTrees += Configuration.BUGGY_TREE_TOKEN + "\n" + simpleTree.toString() + "\n";
String tokens = Tokenizer.getTokensDeepFirst(simpleTree).trim();
if ("Block Block".equals(tokens)) {
@@ -318,6 +281,149 @@ public class FixedViolationHunkParser extends FixedViolationParser {
}
}
// private List<HierarchicalActionSet> getExactActionSets(Violation violation,
// List<HierarchicalActionSet> hunkActionSets) {
// List<HierarchicalActionSet> deleteActionSets = new ArrayList<>();
// for (HierarchicalActionSet hunkActionSet : hunkActionSets) {
// int startLine = hunkActionSet.getBugStartLineNum();
// int endLine = hunkActionSet.getBugEndLineNum();
// if (violation.getStartLineNum() <= startLine && endLine <= violation.getEndLineNum()) {
// String astNodeType = hunkActionSet.getAstNodeType();
// if (astNodeType.endsWith("Statement") || astNodeType.endsWith("FieldDeclaration")) {
//
// }
// }
// }
// return deleteActionSets;
// }
private List<Violation> identifyFixRangeHeuristically(List<Violation> violations, List<DiffEntryHunk> diffentryHunks, File revFile) {
List<Violation> selectedViolations = new ArrayList<>();
for (Violation violation : violations) {
int violationStartLineNum = violation.getBugStartLineNum();
if (violationStartLineNum > 0) {
int violationEndLineNum = violation.getEndLineNum();
for (int index = 0, hunkListSize = diffentryHunks.size(); index < hunkListSize; index ++) {
DiffEntryHunk hunk = diffentryHunks.get(index);
int bugStartLine = hunk.getBugLineStartNum();
int bugRange = hunk.getBugRange();
if (violationStartLineNum > bugStartLine + bugRange - 1) continue;
if (violationEndLineNum < bugStartLine) break;
int fixStartLine = hunk.getFixLineStartNum();
String diffentry = hunk.getHunk();
BufferedReader reader = new BufferedReader(new StringReader(diffentry));
String line = null;
try {
int currentBugLine = bugStartLine - 1;
int currentFixLine = fixStartLine - 1;
int fixedLines = 0;
int bugS = 0;
int fixS = 0;
int fixE = 0;
int bugFixStartLine = 0; // the heuristic inferred fix start line of the corresponding bug
int bugFixEndLine = 0; // the heuristic inferred fix end line of the corresponding bug
boolean isFixRange= false;
while ((line = reader.readLine()) != null) {
if (line.startsWith("+")) {
currentFixLine ++;
fixedLines++;
if ((violationStartLineNum <= currentBugLine && currentBugLine <= violationEndLineNum) ||
(violationStartLineNum <= bugS && bugS <= violationEndLineNum)) {
if (fixS == 0) fixS = currentFixLine;
if (bugFixStartLine == -1) bugFixStartLine = currentFixLine;
fixE = currentFixLine;
if (isFixRange) bugFixEndLine = currentFixLine;
} else {
fixedLines = 0;
bugS = 0;
}
// bugS = 0; // fixS = 0;
} else if (line.startsWith("-")) {
currentBugLine ++;
if (bugS == 0 || fixS != 0) bugS = currentBugLine;// currentBugLine may be larger than the violation end line.
// INS combined with DEL, UPD or MOV.
// Infer the fix range for a violation heuristically.
if (currentBugLine >= violation.getStartLineNum() && currentBugLine <= violation.getEndLineNum()) {
bugFixStartLine = -1;
}
if (currentBugLine <= violation.getEndLineNum() && bugFixStartLine != 0) {
isFixRange = true;
}
if (bugFixEndLine > 0) isFixRange = false;
if (bugFixStartLine <= 0) fixS = 0;
} else {
currentBugLine ++;
currentFixLine ++;
// pure INS
// Infer the fix range for a violation heuristically.
if (currentBugLine == violation.getStartLineNum()) {
if (bugFixStartLine <= 0) {
if (fixedLines != 0) {
if (fixedLines > THRESHOLD_LINE) {
fixedLines = THRESHOLD_LINE;// insert 3 lines.
}
bugFixStartLine = currentFixLine - fixedLines;
if (bugFixStartLine < violationStartLineNum) {
bugFixStartLine = violationStartLineNum;
}
}
else bugFixStartLine = currentFixLine;
}
}
if (currentBugLine == violation.getEndLineNum()) {
if (bugFixStartLine != 0 && bugFixEndLine == 0) {
bugFixEndLine = currentFixLine + THRESHOLD_LINE;
if (bugFixEndLine > violationEndLineNum) {
bugFixEndLine = violationEndLineNum;
}
}
}
if (bugFixEndLine > 0) isFixRange = false;
}
if ((violationStartLineNum <= currentBugLine && currentBugLine <= violationEndLineNum)) {
if (!violation.getHunks().contains(hunk))
violation.getHunks().add(hunk);
}
}
if (violation.getFixStartLineNum() == 0) {
violation.setFixStartLineNum(fixS);
}
violation.setFixEndLineNum(fixE);
if (violation.getBugFixStartLineNum() == 0 && bugFixStartLine > 0) {
violation.setBugFixStartLineNum(bugFixStartLine);
}
if (bugFixEndLine > 0) {
violation.setBugFixEndLineNum(bugFixEndLine);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
if (violation.getHunks().size() == 0) {
// This fixed violation cannot be matched with a DiffEntry, it is difficult to identify related source code change for it.
this.nullMatchedDiffEntry ++;
System.err.println("#NullDiffEntry: " + revFile.getName() + " : " +violation.getStartLineNum() + " : " + violation.getEndLineNum() + " : " + violation.getViolationType());
} else {
selectedViolations.add(violation);
}
}
}
return selectedViolations;
}
private boolean containsFiledDeclaration(List<HierarchicalActionSet> hunkActionSets) {
for (HierarchicalActionSet hunkActionSet : hunkActionSets) {
if (hunkActionSet.getAstNodeType().equals("FieldDeclaration")) {
@@ -15,6 +15,7 @@ import edu.lu.uni.serval.gumtree.regroup.HierarchicalActionSet;
import edu.lu.uni.serval.gumtree.regroup.HierarchicalRegrouper;
import edu.lu.uni.serval.utils.FileHelper;
import edu.lu.uni.serval.utils.ListSorter;
import edu.lu.uni.serval.violation.code.parser.ViolationSourceCodeTree;
/**
* Parse fix patterns with GumTree.
@@ -35,8 +36,8 @@ public class FixedViolationParser extends Parser {
public int resultType = 0;
private File positionFile = null;
protected String alarmTypes = "";
protected List<Violation> uselessViolations;
protected String violationTypes = "";
// protected List<Violation> uselessViolations;
public void setPositionFile(File positionFile) {
this.positionFile = positionFile;
@@ -66,18 +67,18 @@ public class FixedViolationParser extends Parser {
} else {
// Regroup GumTre results.
List<HierarchicalActionSet> allActionSets = new HierarchicalRegrouper().regroupGumTreeResults(gumTreeResults);
for (HierarchicalActionSet actionSet : allActionSets) {
String astNodeType = actionSet.getAstNodeType();
if (astNodeType.endsWith("Statement") || "FieldDeclaration".equals(astNodeType)) {
actionSets.add(actionSet);
}
}
// for (HierarchicalActionSet actionSet : allActionSets) {
// String astNodeType = actionSet.getAstNodeType();
// if (astNodeType.endsWith("Statement") || "FieldDeclaration".equals(astNodeType)) {
// actionSets.add(actionSet);
// }
// }
// Filter out modified actions of changing method names, method parameters, variable names and field names in declaration part.
// TODO: variable effects range, sub-actions are these kinds of modification?
// variable effects range, sub-actions are these kinds of modification?
// actionSets.addAll(new ActionFilter().filterOutUselessActions(allActionSets));
ListSorter<HierarchicalActionSet> sorter = new ListSorter<>(actionSets);
ListSorter<HierarchicalActionSet> sorter = new ListSorter<>(allActionSets);
actionSets = sorter.sortAscending();
if (actionSets.size() == 0) {
@@ -88,7 +89,8 @@ public class FixedViolationParser extends Parser {
}
}
protected List<Violation> readViolations(String fileName) {
protected List<Violation> readViolations(File prevFile, File revFile) {
String fileName = revFile.getName();
List<Violation> violations = new ArrayList<>();
String fileContent = FileHelper.readFile(positionFile);
BufferedReader reader = null;
@@ -99,12 +101,24 @@ public class FixedViolationParser extends Parser {
String[] positionStr = line.split(":");
int startLine = Integer.parseInt(positionStr[1]);
int endLine = Integer.parseInt(positionStr[2]);
String alarmType = positionStr[0];
String violationType = positionStr[0];
Violation violation = new Violation(startLine, endLine, alarmType);
violation.setFileName(fileName.replaceAll("#", "/"));
if (uselessViolations.contains(violation)) {
continue;
// if ("UPM_UNCALLED_PRIVATE_METHOD".equals(violationType)) continue;
Violation violation = new Violation(startLine, endLine, violationType);
violation.setFileName(fileName);
/*
* Get the parent range of a violation.
* Read DiffEntries with this range to get the start line and end line of a violation.
*/
ViolationSourceCodeTree alarmTree = new ViolationSourceCodeTree(prevFile, startLine, endLine);
alarmTree.locateParentNode(violationType);
int violationStartLine = alarmTree.getViolationFinalStartLine();
violation.setBugStartLineNum(violationStartLine);
if (violationStartLine > 0) {// 0: no source code, -1: range is too large, which contains several inner classes, methods or fields.
// FileHelper.outputToFile("logs/testV3.txt", "\n", true);
violation.setBugEndLineNum(alarmTree.getViolationFinalEndLine());
}
violations.add(violation);
}
@@ -165,10 +179,10 @@ public class FixedViolationParser extends Parser {
}
public String getAlarmTypes() {
return alarmTypes;
return violationTypes;
}
public void setUselessViolations(List<Violation> uselessViolations) {
this.uselessViolations = uselessViolations;
}
// public void setUselessViolations(List<Violation> uselessViolations) {
// this.uselessViolations = uselessViolations;
// }
}
@@ -39,7 +39,7 @@ public class FixedViolationSingleStatementParser extends FixedViolationParser {
}
// Read the positions of checked violations
List<Violation> violations = readViolations(revFile.getName());
List<Violation> violations = readViolations(prevFile, revFile);
for (HierarchicalActionSet actionSet : actionSets) {
// position of buggy statements
int startPosition = 0;
@@ -127,7 +127,7 @@ public class FixedViolationSingleStatementParser extends FixedViolationParser {
this.patchesSourceCode += Configuration.PATCH_SIGNAL + "\n" + revFile.getName() + "\n" + patchSourceCode + "\n";
this.sizes += size + "\n";
this.astEditScripts += astEditScripts + "\n";
this.alarmTypes += violation.getAlarmType() + "\n";
this.violationTypes += violation.getViolationType() + "\n";
// 2. source code: raw tokens
// String rawTokenEditScripts = getRawTokenEditScripts(actionSet);
// // 3. abstract identifiers:
@@ -13,17 +13,19 @@ public class Violation implements Comparable<Violation> {
private int endLineNum;
private int bugStartLineNum;
private int bugEndLineNum;
private int fixStartLineNum;
private int fixStartLineNum = 0;
private int fixEndLineNum;
private String alarmType;
private String violationType;
private List<DiffEntryHunk> hunks = new ArrayList<>();
private List<HierarchicalActionSet> actionSets;
private int bugFixStartLineNum = 0; // the heuristic matched fix start line of a violation
private int bugFixEndLineNum = 0; // the heuristic matched fix end line of a violation
public Violation(Integer startLineNum, int endLineNum, String alarmType) {
public Violation(Integer startLineNum, int endLineNum, String violationType) {
super();
this.startLineNum = startLineNum;
this.endLineNum = endLineNum;
this.alarmType = alarmType;
this.violationType = violationType;
this.actionSets = new ArrayList<>();
}
@@ -83,14 +85,30 @@ public class Violation implements Comparable<Violation> {
this.hunks = hunks;
}
public String getAlarmType() {
return alarmType;
public String getViolationType() {
return violationType;
}
public List<HierarchicalActionSet> getActionSets() {
return actionSets;
}
public int getBugFixStartLineNum() {
return bugFixStartLineNum;
}
public void setBugFixStartLineNum(int bugFixStartLineNum) {
this.bugFixStartLineNum = bugFixStartLineNum;
}
public int getBugFixEndLineNum() {
return bugFixEndLineNum;
}
public void setBugFixEndLineNum(int bugFixEndLineNum) {
this.bugFixEndLineNum = bugFixEndLineNum;
}
@Override
public int compareTo(Violation v) {
return this.startLineNum.compareTo(v.startLineNum);
@@ -98,14 +116,14 @@ public class Violation implements Comparable<Violation> {
@Override
public String toString() {
return this.startLineNum + " : " + this.endLineNum + " : " + this.alarmType;
return this.startLineNum + " : " + this.endLineNum + " : " + this.violationType;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Violation) {
Violation v = (Violation) obj;
if (this.fileName.equals(v.fileName) && this.alarmType.equals(v.alarmType) && this.startLineNum == v.startLineNum && this.endLineNum == v.endLineNum) {
if (this.fileName.equals(v.fileName) && this.violationType.equals(v.violationType) && this.startLineNum == v.startLineNum && this.endLineNum == v.endLineNum) {
return true;
}
}
@@ -44,7 +44,7 @@ public class AkkaParser {
ActorSystem system = null;
ActorRef parsingActor = null;
int numberOfWorkers = 400;
int numberOfWorkers = 431;
final WorkMessage msg = new WorkMessage(0, msgFiles);
try {
log.info("Akka begins...");
@@ -50,7 +50,7 @@ public class ParseFixPatternActor extends UntypedActor {
if (message instanceof WorkMessage) {
List<MessageFile> files = ((WorkMessage) message).getMsgFiles();
int size = files.size();
int average = size / numberOfWorkers;
int average = (int) Math.round((double) size / numberOfWorkers);
for (int i = 0; i < numberOfWorkers; i ++) {
int fromIndex = i * average;
@@ -4,7 +4,6 @@ import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
@@ -73,27 +72,29 @@ public class ParseFixPatternWorker extends UntypedActor {
int id = msg.getId();
int counter = 0;
int testAlarms = 0;
int testViolations = 0;
int nullGumTreeResults = 0;
int nullMappingGumTreeResults = 0;
int noSourceCodeChanges = 0;
int noStatementChanges = 0;
int nullDiffEntry = 0;
int nullMappingGumTreeResults = 0;
int pureDeletion = 0;
int expNums = 0;
int largeHunk = 0;
int nullSourceCode = 0;
int illegalV = 0;
int testInfos = 0;
int timeouts = 0;
// Read violations with Null_Violation_Hunk or Illegal_Line_Position
List<Violation> uselessViolations = readUselessViolations("logs/FixedViolationCodeParseResults.log");
// List<Violation> uselessViolations = new ArrayList<>();// readUselessViolations("logs/FixedViolationCodeParseResults.log");
for (MessageFile msgFile : files) {
File revFile = msgFile.getRevFile();
File prevFile = msgFile.getPrevFile();
File diffentryFile = msgFile.getDiffEntryFile();
File positionFile = msgFile.getPositionFile();
if (revFile.getName().toLowerCase().contains("/test/")) {
testAlarms += countAlarms(positionFile, "#TestViolation:", uselessViolations);
if (revFile.getName().toLowerCase().contains("test#") || revFile.getName().toLowerCase().contains("tests#")) {
// testViolations += countAlarms(positionFile, "#TestViolation:", uselessViolations);
testViolations += countAlarms(positionFile, "#TestViolation:");
continue;
}
// Parser parser = null;
@@ -104,7 +105,7 @@ public class ParseFixPatternWorker extends UntypedActor {
// parser = new CommitPatchSingleStatementParser();
// }
FixedViolationHunkParser parser = new FixedViolationHunkParser(positionFile);
parser.setUselessViolations(uselessViolations);
// parser.setUselessViolations(uselessViolations);
final ExecutorService executor = Executors.newSingleThreadExecutor();
// schedule the work
@@ -112,23 +113,25 @@ public class ParseFixPatternWorker extends UntypedActor {
try {
// wait for task to complete
future.get(Configuration.SECONDS_TO_WAIT, TimeUnit.SECONDS);
nullDiffEntry += parser.nullMatchedDiffEntry;
nullMappingGumTreeResults += parser.nullMappingGumTreeResult;
pureDeletion += parser.pureDeletions;
largeHunk += parser.largeHunk;
nullSourceCode += parser.nullSourceCode;
testInfos += parser.testInfos;
testingInfo.append(parser.testingInfo);
String editScript = parser.getAstEditScripts();
if ("".equals(editScript)) {
if (parser.resultType == 1) {
nullGumTreeResults += countAlarms(positionFile, "", uselessViolations);
nullGumTreeResults += countAlarms(positionFile, "");
// System.err.println("#NullGumTreeResult:" + revFile.getName());
} else if (parser.resultType == 2) {
noSourceCodeChanges += countAlarms(positionFile, "", uselessViolations);
noSourceCodeChanges += countAlarms(positionFile, "");
} else if (parser.resultType == 3) {
noStatementChanges += countAlarms(positionFile, "", uselessViolations);
} else if (parser.resultType == 4) {
noStatementChanges += countAlarms(positionFile, "");
// } else if (parser.resultType == 4) {
// illegalV += countAlarms(positionFile, "", uselessViolations);
}
} else {
@@ -159,14 +162,14 @@ public class ParseFixPatternWorker extends UntypedActor {
} catch (TimeoutException e) {
// err.println("task timed out");
future.cancel(true);
expNums += countAlarms(positionFile, "#Timeout:", uselessViolations);
timeouts += countAlarms(positionFile, "#Timeout:");
// System.err.println("#Timeout: " + revFile.getName());
} catch (InterruptedException e) {
expNums += countAlarms(positionFile, "#TimeInterrupted:", uselessViolations);
timeouts += countAlarms(positionFile, "#TimeInterrupted:");
// err.println("task interrupted");
// System.err.println("#TimeInterrupted: " + revFile.getName());
} catch (ExecutionException e) {
expNums += countAlarms(positionFile, "#TimeAborted:", uselessViolations);
timeouts += countAlarms(positionFile, "#TimeAborted:");
// err.println("task aborted");
// System.err.println("#TimeAborted: " + revFile.getName());
} finally {
@@ -189,9 +192,10 @@ public class ParseFixPatternWorker extends UntypedActor {
FileHelper.outputToFile("OUTPUT/testingInfo_" + id + ".list", testingInfo, true);
testingInfo.setLength(0);
}
String statistic = "testAlarms: " + testAlarms + "\nnullGumTreeResults: " + nullGumTreeResults + "\nnullMappingGumTreeResults: " + nullMappingGumTreeResults +
"\nnoSourceCodeChanges: " + noSourceCodeChanges + "\npureDeletion: " + pureDeletion + "\nTimeout: " + expNums +
"\nlargeHunk: " + largeHunk + "\nnullSourceCode: " + nullSourceCode + "\nnoStatementChanges: " + noStatementChanges + "\nIllegalV: " + illegalV;
String statistic = "TestViolations: " + testViolations + "\nNullGumTreeResults: " + nullGumTreeResults + "\nNoSourceCodeChanges: " + noSourceCodeChanges +
"\nNoStatementChanges: " + noStatementChanges + "\nNullDiffEntry: " + nullDiffEntry + "\nNullMatchedGumTreeResults: " + nullMappingGumTreeResults +
"\nPureDeletion: " + pureDeletion + "\nLargeHunk: " + largeHunk + "\nNullSourceCode: " + nullSourceCode +
"\nTestingInfo: " + testInfos + "\nTimeout: " + timeouts;// + "\nIllegalV: " + illegalV;
FileHelper.outputToFile("OUTPUT/statistic_" + id + ".list", statistic, false);
log.info("Worker #" + id +"Finish of parsing " + counter + " files...");
@@ -202,39 +206,39 @@ public class ParseFixPatternWorker extends UntypedActor {
}
}
private List<Violation> readUselessViolations(String filePath) {
List<Violation> uselessViolations = new ArrayList<>();
String content = FileHelper.readFile(filePath);
BufferedReader reader = new BufferedReader(new StringReader(content));
String line = null;
try {
while ((line = reader.readLine()) != null) {
if (line.startsWith("#")) {
String[] elements = line.split(":");
String fileName = elements[1];
int startLine = Integer.parseInt(elements[2]);
int endLine = Integer.parseInt(elements[3]);
String violationType = elements[4];
Violation violation = new Violation(startLine, endLine, violationType);
violation.setFileName(fileName);
uselessViolations.add(violation);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return uselessViolations;
}
// private List<Violation> readUselessViolations(String filePath) {
// List<Violation> uselessViolations = new ArrayList<>();
//
// String content = FileHelper.readFile(filePath);
// BufferedReader reader = new BufferedReader(new StringReader(content));
// String line = null;
// try {
// while ((line = reader.readLine()) != null) {
// if (line.startsWith("#")) {
// String[] elements = line.split(":");
// String fileName = elements[1];
// int startLine = Integer.parseInt(elements[2]);
// int endLine = Integer.parseInt(elements[3]);
// String violationType = elements[4];
//
// Violation violation = new Violation(startLine, endLine, violationType);
// violation.setFileName(fileName);
// uselessViolations.add(violation);
// }
// }
// } catch (IOException e) {
// e.printStackTrace();
// } finally {
// try {
// reader.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
// return uselessViolations;
// }
private int countAlarms(File positionFile, String type, List<Violation> uselessViolations) {
private int countAlarms(File positionFile, String type) {//, List<Violation> uselessViolations) {
int counter = 0;
String content = FileHelper.readFile(positionFile);
BufferedReader reader = new BufferedReader(new StringReader(content));
@@ -243,16 +247,11 @@ public class ParseFixPatternWorker extends UntypedActor {
while ((line = reader.readLine()) != null) {
String[] elements = line.split(":");
Violation v = new Violation(Integer.parseInt(elements[1]), Integer.parseInt(elements[2]), elements[0]);
String fileName = positionFile.getName().replaceAll("#", "/").replace(".txt", ".java");
String fileName = positionFile.getName().replace(".txt", ".java");
v.setFileName(fileName);
if (!uselessViolations.contains(v)) {
counter ++;
if (!"".equals(type)) {
if (!uselessViolations.contains(v)) {
System.err.println(type + fileName + ":" + elements[1] + ":" + elements[2] + ":" + elements[0]);
}
}
counter ++;
if (!"".equals(type)) {
System.err.println(type + fileName + ":" + elements[1] + ":" + elements[2] + ":" + elements[0]);
}
}
} catch (IOException e) {
@@ -0,0 +1,213 @@
package edu.lu.uni.serval.MultipleThreadsParser2;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import edu.lu.uni.serval.config.Configuration;
import edu.lu.uni.serval.utils.FileHelper;
import edu.lu.uni.serval.utils.MapSorter;
/**
* Multi-thread parser of parsing the difference between buggy code file and fixed code file.
*
* @author kui.liu
*
*/
public class AkkaMatcher {
private static Logger log = LoggerFactory.getLogger(AkkaMatcher.class);
public static void main(String[] args) {
List<Double[]> extractedFeatures = readStringList(Configuration.ROOT_PATH + "TestData/2_CNNinput.csv");
int size = extractedFeatures.size();
List<Double[]> trainingFeatures = extractedFeatures.subList(0, size - 178);
List<Double[]> bugFeatures = extractedFeatures.subList(size - 178, size);
for (int index = 0; index < 178; index ++) {
// AkkaMatcher computor = new AkkaMatcher();
// computor.matchFixPatterns(bugFeatures.get(index), index, trainingFeatures);
Map<Integer, Double> similarities = new HashMap<>();
for (int i = 0; i < size - 178; i ++) {
Double similarity = Math.abs(computeSimilarity(bugFeatures.get(index), trainingFeatures.get(i)));
similarities.put(i + 1, similarity);
}
MapSorter<Integer, Double> mapSorter = new MapSorter<Integer, Double>();
Map<Integer, Double> sortedSimilarities = mapSorter.sortByValueDescending(similarities);
List<Integer> similarityList = new ArrayList<>();
double similarity = 0;
int num = 0;
for (Map.Entry<Integer, Double> entry : sortedSimilarities.entrySet()) {
if (entry.getValue().equals(Double.NaN)) {
continue;
}
if (entry.getValue() == similarity) {
continue;
}
similarityList.add(entry.getKey());
similarity = entry.getValue();
if (++ num % 100 == 0) {
break;
}
}
outputMatchedPatterns(similarityList, index + 1);
}
}
private static Double computeSimilarity(Double[] feature, Double[] trainingFeature) {
Double similarity = DistanceCalculator.cosineSimilarityDistance(trainingFeature, feature);
return similarity;
}
private static void outputMatchedPatterns(List<Integer> similarityList, int bugId) {
String outputFile = Configuration.ROOT_PATH + "TestData/MatchedFixPatterns/Bug_" + bugId + ".list";
StringBuilder builder = new StringBuilder("BugId: " + bugId + "\n\n\n");
for (int i = 0, size = similarityList.size(); i < size; i ++) {
int patternPosition = similarityList.get(i);
builder.append(readPattern(patternPosition));
}
FileHelper.outputToFile(outputFile, builder, false);
}
private static String readPattern(int patternPosition) {
String patternInfo = "";
FileInputStream fis = null;
Scanner scanner = null;
try {
fis = new FileInputStream(Configuration.SELECTED_PATCHES_SOURE_CODE_FILE);
scanner = new Scanner(fis);
int index = 0;
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (line.equals("PATCH###")) {
if (patternPosition == index) {
break;
}
index ++;
patternInfo = "";
}
patternInfo += line + "\n";
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
scanner.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return patternInfo;
}
@SuppressWarnings("deprecation")
public void matchFixPatterns(Double[] bugFeature, int bugIndex, List<Double[]> trainingFeatures) {
ActorSystem system = null;
ActorRef parsingActor = null;
int numberOfWorkers = 100;
final WorkMessage msg = new WorkMessage(bugIndex, bugFeature, trainingFeatures);
try {
log.info("Akka begins...");
system = ActorSystem.create("Matching-FixPattern-System");
parsingActor = system.actorOf(MatchFixPatternActor.props(numberOfWorkers), "match-fix-pattern-actor");
parsingActor.tell(msg, ActorRef.noSender());
} catch (Exception e) {
system.shutdown();
e.printStackTrace();
}
}
/**
* Get bug commit-related files.
*
* @return
*/
public static List<MessageFile> getMessageFiles() {
String inputPath = Configuration.GUM_TREE_INPUT; //DiffEntries prevFiles revFiles
File inputFileDirector = new File(inputPath);
File[] files = inputFileDirector.listFiles(); // project folders
log.info("Projects: " + files.length);
List<MessageFile> msgFiles = new ArrayList<>();
for (File file : files) {
if (!file.isDirectory()) continue;
// if (!(file.getName().startsWith("k") || file.getName().startsWith("l"))) continue;
if (file.getName().startsWith("a") || file.getName().startsWith("b")
|| file.getName().startsWith("c") || file.getName().startsWith("d")
|| file.getName().startsWith("e") || file.getName().startsWith("f")
|| file.getName().startsWith("g") || file.getName().startsWith("h")
||file.getName().startsWith("h") || file.getName().startsWith("i")
|| file.getName().startsWith("k") || file.getName().startsWith("l")
|| file.getName().startsWith("j") || file.getName().startsWith("t")) continue;
// if (!file.getName().startsWith("j")) continue;
log.info("Project name: " + file.getName());
String projectFolder = file.getPath();
File revFileFolder = new File(projectFolder + "/revFiles/");// revised file folder
File[] revFiles = revFileFolder.listFiles();
for (File revFile : revFiles) {
if (revFile.getName().endsWith(".java")) {
File prevFile = new File(projectFolder + "/prevFiles/prev_" + revFile.getName());// previous file
File diffentryFile = new File(projectFolder + "/DiffEntries/" + revFile.getName().replace(".java", ".txt")); // DiffEntry file
MessageFile msgFile = new MessageFile(revFile, prevFile, diffentryFile);
msgFiles.add(msgFile);
}
}
}
return msgFiles;
}
public static List<Double[]> readStringList(String inputFile) {
List<Double[]> list = new ArrayList<>();
FileInputStream fis = null;
Scanner scanner = null;
try {
fis = new FileInputStream(inputFile);
scanner = new Scanner(fis);
while(scanner.hasNextLine()) {
String line = scanner.nextLine();
Double[] features = doubleParseFeature(line);
list.add(features);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
scanner.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return list;
}
private static Double[] doubleParseFeature(String feature) {
String[] features = feature.split(", ");
int length = features.length;
Double[] doubleFeatures = new Double[length];
for (int i = 0; i < length; i ++) {
doubleFeatures[i] = Double.parseDouble(features[i]);
}
return doubleFeatures;
}
}
@@ -0,0 +1,68 @@
package edu.lu.uni.serval.MultipleThreadsParser2;
public class DistanceCalculator {
public static double calculateDistance(DistanceFunction distanceType, Double[] targetPoint, Double[] selfPoint) {
double distance = 0;
switch (distanceType) {
case EUCLIDEAN:
distance = DistanceCalculator.euclideanDistance(targetPoint, selfPoint);
break;
case COSINESIMILARITY:
distance = DistanceCalculator.cosineSimilarityDistance(targetPoint, selfPoint);
break;
case MANHATTAN:
distance = DistanceCalculator.manhattanDistance(targetPoint, selfPoint);
break;
default:
distance = DistanceCalculator.euclideanDistance(targetPoint, selfPoint);
break;
}
return distance;
}
public static double euclideanDistance(Double[] targetPoint, Double[] selfPoint) {
double sum = 0.0;
for (int i = 0, length = targetPoint.length; i < length; i++) {
double diff = targetPoint[i] - selfPoint[i];
sum += diff * diff;
}
return Math.sqrt(sum);
}
public static Double cosineSimilarityDistance(Double[] targetPoint, Double[] selfPoint) {
Double sim = 0.0d;
int length = targetPoint.length;
double dot = 0.0d;
double mag1 = 0.0d;
double mag2 = 0.0d;
for (int i = 0; i < length; i ++) {
dot += targetPoint[i] * selfPoint[i];
mag1 += Math.pow(targetPoint[i], 2);
mag2 += Math.pow(selfPoint[i], 2);
}
sim = dot / (Math.sqrt(mag1) * Math.sqrt(mag2));
return sim;
}
public static Double manhattanDistance(Double[] targetPoint, Double[] selfPoint) {
double result = 0.0;
for (int i = 0; i < targetPoint.length; i++) {
result += Math.abs(selfPoint[i] - targetPoint[i]);
}
return result;
}
public static Double minkowskiDistance(Double[] targetPoint, Double[] selfPoint) {
return null;
}
public static Double jaccardSimilarity(Double[] targetPoint, Double[] selfPoint) {
return null;
}
}
@@ -0,0 +1,5 @@
package edu.lu.uni.serval.MultipleThreadsParser2;
public enum DistanceFunction {
EUCLIDEAN, COSINESIMILARITY, MANHATTAN
}
@@ -0,0 +1,172 @@
package edu.lu.uni.serval.MultipleThreadsParser2;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import akka.actor.ActorRef;
import akka.actor.Props;
import akka.actor.UntypedActor;
import akka.japi.Creator;
import akka.routing.RoundRobinPool;
import edu.lu.uni.serval.config.Configuration;
import edu.lu.uni.serval.utils.FileHelper;
import edu.lu.uni.serval.utils.ListSorter;
import edu.lu.uni.serval.utils.MapSorter;
public class MatchFixPatternActor extends UntypedActor {
private static Logger logger = LoggerFactory.getLogger(MatchFixPatternActor.class);
private ActorRef mineRouter;
private final int numberOfWorkers;
private int counter = 0;
public MatchFixPatternActor(int numberOfWorkers) {
mineRouter = this.getContext().actorOf(new RoundRobinPool(numberOfWorkers)
.props(MatchFixPatternWorker.props()), "match-fix-pattern-router");
this.numberOfWorkers = numberOfWorkers;
}
public static Props props(final int numberOfWorkers) {
return Props.create(new Creator<MatchFixPatternActor>() {
private static final long serialVersionUID = 9207427376110704705L;
@Override
public MatchFixPatternActor create() throws Exception {
return new MatchFixPatternActor(numberOfWorkers);
}
});
}
@SuppressWarnings("deprecation")
@Override
public void onReceive(Object message) throws Exception {
if (message instanceof WorkMessage) {
WorkMessage msg = (WorkMessage) message;
List<Double[]> trainingFeatures = msg.getTrainingFeatures();
int size = trainingFeatures.size();
int average = size / numberOfWorkers;
this.bugId = msg.getId();
Double[] bugFeature = msg.getBugFeature();
for (int i = 0; i < numberOfWorkers; i ++) {
int fromIndex = i * average;
int toIndex = (i + 1) * average;
if (i == numberOfWorkers - 1) {
toIndex = size;
}
List<Double[]> subTrainingFeatures = new ArrayList<>();
subTrainingFeatures.addAll(trainingFeatures.subList(fromIndex, toIndex));
final WorkMessage workMsg = new WorkMessage(bugId, bugFeature, subTrainingFeatures);
workMsg.setNum(i + 1);
mineRouter.tell(workMsg, getSelf());
logger.info("Assign a task to worker #" + (i + 1) + "...");
}
} else if (message instanceof ReturnMessage) {
counter ++;
logger.info(counter + " workers finished their work...");
ReturnMessage rMsg = (ReturnMessage) message;
returnMessages.add(rMsg);
if (counter >= numberOfWorkers) {
ListSorter<ReturnMessage> sorter = new ListSorter<ReturnMessage>(returnMessages);
returnMessages = sorter.sortAscending();
Map<Integer, Double> similarities = new HashMap<>();
int index = 0;
for (int i = 0; i < numberOfWorkers; i ++) {
ReturnMessage returnMessage = returnMessages.get(i);
List<Double> similarity = returnMessage.getSimilarities();
for (int j = 0, size = similarity.size(); j < size; j ++) {
index ++;
similarities.put(index, similarity.get(j));
}
}
MapSorter<Integer, Double> mapSorter = new MapSorter<Integer, Double>();
Map<Integer, Double> sortedSimilarities = mapSorter.sortByValueDescending(similarities);
List<Integer> similarityList = new ArrayList<>();
double similarity = 0;
int num = 0;
for (Map.Entry<Integer, Double> entry : sortedSimilarities.entrySet()) {
if (entry.getValue() == similarity) {
continue;
}
similarityList.add(entry.getKey());
similarity = entry.getValue();
if (++ num % 100 == 0) {
break;
}
}
outputMatchedPatterns(similarityList);
logger.info("All workers finished their work...");
this.getContext().stop(mineRouter);
this.getContext().stop(getSelf());
this.getContext().system().shutdown();
}
} else {
unhandled(message);
}
}
private void outputMatchedPatterns(List<Integer> similarityList) {
String outputFile = Configuration.ROOT_PATH + "TestData/MatchedFixPatterns/Bug_" + bugId + ".list";
StringBuilder builder = new StringBuilder("BugId: " + bugId + "\n\n\n");
for (int i = 0, size = similarityList.size(); i < size; i ++) {
int patternPosition = similarityList.get(i);
builder.append(readPattern(patternPosition));
}
FileHelper.outputToFile(outputFile, builder, false);
}
private String readPattern(int patternPosition) {
String patternInfo = "";
FileInputStream fis = null;
Scanner scanner = null;
try {
fis = new FileInputStream(Configuration.SELECTED_PATCHES_SOURE_CODE_FILE);
scanner = new Scanner(fis);
int index = 0;
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (line.equals("PATCH###")) {
if (patternPosition == index) {
break;
}
index ++;
patternInfo = "";
}
patternInfo += line + "\n";
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
scanner.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return patternInfo;
}
private int bugId;
private List<ReturnMessage> returnMessages = new ArrayList<>();
}
@@ -0,0 +1,60 @@
package edu.lu.uni.serval.MultipleThreadsParser2;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import akka.actor.Props;
import akka.actor.UntypedActor;
import akka.japi.Creator;
public class MatchFixPatternWorker extends UntypedActor {
private static Logger log = LoggerFactory.getLogger(MatchFixPatternActor.class);
public MatchFixPatternWorker() {
}
public static Props props() {
return Props.create(new Creator<MatchFixPatternWorker>() {
private static final long serialVersionUID = -7615153844097275009L;
@Override
public MatchFixPatternWorker create() throws Exception {
return new MatchFixPatternWorker();
}
});
}
@Override
public void onReceive(Object message) throws Exception {
if (message instanceof WorkMessage) {
WorkMessage msg = (WorkMessage) message;
List<Double[]> trainingFeatures = msg.getTrainingFeatures();
Double[] bugFeature = msg.getBugFeature();
int bugID = msg.getId();
int workNum = msg.getNum();
List<Double> similarities = new ArrayList<>();
for (int i = 0, size = trainingFeatures.size(); i < size; i ++) {
Double similarity = Math.abs(computeSimilarity(bugFeature, trainingFeatures.get(i)));
similarities.add(similarity);
}
final ReturnMessage rMsg = new ReturnMessage(bugID, workNum, similarities);
log.info("Worker #" + workNum + " finished the work...");
this.getSender().tell(rMsg, getSelf());
} else {
unhandled(message);
}
}
private Double computeSimilarity(Double[] feature, Double[] trainingFeature) {
Double similarity = DistanceCalculator.cosineSimilarityDistance(trainingFeature, feature);
return similarity;
}
}
@@ -0,0 +1,39 @@
package edu.lu.uni.serval.MultipleThreadsParser2;
import java.io.File;
public class MessageFile {
private File revFile;
private File prevFile;
private File diffEntryFile;
private File positionFile;
public MessageFile(File revFile, File prevFile, File diffEntryFile) {
super();
this.revFile = revFile;
this.prevFile = prevFile;
this.diffEntryFile = diffEntryFile;
}
public File getRevFile() {
return revFile;
}
public File getPrevFile() {
return prevFile;
}
public File getDiffEntryFile() {
return diffEntryFile;
}
public File getPositionFile() {
return positionFile;
}
public void setPositionFile(File positionFile) {
this.positionFile = positionFile;
}
}
@@ -0,0 +1,35 @@
package edu.lu.uni.serval.MultipleThreadsParser2;
import java.util.List;
public class ReturnMessage implements Comparable<ReturnMessage>{
private int bugId;
private Integer workerId;
private List<Double> similarities;
public ReturnMessage(int bugId, int workerId, List<Double> similarities) {
super();
this.bugId = bugId;
this.workerId = workerId;
this.similarities = similarities;
}
public int getBugId() {
return bugId;
}
public int getWorkerId() {
return workerId;
}
public List<Double> getSimilarities() {
return similarities;
}
@Override
public int compareTo(ReturnMessage o) {
return this.workerId.compareTo(o.workerId);
}
}
@@ -0,0 +1,39 @@
package edu.lu.uni.serval.MultipleThreadsParser2;
import java.util.List;
public class WorkMessage {
private int id;
private Double[] bugFeature;
private List<Double[]> trainingFeatures;
private int num;
public WorkMessage(int id, Double[] bugFeature, List<Double[]> trainingFeatures) {
super();
this.id = id;
this.bugFeature = bugFeature;
this.trainingFeatures = trainingFeatures;
}
public int getId() {
return id;
}
public Double[] getBugFeature() {
return bugFeature;
}
public List<Double[]> getTrainingFeatures() {
return trainingFeatures;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
}
@@ -14,7 +14,7 @@ import edu.lu.uni.serval.config.Configuration;
import edu.lu.uni.serval.gumtree.regroup.SimpleTree;
import edu.lu.uni.serval.gumtree.regroup.SimplifyTree;
import edu.lu.uni.serval.utils.FileHelper;
import edu.lu.uni.serval.violation.code.parser.AlarmTree;
import edu.lu.uni.serval.violation.code.parser.ViolationSourceCodeTree;
/**
*
@@ -60,9 +60,9 @@ public class BuggyTreeParser {
for (File javaFile : javaFiles) {
if (javaFile.getPath().endsWith(fileName)) {
AlarmTree alarmTree = new AlarmTree(javaFile, startLine, endLine);
ViolationSourceCodeTree alarmTree = new ViolationSourceCodeTree(javaFile, startLine, endLine);
alarmTree.extract();
List<ITree> matchedTrees = alarmTree.getAlarmTrees();
List<ITree> matchedTrees = alarmTree.getViolationSourceCodeTrees();
SimpleTree simpleTree = new SimpleTree();
simpleTree.setLabel("Block");
simpleTree.setNodeType("Block");
@@ -0,0 +1,100 @@
package edu.lu.uni.serval.bugLocalization;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import edu.lu.uni.serval.config.Configuration;
import edu.lu.uni.serval.utils.Exporter;
import edu.lu.uni.serval.utils.FileHelper;
import edu.lu.uni.serval.utils.MapSorter;
public class FixedAlarms {
/**
*
unfixed alarms
321403
5336
MaxLength: 1508
* @param args
*/
public static void main(String[] args) {
String fileName = "unfixedAlarms";
String tokensFile = Configuration.ROOT_PATH + "Alarms_tokens/" + fileName + ".list"; // MaxLength: 1508
FileHelper.deleteFile(Configuration.ROOT_PATH + "Alarms_tokens/" + fileName + "Tokens.list");
int maxLength = 0;
StringBuilder tokensBuilder = new StringBuilder();
StringBuilder sizesBuilder = new StringBuilder();
Map<String, Integer> alarmTypes = new HashMap<>();
FileInputStream fis = null;
Scanner scanner = null;
int counter = 0;
try {
fis = new FileInputStream(tokensFile);
scanner = new Scanner(fis);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
int colonIndex = line.indexOf(":");
String alarmType = line.substring(0, colonIndex);
if (alarmTypes.containsKey(alarmType)) {
alarmTypes.put(alarmType, alarmTypes.get(alarmType) + 1);
} else {
alarmTypes.put(alarmType, 1);
}
line = line.substring(colonIndex + 1);
colonIndex = line.indexOf(":");
String violationFileName = line.substring(0, colonIndex);
line = line.substring(colonIndex + 1);
colonIndex = line.indexOf(":");
String startLine = line.substring(0, colonIndex);
line = line.substring(colonIndex + 1);
colonIndex = line.indexOf(":");
String endLine = line.substring(0, colonIndex);
String tokens = line.substring(colonIndex + 1);
String[] tokensArray = tokens.split(" ");
int length = tokensArray.length;
if (length > maxLength) maxLength = length;
tokensBuilder.append(tokens + "\n");
sizesBuilder.append(length + "\n");
if (++ counter % 5000 == 0) {
FileHelper.outputToFile(Configuration.ROOT_PATH + "Alarms_tokens/" + fileName + "Tokens.list", tokensBuilder, true);
tokensBuilder.setLength(0);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
scanner.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("MaxLength: " + maxLength);
FileHelper.outputToFile(Configuration.ROOT_PATH + "Alarms_tokens/" + fileName + "Tokens.list", tokensBuilder, true);
tokensBuilder.setLength(0);
FileHelper.outputToFile(Configuration.ROOT_PATH + "Alarms_tokens/" + fileName + "TokenSizes.csv", sizesBuilder, false);
MapSorter<String, Integer> sorter = new MapSorter<String, Integer>();
alarmTypes = sorter.sortByKeyAscending(alarmTypes);
String[] columns = { "Alarm Type", "amount" };
Exporter.exportOutliers(alarmTypes, new File(Configuration.ROOT_PATH + "Alarms_tokens/" + fileName + "TokenTypes.xls"), 1, columns);
// String s = "";
// for (Map.Entry<String, Integer> entry : alarmTypes.entrySet()) {
// s += entry.getKey() + " : " + entry.getValue() + "\n";
//
// }
// FileHelper.outputToFile(Configuration.ROOT_PATH + "Alarms_tokens/fixedAlarmTypesAmount.list", s, false);
}
}
@@ -2,7 +2,7 @@ package edu.lu.uni.serval.config;
public class Configuration {
public static final long SECONDS_TO_WAIT = 120L;
public static final long SECONDS_TO_WAIT = 30L;
public static final String ROOT_PATH = "../FPM_Violations/"; // The root path of all output data.
@@ -186,6 +186,8 @@ public class HierarchicalRegrouper {
private Action findParentAction(Action action, List<Action> actions) {
ITree parent = action.getNode().getParent();
if (parent.getType() == 55) return null;
if (action instanceof Addition) {
parent = ((Addition) action).getParent(); // parent in the fixed source code tree
}
@@ -8,8 +8,6 @@ import java.util.ArrayList;
import java.util.List;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.github.gumtreediff.actions.model.Move;
import com.github.gumtreediff.actions.model.Update;
@@ -21,10 +19,6 @@ import edu.lu.uni.serval.diffentry.DiffEntryHunk;
public class HunkActionFilter {
private static final int LIMITATION_OF_LINE_NUMBERS = 5;
private static Logger log = LoggerFactory.getLogger(HunkActionFilter.class);
/**
* Filter out the modify actions, which are not in the DiffEntry hunks, without considering the same parent node.
*
@@ -200,7 +194,14 @@ public class HunkActionFilter {
private int getEndPosition(List<ITree> children) {
int endPosition = 0;
for (ITree child : children) {
if (child.getLabel().endsWith("Body")) {
int type = child.getType();
if (type == 6 || type == 10 || type == 12 || type == 17 || type == 18 || type == 19 || type == 21 || type == 8// Block, EmptyStatement
|| type == 24 || type == 25 || type == 30 || type == 41 || type == 46 || type == 49 || type == 50
|| type == 51 || type == 53 || type == 54 || type == 56 || type == 60 || type == 61 || type == 70) {
//AssertStatement, BreakStatement, CatchClause, ConstructorInvocation, ContinueStatement, DoStatement
// ExpressionStatement, ForStatement, IfStatement, LabeledStatement, ReturnStatement, SuperConstructorInvocation
// SwitchCase, SwitchStatement, SynchronizedStatement, ThrowStatement, TryStatement
// TypeDeclarationStatement, VariableDeclarationStatement, WhileStatement, EnhancedForStatement
endPosition = child.getPos() - 1;
break;
}
@@ -377,86 +378,127 @@ public class HunkActionFilter {
for (Violation violation : violations) {
int violationStartLine = violation.getStartLineNum();
int violationEndLine = violation.getEndLineNum();
// int bugHunkStartLine = violation.getBugStartLineNum();
// int bugHunkEndLine = violation.getBugEndLineNum();
// int fixHunkStartLine = violation.getFixStartLineNum();
// int fixHunkEndLine = violation.getFixEndLineNum();
for (HierarchicalActionSet actionSet : actionSets) {
int actionBugStartLine = actionSet.getBugStartLineNum();
if (actionBugStartLine == 0) {
actionBugStartLine = setLineNumbers(actionSet, prevUnit, revUnit);
}
int actionBugEndLine = actionSet.getBugEndLineNum();
int actionFixStartLine = actionSet.getFixStartLineNum();
int actionFixEndLine = actionSet.getFixEndLineNum();
int bugHunkStartLine = violation.getBugStartLineNum();
if (bugHunkStartLine == 0) {// Null source code matched for this violation.
// String type = getType(violation);
// continue;
} else if (bugHunkStartLine == -1) {//
specialVioaltionTypes(violation, actionSets);
// continue;
} else {
int bugHunkEndLine = violation.getBugEndLineNum();
int fixHunkStartLine = violation.getFixStartLineNum();
int fixHunkEndLine = violation.getFixEndLineNum();
// if (actionBugStartLine > violationEndLine && actionFixStartLine > fixHunkEndLine) break;
String actionStr = actionSet.getActionString();
if (actionStr.startsWith("INS")) {
// if (fixHunkStartLine <= actionFixStartLine && actionFixEndLine <= fixHunkEndLine) {
// if (actionBugStartLine != 0) {
// if (violationStartLine <= actionBugEndLine && violationEndLine >= actionBugStartLine) {
// violation.getActionSets().add(actionSet);
// }
// } else {
//// if (isRanged(actionSet, violation)) {
//// }
// if (Math.abs(violationStartLine - actionFixStartLine) <= LIMITATION_OF_LINE_NUMBERS
// && Math.abs(violationEndLine - actionFixEndLine) <= LIMITATION_OF_LINE_NUMBERS) {
// violation.getActionSets().add(actionSet);
// }
// }
// }
if (actionBugStartLine != 0) {
if (violationStartLine <= actionBugEndLine && violationEndLine >= actionBugStartLine) {
violation.getActionSets().add(actionSet);
for (HierarchicalActionSet actionSet : actionSets) {
int actionBugStartLine = actionSet.getBugStartLineNum();
if (actionBugStartLine == 0) {
actionBugStartLine = setLineNumbers(actionSet, prevUnit, revUnit);
}
int actionBugEndLine = actionSet.getBugEndLineNum();
int actionFixStartLine = actionSet.getFixStartLineNum();
int actionFixEndLine = actionSet.getFixEndLineNum();
String actionStr = actionSet.getActionString();
if (actionStr.startsWith("INS")) {
if (fixHunkStartLine <= actionFixStartLine && actionFixEndLine <= fixHunkEndLine) {
if (actionBugStartLine != 0) {
if (violationStartLine <= actionBugEndLine && violationEndLine >= actionBugStartLine) {
violation.getActionSets().add(actionSet);
continue;
}
}
// INS with MOV actions that are not identified in previous IF predicate, and pure INS actions
if (violation.getBugFixStartLineNum() >= actionFixEndLine && actionFixStartLine <= violation.getBugFixEndLineNum()) {
violation.getActionSets().add(actionSet);
}
}
} else {
// if (isRanged(actionSet, violation)) {
// }
if (Math.abs(violationStartLine - actionFixStartLine) <= LIMITATION_OF_LINE_NUMBERS
&& Math.abs(violationEndLine - actionFixEndLine) <= LIMITATION_OF_LINE_NUMBERS) {
violation.getActionSets().add(actionSet);
if (bugHunkStartLine <= actionBugStartLine && violationEndLine <= bugHunkEndLine) {
if (violationStartLine <= actionBugEndLine && violationEndLine >= actionBugStartLine) {
violation.getActionSets().add(actionSet);
}
}
}
} else {
// if (bugHunkStartLine <= actionBugStartLine && actionBugEndLine <= bugHunkEndLine) {
// if (violationStartLine <= actionBugEndLine && violationEndLine >= actionBugStartLine) {
// violation.getActionSets().add(actionSet);
// }
// }
if (violationStartLine <= actionBugEndLine && violationEndLine >= actionBugStartLine) {
violation.getActionSets().add(actionSet);
}
}
}
if (violation.getActionSets().size() > 0) {
// FieldDeclaration: single insert, move, delete or update.
List<HierarchicalActionSet> matchedActionSets = filterActionSets(violation.getActionSets());
violation.getActionSets().clear();
violation.getActionSets().addAll(matchedActionSets);
// List<HierarchicalActionSet> matchedActionSets = filterActionSets(violation.getActionSets());
// violation.getActionSets().clear();
// violation.getActionSets().addAll(matchedActionSets);
selectedViolations.add(violation);
} else {
System.err.println("#NullMatchedGumTreeResult:" + revFile.getName().replace("#", "/") + ":" +violation.getStartLineNum() + ":" +
violation.getEndLineNum() + ":" + violation.getAlarmType());
System.err.println("#NullMatchedGumTreeResult:" + revFile.getName() + ":" +violation.getStartLineNum() + ":" +
violation.getEndLineNum() + ":" + violation.getViolationType());
}
}
return selectedViolations;
}
private void specialVioaltionTypes(Violation violation, List<HierarchicalActionSet> actionSets) {
String type = violation.getViolationType();
if ("SE_NO_SUITABLE_CONSTRUCTOR".equals(type)) {// 158, 47
for (HierarchicalActionSet actionSet : actionSets) {
if (actionSet.getActionString().startsWith("UPD TypeDeclaration@@")) {
violation.getActionSets().add(actionSet);
break;
}
}
} else if ("CN_IDIOM".equals(type)) { // 202 23
//add clone method. or update clone method
for (HierarchicalActionSet actionSet : actionSets) {
if (actionSet.getActionString().startsWith("INS MethodDeclaration@@clone")) {
// || actionSet.getActionString().startsWith("UPD MethodDeclaration@@clone")) {
violation.getActionSets().add(actionSet);
break;
}
}
} else if ("SE_NO_SERIALVERSIONID".equals(type)) {// 12 1960
// change superclass or interface, add field or remove @SuppressWarnings("serial"), some are inner class
for (HierarchicalActionSet actionSet : actionSets) {
if (actionSet.getActionString().startsWith("UPD TypeDeclaration@")) {
violation.getActionSets().add(actionSet);
}
if (actionSet.getActionString().startsWith("INS FieldDeclaration@") && actionSet.getNode().getLabel().contains("serialVersionUID")) {
violation.getActionSets().add(actionSet);
break;
}
}
} else if ("SE_NO_SUITABLE_CONSTRUCTOR_FOR_EXTERNALIZATION".equals(type)) { // 175 34
// constructor
for (HierarchicalActionSet actionSet : actionSets) {
if (actionSet.getActionString().startsWith("UPD MethodDeclaration@@")) {
violation.getActionSets().add(actionSet);
break;
}
}
} else if ("SE_COMPARATOR_SHOULD_BE_SERIALIZABLE".equals(type)) { // 89 148
//class, and add a field se...?
for (HierarchicalActionSet actionSet : actionSets) {
if (actionSet.getActionString().startsWith("UPD TypeDeclaration@")) {
violation.getActionSets().add(actionSet);
}
if (actionSet.getActionString().startsWith("INS FieldDeclaration@") && actionSet.getNode().getLabel().contains("serialVersionUID")) {
violation.getActionSets().add(actionSet);
break;
}
}
}
}
private List<HierarchicalActionSet> filterActionSets(List<HierarchicalActionSet> actionSets) {
List<HierarchicalActionSet> matchedActionSets = new ArrayList<>();
// find the update
HierarchicalActionSet updateActionSet = null;
for (HierarchicalActionSet actionSet :actionSets) {
for (HierarchicalActionSet actionSet : actionSets) {
if (actionSet.getAction() instanceof Update) {
updateActionSet = actionSet;
}
}
if (updateActionSet != null) {
if (updateActionSet.getAstNodeType().equals("FieldDeclaration")) {
matchedActionSets.clear();
@@ -464,9 +506,67 @@ public class HunkActionFilter {
return matchedActionSets;
}
}
return actionSets;
}
private String getType(Violation violation) {
String type = violation.getViolationType();
switch (type) {
case "CI_CONFUSED_INHERITANCE":// field
// update fieldDeclaration
break;
case "CO_ABSTRACT_SELF":
// java file is an interface, and delete compareTo().
break;
case "EQ_ABSTRACT_SELF":
// java file is an interface, and delete equals().
break;
case "SE_NO_SERIALVERSIONID":
// add a field: serialVersionUID
break;
case "EQ_COMPARETO_USE_OBJECT_EQUALS":
//Update or Delete compareTo(), Add equals()
break;
case "EQ_DOESNT_OVERRIDE_EQUALS":
//override equals()
break;
case "HE_SIGNATURE_DECLARES_HASHING_OF_UNHASHABLE_CLASS":
//remove equals()
break;
case "ME_MUTABLE_ENUM_FIELD":
// under enum, field add final keyword
break;
case "MF_CLASS_MASKS_FIELD":
// change super class or delete the field with a same name in super class.
break;
case "MS_SHOULD_BE_FINAL":
// add final to field
break;
case "STCAL_STATIC_SIMPLE_DATE_FORMAT_INSTANCE":
//remove public static final DateFormat DATE_FORMAT.... or SimpleDateFormat
break;
case "UUF_UNUSED_FIELD":
//remove unused fields. not sure
break;
case "UUF_UNUSED_PUBLIC_OR_PROTECTED_FIELD":
//remove unused fields. not sure
case "UWF_NULL_FIELD":
//update field, remove field
break;
case "UWF_UNWRITTEN_FIELD":
//field
break;
case "UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD":
//remove field
break;
case "VO_VOLATILE_REFERENCE_TO_ARRAY":
//field
break;
default:
break;
}
return null;
}
private boolean isRanged(HierarchicalActionSet actionSet, Violation violation) {
int actionStartLine = actionSet.getFixStartLineNum();
@@ -673,12 +773,23 @@ public class HunkActionFilter {
if ("EnhancedForStatement".equals(astNodeType) || "ForStatement".equals(astNodeType)
|| "DoStatement".equals(astNodeType) || "WhileStatement".equals(astNodeType)
|| "LabeledStatement".equals(astNodeType) || "SynchronizedStatement".equals(astNodeType)
|| "IfStatement".equals(astNodeType) || "TryStatement".equals(astNodeType)) {
|| "IfStatement".equals(astNodeType) || "TryStatement".equals(astNodeType)
|| "MethodDeclaration".equals(astNodeType)) {
List<ITree> children = update.getNode().getChildren();
bugEndPosition = getEndPosition(children);
List<ITree> newChildren = newNode.getChildren();
fixEndPosition = getEndPosition(newChildren);
if (bugEndPosition == 0) {
bugEndPosition = bugStartPosition + actionSet.getLength();
}
if (fixEndPosition == 0) {
fixEndPosition = fixStartPosition + newNode.getLength();
}
} else if ("TypeDeclaration".equals(astNodeType)) {
bugEndPosition = getClassBodyStartPosition(update.getNode());
fixEndPosition = getClassBodyStartPosition(newNode);
if (bugEndPosition == 0) {
bugEndPosition = bugStartPosition + actionSet.getLength();
}
@@ -701,5 +812,27 @@ public class HunkActionFilter {
return actionBugStartLine;
}
private int getClassBodyStartPosition(ITree tree) {
List<ITree> children = tree.getChildren();
for (int i = 0, size = children.size(); i < size; i ++) {
ITree child = children.get(i);
int type = child.getType();
// Modifier, NormalAnnotation, MarkerAnnotation, SingleMemberAnnotation
if (type != 83 && type != 77 && type != 78 && type != 79
&& type != 5 && type != 39 && type != 43 && type != 74 && type != 75
&& type != 76 && type != 84 && type != 87 && type != 88) {
// ArrayType, PrimitiveType, SimpleType, ParameterizedType,
// QualifiedType, WildcardType, UnionType, IntersectionType, NameQualifiedType
if (i > 0) {
child = children.get(i - 1);
return child.getPos() + child.getLength() + 1;
} else {
return child.getPos() - 1;
}
}
}
return 0;
}
}
@@ -0,0 +1,33 @@
package edu.lu.uni.serval.livestudy;
public class Alarm {
private String alarmType;
private String fileName;
private int startLine;
private int endLine;
public String getAlarmType() {
return alarmType;
}
public String getFileName() {
return fileName;
}
public int getStartLine() {
return startLine;
}
public int getEndLine() {
return endLine;
}
public Alarm(String alarmType, String fileName, int startLine, int endLine) {
super();
this.alarmType = alarmType;
this.fileName = fileName;
this.startLine = startLine;
this.endLine = endLine;
}
}
@@ -0,0 +1,87 @@
package edu.lu.uni.serval.livestudy;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import edu.lu.uni.serval.config.Configuration;
import edu.lu.uni.serval.utils.FileHelper;
public class ParseViolations {
public static void main(String[] args) {
ParseViolations parser = new ParseViolations();
String violationFile = Configuration.ROOT_PATH + "LiveStudy/violations.list";
Map<String, Violation> violations = new HashMap<>();
violations = parser.readViolations(violationFile);
String projectsPath = Configuration.ROOT_PATH + "LiveStudy/projects/";
parser.parseViolationToTokens(violations, projectsPath);
}
public void parseViolationToTokens(Map<String, Violation> violations, String projectsPath) {
for (Map.Entry<String, Violation> entry : violations.entrySet()) {
String projectName = entry.getKey();
List<Alarm> alarms = entry.getValue().getAlarms();
List<File> javaFiles = FileHelper.getAllFiles(projectsPath + projectName, ".java");
}
}
public Map<String, Violation> readViolations(String violationFile) {
Map<String, Violation> violations = new HashMap<>();
FileInputStream fis = null;
Scanner scanner = null;
try {
fis = new FileInputStream(violationFile);
scanner = new Scanner(fis);
while (scanner.hasNextLine()) {
//commons-math : DLS_DEAD_LOCAL_STORE : org/apache/commons/math4/dfp/Dfp.java : 2049 : 2049
String line = scanner.nextLine();
String[] elements = line.split(" : ");
String projectName = elements[0];
String alarmType = elements[1];
String fileName = elements[2];
int startLine = Integer.parseInt(elements[3]);
int endLine = Integer.parseInt(elements[4]);
Alarm alarm = new Alarm(alarmType, fileName, startLine, endLine);
addAlarmToViolations(projectName, alarm, violations);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
scanner.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return violations;
}
private void addAlarmToViolations(String projectName, Alarm alarm, Map<String, Violation> violations) {
Violation violation = null;
if (violations.containsKey(projectName)) {
violation = violations.get(projectName);
} else {
violation = new Violation(projectName);
violations.put(projectName, violation);
}
List<Alarm> alarms = violation.getAlarms();
alarms.add(alarm);
}
}
@@ -0,0 +1,25 @@
package edu.lu.uni.serval.livestudy;
import java.util.ArrayList;
import java.util.List;
public class Violation {
private String project;
private List<Alarm> alarms;
public String getProject() {
return project;
}
public List<Alarm> getAlarms() {
return alarms;
}
public Violation(String project) {
super();
this.project = project;
this.alarms = new ArrayList<>();
}
}
@@ -0,0 +1,145 @@
package edu.lu.uni.serval.parameters;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import com.github.gumtreediff.tree.ITree;
import edu.lu.uni.serval.FixPatternParser.Tokenizer;
import edu.lu.uni.serval.FixPatternParser.violations.Violation;
import edu.lu.uni.serval.config.Configuration;
import edu.lu.uni.serval.gumtree.regroup.SimpleTree;
import edu.lu.uni.serval.gumtree.regroup.SimplifyTree;
import edu.lu.uni.serval.utils.FileHelper;
import edu.lu.uni.serval.violation.code.parser.ViolationSourceCodeTree;
/**
* Prepare data for tuning parameters of deep learning models.
*
* @author kui.liu
*
*/
public class ParseAlarms {
public static void main(String[] args) {
ParseAlarms parser = new ParseAlarms();
String outputPath = Configuration.ROOT_PATH + "TuneParameters/fixedAlarmTokens.list";
String outputPath2 = Configuration.ROOT_PATH + "TuneParameters/EmptyStatement/fixedAlarmTokens.list";
FileHelper.deleteFile(outputPath);
FileHelper.deleteFile(outputPath2);
int subIndex = 5;
String fixedAlarmFilesPath = Configuration.GUM_TREE_INPUT + "prevFiles/";
String positionsFilePath = Configuration.GUM_TREE_INPUT + "positions/";
parser.dataPreparation(fixedAlarmFilesPath, positionsFilePath, subIndex, outputPath, outputPath2);
outputPath = Configuration.ROOT_PATH + "TuneParameters/unfixedAlarmTokens.list";
outputPath2 = Configuration.ROOT_PATH + "TuneParameters/EmptyStatement/unfixedAlarmTokens.list";
FileHelper.deleteFile(outputPath);
FileHelper.deleteFile(outputPath2);
subIndex = 8;
String unfixedAlarmFilesPath = Configuration.GUM_TREE_INPUT + "unfixAlarms/";
String unfixedPositionsFilePath = Configuration.GUM_TREE_INPUT + "un_positions/";
parser.dataPreparation(unfixedAlarmFilesPath, unfixedPositionsFilePath, subIndex, outputPath, outputPath2);
}
public void dataPreparation(String sourceCodeFilePath, String positionFilePath, int subIndex, String outputPath, String outputPath2) {
StringBuilder tokensBuilder = new StringBuilder();
List<File> javaFiles = FileHelper.getAllFilesInCurrentDiectory(sourceCodeFilePath, ".java");
int counter = 0;
int a = 0;
StringBuilder emptyStatements = new StringBuilder();
StringBuilder sizes = new StringBuilder();
for (File javaFile : javaFiles) {
String fileName = javaFile.getName().replace(".java", ".txt");
fileName = fileName.substring(subIndex);
// if (fileName.endsWith("apache-commons-configuration_8c42aa_8b26e6src#java#org#apache#commons#configuration#plist#XMLPropertyListConfiguration.txt")) {
// System.out.println();
// }
List<Violation> violations = readViolations(positionFilePath + fileName);
for (Violation violation : violations) {
int startLine = violation.getStartLineNum();
int endLine = violation.getEndLineNum();
String alarmType = violation.getViolationType();
if (endLine > startLine + 5) continue;
ViolationSourceCodeTree alarmTree = new ViolationSourceCodeTree(javaFile, startLine, endLine);
alarmTree.extract(alarmType);
List<ITree> matchedTrees = alarmTree.getViolationSourceCodeTrees();
if (matchedTrees.size() == 0) {
emptyStatements.append(alarmType + "," + fileName + "," + startLine + "," + endLine + "\n");
a ++;
continue;
}
SimpleTree simpleTree = new SimpleTree();
simpleTree.setLabel("Block");
simpleTree.setNodeType("Block");
List<SimpleTree> children = new ArrayList<>();
for (ITree matchedTree : matchedTrees) {
SimpleTree simpleT = new SimplifyTree().canonicalizeSourceCodeTree(matchedTree, null);
children.add(simpleT);
}
simpleTree.setChildren(children);
String tokens = Tokenizer.getTokensDeepFirst(simpleTree);
String[] tokensArray = tokens.split(" ");
int length = tokensArray.length;
sizes.append(length + "\n");
tokensBuilder.append(alarmType + ":" + fileName + ":" + alarmTree.getViolationFinalStartLine() + ":" + alarmTree.getViolationFinalEndLine() + ":" + tokens + "\n");
counter ++;
if (counter % 10000 == 0) {
FileHelper.outputToFile(outputPath, tokensBuilder, true);
tokensBuilder.setLength(0);
}
}
}
System.out.println("Volidated Instances: " + counter);
System.out.println("Empty Instances: " + a);
FileHelper.outputToFile(outputPath2, emptyStatements, false);
FileHelper.outputToFile(outputPath.replace(".list", "Sizes.csv"), sizes, false);
FileHelper.outputToFile(outputPath, tokensBuilder, true);
}
private List<Violation> readViolations(String file) {
List<Violation> violations = new ArrayList<>();
String fileContent = FileHelper.readFile(file);
BufferedReader reader = null;
reader = new BufferedReader(new StringReader(fileContent));
String line = null;
try {
while ((line = reader.readLine()) != null) {
String[] positionStr = line.split(":");
int startLine = Integer.parseInt(positionStr[1]);
int endLine = Integer.parseInt(positionStr[2]);
if (startLine == -1 || endLine == -1) {
continue;
}
String alarmType = positionStr[0];
Violation violation = new Violation(startLine, endLine, alarmType);
violations.add(violation);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return violations;
}
}
@@ -48,8 +48,8 @@ public class Statistic {
/**
* Do statistics from two files:
*/
statistics("../FPM_Violations/RQ1/all-leafnodes-per-project-vtype.csv", "");
statistics("../FPM_Violations/RQ1/distinct-fixed-summary-per-project-vtype.csv", "Fixed");
// statistics("../FPM_Violations/RQ1/all-leafnodes-per-project-vtype.csv", "");
// statistics("../FPM_Violations/RQ1/distinct-fixed-summary-per-project-vtype.csv", "Fixed");
// fixedVSunfixed();
String statistic = "../FPM_Violations/OUTPUT";
@@ -64,34 +64,39 @@ public class Statistic {
int largeHunk = 0;
int nullSourceCode = 0;
int noStatementChanges = 0;
int IllegalV = 0;
int nullDiffentry = 0;
int TestingInfo = 0;
int i = 0;
for (File file : files) {
if (file.getName().startsWith("test")) continue;
String content = FileHelper.readFile(file);
BufferedReader reader = new BufferedReader(new StringReader(content));
String line = null;
i ++;
try {
while ((line = reader.readLine()) != null) {
if (line.startsWith("test")) {
if (line.startsWith("TestViolations")) {
testAlarms += Integer.parseInt(line.substring(line.lastIndexOf(":") + 1).trim());
} else if (line.startsWith("nullGum")) {
} else if (line.startsWith("NullGumTreeResults")) {
nullGumTreeResults += Integer.parseInt(line.substring(line.lastIndexOf(":") + 1).trim());
} else if (line.startsWith("nullMap")) {
nullMappingGumTreeResults += Integer.parseInt(line.substring(line.lastIndexOf(":") + 1).trim());
} else if (line.startsWith("pure")) {
pureDeletion += Integer.parseInt(line.substring(line.lastIndexOf(":") + 1).trim());
} else if (line.startsWith("Time")) {
timeout += Integer.parseInt(line.substring(line.lastIndexOf(":") + 1).trim());
} else if (line.startsWith("noSourceCodeChanges")) {
} else if (line.startsWith("NoSourceCodeChanges")) {
noSourceCodeChagnes += Integer.parseInt(line.substring(line.lastIndexOf(":") + 1).trim());
} else if (line.startsWith("largeHunk")) {
largeHunk += Integer.parseInt(line.substring(line.lastIndexOf(":") + 1).trim());
} else if (line.startsWith("nullSourceCode")) {
nullSourceCode += Integer.parseInt(line.substring(line.lastIndexOf(":") + 1).trim());
} else if (line.startsWith("noStatementChanges")) {
} else if (line.startsWith("NoStatementChanges")) {
noStatementChanges += Integer.parseInt(line.substring(line.lastIndexOf(":") + 1).trim());
} else if (line.startsWith("IllegalV")) {
IllegalV +=Integer.parseInt(line.substring(line.lastIndexOf(":") + 1).trim());
} else if (line.startsWith("NullDiffEntry")) {
nullDiffentry += Integer.parseInt(line.substring(line.lastIndexOf(":") + 1).trim());
} else if (line.startsWith("NullMatchedGumTreeResults")) {
nullMappingGumTreeResults += Integer.parseInt(line.substring(line.lastIndexOf(":") + 1).trim());
} else if (line.startsWith("PureDeletion")) {
pureDeletion += Integer.parseInt(line.substring(line.lastIndexOf(":") + 1).trim());
} else if (line.startsWith("LargeHunk")) {
largeHunk += Integer.parseInt(line.substring(line.lastIndexOf(":") + 1).trim());
} else if (line.startsWith("NullSourceCode")) {
nullSourceCode += Integer.parseInt(line.substring(line.lastIndexOf(":") + 1).trim());
} else if (line.startsWith("Timeout")) {
timeout += Integer.parseInt(line.substring(line.lastIndexOf(":") + 1).trim());
} else if (line.startsWith("TestingInfo")) {
TestingInfo +=Integer.parseInt(line.substring(line.lastIndexOf(":") + 1).trim());
}
}
} catch (IOException e) {
@@ -104,34 +109,70 @@ public class Statistic {
}
}
}
// 22280, 23275
// Statistics:
// TestViolation: 4682
// NullGumTreeResults: 0
// NoSourceCodeChange: 5995
// NoStatementChange: 0
// NullDiffEntry: 6450
// NullMatchedGumTreeResult: 15675
// PureDeletion: 15740
// LargeHunk: 13521
// NullSourceCode: 0
// Timeout: 7205
// TestingInfo: 8703
// 62231 22634
// #PureDeletion1: 16864
// #NullMatchedGumTreeResult: 17511
// #NullDiffEntry: 6986
// #LargeHunk: 14625
// #NoSourceCodeChange: 7010
// #TestingInfo: 9071
// #TestViolation: 4682
// #Timeout: 7205
// 67090 22634
// TestAlarms: 5175
// nullGumTreeResults: 13449
// nullMappingGumTreeResults: 33010
// pureDeletion: 7598
// Timeout: 263
// Statistics:
// TestAlarms: 5696
// nullGumTreeResults: 0
// nullMappingGumTreeResults: 29056 29078
// pureDeletion: 40728
// Timeout: 135
// noSourceCodeChange: 7496
// largeHunk: 2584
// nullSourceCode: 0
// noStatementChanges: 7567
// IllegalV: 1194
System.out.println("\n\nStatistics:\nTestAlarms: " + testAlarms);
System.out.println("nullGumTreeResults: " + nullGumTreeResults);
System.out.println("nullMappingGumTreeResults: " + nullMappingGumTreeResults);
System.out.println("pureDeletion: " + pureDeletion);
System.out.println(i);
System.out.println("\n\nStatistics:\nTestViolation: " + testAlarms);
System.out.println("NullGumTreeResults: " + nullGumTreeResults);
System.out.println("NoSourceCodeChange: " + noSourceCodeChagnes);
System.out.println("NoStatementChange: " + noStatementChanges);
System.out.println("NullDiffEntry: " + nullDiffentry);
System.out.println("NullMatchedGumTreeResult: " + nullMappingGumTreeResults);
System.out.println("PureDeletion: " + pureDeletion);
System.out.println("LargeHunk: " + largeHunk);
System.out.println("NullSourceCode: " + nullSourceCode);
System.out.println("Timeout: " + timeout);
System.out.println("noSourceCodeChange: " + noSourceCodeChagnes);
System.out.println("largeHunk: " + largeHunk);
System.out.println("nullSourceCode: " + nullSourceCode);
System.out.println("noStatementChanges: " + noStatementChanges);
System.out.println("IllegalV: " + IllegalV);
System.out.println("TestingInfo: " + TestingInfo);
System.out.println(testAlarms + nullGumTreeResults + noSourceCodeChagnes + noStatementChanges +
nullDiffentry + nullMappingGumTreeResults + largeHunk + nullSourceCode + timeout + TestingInfo);
Map<String, Integer> types = new HashMap<>();
FileInputStream fis = new FileInputStream("../FPM_Violations/OAR.FPM.4222170.stderr");
Scanner scanner = new Scanner(fis);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (line.startsWith("## OAR [")) break;
String type = line.substring(0, line.indexOf(":"));
if (types.containsKey(type)) {
types.put(type, types.get(type) + 1);
} else {
types.put(type, 1);
}
}
scanner.close();
fis.close();
int sum = 0;
for (Map.Entry<String, Integer> entry : types.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
if (!entry.getKey().startsWith("#PureDeletion")) {
sum += entry.getValue();
}
}
System.out.println(sum);
}
public static void statistics(String fileName, String type) throws IOException {
@@ -0,0 +1,55 @@
package edu.lu.uni.serval.statistics;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import edu.lu.uni.serval.FixPatternParser.violations.Violation;
import edu.lu.uni.serval.utils.FileHelper;
public class UnparsedFiles {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("../FPM_Violations/OAR.FPM.4221129.stderr");
Scanner scanner = new Scanner(fis);
List<Violation> vList = new ArrayList<>();
String test1 = "";
String test2 = "";
String test3 = "";
int i = 0;
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] elements = line.split(":");
Violation v = new Violation(Integer.parseInt(elements[2]), Integer.parseInt(elements[3]), elements[4]);
v.setFileName(elements[1]);
if (vList.contains(v)) {
i ++;
// System.out.println(elements[0]);
} else {
vList.add(v);
}
if (line.startsWith("#TestViolation:")) {
// System.out.println(line);
if (elements[1].toLowerCase().contains("/test/")) {
test1 += elements[1] + "\n";
} else if (elements[1].toLowerCase().contains("tests/")) {
test2 += elements[1] + "\n";
} else {
test3 += elements[1] + "\n";
}
}
}
scanner.close();
fis.close();
System.out.println(vList.size());
System.out.println(i);
FileHelper.outputToFile("logs/test1.txt", test1, false);
FileHelper.outputToFile("logs/test2.txt", test2, false);
FileHelper.outputToFile("logs/test3.txt", test3, false);
}
}
@@ -1,264 +0,0 @@
package edu.lu.uni.serval.violation.code.parser;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jdt.core.dom.CompilationUnit;
import com.github.gumtreediff.tree.ITree;
import edu.lu.uni.serval.FixPatternParser.CUCreator;
import edu.lu.uni.serval.gumtree.GumTreeGenerator;
import edu.lu.uni.serval.gumtree.GumTreeGenerator.GumTreeType;
public class AlarmTree {
private File file;
private int alarmStartLine;
private int alarmEndLine;
private CompilationUnit cUnit;
private int alarmFinalStartLine;
private int alarmFinalEndLine;
private List<ITree> matchedTrees = new ArrayList<>();
public AlarmTree(File file, int startLine, int endLine) {
super();
this.file = file;
this.alarmStartLine = startLine;
this.alarmEndLine = endLine;
CUCreator cuCreator = new CUCreator();
this.cUnit = cuCreator.createCompilationUnit(this.file);
}
public AlarmTree(String fileName, int startLine, int endLine) {
this(new File(fileName), startLine, endLine);
}
public List<ITree> getAlarmTrees() {
return this.matchedTrees;
}
public int getAlarmFinalStartLine() {
return alarmFinalStartLine;
}
public int getAlarmFinalEndLine() {
return alarmFinalEndLine;
}
public void extract() {
ITree rootTree = new GumTreeGenerator().generateITreeForJavaFile(file, GumTreeType.EXP_JDT);
List<ITree> trees = rootTree.getChildren();
for (ITree tree : trees) {
int startPosition = tree.getPos();
int startLine = cUnit.getLineNumber(startPosition);
if (startLine > alarmEndLine) {
break;
}
int endPosition = startPosition + tree.getLength();
int endLine = cUnit.getLineNumber(endPosition - 1);
if (endLine < alarmStartLine) continue;
matchTrees(tree.getChildren());
}
int size = matchedTrees.size();
if (size > 0) {
this.alarmFinalStartLine = cUnit.getLineNumber(this.matchedTrees.get(0).getPos());
ITree lastTree = matchedTrees.get(size - 1);
this.alarmFinalEndLine = cUnit.getLineNumber(lastTree.getPos() + lastTree.getLength());
} else {
System.err.println(this.file.getName() + "===" + this.alarmStartLine + ":" + this.alarmEndLine);
}
}
public void extract(String type) {
ITree rootTree = new GumTreeGenerator().generateITreeForJavaFile(file, GumTreeType.EXP_JDT);
List<ITree> trees = rootTree.getChildren();
for (ITree tree : trees) {
int startPosition = tree.getPos();
int startLine = cUnit.getLineNumber(startPosition);
if (startLine > alarmEndLine) {
break;
}
int endPosition = startPosition + tree.getLength();
int endLine = cUnit.getLineNumber(endPosition - 1);
if (endLine < alarmStartLine) continue;
matchTrees(tree.getChildren());
}
int size = matchedTrees.size();
if (size > 0) {
this.alarmFinalStartLine = cUnit.getLineNumber(this.matchedTrees.get(0).getPos());
ITree lastTree = matchedTrees.get(size - 1);
this.alarmFinalEndLine = cUnit.getLineNumber(lastTree.getPos() + lastTree.getLength());
} else {
System.err.println(type);
System.err.println(this.file.getName() + "===" + this.alarmStartLine + ":" + this.alarmEndLine);
}
}
private void matchTrees(List<ITree> trees) {
for (ITree tree : trees) {
int startPosition = tree.getPos();
int startLine = cUnit.getLineNumber(startPosition);
if (startLine > alarmEndLine) {
break;
}
int endPosition = startPosition + tree.getLength();
int endLine = cUnit.getLineNumber(endPosition);
if (endLine < alarmStartLine) continue;
if (endLine == alarmEndLine) {
if (tree.getType() == 31) { // MethodDeclaration
matchTrees(tree.getChildren());
} else if (isStatement(tree)) {
addToMatchedTrees(tree);
} else {
ITree parent = getParentStatement(tree);
if (parent == null) {
if (tree.getType() == 8) { // 8: Block
matchTrees(tree.getChildren());
}
continue;
}
addToMatchedTrees(parent);
}
continue;
}
if (startLine >= alarmStartLine) {
if (isStatement(tree)) {
addToMatchedTrees(tree);
} else {
ITree parent = getParentStatement(tree);
if (parent == null) {
if (tree.getType() == 8) {
matchTrees(tree.getChildren());
}
continue;
}
addToMatchedTrees(parent);
}
} else {
// if (tree.getType() == 14) {
// ITree parent = getParentStatement(tree);
// if (parent == null) {
// matchTrees(tree.getChildren());
// } else {
// addToMatchedTrees(parent);
// }
// } else {
// matchTrees(tree.getChildren());
// }
matchTrees(tree.getChildren());
}
}
}
private void addToMatchedTrees(ITree tree) {
if (!matchedTrees.contains(tree)) {
/*
* TODO with the same parent, or the sub trees.
* In the same method body.
*/
matchedTrees.add(tree);
}
if (containsBlockStatement(tree)) {
int endLine = cUnit.getLineNumber(tree.getPos() + tree.getLength());
if (endLine > alarmEndLine) {
tree = removeBlock(tree);
}
}
}
private ITree removeBlock(ITree tree) {
List<ITree> oldChildren = tree.getChildren();
List<ITree> newChildren = new ArrayList<>();
for (ITree child : oldChildren) {
int startPosition = child.getPos();
int startLine = cUnit.getLineNumber(startPosition);
if (startLine > alarmEndLine) {
break;
}
int endPosition = startPosition + child.getLength();
int endLine = cUnit.getLineNumber(endPosition);
if (endLine > this.alarmEndLine) {
if (child.getType() == 8 || containsBlockStatement(child)) { // 8: Block
child = removeBlock(child);
if (child.getChildren().size() == 0) {
continue;
}
}
}
newChildren.add(child);
}
tree.setChildren(newChildren);
return tree;
}
private ITree getParentStatement(ITree tree) {
ITree parent = tree;
do {
parent = parent.getParent();
if (parent == null) {
return null;
}
int type = parent.getType();
if (type == 1 || type == 31 || type == 55 || type == 71) {
// AnonymousClassDeclaration
// MethodDeclaration Initializer (type == 28)
// TypeDeclaration
return null;
}
} while (!isStatement(parent));
return parent;
}
private boolean isStatement(ITree tree) {
int type = tree.getType(); // 8 Block
if (type == 6) return true; // AssertStatement
if (type == 10) return true; // BreakStatement
if (type == 17) return true; // ConstructorInvocation
if (type == 18) return true; // ContinueStatement
if (type == 21) return true; // ExpressionStatement
if (type == 23) return true; // FieldDeclaration
if (type == 41) return true; // ReturnStatement
if (type == 46) return true; // SuperConstructorInvocation
if (type == 49) return true; // SwitchCase
if (type == 53) return true; // ThrowStatement
if (type == 56) return true; // TypeDeclarationStatement
if (type == 60) return true; // VariableDeclarationStatement
if (containsBlockStatement(tree)) return true;
return false;
}
private boolean containsBlockStatement(ITree tree) {
int type = tree.getType();
if (type == 12) return true; // catchClause
if (type == 19) return true; // DoStatement
if (type == 24) return true; // ForStatement
if (type == 25) return true; // IfStatement
if (type == 30) return true; // LabeledStatement
if (type == 50) return true; // SwitchStatement
if (type == 51) return true; // SynchronizedStatement
if (type == 54) return true; // TryStatement
if (type == 61) return true; // WhileStatement
if (type == 70) return true; // EnhancedForStatement
return false;
}
}
@@ -0,0 +1,211 @@
package edu.lu.uni.serval.violation.code.parser;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import edu.lu.uni.serval.diffentry.DiffEntryHunk;
import edu.lu.uni.serval.diffentry.DiffEntryReader;
import edu.lu.uni.serval.utils.FileHelper;
import edu.lu.uni.serval.utils.ListSorter;
public class Test {
public static void main(String[] args) throws IOException {
// testV1();
// testV2();
// testV3("../FPM_Violations/OAR.FPM.4222152.stderr", "OUTPUT/unparsedviolations.txt");
testV4("../FPM_Violations/OAR.FPM.4222152.stderr");
}
private static void testV4(String inputFile) throws IOException {
FileInputStream fis = new FileInputStream(inputFile);
Scanner scanner = new Scanner(fis);
StringBuilder builder = new StringBuilder();
// boolean isDiff = false;
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (line.startsWith("## OAR")) break;
// if (line.startsWith("#NullDiffEntry")) {
// isDiff = true;
// continue;
// } else if (line.startsWith("#")) {
// isDiff = false;
// continue;
// }
// if (!isDiff) continue;
String[] elements = line.trim().split(":");
String type = elements[0];
if (type.equals("#NullDiffEntry")) {
List<DiffEntryHunk> diffentryHunks = new DiffEntryReader().readHunks2(new File("/Users/kui.liu/Public/git/FPM_Violations/GumTreeInput/diffentries/" + elements[1].replace(".java", ".txt").trim()));
int startLine = Integer.parseInt(elements[2].trim());
int endLine = Integer.parseInt(elements[3].trim());
for (DiffEntryHunk hunk : diffentryHunks) {
int hunkStart = hunk.getBugLineStartNum();
int hunkEnd = hunk.getBugRange() + hunkStart - 1;
if (startLine <= hunkEnd && hunkStart <= endLine) {
builder.append("vi " + elements[1].replace(".java", ".txt") + "\n : " + elements[2] + " : " + elements[3] + " : " + elements[4] + "\n");
}
}
}
// List<DiffEntryHunk> diffentryHunks = new DiffEntryReader().readHunks2(new File("/Users/kui.liu/Public/git/FPM_Violations/GumTreeInput/diffentries/" + elements[0].replace(".java", ".txt").trim()));
// int startLine = Integer.parseInt(elements[1].trim());
// int endLine = Integer.parseInt(elements[2].trim());
// for (DiffEntryHunk hunk : diffentryHunks) {
// int hunkStart = hunk.getBugLineStartNum();
// int hunkEnd = hunk.getBugRange() + hunkStart - 1;
//
// if (startLine <= hunkEnd && hunkStart <= endLine) {
// builder.append("vi " + elements[0].replace(".java", ".txt") + "\n : " + elements[1] + " : " + elements[2] + "\n"); //+ " : " + elements[4]
// }
// }
}
scanner.close();
fis.close();
FileHelper.outputToFile("Dataset/a.txt", builder, false);
}
private static void testV3(String inputFile, String outputFile) throws IOException {
FileInputStream fis = new FileInputStream(inputFile);
Scanner scanner = new Scanner(fis);
Map<String, List<String>> types = new HashMap<>();
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (line.startsWith("## OAR")) break;
String[] elements = line.split(":");
String type = elements[0];
if (types.containsKey(type)) {
types.get(type).add(elements[1] + " : " + elements[2] + " : " + elements[3] + " : " + elements[4]);
} else {
List<String> files = new ArrayList<>();
files.add(elements[1] + " : " + elements[2] + " : " + elements[3] + " : " + elements[4]);
types.put(type, files);
}
}
scanner.close();
fis.close();
StringBuilder builder = new StringBuilder();
for (Map.Entry<String, List<String>> entry : types.entrySet()) {
System.out.println(entry.getKey());
builder.append(entry.getKey() + "\n");
List<String> files = entry.getValue();
for (String file : files) {
builder.append(" " + file + "\n");
}
}
FileHelper.outputToFile(outputFile, builder, false);
}
public static void testV2() throws IOException {
String content = FileHelper.readFile("logs/testV2.txt");
BufferedReader reader = new BufferedReader(new StringReader(content));
String line = null;
Map<String, List<String>> types = new HashMap<>();
while ((line = reader.readLine()) != null) {
String[] elements = line.split(" : ");
String type = elements[0];
if (types.containsKey(type)) {
types.get(type).add(elements[1] + " : " + elements[2] + " : " + elements[3]);
} else {
List<String> files = new ArrayList<>();
files.add(elements[1] + " : " + elements[2] + " : " + elements[3]);
types.put(type, files);
}
}
reader.close();
StringBuilder builder = new StringBuilder();
for (Map.Entry<String, List<String>> entry : types.entrySet()) {
System.out.println(entry.getKey());
builder.append(entry.getKey() + "\n");
List<String> files = entry.getValue();
for (String file : files) {
builder.append(file + "\n");
}
}
FileHelper.outputToFile("logs/MultiV.txt", builder, false);
}
public static void testV1() throws IOException {
Map<String, Map<String, List<String>>> map = new HashMap<>();
String content = FileHelper.readFile("logs/testV1.txt");
BufferedReader reader = new BufferedReader(new StringReader(content));
String line = null;
List<String> types1 = new ArrayList<>();
List<String> types2 = new ArrayList<>();
while ((line = reader.readLine()) != null) {
String[] elements = line.split(" : ");
String type = elements[0];
String startLine = elements[2];
if (!"-1".equals(startLine)) {
startLine = "1";
if (!types1.contains(type)) {
types1.add(type);
}
} else {
if (!types2.contains(type)) {
types2.add(type);
}
}
if (map.containsKey(startLine)) {
Map<String, List<String>> types = map.get(startLine);
if (types.containsKey(type)) {
List<String> files = types.get(type);
files.add(elements[1] + "\n" + elements[2] + " : " + elements[3]);
} else {
List<String> files = new ArrayList<>();
files.add(elements[1] + "\n" + elements[2] + " : " + elements[3]);
types.put(type, files);
}
} else {
Map<String, List<String>> types = new HashMap<>();
List<String> files = new ArrayList<>();
files.add(elements[1] + "\n" + elements[2] + " : " + elements[3]);
types.put(type, files);
map.put(startLine, types);
}
}
reader.close();
StringBuilder builder = new StringBuilder();
for (Map.Entry<String, Map<String, List<String>>> entry : map.entrySet()) {
builder.append(entry.getKey() + "\n");
List<String> tList = null;
if (entry.getKey().equals("1")) {
tList = types1;
} else {
tList = types2;
}
ListSorter<String> sorter = new ListSorter<String>(tList);
tList = sorter.sortAscending();
Map<String, List<String>> types = entry.getValue();
for (String type : tList) {
builder.append(" " + type + "\n");
List<String> files = types.get(type);
for (String file : files) {
builder.append(file + "\n");
}
}
}
FileHelper.outputToFile("logs/NullV.txt", builder, false);
}
}
@@ -29,12 +29,12 @@ public class ViolationCodeParser {
* Violation code files.
* Position files.
*/
String fixedViolationFilesPath = Configuration.GUM_TREE_INPUT + "prevFiles/";
String positionsFilePath = Configuration.GUM_TREE_INPUT + "positions/";
int subIndex = 5;
String outputPath = Configuration.ROOT_PATH + "Alarms_tokens/fixedAlarms.list";
FileHelper.deleteFile(outputPath);
new ViolationCodeParser().parse(fixedViolationFilesPath, positionsFilePath, subIndex, outputPath);
// String fixedViolationFilesPath = Configuration.GUM_TREE_INPUT + "prevFiles/";
// String positionsFilePath = Configuration.GUM_TREE_INPUT + "positions/";
// int subIndex = 5;
// String outputPath = Configuration.ROOT_PATH + "Alarms_tokens/fixedAlarms.list";
// FileHelper.deleteFile(outputPath);
// new ViolationCodeParser().parse(fixedViolationFilesPath, positionsFilePath, subIndex, outputPath);
/*
* UnFixed violations:
@@ -43,10 +43,10 @@ public class ViolationCodeParser {
*/
String unfixedViolationFilesPath = Configuration.GUM_TREE_INPUT + "unfixAlarms/";
String un_positionsFilePath = Configuration.GUM_TREE_INPUT + "un_positions/";
subIndex = 8;
outputPath = Configuration.ROOT_PATH + "Alarms_tokens/unfixedAlarms.list";
FileHelper.deleteFile(outputPath);
new ViolationCodeParser().parse(unfixedViolationFilesPath, un_positionsFilePath, subIndex, outputPath);
int subIndex2 = 8;
String outputPath2 = Configuration.ROOT_PATH + "Alarms_tokens/unfixedAlarms.list";
FileHelper.deleteFile(outputPath2);
new ViolationCodeParser().parse(unfixedViolationFilesPath, un_positionsFilePath, subIndex2, outputPath2);
}
public void parse(String alarmFilesPath, String positionFilesPath, int subIndex, String outputPath) {
@@ -64,16 +64,16 @@ public class ViolationCodeParser {
for (Violation violation : violations) {
int startLine = violation.getStartLineNum();
int endLine = violation.getEndLineNum();
String alarmType = violation.getAlarmType();
String alarmType = violation.getViolationType();
// if (endLine > startLine + 5) {
// log.warn("#Large_Violation_Hunk: " + fileName.replace("#", "/").replace(".txt", ".java") + ":" + startLine + ":" + endLine + ":" + alarmType);
// continue;
// }
AlarmTree alarmTree = new AlarmTree(javaFile, startLine, endLine);
ViolationSourceCodeTree alarmTree = new ViolationSourceCodeTree(javaFile, startLine, endLine);
alarmTree.extract();
List<ITree> matchedTrees = alarmTree.getAlarmTrees();
List<ITree> matchedTrees = alarmTree.getViolationSourceCodeTrees();
if (matchedTrees.size() == 0) {
System.out.println(fileName + " == " + startLine + " : " + endLine);
a ++;
@@ -95,7 +95,7 @@ public class ViolationCodeParser {
String[] tokensArray = tokens.split(" ");
int length = tokensArray.length;
if (length > maxLength) maxLength = length;
tokensBuilder.append(alarmType + ":" + fileName + ":" + alarmTree.getAlarmFinalStartLine() + ":" + alarmTree.getAlarmFinalEndLine() + ":" + tokens + "\n");
tokensBuilder.append(alarmType + ":" + fileName + ":" + alarmTree.getViolationFinalStartLine() + ":" + alarmTree.getViolationFinalEndLine() + ":" + tokens + "\n");
counter ++;
if (counter % 5000 == 0) {
FileHelper.outputToFile(outputPath, tokensBuilder, true);
@@ -0,0 +1,420 @@
package edu.lu.uni.serval.violation.code.parser;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jdt.core.dom.CompilationUnit;
import com.github.gumtreediff.tree.ITree;
import edu.lu.uni.serval.FixPatternParser.CUCreator;
import edu.lu.uni.serval.gumtree.GumTreeGenerator;
import edu.lu.uni.serval.gumtree.GumTreeGenerator.GumTreeType;
public class ViolationSourceCodeTree {
private File file;
private int violationStartLine;
private int violationEndLine;
private CompilationUnit cUnit;
private int violationFinalStartLine = 0;
private int violationFinalEndLine;
private List<ITree> matchedTrees = new ArrayList<>();
public ViolationSourceCodeTree(File file, int violationStartLine, int violationEndLine) {
super();
this.file = file;
this.violationStartLine = violationStartLine;
this.violationEndLine = violationEndLine;
CUCreator cuCreator = new CUCreator();
this.cUnit = cuCreator.createCompilationUnit(this.file);
}
public ViolationSourceCodeTree(String fileName, int violationStartLine, int violationEndLine) {
this(new File(fileName), violationStartLine, violationEndLine);
}
public List<ITree> getViolationSourceCodeTrees() {
return this.matchedTrees;
}
public int getViolationFinalStartLine() {
return violationFinalStartLine;
}
public int getViolationFinalEndLine() {
return violationFinalEndLine;
}
public void extract() {
ITree rootTree = new GumTreeGenerator().generateITreeForJavaFile(file, GumTreeType.EXP_JDT);
List<ITree> trees = rootTree.getChildren();
for (ITree tree : trees) {
int startPosition = tree.getPos();
int startLine = cUnit.getLineNumber(startPosition);
if (startLine > violationEndLine) {
break;
}
int endPosition = startPosition + tree.getLength();
int endLine = cUnit.getLineNumber(endPosition - 1);
if (endLine < violationStartLine) continue;
matchTrees(tree.getChildren());
}
int size = matchedTrees.size();
if (size > 0) {
this.violationFinalStartLine = cUnit.getLineNumber(this.matchedTrees.get(0).getPos());
ITree lastTree = matchedTrees.get(size - 1);
this.violationFinalEndLine = cUnit.getLineNumber(lastTree.getPos() + lastTree.getLength());
} else {
System.err.println(this.file.getName() + "===" + this.violationStartLine + ":" + this.violationEndLine);
}
}
public void extract(String type) {
ITree rootTree = new GumTreeGenerator().generateITreeForJavaFile(file, GumTreeType.EXP_JDT);
List<ITree> trees = rootTree.getChildren();
for (ITree tree : trees) {
int startPosition = tree.getPos();
int startLine = cUnit.getLineNumber(startPosition);
if (startLine > violationEndLine) {
break;
}
int endPosition = startPosition + tree.getLength();
int endLine = cUnit.getLineNumber(endPosition - 1);
if (endLine < violationStartLine) continue;
matchTrees(tree.getChildren());
}
int size = matchedTrees.size();
if (size > 0) {
this.violationFinalStartLine = cUnit.getLineNumber(this.matchedTrees.get(0).getPos());
ITree lastTree = matchedTrees.get(size - 1);
this.violationFinalEndLine = cUnit.getLineNumber(lastTree.getPos() + lastTree.getLength());
} else {
System.err.println(type);
System.err.println(this.file.getName() + "===" + this.violationStartLine + ":" + this.violationEndLine);
}
}
private void matchTrees(List<ITree> trees) {
for (ITree tree : trees) {
int startPosition = tree.getPos();
int startLine = cUnit.getLineNumber(startPosition);
if (startLine > violationEndLine) {
break;
}
int endPosition = startPosition + tree.getLength();
int endLine = cUnit.getLineNumber(endPosition);
if (endLine < violationStartLine) continue;
if (endLine == violationEndLine) {
if (tree.getType() == 31) { // MethodDeclaration
matchTrees(tree.getChildren());
} else if (isStatement(tree)) {
addToMatchedTrees(tree);
} else {
ITree parent = getParentStatement(tree);
if (parent == null) {
if (tree.getType() == 8) { // 8: Block
matchTrees(tree.getChildren());
}
continue;
}
addToMatchedTrees(parent);
}
continue;
}
if (startLine >= violationStartLine) {
if (isStatement(tree)) {
addToMatchedTrees(tree);
} else {
ITree parent = getParentStatement(tree);
if (parent == null) {
if (tree.getType() == 8) {
matchTrees(tree.getChildren());
}
continue;
}
addToMatchedTrees(parent);
}
} else {
// if (tree.getType() == 14) {
// ITree parent = getParentStatement(tree);
// if (parent == null) {
// matchTrees(tree.getChildren());
// } else {
// addToMatchedTrees(parent);
// }
// } else {
// matchTrees(tree.getChildren());
// }
matchTrees(tree.getChildren());
}
}
}
private void addToMatchedTrees(ITree tree) {
if (!matchedTrees.contains(tree)) {
/*
* TODO with the same parent, or the sub trees.
* In the same method body.
*/
matchedTrees.add(tree);
}
if (containsBlockStatement(tree)) {
int endLine = cUnit.getLineNumber(tree.getPos() + tree.getLength());
if (endLine > violationEndLine) {
tree = removeBlock(tree);
}
}
}
private ITree removeBlock(ITree tree) {
List<ITree> oldChildren = tree.getChildren();
List<ITree> newChildren = new ArrayList<>();
for (ITree child : oldChildren) {
int startPosition = child.getPos();
int startLine = cUnit.getLineNumber(startPosition);
if (startLine > violationEndLine) {
break;
}
int endPosition = startPosition + child.getLength();
int endLine = cUnit.getLineNumber(endPosition);
if (endLine > this.violationEndLine) {
if (child.getType() == 8 || containsBlockStatement(child)) { // 8: Block
child = removeBlock(child);
if (child.getChildren().size() == 0) {
continue;
}
}
}
newChildren.add(child);
}
tree.setChildren(newChildren);
return tree;
}
private ITree getParentStatement(ITree tree) {
ITree parent = tree;
do {
parent = parent.getParent();
if (parent == null) {
return null;
}
int type = parent.getType();
if (type == 1 || type == 31 || type == 55 || type == 71) {
// AnonymousClassDeclaration
// MethodDeclaration Initializer (type == 28)
// TypeDeclaration
// EnumDeclaration
return null;
}
} while (!isStatement(parent));
return parent;
}
private boolean isStatement(ITree tree) {
int type = tree.getType(); // 8 Block
if (type == 6) return true; // AssertStatement
if (type == 10) return true; // BreakStatement
if (type == 17) return true; // ConstructorInvocation
if (type == 18) return true; // ContinueStatement
if (type == 21) return true; // ExpressionStatement
if (type == 23) return true; // FieldDeclaration
if (type == 41) return true; // ReturnStatement
if (type == 46) return true; // SuperConstructorInvocation
if (type == 49) return true; // SwitchCase
if (type == 53) return true; // ThrowStatement
if (type == 56) return true; // TypeDeclarationStatement
if (type == 60) return true; // VariableDeclarationStatement
if (containsBlockStatement(tree)) return true;
return false;
}
private boolean containsBlockStatement(ITree tree) {
int type = tree.getType();
if (type == 12) return true; // catchClause
if (type == 19) return true; // DoStatement
if (type == 24) return true; // ForStatement
if (type == 25) return true; // IfStatement
if (type == 30) return true; // LabeledStatement
if (type == 50) return true; // SwitchStatement
if (type == 51) return true; // SynchronizedStatement
if (type == 54) return true; // TryStatement
if (type == 61) return true; // WhileStatement
if (type == 70) return true; // EnhancedForStatement
return false;
}
public void locateParentNode(String type) {
ITree rootTree = new GumTreeGenerator().generateITreeForJavaFile(file, GumTreeType.EXP_JDT);
List<ITree> trees = rootTree.getChildren();
for (ITree tree : trees) {
int startPosition = tree.getPos();
int startLine = cUnit.getLineNumber(startPosition);
if (startLine > violationEndLine) {
break;
}
int endPosition = startPosition + tree.getLength();
int endLine = cUnit.getLineNumber(endPosition - 1);
if (endLine < violationStartLine) continue;
locateParentNode(tree.getChildren(), type);
}
// if (this.violationFinalStartLine == 0) {
// FileHelper.outputToFile("logs/testV1.txt", type + " : " + this.file.getName() + " : " + this.violationStartLine + " : " + this.violationEndLine + "\n", true);
// }
}
private void locateParentNode(List<ITree> trees, String type) {
for (ITree tree : trees) {
int startPosition = tree.getPos();
int startLine = cUnit.getLineNumber(startPosition);
if (startLine > violationEndLine) {
break;
}
int endPosition = startPosition + tree.getLength();
int endLine = cUnit.getLineNumber(endPosition);
if (endLine < violationStartLine) continue;
if (endLine < violationEndLine) {
if ("NM_SAME_SIMPLE_NAME_AS_INTERFACE".equals(type) || "NM_SAME_SIMPLE_NAME_AS_SUPERCLASS".equals(type) || "NM_CLASS_NAMING_CONVENTION".equals(type)
|| "NM_CLASS_NOT_EXCEPTION".equals(type) || "RI_REDUNDANT_INTERFACES".equals(type)
// inner class
|| "SE_INNER_CLASS".equals(type) || "SE_BAD_FIELD_INNER_CLASS".equals(type) || "SIC_INNER_SHOULD_BE_STATIC_ANON".equals(type)
|| "SIC_INNER_SHOULD_BE_STATIC".equals(type) || "SIC_INNER_SHOULD_BE_STATIC_NEEDS_THIS".equals(type)) {
if (tree.getType() != 55) {
ITree parent = getParentTypeDeclaration(tree);
if (parent == null) {
this.violationFinalStartLine = -1;
break;
}
tree = parent;
}
startPosition = tree.getPos();
this.violationFinalStartLine = cUnit.getLineNumber(startPosition);
endPosition = getClassBodyStartPosition(tree);
if (endPosition == 0) {
endPosition = startPosition + tree.getLength();
}
this.violationFinalEndLine = cUnit.getLineNumber(endPosition);
} else if ("NM_METHOD_NAMING_CONVENTION".equals(type)){
// method name level
if (tree.getType() != 31) {
ITree parent = getParentMethodDeclaration(tree);
if (parent == null) {
this.violationFinalStartLine = -1;
break;
}
tree = parent;
}
startPosition = tree.getPos();
this.violationFinalStartLine = cUnit.getLineNumber(startPosition);
endPosition = getMethodBodyStartPosition(tree);
if (endPosition == 0) {
endPosition = startPosition + tree.getLength();
}
this.violationFinalEndLine = cUnit.getLineNumber(endPosition);
// } else if ("SE_NO_SUITABLE_CONSTRUCTOR".equals(type) || "CN_IDIOM".equals(type)
// || "SE_NO_SERIALVERSIONID".equals(type) || "SE_NO_SUITABLE_CONSTRUCTOR_FOR_EXTERNALIZATION".equals(type)
// || "SE_COMPARATOR_SHOULD_BE_SERIALIZABLE".equals(type)) {
// this.violationFinalStartLine = -1;
} else {
this.violationFinalStartLine = -1;
}
// FileHelper.outputToFile("logs/testV2.txt", type + " : " + this.file.getName() + " : " + this.violationStartLine + " : " + this.violationEndLine + "\n", true);
break;
}
if (tree.getType() == 31 || tree.getType() == 23) { // MethodDeclaration, FieldDeclaration
this.violationFinalStartLine = startLine;
this.violationFinalEndLine = endLine;
break;
} else {
locateParentNode(tree.getChildren(), type);
}
}
}
private int getMethodBodyStartPosition(ITree tree) {
List<ITree> children = tree.getChildren();
for (int i = 0, size = children.size(); i < size; i ++) {
ITree child = children.get(i);
int type = child.getType();
if (type == 8) {
return child.getPos() - 1;
}
}
return 0;
}
private ITree getParentMethodDeclaration(ITree tree) {
ITree parent = tree;
int type = 0;
do {
parent = parent.getParent();
if (parent == null) {
return null;
}
type = parent.getType();
} while (type != 31);
return parent;
}
private int getClassBodyStartPosition(ITree tree) {
List<ITree> children = tree.getChildren();
for (int i = 0, size = children.size(); i < size; i ++) {
ITree child = children.get(i);
int type = child.getType();
// Modifier, NormalAnnotation, MarkerAnnotation, SingleMemberAnnotation
if (type != 83 && type != 77 && type != 78 && type != 79
&& type != 5 && type != 39 && type != 43 && type != 74 && type != 75
&& type != 76 && type != 84 && type != 87 && type != 88) {
// ArrayType, PrimitiveType, SimpleType, ParameterizedType,
// QualifiedType, WildcardType, UnionType, IntersectionType, NameQualifiedType
if (i > 0) {
child = children.get(i - 1);
return child.getPos() + child.getLength() + 1;
} else {
return child.getPos() - 1;
}
}
}
return 0;
}
private ITree getParentTypeDeclaration(ITree tree) {
ITree parent = tree;
int type = 0;
do {
parent = parent.getParent();
if (parent == null) {
return null;
}
type = parent.getType();
} while (type != 55);
return parent;
}
}
@@ -0,0 +1,81 @@
package edu.lu.uni.serval.violation.hunksize;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import edu.lu.uni.serval.utils.FileHelper;
import edu.lu.uni.serval.utils.ListSorter;
public class HunkSizeComputer {
public static void main(String[] args) throws IOException {
String unfixedAlarmFile = "Dataset/Unfixed-Alarms/";
List<File> unfixedAlarmFiles = FileHelper.getAllFilesInCurrentDiectory(unfixedAlarmFile, ".csv");
StringBuilder builder = new StringBuilder();
int counter = 0;
int size = 0;
List<Integer> sizes = new ArrayList<>();
for (File file : unfixedAlarmFiles) {
String content = FileHelper.readFile(file);
BufferedReader reader = new BufferedReader(new StringReader(content));
String line = null;
while ((line = reader.readLine()) != null) {
String[] elements = line.split(",");
int startLine = Integer.parseInt(elements[4]);
int endLine = Integer.parseInt(elements[5]);
if (startLine == -1 || endLine == -1) continue;
int range = endLine - startLine + 1;
builder.append(range + "\n");
if (range != 1) {
counter ++;
}
size ++;
sizes.add(range);
}
reader.close();
}
String content = FileHelper.readFile("Dataset/fixed-alarms-v1.0.list");
BufferedReader reader = new BufferedReader(new StringReader(content));
String line = null;
while ((line = reader.readLine()) != null) {
int arrowIndex = line.indexOf("=>");
String buggyInfo = line.substring(0, arrowIndex);
String[] buggyElements = buggyInfo.split(":");
int startLine = Integer.parseInt(buggyElements[4]);
int endLine = Integer.parseInt(buggyElements[5]);
if (startLine == -1 || endLine == -1) continue;
int range = endLine - startLine + 1;
if (range != 1) {
counter ++;
}
builder.append(range + "\n");
size ++;
sizes.add(range);
}
reader.close();
FileHelper.outputToFile("Dataset/sizes.csv", builder, false);
System.out.println(size);
System.out.println(counter);
System.out.println(sizes.size());
ListSorter<Integer> sorter = new ListSorter<>(sizes);
sizes = sorter.sortAscending();
System.out.println(sizes.get((int) (sizes.size() * 0.7)));
System.out.println(sizes.get((int) (sizes.size() * 0.8)));
System.out.println(sizes.get((int) (sizes.size() * 0.9)));
System.out.println(sizes.get((int) (sizes.size() * 0.95)));
}
}
@@ -123,23 +123,19 @@ public class AlarmsReader {
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
int arrowIndex = line.indexOf("=>");
String alarmInfo = arrowIndex > 0 ? line.substring(0, arrowIndex) : line;
// int arrowIndex = line.indexOf("=>");
// String alarmInfo = arrowIndex > 0 ? line.substring(0, arrowIndex) : line;
// String[] buggyElements = alarmInfo.split(",");
String[] buggyElements = line.split(",");
String[] buggyElements = alarmInfo.split(",");
// String alarmType = buggyElements[0];
String projectName = buggyElements[1];
String buggyCommitId = buggyElements[2];
String buggyFile = buggyElements[3];
// int startLine = Integer.parseInt(buggyElements[4]);
// int endLine = Integer.parseInt(buggyElements[5]);
// if (startLine == -1 || endLine == -1 || endLine == 1) {
//// log.error("UNFIXED ALARM WRONG_POSITION: " + line);
// builder.append(line + "\n");
// continue;
// }
// String alarmTypeAndPosition = buggyElements[0] + ":" + startLine + ":" + endLine; // Alarm type : start line : end line.
String alarmTypeAndPosition = buggyElements[0] + ":" + buggyElements[4] + ":" + buggyElements[5]; // Alarm type : start line : end line.
Alarm alarm = new Alarm(buggyCommitId, buggyFile, "", "");
@@ -13,34 +13,34 @@ import edu.lu.uni.serval.utils.FileHelper;
public class Test {
public static void main(String[] args) throws IOException {
String filePath = Configuration.GUM_TREE_INPUT + "un_positions/";
List<File> files = FileHelper.getAllFiles(filePath, ".txt");
System.out.println(files.size());
List<String> projects = new ArrayList<>();
int unfixedI = 0;
for (File file : files) {
String fileName = file.getName();
fileName = fileName.substring(0, fileName.indexOf("#"));
fileName = fileName.substring(0, fileName.lastIndexOf("_"));
fileName = fileName.substring(0, fileName.lastIndexOf("_"));
if (!projects.contains(fileName)) {
projects.add(fileName);
}
String content = FileHelper.readFile(file);
BufferedReader reader = new BufferedReader(new StringReader(content));
while (reader.readLine() != null) {
unfixedI ++;
}
reader.close();
}
for (String str : projects) {
System.out.println(str);
}
System.out.println("Unfixed: " + unfixedI);
// String filePath = Configuration.GUM_TREE_INPUT + "un_positions/";
// List<File> files = FileHelper.getAllFiles(filePath, ".txt");
// System.out.println(files.size());
// List<String> projects = new ArrayList<>();
// int unfixedI = 0;
// for (File file : files) {
// String fileName = file.getName();
// fileName = fileName.substring(0, fileName.indexOf("#"));
// fileName = fileName.substring(0, fileName.lastIndexOf("_"));
// fileName = fileName.substring(0, fileName.lastIndexOf("_"));
// if (!projects.contains(fileName)) {
// projects.add(fileName);
// }
// String content = FileHelper.readFile(file);
// BufferedReader reader = new BufferedReader(new StringReader(content));
// while (reader.readLine() != null) {
// unfixedI ++;
// }
// reader.close();
// }
//
// for (String str : projects) {
// System.out.println(str);
// }
// System.out.println("Unfixed: " + unfixedI);
String positionFath = Configuration.GUM_TREE_INPUT + "positions/";
files = FileHelper.getAllFilesInCurrentDiectory(positionFath, ".txt");
List<File> files = FileHelper.getAllFilesInCurrentDiectory(positionFath, ".txt");
System.out.println("File Path: " + positionFath);
System.out.println(files.size());
int i = 0;
@@ -56,18 +56,18 @@ public class Test {
System.out.println("Fixed: " + i);
String unfixedAlarmFile = "Dataset/Unfixed-Alarms/";
List<File> unfixedAlarmFiles = FileHelper.getAllFilesInCurrentDiectory(unfixedAlarmFile, ".csv");
int a = 0;
for (File file : unfixedAlarmFiles) {
String content = FileHelper.readFile(file);
BufferedReader reader = new BufferedReader(new StringReader(content));
while (reader.readLine() != null) {
a ++;
}
reader.close();
}
System.out.println("unfixed: " + a);
// String unfixedAlarmFile = "Dataset/Unfixed-Alarms/";
// List<File> unfixedAlarmFiles = FileHelper.getAllFilesInCurrentDiectory(unfixedAlarmFile, ".csv");
// int a = 0;
// for (File file : unfixedAlarmFiles) {
// String content = FileHelper.readFile(file);
// BufferedReader reader = new BufferedReader(new StringReader(content));
// while (reader.readLine() != null) {
// a ++;
// }
// reader.close();
// }
// System.out.println("unfixed: " + a);
String fixedAlarmFile = "Dataset/fixed-alarms-v1.0.list";
int b = 0;
@@ -27,31 +27,46 @@ public class TestViolationParser {
}
}
String unfixedAlarmFile = "Dataset/Unfixed-Alarms/";
final String unfixedFilesPath = Configuration.GUM_TREE_INPUT + "unfixAlarms/";
final String unfixedOositionsFilePath = Configuration.GUM_TREE_INPUT + "un_positions/";
// Violation instances of single violation type
String unfixedViolations = "../FPM_Violations/unFixedInstances/";
String unfixedFilesPath = Configuration.GUM_TREE_INPUT + "UnfixedViolations/";
String unfixedPositionsFilePath = Configuration.GUM_TREE_INPUT + "UnFV_positions/";
FileHelper.createDirectory(unfixedFilesPath);
FileHelper.createDirectory(unfixedOositionsFilePath);
List<File> unfixedAlarmFiles = FileHelper.getAllFilesInCurrentDiectory(unfixedAlarmFile, ".csv");
FileHelper.createDirectory(unfixedPositionsFilePath);
List<File> unfixedAlarmFiles = FileHelper.getAllFilesInCurrentDiectory(unfixedViolations, ".list");
for (File file : unfixedAlarmFiles) {
String fileName = FileHelper.getFileNameWithoutExtension(file);
FileHelper.createDirectory(unfixedFilesPath + fileName + "/");
FileHelper.createDirectory(unfixedPositionsFilePath + fileName + "/");
ViolationParser parser = new ViolationParser();
parser.parseViolations(file, repositoriesList, unfixedFilesPath, unfixedOositionsFilePath);
parser.parseViolations(file, repositoriesList, unfixedFilesPath + fileName + "/", unfixedPositionsFilePath + fileName + "/");
}
String fixedAlarmFile = "Dataset/fixed-alarms-v1.0.list";
final String previousFilesPath = Configuration.GUM_TREE_INPUT + "prevFiles/";
final String revisedFilesPath = Configuration.GUM_TREE_INPUT + "revFiles/";
final String positionsFilePath = Configuration.GUM_TREE_INPUT + "positions/";
final String diffentryFilePath = Configuration.GUM_TREE_INPUT + "diffentries/";
FileHelper.createDirectory(previousFilesPath);
FileHelper.createDirectory(revisedFilesPath);
FileHelper.createDirectory(positionsFilePath);
FileHelper.createDirectory(diffentryFilePath);
// String unfixedAlarmFile = "Dataset/Unfixed-Alarms/";
// final String unfixedFilesPath = Configuration.GUM_TREE_INPUT + "unfixAlarms/";
// final String unfixedOositionsFilePath = Configuration.GUM_TREE_INPUT + "un_positions/";
// FileHelper.createDirectory(unfixedFilesPath);
// FileHelper.createDirectory(unfixedOositionsFilePath);
//
// List<File> unfixedAlarmFiles = FileHelper.getAllFilesInCurrentDiectory(unfixedAlarmFile, ".csv");
//
// for (File file : unfixedAlarmFiles) {
// ViolationParser parser = new ViolationParser();
// parser.parseViolations(file, repositoriesList, unfixedFilesPath, unfixedOositionsFilePath);
// }
ViolationParser parser = new ViolationParser();
parser.parseViolations(fixedAlarmFile, repositoriesList, previousFilesPath, revisedFilesPath, positionsFilePath, diffentryFilePath);
// String fixedAlarmFile = "Dataset/fixed-alarms-v1.0.list";
// final String previousFilesPath = Configuration.GUM_TREE_INPUT + "prevFiles/";
// final String revisedFilesPath = Configuration.GUM_TREE_INPUT + "revFiles/";
// final String positionsFilePath = Configuration.GUM_TREE_INPUT + "positions/";
// final String diffentryFilePath = Configuration.GUM_TREE_INPUT + "diffentries/";
// FileHelper.createDirectory(previousFilesPath);
// FileHelper.createDirectory(revisedFilesPath);
// FileHelper.createDirectory(positionsFilePath);
// FileHelper.createDirectory(diffentryFilePath);
//
// ViolationParser parser = new ViolationParser();
// parser.parseViolations(fixedAlarmFile, repositoriesList, previousFilesPath, revisedFilesPath, positionsFilePath, diffentryFilePath);
}