Skip to content

Commit

Permalink
Support matching modified comment groups
Browse files Browse the repository at this point in the history
  • Loading branch information
tsantalis committed Oct 2, 2024
1 parent 244eade commit f1137b8
Show file tree
Hide file tree
Showing 8 changed files with 3,099 additions and 2 deletions.
13 changes: 12 additions & 1 deletion src/main/java/gr/uom/java/xmi/UMLCommentGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import gr.uom.java.xmi.diff.UMLCommentListDiff;

public class UMLCommentGroup {
private List<UMLComment> group;
Expand Down Expand Up @@ -35,4 +36,14 @@ public boolean sameText(UMLCommentGroup other) {
}
return false;
}

public boolean modifiedMatchingText(UMLCommentGroup other) {
if(this.group.size() == other.group.size() && this.group.size() > 1) {
UMLCommentListDiff diff = new UMLCommentListDiff(this, other);
if(diff.getCommonComments().size() == this.group.size()) {
return true;
}
}
return false;
}
}
31 changes: 30 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 @@ -28,9 +28,30 @@ public UMLCommentListDiff(List<UMLComment> commentsBefore, List<UMLComment> comm
//check if there exist comment groups, consecutive line comments
List<UMLCommentGroup> groupsBefore = createCommentGroups(commentsBefore);
List<UMLCommentGroup> groupsAfter = createCommentGroups(commentsAfter);
List<UMLCommentGroup> groupsBeforeToBeRemoved = new ArrayList<UMLCommentGroup>();
List<UMLCommentGroup> groupsAfterToBeRemoved = new ArrayList<UMLCommentGroup>();
for(UMLCommentGroup groupBefore : groupsBefore) {
for(UMLCommentGroup groupAfter : groupsAfter) {
if(groupBefore.sameText(groupAfter)) {
for(int i=0; i<groupBefore.getGroup().size(); i++) {
UMLComment commentBefore = groupBefore.getGroup().get(i);
UMLComment commentAfter = groupAfter.getGroup().get(i);
Pair<UMLComment, UMLComment> pair = Pair.of(commentBefore, commentAfter);
commonComments.add(pair);
deletedComments.remove(commentBefore);
addedComments.remove(commentAfter);
}
groupsBeforeToBeRemoved.add(groupBefore);
groupsAfterToBeRemoved.add(groupAfter);
break;
}
}
}
groupsBefore.removeAll(groupsBeforeToBeRemoved);
groupsAfter.removeAll(groupsAfterToBeRemoved);
for(UMLCommentGroup groupBefore : groupsBefore) {
for(UMLCommentGroup groupAfter : groupsAfter) {
if(groupBefore.modifiedMatchingText(groupAfter)) {
for(int i=0; i<groupBefore.getGroup().size(); i++) {
UMLComment commentBefore = groupBefore.getGroup().get(i);
UMLComment commentAfter = groupAfter.getGroup().get(i);
Expand All @@ -46,6 +67,13 @@ public UMLCommentListDiff(List<UMLComment> commentsBefore, List<UMLComment> comm
processRemainingComments(deletedComments, addedComments);
}

public UMLCommentListDiff(UMLCommentGroup groupBefore, UMLCommentGroup groupAfter) {
this.commonComments = new ArrayList<Pair<UMLComment,UMLComment>>();
this.deletedComments = new ArrayList<UMLComment>();
this.addedComments = new ArrayList<UMLComment>();
processRemainingComments(groupBefore.getGroup(), groupAfter.getGroup());
}

private void processRemainingComments(List<UMLComment> commentsBefore, List<UMLComment> commentsAfter) {
List<UMLComment> deletedComments = new ArrayList<UMLComment>(commentsBefore);
List<UMLComment> addedComments = new ArrayList<UMLComment>(commentsAfter);
Expand Down Expand Up @@ -107,7 +135,8 @@ private List<UMLCommentGroup> createCommentGroups(List<UMLComment> commentsBefor
}
}
}
groups.add(currentGroup);
if(!currentGroup.getGroup().isEmpty())
groups.add(currentGroup);
return groups;
}

Expand Down
26 changes: 26 additions & 0 deletions src/test/java/org/refactoringminer/test/TestJavadocDiff.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,24 @@ public void testClassCommentMappings(String url, String commitId, String contain
Assertions.assertTrue(expected.size() == actual.size() && expected.containsAll(actual) && actual.containsAll(expected));
}

@ParameterizedTest
@CsvSource({
"https://github.com/eclipse-jgit/jgit.git, 1b783d037091266b035e1727db6b6ce7a397ef63, org.eclipse.jgit.storage.pack.PackWriter, searchForDeltas, jgit-1b783d037091266b035e1727db6b6ce7a397ef63.txt"
})
public void testMethodCommentMappings(String url, String commitId, String className, String containerName, String testResultFileName) throws Exception {
final List<String> actual = new ArrayList<>();
UMLClassDiff classDiff = generateClassDiff(url, commitId, new File(REPOS), className);
classDiff.process();
for(UMLOperationBodyMapper mapper : classDiff.getOperationBodyMapperList()) {
if(mapper.getContainer1().getName().equals(containerName) && mapper.getContainer2().getName().equals(containerName)) {
commentInfo(mapper, actual);
//break;
}
}
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 Expand Up @@ -150,6 +168,14 @@ private void javadocInfo(UMLOperationBodyMapper bodyMapper, final List<String> a
}
}

private void commentInfo(UMLOperationBodyMapper bodyMapper, final List<String> actual) {
actual.add(bodyMapper.toString());
for(Pair<UMLComment, UMLComment> mapping : bodyMapper.getCommentListDiff().getCommonComments()) {
String line = mapping.getLeft().getLocationInfo() + "==" + mapping.getRight().getLocationInfo();
actual.add(line);
}
}

private void javadocInfo(UMLClassDiff classDiff, final List<String> actual) {
if(classDiff.getJavadocDiff().isPresent()) {
actual.add(classDiff.getNextClassName());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
private searchForDeltas(monitor ProgressMonitor) : void -> private searchForDeltas(monitor ProgressMonitor) : void
line range:882-882==line range:908-908
line range:883-883==line range:909-909
line range:884-884==line range:910-910
line range:885-885==line range:911-911
line range:896-896==line range:922-922
line range:897-897==line range:923-923
line range:898-898==line range:924-924
line range:899-899==line range:925-925
line range:905-905==line range:931-931
line range:906-906==line range:932-932
line range:907-907==line range:933-933
line range:908-908==line range:934-934
line range:916-916==line range:942-942
line range:917-917==line range:943-943
line range:918-918==line range:944-944
line range:919-919==line range:945-945
line range:920-920==line range:946-946
private searchForDeltas(monitor ProgressMonitor, list ObjectToPack[], cnt int) : void -> private searchForDeltas(monitor ProgressMonitor, list ObjectToPack[], cnt int) : void
line range:989-989==line range:1012-1012
line range:990-990==line range:1013-1013
line range:991-991==line range:1014-1014
line range:992-992==line range:1015-1015
line range:993-993==line range:1016-1016
line range:1003-1003==line range:1027-1027
line range:1004-1004==line range:1028-1028
line range:1005-1005==line range:1029-1029
line range:1008-1008==line range:1032-1032
line range:1009-1009==line range:1033-1033
line range:1054-1054==line range:1105-1105
line range:1055-1055==line range:1106-1106
line range:1056-1056==line range:1107-1107
line range:1042-1042==line range:1053-1053
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"parentCommitId":"21f76c2a69836ec618c82eb9305656dcec70befb","currentCommitId":"1b783d037091266b035e1727db6b6ce7a397ef63","filesBefore":["org.eclipse.jgit/src/org/eclipse/jgit/storage/pack/PackWriter.java"],"filesCurrent":["org.eclipse.jgit/src/org/eclipse/jgit/storage/pack/DeltaTask.java","org.eclipse.jgit/src/org/eclipse/jgit/storage/pack/PackWriter.java"],"renamedFilesHint":{},"repositoryDirectoriesBefore":["org.eclipse.jgit/src/org/eclipse/jgit","org.eclipse.jgit/src/org/eclipse/jgit/storage","org.eclipse.jgit/src","org.eclipse.jgit/src/org","org.eclipse.jgit/src/org/eclipse/jgit/storage/pack","org.eclipse.jgit/src/org/eclipse","org.eclipse.jgit"],"repositoryDirectoriesCurrent":["org.eclipse.jgit/src/org/eclipse/jgit","org.eclipse.jgit/src/org/eclipse/jgit/storage","org.eclipse.jgit/src","org.eclipse.jgit/src/org","org.eclipse.jgit/src/org/eclipse/jgit/storage/pack","org.eclipse.jgit/src/org/eclipse","org.eclipse.jgit"],"commitTime":0,"authoredTime":0,"commitAuthorName":null}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright (C) 2010, Google Inc.
* and other copyright owners as documented in the project's IP log.
*
* This program and the accompanying materials are made available
* under the terms of the Eclipse Distribution License v1.0 which
* accompanies this distribution, is reproduced below, and is
* available at http://www.eclipse.org/org/documents/edl-v10.php
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* - Neither the name of the Eclipse Foundation, Inc. nor the
* names of its contributors may be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package org.eclipse.jgit.storage.pack;

import java.util.concurrent.Callable;

import org.eclipse.jgit.lib.ObjectReader;
import org.eclipse.jgit.lib.ProgressMonitor;

final class DeltaTask implements Callable<Object> {
private final PackWriter writer;

private final ObjectReader templateReader;

private final DeltaCache dc;

private final ProgressMonitor pm;

private final int batchSize;

private final int start;

private final ObjectToPack[] list;

DeltaTask(PackWriter writer, ObjectReader reader, DeltaCache dc,
ProgressMonitor pm, int batchSize, int start, ObjectToPack[] list) {
this.writer = writer;
this.templateReader = reader;
this.dc = dc;
this.pm = pm;
this.batchSize = batchSize;
this.start = start;
this.list = list;
}

public Object call() throws Exception {
final ObjectReader or = templateReader.newReader();
try {
DeltaWindow dw;
dw = new DeltaWindow(writer, dc, or);
dw.search(pm, list, start, batchSize);
} finally {
or.release();
}
return null;
}
}
Loading

0 comments on commit f1137b8

Please sign in to comment.