Skip to content

Commit

Permalink
Update to use new RMiner APIs accepting source folder
Browse files Browse the repository at this point in the history
  • Loading branch information
tsantalis committed Nov 27, 2024
1 parent b0cc486 commit 35211b9
Show file tree
Hide file tree
Showing 15 changed files with 132 additions and 134 deletions.
187 changes: 93 additions & 94 deletions src/main/java/org/codetracker/AbstractTracker.java
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,7 @@ protected static boolean isNewlyAddedFile(CommitModel commitModel, String curren

protected static Set<String> getRightSideFileNames(Method currentMethod, CommitModel commitModel, UMLModelDiff umlModelDiff) {
String currentFilePath = currentMethod.getFilePath();
String currentSourceFolder = currentMethod.getLocation().getSourceFolder();
String currentClassName = currentMethod.getUmlOperation().getClassName();
Set<String> toBeAddedFileNamesIfTheyAreNewFiles = new HashSet<>();
if (currentMethod.getUmlOperation() instanceof UMLOperation) {
Expand All @@ -501,7 +502,7 @@ protected static Set<String> getRightSideFileNames(Method currentMethod, CommitM
continue;
toBeAddedFileNamesIfTheyAreNewFiles.add(parameterType + ".java");
}
Set<String> rightSideFileNames = getRightSideFileNames(currentFilePath, currentClassName, toBeAddedFileNamesIfTheyAreNewFiles, commitModel, umlModelDiff);
Set<String> rightSideFileNames = getRightSideFileNames(currentFilePath, currentSourceFolder, currentClassName, toBeAddedFileNamesIfTheyAreNewFiles, commitModel, umlModelDiff);
//add all right side files having a main method
if (currentMethod.getUmlOperation().isMain()) {
for (String filePath : commitModel.fileContentsCurrentOriginal.keySet()) {
Expand All @@ -514,96 +515,94 @@ protected static Set<String> getRightSideFileNames(Method currentMethod, CommitM
return rightSideFileNames;
}

protected static Set<String> getRightSideFileNames(String currentFilePath, String currentClassName, Set<String> toBeAddedFileNamesIfTheyAreNewFiles, CommitModel commitModel, UMLModelDiff umlModelDiff) {
protected static Set<String> getRightSideFileNames(String currentFilePath, String sourceFolder, String currentClassName, Set<String> toBeAddedFileNamesIfTheyAreNewFiles, CommitModel commitModel, UMLModelDiff umlModelDiff) {
Set<String> fileNames = new HashSet<>();
fileNames.add(currentFilePath);
Set<UMLAbstractClass> classesInChildModel = umlModelDiff.findClassesInChildModel(currentClassName);
UMLAbstractClass classInChildModel = umlModelDiff.findClassInChildModel(sourceFolder, currentClassName);
boolean newlyAddedFile = isNewlyAddedFile(commitModel, currentFilePath);
for (UMLAbstractClass classInChildModel : classesInChildModel) {
if (classInChildModel instanceof UMLClass) {
UMLClass umlClass = (UMLClass) classInChildModel;

StringBuilder regxSb = new StringBuilder();

String orChar = "";
if (umlClass.getSuperclass() != null) {
regxSb.append(orChar).append("\\s*extends\\s*").append(umlClass.getSuperclass().getClassType());
orChar = "|";
if (newlyAddedFile) {
regxSb.append(orChar).append("\\s*class\\s*").append(umlClass.getSuperclass().getClassType()).append("\\s\\s*");
}
}

for (UMLType implementedInterface : umlClass.getImplementedInterfaces()) {
regxSb.append(orChar).append("\\s*implements\\s*.*").append(implementedInterface).append("\\s*");
orChar = "|";
if (newlyAddedFile) {
regxSb.append(orChar).append("\\s*interface\\s*").append(implementedInterface.getClassType()).append("\\s*\\{");
}
}

//newly added file
if (newlyAddedFile) {
regxSb.append(orChar).append("@link\\s*").append(umlClass.getNonQualifiedName());
orChar = "|";
regxSb.append(orChar).append("new\\s*").append(umlClass.getNonQualifiedName()).append("\\(");
regxSb.append(orChar).append("@deprecated\\s*.*").append(umlClass.getNonQualifiedName()).append("\\s*.*\n");
regxSb.append(orChar).append("\\s*extends\\s*").append(umlClass.getNonQualifiedName()).append("\\s*\\{");
}

String regx = regxSb.toString();
if (!regx.isEmpty()) {
Pattern pattern = Pattern.compile(regx);
for (Map.Entry<String, String> entry : commitModel.fileContentsCurrentTrimmed.entrySet()) {
Matcher matcher = pattern.matcher(entry.getValue());
if (matcher.find()) {
String matcherGroup = matcher.group().trim();
String filePath = entry.getKey();
boolean isAnExistingFile = commitModel.fileContentsBeforeTrimmed.containsKey(filePath) || commitModel.renamedFilesHint.values().stream().anyMatch(s -> s.equals(filePath));
if (matcherGroup.startsWith("extends") && matcherGroup.contains(umlClass.getNonQualifiedName())) {
if (isAnExistingFile) {
fileNames.add(filePath);
}
} else if (matcherGroup.startsWith("implements") || matcherGroup.startsWith("extends")) {
if (isAnExistingFile) {
String[] split = matcherGroup.split("\\s");
String className = split[split.length - 1];
if (className.contains(".")) {
className = className.substring(0, className.indexOf("."));
}
String[] tokens = CAMEL_CASE_SPLIT_PATTERN.split(className);
final String fileName = className + ".java";
if (commitModel.fileContentsCurrentTrimmed.keySet().stream().anyMatch(s -> s.endsWith(fileName) || s.endsWith(tokens[tokens.length - 1] + ".java"))) {
fileNames.add(filePath);
}
}
} else if (matcherGroup.startsWith("new")) {
if (isAnExistingFile) {
fileNames.add(filePath);
}
} else if (matcherGroup.startsWith("@link")) {
fileNames.add(filePath); //TODO: add existing file condition and test
} else if (matcherGroup.startsWith("class")) {
if (isAnExistingFile) {
fileNames.add(filePath);
}
} else if (matcherGroup.startsWith("@deprecated")) {
if (isAnExistingFile) {
fileNames.add(filePath);
}
} else if (matcherGroup.startsWith("interface")) {
if (isAnExistingFile) {
fileNames.add(filePath);
}
}

}
}
}
if (!umlClass.isTopLevel()) {
fileNames.addAll(getRightSideFileNames(currentFilePath, umlClass.getPackageName(), toBeAddedFileNamesIfTheyAreNewFiles, commitModel, umlModelDiff));
}
}
if (classInChildModel instanceof UMLClass) {
UMLClass umlClass = (UMLClass) classInChildModel;

StringBuilder regxSb = new StringBuilder();

String orChar = "";
if (umlClass.getSuperclass() != null) {
regxSb.append(orChar).append("\\s*extends\\s*").append(umlClass.getSuperclass().getClassType());
orChar = "|";
if (newlyAddedFile) {
regxSb.append(orChar).append("\\s*class\\s*").append(umlClass.getSuperclass().getClassType()).append("\\s\\s*");
}
}

for (UMLType implementedInterface : umlClass.getImplementedInterfaces()) {
regxSb.append(orChar).append("\\s*implements\\s*.*").append(implementedInterface).append("\\s*");
orChar = "|";
if (newlyAddedFile) {
regxSb.append(orChar).append("\\s*interface\\s*").append(implementedInterface.getClassType()).append("\\s*\\{");
}
}

//newly added file
if (newlyAddedFile) {
regxSb.append(orChar).append("@link\\s*").append(umlClass.getNonQualifiedName());
orChar = "|";
regxSb.append(orChar).append("new\\s*").append(umlClass.getNonQualifiedName()).append("\\(");
regxSb.append(orChar).append("@deprecated\\s*.*").append(umlClass.getNonQualifiedName()).append("\\s*.*\n");
regxSb.append(orChar).append("\\s*extends\\s*").append(umlClass.getNonQualifiedName()).append("\\s*\\{");
}

String regx = regxSb.toString();
if (!regx.isEmpty()) {
Pattern pattern = Pattern.compile(regx);
for (Map.Entry<String, String> entry : commitModel.fileContentsCurrentTrimmed.entrySet()) {
Matcher matcher = pattern.matcher(entry.getValue());
if (matcher.find()) {
String matcherGroup = matcher.group().trim();
String filePath = entry.getKey();
boolean isAnExistingFile = commitModel.fileContentsBeforeTrimmed.containsKey(filePath) || commitModel.renamedFilesHint.values().stream().anyMatch(s -> s.equals(filePath));
if (matcherGroup.startsWith("extends") && matcherGroup.contains(umlClass.getNonQualifiedName())) {
if (isAnExistingFile) {
fileNames.add(filePath);
}
} else if (matcherGroup.startsWith("implements") || matcherGroup.startsWith("extends")) {
if (isAnExistingFile) {
String[] split = matcherGroup.split("\\s");
String className = split[split.length - 1];
if (className.contains(".")) {
className = className.substring(0, className.indexOf("."));
}
String[] tokens = CAMEL_CASE_SPLIT_PATTERN.split(className);
final String fileName = className + ".java";
if (commitModel.fileContentsCurrentTrimmed.keySet().stream().anyMatch(s -> s.endsWith(fileName) || s.endsWith(tokens[tokens.length - 1] + ".java"))) {
fileNames.add(filePath);
}
}
} else if (matcherGroup.startsWith("new")) {
if (isAnExistingFile) {
fileNames.add(filePath);
}
} else if (matcherGroup.startsWith("@link")) {
fileNames.add(filePath); //TODO: add existing file condition and test
} else if (matcherGroup.startsWith("class")) {
if (isAnExistingFile) {
fileNames.add(filePath);
}
} else if (matcherGroup.startsWith("@deprecated")) {
if (isAnExistingFile) {
fileNames.add(filePath);
}
} else if (matcherGroup.startsWith("interface")) {
if (isAnExistingFile) {
fileNames.add(filePath);
}
}

}
}
}
if (!umlClass.isTopLevel()) {
fileNames.addAll(getRightSideFileNames(currentFilePath, sourceFolder, umlClass.getPackageName(), toBeAddedFileNamesIfTheyAreNewFiles, commitModel, umlModelDiff));
}
}


Expand Down Expand Up @@ -632,7 +631,7 @@ protected static boolean isClassAdded(UMLModelDiff modelDiff, String className)
return false;
}

protected static boolean isAttributeAdded(UMLModelDiff modelDiff, String className, Predicate<Attribute> equalOperator, Version currentVersion) {
protected static boolean isAttributeAdded(UMLModelDiff modelDiff, String sourceFolder, String className, Predicate<Attribute> equalOperator, Version currentVersion) {
List<UMLAttribute> addedAttributes = getAllClassesDiff(modelDiff)
.stream()
.map(UMLClassBaseDiff::getAddedAttributes)
Expand All @@ -652,8 +651,8 @@ protected static boolean isAttributeAdded(UMLModelDiff modelDiff, String classNa
return true;
}

Set<UMLClass> addedClasses = modelDiff.getAllAddedClasses(className);
for (UMLClass addedClass : addedClasses) {
UMLClass addedClass = modelDiff.getAddedClass(sourceFolder, className);
if (addedClass != null) {
for (UMLAttribute attribute : addedClass.getAttributes()) {
if (isAttributeAdded(attribute, equalOperator, currentVersion))
return true;
Expand Down Expand Up @@ -713,7 +712,7 @@ protected static boolean isAttributeAdded(UMLAttribute addedAttribute, Predicate
return false;
}

protected static boolean isMethodAdded(UMLModelDiff modelDiff, String className, Predicate<Method> equalOperator, Consumer<Method> addedMethodHandler, Version currentVersion) {
protected static boolean isMethodAdded(UMLModelDiff modelDiff, String sourceFolder, String className, Predicate<Method> equalOperator, Consumer<Method> addedMethodHandler, Version currentVersion) {
List<UMLOperation> addedOperations = getAllClassesDiff(modelDiff)
.stream()
.map(UMLClassBaseDiff::getAddedOperations)
Expand All @@ -724,8 +723,8 @@ protected static boolean isMethodAdded(UMLModelDiff modelDiff, String className,
return true;
}

Set<UMLClass> addedClasses = modelDiff.getAllAddedClasses(className);
for (UMLClass addedClass : addedClasses) {
UMLClass addedClass = modelDiff.getAddedClass(sourceFolder, className);
if (addedClass != null) {
for (UMLOperation operation : addedClass.getOperations()) {
if (isMethodAdded(operation, equalOperator, addedMethodHandler, currentVersion))
return true;
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/codetracker/AnnotationTrackerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ else if (leftOperation instanceof UMLInitializer && rightOperation instanceof UM
}
}

if (isMethodAdded(umlModelDiffAll, rightMethod.getUmlOperation().getClassName(), rightMethod::equalIdentifierIgnoringVersion, method -> {
if (isMethodAdded(umlModelDiffAll, rightMethod.getUmlOperation().getLocationInfo().getSourceFolder(), rightMethod.getUmlOperation().getClassName(), rightMethod::equalIdentifierIgnoringVersion, method -> {
}, currentVersion)) {
Annotation annotationBefore = Annotation.of(rightAnnotation.getAnnotation(), rightAnnotation.getOperation().get(), parentVersion);
changeHistory.get().handleAdd(annotationBefore, rightAnnotation, "added with method");
Expand Down Expand Up @@ -441,7 +441,7 @@ else if (currentAnnotation.getOperation().get() instanceof UMLAttribute) {
}
}
{
Set<String> fileNames = getRightSideFileNames(currentAttribute.getFilePath(), currentAttribute.getUmlAttribute().getClassName(), Collections.emptySet(), commitModel, umlModelDiffLocal);
Set<String> fileNames = getRightSideFileNames(currentAttribute.getFilePath(), currentAttribute.getUmlAttribute().getLocationInfo().getSourceFolder(), currentAttribute.getUmlAttribute().getClassName(), Collections.emptySet(), commitModel, umlModelDiffLocal);
Pair<UMLModel, UMLModel> umlModelPairAll = getUMLModelPair(commitModel, currentAttribute.getFilePath(), fileNames::contains, false);
UMLModelDiff umlModelDiffAll = umlModelPairAll.getLeft().diff(umlModelPairAll.getRight());

Expand Down Expand Up @@ -505,7 +505,7 @@ else if (RefactoringType.MOVE_RENAME_ATTRIBUTE.equals(refactoring.getRefactoring
}
}

if (isAttributeAdded(umlModelDiffAll, rightAttribute.getUmlAttribute().getClassName(), rightAttribute::equalIdentifierIgnoringVersion, currentVersion)) {
if (isAttributeAdded(umlModelDiffAll, rightAttribute.getUmlAttribute().getLocationInfo().getSourceFolder(), rightAttribute.getUmlAttribute().getClassName(), rightAttribute::equalIdentifierIgnoringVersion, currentVersion)) {
Annotation annotationBefore = Annotation.of(rightAnnotation.getAnnotation(), rightAnnotation.getOperation().get(), parentVersion);
changeHistory.get().handleAdd(annotationBefore, rightAnnotation, "added with attribute");
changeHistory.add(annotationBefore);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ else if (leftInitializer != null && rightInitializer == null) {
return false;
}

public boolean isAttributeAdded(UMLModelDiff modelDiff, String className, Version currentVersion, Version parentVersion, Predicate<Attribute> equalOperator, List<UMLClassBaseDiff> allClassesDiff) {
public boolean isAttributeAdded(UMLModelDiff modelDiff, String sourceFolder, String className, Version currentVersion, Version parentVersion, Predicate<Attribute> equalOperator, List<UMLClassBaseDiff> allClassesDiff) {
List<UMLAttribute> addedAttributes = allClassesDiff
.stream()
.map(UMLClassBaseDiff::getAddedAttributes)
Expand All @@ -261,8 +261,8 @@ public boolean isAttributeAdded(UMLModelDiff modelDiff, String className, Versio
return true;
}

Set<UMLClass> addedClasses = modelDiff.getAllAddedClasses(className);
for (UMLClass addedClass : addedClasses) {
UMLClass addedClass = modelDiff.getAddedClass(sourceFolder, className);
if (addedClass != null) {
for (UMLAttribute umlAttribute : addedClass.getAttributes()) {
if (handleAddAttribute(currentVersion, parentVersion, equalOperator, umlAttribute, "added with new class"))
return true;
Expand Down
Loading

0 comments on commit 35211b9

Please sign in to comment.