Skip to content

Commit

Permalink
Enable detection of Inline Variable in commit
Browse files Browse the repository at this point in the history
pmd/pmd@07516ee
Support comment multi-mappings in commit
jOOQ/jOOQ@58a4e74
Fix bug in the generation of line-comment groups
Add tests for comment multi-mappings
  • Loading branch information
tsantalis committed Oct 8, 2024
1 parent 90eab8a commit b852322
Show file tree
Hide file tree
Showing 10 changed files with 236 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,20 @@ public void temporaryVariableAssignment(AbstractCodeFragment statement,
}
}
}
else if(initializer != null && initializer.toString().equals(before)) {
ExtractVariableRefactoring ref = new ExtractVariableRefactoring(declaration, operation1, operation2, insideExtractedOrInlinedMethod);
List<LeafExpression> subExpressions = getFragment1().findExpression(before);
for(LeafExpression subExpression : subExpressions) {
LeafMapping leafMapping = new LeafMapping(subExpression, initializer, operation1, operation2);
ref.addSubExpressionMapping(leafMapping);
}
processExtractVariableRefactoring(ref, refactorings);
checkForNestedExtractVariable(ref, refactorings, nonMappedLeavesT2, insideExtractedOrInlinedMethod);
if(identical()) {
identicalWithExtractedVariable = true;
}
return;
}
}
else if(after.startsWith("()" + JAVA.LAMBDA_ARROW + variableName + ".")) {
int extraLength = "()".length() + JAVA.LAMBDA_ARROW.length();
Expand Down Expand Up @@ -836,6 +850,19 @@ public void inlinedVariableAssignment(AbstractCodeFragment statement,
}
}
}
else if(initializer != null && initializer.toString().equals(after)) {
InlineVariableRefactoring ref = new InlineVariableRefactoring(declaration, operation1, operation2, insideExtractedOrInlinedMethod);
List<LeafExpression> subExpressions = getFragment2().findExpression(after);
for(LeafExpression subExpression : subExpressions) {
LeafMapping leafMapping = new LeafMapping(initializer, subExpression, operation1, operation2);
ref.addSubExpressionMapping(leafMapping);
}
processInlineVariableRefactoring(ref, refactorings);
if(identical()) {
identicalWithInlinedVariable = true;
}
return;
}
}
else if(before.startsWith("()" + JAVA.LAMBDA_ARROW + variableName + ".")) {
int extraLength = "()".length() + JAVA.LAMBDA_ARROW.length();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2651,13 +2651,23 @@ else if(streamAPIStatements1.size() > 0 && streamAPIStatements2.size() == 0) {
inlinedVariableAssignment(statement, nonMappedLeavesT2);
}
if(parentMapper != null && parentMapper.commentListDiff != null) {
List<UMLComment> deletedComments = new ArrayList<UMLComment>();
deletedComments.addAll(parentMapper.commentListDiff.getDeletedComments());
AbstractCodeMapping parentMapping = findParentMappingContainingOperationInvocation();
Set<UMLComment> deletedComments = new LinkedHashSet<UMLComment>();
if(parentMapping != null) {
for(UMLComment deletedComment : parentMapper.commentListDiff.getDeletedComments()) {
if(parentMapping.getFragment1().getLocationInfo().subsumes(deletedComment.getLocationInfo())) {
deletedComments.add(deletedComment);
}
}
}
else {
deletedComments.addAll(parentMapper.commentListDiff.getDeletedComments());
}
for(UMLOperationBodyMapper childMapper : parentMapper.getChildMappers()) {
if(childMapper.commentListDiff != null)
deletedComments.addAll(childMapper.commentListDiff.getDeletedComments());
}
this.commentListDiff = new UMLCommentListDiff(deletedComments, container2.getComments());
this.commentListDiff = new UMLCommentListDiff(new ArrayList<>(deletedComments), container2.getComments());
checkUnmatchedStatementsBeingCommented();
}
}
Expand Down Expand Up @@ -3041,13 +3051,23 @@ else if(streamAPIStatements1.size() > 0 && streamAPIStatements2.size() == 0) {
inlinedVariableAssignment(statement, nonMappedLeavesT2);
}
if(parentMapper != null && parentMapper.commentListDiff != null) {
List<UMLComment> addedComments = new ArrayList<UMLComment>();
addedComments.addAll(parentMapper.commentListDiff.getAddedComments());
AbstractCodeMapping parentMapping = findParentMappingContainingOperationInvocation();
Set<UMLComment> addedComments = new LinkedHashSet<UMLComment>();
if(parentMapping != null) {
for(UMLComment addedComment : parentMapper.commentListDiff.getAddedComments()) {
if(parentMapping.getFragment2().getLocationInfo().subsumes(addedComment.getLocationInfo())) {
addedComments.add(addedComment);
}
}
}
else {
addedComments.addAll(parentMapper.commentListDiff.getAddedComments());
}
for(UMLOperationBodyMapper childMapper : parentMapper.getChildMappers()) {
if(childMapper.commentListDiff != null)
addedComments.addAll(childMapper.commentListDiff.getAddedComments());
}
this.commentListDiff = new UMLCommentListDiff(container1.getComments(), addedComments);
this.commentListDiff = new UMLCommentListDiff(container1.getComments(), new ArrayList<>(addedComments));
checkUnmatchedStatementsBeingCommented();
}
}
Expand Down
17 changes: 16 additions & 1 deletion src/main/java/gr/uom/java/xmi/diff/UMLCommentListDiff.java
Original file line number Diff line number Diff line change
Expand Up @@ -177,17 +177,32 @@ private List<UMLCommentGroup> createCommentGroups(List<UMLComment> commentsBefor
}
else {
//make new group
groups.add(new UMLCommentGroup(currentGroup.getGroup()));
if (currentGroup.getGroup().size() > 0) {
groups.add(new UMLCommentGroup(currentGroup.getGroup()));
}
currentGroup = new UMLCommentGroup();
currentGroup.addComment(current);
}
}
else {
//make new group
if (currentGroup.getGroup().size() > 0) {
groups.add(new UMLCommentGroup(currentGroup.getGroup()));
}
currentGroup = new UMLCommentGroup();
currentGroup.addComment(current);
}
}
else {
//this is the first line comment
currentGroup.addComment(current);
}
}
else if (currentGroup.getGroup().size() > 0) {
//there is a comment of different type
groups.add(new UMLCommentGroup(currentGroup.getGroup()));
currentGroup = new UMLCommentGroup();
}
}
if(!currentGroup.getGroup().isEmpty())
groups.add(currentGroup);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ public void testAllRefactorings() throws Exception {
GitHistoryRefactoringMinerImpl detector = new GitHistoryRefactoringMinerImpl();
TestBuilder test = new TestBuilder(detector, REPOS, Refactorings.All.getValue());
RefactoringPopulator.feedRefactoringsInstances(Refactorings.All.getValue(), Systems.FSE.getValue(), test);
test.assertExpectationsWithGitHubAPI(12265, 20, 233);
test.assertExpectationsWithGitHubAPI(12269, 20, 233);
}
}
44 changes: 44 additions & 0 deletions src/test/java/org/refactoringminer/test/TestJavadocDiff.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.refactoringminer.api.Refactoring;
import org.refactoringminer.rm1.GitHistoryRefactoringMinerImpl;
import org.refactoringminer.rm1.GitHistoryRefactoringMinerImpl.ChangedFileInfo;

