Skip to content

Commit c26f941

Browse files
committed
code to detect comments that have technical debt specific keyword(s)
1 parent e6fbc6c commit c26f941

1 file changed

Lines changed: 55 additions & 0 deletions

File tree

src/main/java/TestFileDetector.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.github.javaparser.ast.ImportDeclaration;
44
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
55
import com.github.javaparser.ast.body.MethodDeclaration;
6+
import com.github.javaparser.ast.comments.Comment;
67
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
78
import entity.ClassEntity;
89
import entity.MethodEntity;
@@ -12,6 +13,8 @@
1213
import java.nio.file.Path;
1314
import java.util.ArrayList;
1415
import java.util.List;
16+
import java.util.regex.Matcher;
17+
import java.util.regex.Pattern;
1518
import java.util.stream.Collectors;
1619

1720
public class TestFileDetector {
@@ -46,6 +49,7 @@ public ClassEntity runAnalysis(Path filePath) throws FileNotFoundException {
4649
CompilationUnit compilationUnit=null;
4750

4851
if(filePath != null){
52+
System.out.println("Processing: "+filePath);
4953
classEntity = new ClassEntity(filePath);
5054
FileInputStream fileInputStream = new FileInputStream(classEntity.getFilePath());
5155
compilationUnit = JavaParser.parse(fileInputStream);
@@ -61,12 +65,63 @@ public ClassEntity runAnalysis(Path filePath) throws FileNotFoundException {
6165

6266
private class ClassVisitor extends VoidVisitorAdapter<Void> {
6367

68+
//keywords obtained from: http://ieeexplore.ieee.org/document/7820211/
69+
private final String[] DEBT_KEYWORDS = {"hack", "workaround", "yuck!", "kludge", "stupidity","needed?","unused?","fixme","todo","wtf?"};
70+
6471
@Override
6572
public void visit(ClassOrInterfaceDeclaration n, Void arg) {
6673
classEntity.setClassName(n.getNameAsString());
74+
75+
for (Comment comment:n.getAllContainedComments()) {
76+
if (containsKeyword(comment.getContent())){
77+
System.out.println(comment.getContent());
78+
}
79+
}
80+
81+
if(n.getComment().isPresent()){
82+
if (containsKeyword(n.getComment().get().getContent())){
83+
System.out.println(n.getComment().get().getContent());
84+
}
85+
}
86+
87+
if(n.getJavadocComment().isPresent()){
88+
if (containsKeyword(n.getJavadocComment().get().getContent())){
89+
System.out.println(n.getJavadocComment().get().getContent());
90+
}
91+
}
92+
93+
for (Comment comment:n.getOrphanComments()) {
94+
if (containsKeyword(comment.getContent())){
95+
System.out.println(comment.getContent());
96+
}
97+
}
98+
99+
67100
super.visit(n, arg);
68101
}
69102

103+
private Boolean containsKeyword(String input){
104+
Boolean hasKeyword = false;
105+
106+
Pattern pattern;
107+
Matcher matcher;
108+
for (String keyword : DEBT_KEYWORDS) {
109+
pattern = Pattern.compile("\\b"+keyword+"\\b",Pattern.CASE_INSENSITIVE);
110+
matcher = pattern.matcher(input);
111+
while (matcher.find()) {
112+
/*
113+
System.out.print("Start index: " + matcher.start());
114+
System.out.print(" End index: " + matcher.end() + " ");
115+
System.out.println(matcher.group());
116+
*/
117+
return true;
118+
}
119+
}
120+
121+
return hasKeyword;
122+
}
123+
124+
70125
@Override
71126
public void visit(MethodDeclaration n, Void arg) {
72127
MethodEntity method = new MethodEntity(n.getNameAsString());

0 commit comments

Comments
 (0)