Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Incomplete] Use RMiner to identify similar nodes on renaming #79

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added dependencies/RefactoringMiner.jar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,16 @@
import br.ufpe.cin.mergers.util.RenamingUtils;
import br.ufpe.cin.mergers.util.Side;
import de.ovgu.cide.fstgen.ast.FSTNode;
import gr.uom.java.xmi.UMLModel;
import gr.uom.java.xmi.UMLModelASTReader;
import gr.uom.java.xmi.diff.UMLModelDiff;
import org.apache.commons.lang3.tuple.Pair;
import org.refactoringminer.api.Refactoring;
import org.refactoringminer.api.RefactoringType;

import java.io.File;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
Expand Down Expand Up @@ -55,13 +63,15 @@ private void handleSingleRenamings(MergeContext context) {

private void handleSingleRenamings(MergeContext context, List<Pair<String, FSTNode>> possibleRenamedNodes,
List<FSTNode> addedNodes, Side renamingSide) {
List<Refactoring> refactorings;
if (renamingSide.equals(Side.LEFT)) refactorings = RenamingUtils.getRefactorings(context.getBase(), context.getLeft());
else refactorings = RenamingUtils.getRefactorings(context.getBase(), context.getRight());
for (Pair<String, FSTNode> tuple : possibleRenamedNodes) {
String baseContent = tuple.getLeft();
FSTNode currentNode = tuple.getRight();
if (!RenamingUtils.nodeHasConflict(currentNode)) continue;

List<Pair<Double, String>> similarNodes = RenamingUtils.getSimilarNodes(baseContent, currentNode, addedNodes,
BODY_SIMILARITY_THRESHOLD);
List<Pair<Double, String>> similarNodes = RenamingUtils.getSimilarNodesRMiner(baseContent, currentNode, addedNodes, refactorings);

singleRenamingHandler.handle(context, baseContent, currentNode, similarNodes, renamingSide);
}
Expand Down
95 changes: 91 additions & 4 deletions src/main/java/br/ufpe/cin/mergers/util/RenamingUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,55 @@
import br.ufpe.cin.files.FilesManager;
import de.ovgu.cide.fstgen.ast.FSTNode;
import de.ovgu.cide.fstgen.ast.FSTTerminal;
import gr.uom.java.xmi.UMLModel;
import gr.uom.java.xmi.UMLModelASTReader;
import gr.uom.java.xmi.diff.UMLModelDiff;
import org.apache.commons.lang3.tuple.Pair;
import org.refactoringminer.api.Refactoring;
import org.refactoringminer.api.RefactoringType;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.io.File;
import java.util.*;
import java.util.stream.Collectors;

public class RenamingUtils {
private static ArrayList<String> getFileNames(File file, boolean firstLevel){
ArrayList<String> ret = new ArrayList<>();
if (file != null) {
if (file.isDirectory()){
File[] files = file.listFiles();
for (File childFile: files) {
ArrayList<String> aux = getFileNames(childFile, false);
for(String str: aux){
if (firstLevel)
ret.add("/" + str);
else
ret.add(file.getName() + "/" + str);
}
}
} else {
ret.add(file.getName());
}
}
return ret;

}

public static List<Refactoring> getRefactorings(File base, File after){
if(base.isFile()) base = base.getParentFile();
ArrayList<String> baseFiles = getFileNames(base, true);

if(after.isFile()) after = after.getParentFile();
ArrayList<String> afterFiles = getFileNames(after, true);


UMLModel model1 = new UMLModelASTReader(base, baseFiles).getUmlModel();
UMLModel model2 = new UMLModelASTReader(after, afterFiles).getUmlModel();
UMLModelDiff modelDiff = model1.diff(model2);
List<Refactoring> refactorings = modelDiff.getRefactorings();
return refactorings;
}

public static boolean hasUnstructuredMergeConflict(MergeContext context, String baseContent) {
String signature = getTrimmedSignature(baseContent);

Expand All @@ -20,6 +60,53 @@ public static boolean hasUnstructuredMergeConflict(MergeContext context, String
.anyMatch(conflict -> conflict.contains(signature));
}

private static String convertSignatureFromRMiner(String signature) {
String signatureType = signature.split(":")[1].trim();
signature = signature.split(":")[0].trim();
String[] signatureAsArray = signature.split(" ", 2);
signature = signatureAsArray[0] + " " + signatureType + " " + signatureAsArray[1];
return signature;
}

public static List<Pair<Double, String>> getSimilarNodesRMiner(String baseContent, FSTNode currentNode,
List<FSTNode> addedNodes, List<Refactoring> refactorings) {
List<Pair<Double, String>> similarNodes = new ArrayList<>();

String oldSignature = getSignature(baseContent).trim();
String newSignature = "";
HashMap<String, String> renames;
for (Refactoring refactoring : refactorings) {
if (refactoring.getRefactoringType() == RefactoringType.RENAME_METHOD) {
String renameSignature = refactoring.toString().split("renamed to")[0]
.split("Rename Method")[1]
.split("\t")[1];
renameSignature = convertSignatureFromRMiner(renameSignature);
if(renameSignature.equals(oldSignature)) {
newSignature = refactoring.toString().split("renamed to")[1].split("in class")[0].trim();
newSignature = convertSignatureFromRMiner(newSignature);
System.out.println(newSignature);
break;
}
}
}


//1. getting similar nodes to fulfill renaming conflicts
for (FSTNode newNode : addedNodes) { // a possible renamed node is seem as "new" node due to superimposition
if (!isMethodOrConstructorNode(newNode)) continue;
if (!haveSameParent(newNode, currentNode)) continue;

String possibleRenamingContent = ((FSTTerminal) newNode).getBody();
if (getSignature(possibleRenamingContent).trim().equals(newSignature)) {
System.out.println(newSignature + "!!!!!!!!");
Pair<Double, String> tp = Pair.of(1.0, possibleRenamingContent);
similarNodes.add(tp);
}
}

return similarNodes;
}

public static List<Pair<Double, String>> getSimilarNodes(String baseContent, FSTNode currentNode,
List<FSTNode> addedNodes, double similarityThreshold) {
//list of possible nodes renaming a previous one
Expand Down