Skip to content

Commit 3055ca3

Browse files
committed
Added method tagging for SATD that occurs before a method signature
1 parent 7fa9da2 commit 3055ca3

File tree

3 files changed

+52
-23
lines changed

3 files changed

+52
-23
lines changed

mySQL.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
USERNAME=root
2-
PASSWORD=ROOT
3-
PORT=8129
2+
PASSWORD=root
3+
PORT=3306
44
URL=localhost
55
DB=satd
66
USE_SSL=false

src/main/java/edu/rit/se/git/RepositoryCommitReference.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@
1818

1919
import java.io.IOException;
2020
import java.io.UncheckedIOException;
21-
import java.util.Arrays;
22-
import java.util.HashMap;
23-
import java.util.List;
24-
import java.util.Map;
21+
import java.util.*;
2522
import java.util.stream.Collectors;
2623

2724
/**
@@ -45,7 +42,7 @@ public class RepositoryCommitReference {
4542
public List<RepositoryCommitReference> getParentCommitReferences() {
4643
// Debugging code -- should NOT be included in any releases.
4744
// Used to start a search at a specific diff
48-
// if( this.diff.getName().equals("d0597fefd54392f640dcc751a328841f052799bf") ) {
45+
// if( this.commit.getName().equals("e394516307697ad4ace3d0c0b1155362eeefa2d6") ) {
4946
// return new ArrayList<>();
5047
// }
5148
final RevWalk rw = new RevWalk(this.gitInstance.getRepository());

src/main/java/edu/rit/se/satd/comment/model/GroupedComment.java

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
package edu.rit.se.satd.comment.model;
22

3+
import com.github.javaparser.Position;
34
import com.github.javaparser.Range;
45
import com.github.javaparser.ast.Node;
56
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
67
import com.github.javaparser.ast.body.MethodDeclaration;
8+
import com.github.javaparser.ast.body.VariableDeclarator;
79
import com.github.javaparser.ast.comments.Comment;
10+
import com.github.javaparser.ast.expr.VariableDeclarationExpr;
811
import edu.rit.se.util.JavaParseUtil;
912
import lombok.AccessLevel;
1013
import lombok.AllArgsConstructor;
1114
import lombok.Getter;
1215
import lombok.NoArgsConstructor;
1316

1417
import java.util.Arrays;
18+
import java.util.Comparator;
19+
import java.util.List;
1520
import java.util.Optional;
1621
import java.util.stream.Collectors;
1722

@@ -175,23 +180,50 @@ public static GroupedComment fromJavaParserComment(Comment oldComment) {
175180
newComment.containingClassDeclarationLineEnd = classRange.end.line;
176181

177182
// Method Data
178-
final Optional<MethodDeclaration> thisMethod = classRoot.get().findAll(MethodDeclaration.class).stream()
183+
// We need to check for all comments between the end of the last comment
184+
// and the end of the current comment. This is done to associate comments that
185+
// are not inside the contents of a method with the method that they pertain to.
186+
187+
// Get all class variables
188+
// TODO fix issue where class variables are defined after a method
189+
final List<VariableDeclarator> variables = classRoot.get().findAll(VariableDeclarator.class).stream()
190+
.filter(varDec -> varDec.getParentNode().isPresent() &&
191+
varDec.getParentNode().get().getParentNode().isPresent() &&
192+
varDec.getParentNode().get().getParentNode().get() instanceof ClassOrInterfaceDeclaration)
193+
.collect(Collectors.toList());
194+
final List<MethodDeclaration> allMethods = classRoot.get().findAll(MethodDeclaration.class).stream()
179195
.filter(dec -> dec.getRange().isPresent())
180-
.filter(dec -> JavaParseUtil.isRangeBetweenBounds(
181-
dec.getRange().get(), newComment.startLine, newComment.endLine))
182-
.findFirst();
183-
newComment.containingMethod = thisMethod
184-
.map(method -> method.getDeclarationAsString(false, false, false))
185-
// Trim return type as it is not important for this study, and only causes problems
186-
// when detecting movement of SATD between files
187-
.map(methodDec -> methodDec.substring(methodDec.indexOf(" ")+1))
188-
.orElse(UNKNOWN);
189-
final Range methodRange = thisMethod
190-
.map(Node::getRange)
191-
.orElse(Optional.of(NULL_RANGE))
192-
.orElse(NULL_RANGE);
193-
newComment.containingMethodDeclarationLineStart = methodRange.begin.line;
194-
newComment.containingMethodDeclarationLineEnd = methodRange.end.line;
196+
.sorted(Comparator.comparingInt(x -> x.getRange().get().begin.line))
197+
.collect(Collectors.toList());
198+
199+
// The first method in the class can be assumed to begin after all class variables have
200+
// been defined, so we start our bounds for the first method we search at the end of the
201+
// final variable declaration
202+
int lastMethodEnd = variables.stream()
203+
.filter(var -> var.getRange().isPresent())
204+
.map(var -> var.getRange().get().end.line)
205+
.max(Comparator.comparingInt(x -> x))
206+
.orElse(-1);
207+
for (final MethodDeclaration curMethod : allMethods) {
208+
final Range methodRangeIncludingProceeding = new Range(
209+
new Position(lastMethodEnd, 0),
210+
new Position(curMethod.getRange().get().end.line, 0));
211+
// If the comment is within the bounds, then we can assume it related to this method
212+
if (JavaParseUtil.isRangeBetweenBounds(
213+
methodRangeIncludingProceeding, newComment.startLine, newComment.endLine)) {
214+
newComment.containingMethod = curMethod
215+
.getDeclarationAsString(false, false, false);
216+
newComment.containingMethod = newComment.containingMethod
217+
.substring(newComment.containingMethod.indexOf(" ") + 1);
218+
newComment.containingMethodDeclarationLineStart = curMethod.asMethodDeclaration()
219+
.getRange().get().begin.line;
220+
newComment.containingMethodDeclarationLineEnd = curMethod.asMethodDeclaration()
221+
.getRange().get().end.line;
222+
break;
223+
}
224+
lastMethodEnd = curMethod.getRange().get().end.line;
225+
}
226+
195227
}
196228
return newComment;
197229
}

0 commit comments

Comments
 (0)