Expand All @@ -22,6 +23,8 @@
import gr.uom.java.xmi.UMLDocElement;
import gr.uom.java.xmi.UMLModel;
import gr.uom.java.xmi.decomposition.UMLOperationBodyMapper;
import gr.uom.java.xmi.diff.ExtractOperationRefactoring;
import gr.uom.java.xmi.diff.InlineOperationRefactoring;
import gr.uom.java.xmi.diff.UMLClassDiff;

public class TestJavadocDiff {
Expand Down Expand Up @@ -113,6 +116,47 @@ public void testMethodCommentMappings(String url, String commitId, String classN
Assertions.assertTrue(expected.size() == actual.size() && expected.containsAll(actual) && actual.containsAll(expected));
}

@ParameterizedTest
@CsvSource({
"https://github.com/jOOQ/jOOQ.git, 58a4e74d28073e7c6f15d1f225ac1c2fd9aa4357, org.jooq.tools.Convert.ConvertAll, jOOQ-58a4e74d28073e7c6f15d1f225ac1c2fd9aa4357-comments.txt",
"https://github.com/thymeleaf/thymeleaf.git, 378ba37750a9cb1b19a6db434dfa59308f721ea6, org.thymeleaf.templateparser.reader.BlockAwareReader, thymeleaf-378ba37750a9cb1b19a6db434dfa59308f721ea6-comments.txt",
"https://github.com/eclipse-vertx/vert.x.git, 32a8c9086040fd6d6fa11a214570ee4f75a4301f, io.vertx.core.http.impl.HttpServerImpl.ServerHandler, vertx-32a8c9086040fd6d6fa11a214570ee4f75a4301f-comments.txt"
})
public void testMethodCommentMultiMappings(String url, String commitId, String className, String testResultFileName) throws Exception {
final List<String> actual = new ArrayList<>();
UMLClassDiff classDiff = generateClassDiff(url, commitId, new File(REPOS), className);
classDiff.process();
List<Refactoring> refactorings = classDiff.getRefactorings();
List<UMLOperationBodyMapper> parentMappers = new ArrayList<>();
for (Refactoring ref : refactorings) {
if(ref instanceof ExtractOperationRefactoring) {
ExtractOperationRefactoring ex = (ExtractOperationRefactoring)ref;
UMLOperationBodyMapper bodyMapper = ex.getBodyMapper();
if(!bodyMapper.isNested()) {
if(!parentMappers.contains(bodyMapper.getParentMapper())) {
parentMappers.add(bodyMapper.getParentMapper());
}
}
commentInfo(bodyMapper, actual);
}
else if(ref instanceof InlineOperationRefactoring) {
InlineOperationRefactoring ex = (InlineOperationRefactoring)ref;
UMLOperationBodyMapper bodyMapper = ex.getBodyMapper();
if(!bodyMapper.isNested()) {
if(!parentMappers.contains(bodyMapper.getParentMapper())) {
parentMappers.add(bodyMapper.getParentMapper());
}
}
commentInfo(bodyMapper, actual);
}
}
for(UMLOperationBodyMapper parentMapper : parentMappers) {
commentInfo(parentMapper, actual);
}
List<String> expected = IOUtils.readLines(new FileReader(EXPECTED_PATH + testResultFileName));
Assertions.assertTrue(expected.size() == actual.size() && expected.containsAll(actual) && actual.containsAll(expected));
}

private UMLClassDiff generateClassDiff(String cloneURL, String commitId, File rootFolder, String className) throws Exception {
Set<String> repositoryDirectoriesBefore = ConcurrentHashMap.newKeySet();
Set<String> repositoryDirectoriesCurrent = ConcurrentHashMap.newKeySet();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
public from(from Object) : U -> private millis(temporal Temporal) : long
line range:573-573==line range:1057-1057
line range:584-584==line range:1068-1068
line range:589-589==line range:1073-1073
public from(from Object) : U -> private millis(temporal Temporal) : long
line range:666-666==line range:1057-1057
line range:677-677==line range:1068-1068
line range:682-682==line range:1073-1073
public from(from Object) : U -> public from(from Object) : U
line range:449-449==line range:450-450
line range:450-450==line range:451-451
line range:475-475==line range:476-476
line range:476-476==line range:477-477
line range:943-943==line range:918-918
line range:944-944==line range:919-919
line range:964-964==line range:939-939
line range:965-965==line range:940-940
line range:453-453==line range:454-454
line range:458-458==line range:459-459
line range:470-470==line range:471-471
line range:481-481==line range:482-482
line range:484-484==line range:485-485
line range:489-489==line range:490-490
line range:498-498==line range:499-499
line range:507-507==line range:508-508
line range:569-569==line range:570-570
line range:594-594==line range:574-574
line range:604-604==line range:584-584
line range:662-662==line range:642-642
line range:687-687==line range:646-646
line range:697-697==line range:656-656
line range:779-779==line range:738-738
line range:789-789==line range:748-748
line range:799-799==line range:758-758
line range:809-809==line range:768-768
line range:814-814==line range:773-773
line range:818-818==line range:777-777
line range:820-820==line range:779-779
line range:825-825==line range:784-784
line range:829-829==line range:788-788
line range:831-831==line range:790-790
line range:841-841==line range:800-800
line range:851-851==line range:810-810
line range:861-861==line range:820-820
line range:864-864==line range:823-823
line range:880-880==line range:839-839
line range:896-896==line range:855-855
line range:912-912==line range:871-871
line range:928-928==line range:887-887
line range:941-941==line range:916-916
line range:954-954==line range:929-929
line range:971-1008==line range:946-983
line range:1010-1010==line range:985-985
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
public read(cbuf char[], off int, len int) : int -> private matchOverflow(structure char[]) : boolean
line range:165-165==line range:271-271
line range:180-180==line range:271-271
public read(cbuf char[], off int, len int) : int -> private matchOverflow(structure char[]) : boolean
line range:165-165==line range:271-271
line range:180-180==line range:271-271
public read(cbuf char[], off int, len int) : int -> public read(cbuf char[], off int, len int) : int
line range:127-127==line range:127-127
line range:128-128==line range:128-128
line range:159-159==line range:159-159
line range:160-160==line range:160-160
line range:194-194==line range:172-172
line range:195-195==line range:173-173
line range:196-196==line range:174-174
line range:197-197==line range:175-175
line range:73-73==line range:73-73
line range:80-80==line range:80-80
line range:85-85==line range:85-85
line range:94-94==line range:94-94
line range:117-117==line range:117-117
line range:152-152==line range:152-152
line range:154-154==line range:154-154
line range:168-168==line range:163-163
line range:171-171==line range:167-167
line range:183-183==line range:163-163
line range:186-186==line range:167-167
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
private handleHttp(conn ServerConnection, ch Channel, msg Object) : void -> protected doMessageReceived(conn ServerConnection, ctx ChannelHandlerContext, msg Object) : void
line range:539-539==line range:525-525
private handleExpectWebsockets(conn ServerConnection, ctx ChannelHandlerContext, msg Object) : void -> protected doMessageReceived(conn ServerConnection, ctx ChannelHandlerContext, msg Object) : void
line range:511-511==line range:549-549
line range:512-512==line range:550-550
line range:495-495==line range:533-533
line range:506-506==line range:544-544
private handleHttp(conn ServerConnection, ch Channel, msg Object) : void -> protected doMessageReceived(conn ServerConnection, ctx ChannelHandlerContext, msg Object) : void
line range:539-539==line range:525-525
line range:539-539==line range:575-575
protected doMessageReceived(conn ServerConnection, ctx ChannelHandlerContext, msg Object) : void -> protected doMessageReceived(conn ServerConnection, ctx ChannelHandlerContext, msg Object) : void
28 changes: 28 additions & 0 deletions src/test/resources/oracle/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -3242,6 +3242,13 @@
"validation": "TP",
"detectionTools": "RefactoringMiner",
"validators": null
}, {
"type": "Inline Variable",
"description": "Inline Variable handlers : Map<String,LoggingHandlerImpl> in method public getChildren() : Collection<? extends AbstractTreeNode> from class com.intellij.remoteServer.impl.runtime.ui.tree.ServersTreeStructure.DeploymentNodeImpl",
"comment": null,
"validation": "TP",
"detectionTools": "RefactoringMiner",
"validators": null
}],
"refDiffExecutionTime": 14854
}, {
Expand Down Expand Up @@ -90732,6 +90739,13 @@
"validation": "TP",
"detectionTools": "RefactoringMiner",
"validators": null
}, {
"type": "Extract Variable",
"description": "Extract Variable entry : ConnectorMetadataEntry in method public beginInsert(session Session, tableHandle TableHandle) : InsertTableHandle from class com.facebook.presto.metadata.MetadataManager",
"comment": null,
"validation": "TP",
"detectionTools": "RefactoringMiner",
"validators": null
}],
"refDiffExecutionTime": 9866
}, {
Expand Down Expand Up @@ -101590,6 +101604,13 @@
"validation": "TP",
"detectionTools": "RefactoringMiner",
"validators": null
}, {
"type": "Inline Variable",
"description": "Inline Variable temporal : Temporal in method public from(from Object) : U from class org.jooq.tools.Convert.ConvertAll",
"comment": null,
"validation": "TP",
"detectionTools": "RefactoringMiner",
"validators": null
}],
"refDiffExecutionTime": 1574
}, {
Expand Down Expand Up @@ -103683,6 +103704,13 @@
"validation": "TP",
"detectionTools": "RefactoringMiner",
"validators": null
}, {
"type": "Inline Variable",
"description": "Inline Variable values : List<Object> in method public getParameters() : List<Object> from class org.jbpm.query.jpa.data.QueryCriteria",
"comment": null,
"validation": "TP",
"detectionTools": "RefactoringMiner",
"validators": null
}],
"refDiffExecutionTime": 3454
}, {
Expand Down
8 changes: 4 additions & 4 deletions src/test/resources/oracle/expected.txt
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ ecd0ad5ab99b8d14f28881cf4f49ec01f2221776, 6, 0, 0
cfea606b129dbfc5703eb279d4803185afc99c58, 13, 0, 3
83cfa21578e63956bca0715eefee2860c3b6d39a, 9, 0, 0
a739d16d301f0e89ab0b9dfa56b4585bbad6b793, 15, 0, 0
58a4e74d28073e7c6f15d1f225ac1c2fd9aa4357, 2, 0, 0
58a4e74d28073e7c6f15d1f225ac1c2fd9aa4357, 3, 0, 0
f01d8610b9ceebc1de59d42f569b8af3efbe0a0f, 2, 0, 0
72f61ec9b85a740fd09d10ad711e275d2ec2e564, 3, 0, 0
227254cf769f3e821ed1b2ef2d88c4ec6b20adea, 9, 0, 0
Expand Down Expand Up @@ -314,7 +314,7 @@ d5f10a4958f5e870680be906689d92d1efb42480, 6, 0, 0
2bd1557bc293cb8c2348374771aad832befbe26f, 2, 0, 0
72b5348307d86b1a118e546c24d97f1ac1895bdb, 73, 0, 1
7ccbd4693288dbdbc2e6844aa0877640d63fbd04, 12, 0, 0
11048642b1e6b0e35efefab9e4e693b09c8753f5, 265, 0, 0
11048642b1e6b0e35efefab9e4e693b09c8753f5, 266, 0, 0
0b6ea757e34a63b1421b77ed5fbb61398377aab1, 6, 0, 0
2b89553db5081fe4e55b7b34d636d0ea2acf71c5, 6, 0, 0
b25d3f32ed2e2da86f5c746098686445c2e2a314, 6, 1, 0
Expand Down Expand Up @@ -360,7 +360,7 @@ d69e5cb21c04d9eede314aaa9ad059fc603fb025, 4, 0, 1
ea96643364e91125f560e9508a5cbcdb776bde64, 28, 0, 0
ba5e6d44526a2491a7004423ca2ad780c6992c46, 3, 0, 0
b9a17665b158955ad28ef7f50cc0a8585460f053, 6, 0, 0
3815f293ba9338f423315d93a373608c95002b15, 134, 0, 3
3815f293ba9338f423315d93a373608c95002b15, 135, 0, 3
b8929ccb4057c74ac64679216487a4abcd3ae1c3, 4, 0, 0
3d5e343df6a39ce3b41624b90974d83e9899541e, 2, 0, 0
0b93231025f51c7ec62fd8588985c5dc807854e4, 1, 0, 0
Expand Down Expand Up @@ -451,7 +451,7 @@ e1f0dbc2f09541fc64ce88ee22d8f8f4648004fe, 2, 0, 0
484038e916dc40bf87eca10c77889d79eca96c4d, 2, 0, 0
97811cf971f7ccf6a5fc5e90a491db2f58d49da1, 9, 0, 0
e1625136ba12907696ef4c6e922ce073293f3a2c, 3, 0, 0
219d6ddfd1db62c11efb57e0216436874e087834, 25, 0, 0
219d6ddfd1db62c11efb57e0216436874e087834, 26, 0, 0
7655200f58293e5a30bf8b3cbb29ebadae374564, 16, 0, 0
8d7a26edd1fedb9505b4f2b4fe57b2d2958b4dd9, 5, 0, 0
cc0eaf7faa408a04b68e2b5820f3ebcc75420b5b, 5, 0, 0
Expand Down

0 comments on commit b852322

Please sign in to comment.