Skip to content

Commit

Permalink
Basic infrastructure for comment support
Browse files Browse the repository at this point in the history
  • Loading branch information
tsantalis committed Jul 6, 2024
1 parent e54d18b commit 0a43751
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/main/java/org/codetracker/element/Attribute.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import gr.uom.java.xmi.LocationInfo;
import gr.uom.java.xmi.UMLAnonymousClass;
import gr.uom.java.xmi.UMLAttribute;
import gr.uom.java.xmi.UMLComment;
import gr.uom.java.xmi.UMLOperation;
import gr.uom.java.xmi.LocationInfo.CodeElementType;
import gr.uom.java.xmi.decomposition.AbstractCodeFragment;
Expand Down Expand Up @@ -100,6 +101,34 @@ public Block findBlockWithoutName(Predicate<Block> equalOperator) {
return null;
}

public Comment findComment(Predicate<Comment> equalOperator) {
for (UMLComment umlComment : umlAttribute.getComments()) {
Comment comment = Comment.of(umlComment, this);
if (comment != null && equalOperator.test(comment)) {
return comment;
}
}
for (UMLAnonymousClass anonymousClass : umlAttribute.getAnonymousClassList()) {
for (UMLOperation operation : anonymousClass.getOperations()) {
for (UMLComment umlComment : operation.getComments()) {
Comment comment = Comment.of(umlComment, this);
if (comment != null && equalOperator.test(comment)) {
return comment;
}
}
}
}
for (LambdaExpressionObject lambda : umlAttribute.getAllLambdas()) {
for (UMLComment umlComment : lambda.getComments()) {
Comment comment = Comment.of(umlComment, this);
if (comment != null && equalOperator.test(comment)) {
return comment;
}
}
}
return null;
}

private Block promotionStrategy(Map<CodeElementType, Block> matches) {
//promote catch over try
if (matches.containsKey(CodeElementType.CATCH_CLAUSE)) {
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/org/codetracker/element/Class.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

import gr.uom.java.xmi.LocationInfo;
import gr.uom.java.xmi.UMLAbstractClass;
import gr.uom.java.xmi.UMLComment;

import org.codetracker.api.Version;

import static org.codetracker.util.Util.annotationsToString;
import static org.codetracker.util.Util.getPath;

import java.util.function.Predicate;

public class Class extends BaseCodeElement {
private final UMLAbstractClass umlClass;

Expand All @@ -30,6 +34,16 @@ public static Class of(UMLAbstractClass umlClass, Version version) {
return new Class(umlClass, identifierExcludeVersion, className, umlClass.getLocationInfo().getFilePath(), version);
}

public Comment findComment(Predicate<Comment> equalOperator) {
for (UMLComment umlComment : umlClass.getComments()) {
Comment comment = Comment.of(umlComment, this);
if (comment != null && equalOperator.test(comment)) {
return comment;
}
}
return null;
}

public UMLAbstractClass getUmlClass() {
return umlClass;
}
Expand Down
109 changes: 109 additions & 0 deletions src/main/java/org/codetracker/element/Comment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package org.codetracker.element;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import org.codetracker.api.Version;
import org.codetracker.util.Util;

import gr.uom.java.xmi.LocationInfo;
import gr.uom.java.xmi.UMLAbstractClass;
import gr.uom.java.xmi.UMLComment;
import gr.uom.java.xmi.VariableDeclarationContainer;

public class Comment extends BaseCodeElement {
private final UMLComment comment;
private final VariableDeclarationContainer operation;
private final UMLAbstractClass clazz;

public Comment(UMLComment comment, VariableDeclarationContainer operation, String identifierIgnoringVersion, String name, String filePath, Version version) {
super(identifierIgnoringVersion, name, filePath, version);
this.comment = comment;
this.operation = operation;
this.clazz = null;
}

public Comment(UMLComment comment, UMLAbstractClass clazz, String identifierIgnoringVersion, String name, String filePath, Version version) {
super(identifierIgnoringVersion, name, filePath, version);
this.comment = comment;
this.clazz = clazz;
this.operation = null;
}

public UMLComment getComment() {
return comment;
}

public Optional<VariableDeclarationContainer> getOperation() {
return operation != null ? Optional.of(operation) : Optional.empty();
}

public Optional<UMLAbstractClass> getClazz() {
return clazz != null ? Optional.of(clazz) : Optional.empty();
}

public static Comment of(UMLComment comment, Class clazz) {
LocationInfo commentLocationInfo = comment.getLocationInfo();
String statementType = commentLocationInfo.getCodeElementType().name();
String name = String.format("%s$%s(%d-%d)", clazz.getName(), statementType, commentLocationInfo.getStartLine(), commentLocationInfo.getEndLine());
String sha512 = Util.getSHA512(comment.getText());
String identifierExcludeVersion = String.format(
"%s$%s:{%s,%s}",
clazz.getIdentifierIgnoringVersion(),
statementType,
sha512,
getSignature(comment, clazz.getUmlClass())
);
return new Comment(comment, clazz.getUmlClass(), identifierExcludeVersion, name, commentLocationInfo.getFilePath(), clazz.getVersion());
}

public static Comment of(UMLComment comment, Attribute attribute) {
LocationInfo commentLocationInfo = comment.getLocationInfo();
String statementType = commentLocationInfo.getCodeElementType().name();
String name = String.format("%s$%s(%d-%d)", attribute.getName(), statementType, commentLocationInfo.getStartLine(), commentLocationInfo.getEndLine());
String sha512 = Util.getSHA512(comment.getText());
String identifierExcludeVersion = String.format(
"%s$%s:{%s,%s}",
attribute.getIdentifierIgnoringVersion(),
statementType,
sha512,
getSignature(comment, attribute.getUmlAttribute())
);
return new Comment(comment, attribute.getUmlAttribute(), identifierExcludeVersion, name, commentLocationInfo.getFilePath(), attribute.getVersion());
}

public static Comment of(UMLComment comment, Method method) {
LocationInfo commentLocationInfo = comment.getLocationInfo();
String statementType = commentLocationInfo.getCodeElementType().name();
String name = String.format("%s$%s(%d-%d)", method.getName(), statementType, commentLocationInfo.getStartLine(), commentLocationInfo.getEndLine());
String sha512 = Util.getSHA512(comment.getText());
String identifierExcludeVersion = String.format(
"%s$%s:{%s,%s}",
method.getIdentifierIgnoringVersion(),
statementType,
sha512,
getSignature(comment, method.getUmlOperation())
);
return new Comment(comment, method.getUmlOperation(), identifierExcludeVersion, name, commentLocationInfo.getFilePath(), method.getVersion());
}

private static String getSignature(UMLComment comment, VariableDeclarationContainer operation) {
String statementType = comment.getLocationInfo().getCodeElementType().name();
List<UMLComment> sameTypeSibling = operation.getComments().stream().filter(st -> statementType.equals(st.getLocationInfo().getCodeElementType().name())).collect(Collectors.toList());
int typeIndex = sameTypeSibling.indexOf(comment) + 1;
return String.format("%s%d", statementType, typeIndex);
}

private static String getSignature(UMLComment comment, UMLAbstractClass clazz) {
String statementType = comment.getLocationInfo().getCodeElementType().name();
List<UMLComment> sameTypeSibling = clazz.getComments().stream().filter(st -> statementType.equals(st.getLocationInfo().getCodeElementType().name())).collect(Collectors.toList());
int typeIndex = sameTypeSibling.indexOf(comment) + 1;
return String.format("%s%d", statementType, typeIndex);
}

@Override
public LocationInfo getLocation() {
return comment.getLocationInfo();
}
}
28 changes: 28 additions & 0 deletions src/main/java/org/codetracker/element/Method.java
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,34 @@ private Block promotionStrategy(Map<CodeElementType, Block> matches) {
return null;
}

public Comment findComment(Predicate<Comment> equalOperator) {
for (UMLComment umlComment : umlOperation.getComments()) {
Comment comment = Comment.of(umlComment, this);
if (comment != null && equalOperator.test(comment)) {
return comment;
}
}
for (UMLAnonymousClass anonymousClass : umlOperation.getAnonymousClassList()) {
for (UMLOperation operation : anonymousClass.getOperations()) {
for (UMLComment umlComment : operation.getComments()) {
Comment comment = Comment.of(umlComment, this);
if (comment != null && equalOperator.test(comment)) {
return comment;
}
}
}
}
for (LambdaExpressionObject lambda : umlOperation.getAllLambdas()) {
for (UMLComment umlComment : lambda.getComments()) {
Comment comment = Comment.of(umlComment, this);
if (comment != null && equalOperator.test(comment)) {
return comment;
}
}
}
return null;
}

public VariableDeclarationContainer getUmlOperation() {
return umlOperation;
}
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/org/codetracker/util/AbstractCodeElementLocator.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.codetracker.element.Attribute;
import org.codetracker.element.Block;
import org.codetracker.element.Class;
import org.codetracker.element.Comment;
import org.codetracker.element.Method;
import org.codetracker.element.Variable;

Expand Down Expand Up @@ -91,6 +92,11 @@ protected boolean attributePredicateWithoutName(Attribute attribute) {
attribute.getUmlAttribute().getLocationInfo().getEndLine() >= lineNumber;
}

protected boolean commentPredicate(Comment comment) {
return comment.getComment().getLocationInfo().getStartLine() == lineNumber &&
comment.getComment().getLocationInfo().getEndLine() >= lineNumber;
}

protected boolean blockPredicate(Block block) {
return block.getComposite().getLocationInfo().getStartLine() == lineNumber &&
block.getComposite().getLocationInfo().getEndLine() >= lineNumber;
Expand Down Expand Up @@ -208,6 +214,10 @@ protected CodeElement locateWithoutName(Version version, UMLModel umlModel) thro
return block;
}
}
Comment comment = method.findComment(this::commentPredicate);
if (comment != null) {
return comment;
}
Attribute attribute = getAttribute(umlModel, version, filePath, this::attributePredicateWithoutName);
if (attribute != null) {
return attribute;
Expand All @@ -228,10 +238,18 @@ protected CodeElement locateWithoutName(Version version, UMLModel umlModel) thro
return block;
}
}
Comment comment = attribute.findComment(this::commentPredicate);
if (comment != null) {
return comment;
}
return attribute;
}
Class clazz = getClass(umlModel, version, filePath, this::classPredicateWithoutName);
if (clazz != null) {
Comment comment = clazz.findComment(this::commentPredicate);
if (comment != null) {
return comment;
}
clazz.checkClosingBracket(lineNumber);
return clazz;
}
Expand Down

0 comments on commit 0a43751

Please sign in to comment.