Skip to content

Commit

Permalink
Basic infrastructure for import support
Browse files Browse the repository at this point in the history
  • Loading branch information
tsantalis committed Jul 11, 2024
1 parent 41c2ae4 commit 3071ac1
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/main/java/org/codetracker/element/Comment.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ public static Comment of(UMLAbstractDocumentation comment, VariableDeclarationCo
}

public static Comment of(UMLAbstractDocumentation comment, UMLAbstractClass clazz, Version version) {
Class method = Class.of(clazz, version);
return of(comment, method);
Class c = Class.of(clazz, version);
return of(comment, c);
}

public static Comment of(UMLAbstractDocumentation comment, Class clazz) {
Expand Down
51 changes: 51 additions & 0 deletions src/main/java/org/codetracker/element/Import.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package org.codetracker.element;

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.UMLImport;

public class Import extends BaseCodeElement {
private final UMLImport umlImport;
private final UMLAbstractClass clazz;

public Import(UMLImport umlImport, UMLAbstractClass clazz, String identifierIgnoringVersion, String name, String filePath, Version version) {
super(identifierIgnoringVersion, name, filePath, version);
this.umlImport = umlImport;
this.clazz = clazz;
}

public UMLImport getUmlImport() {
return umlImport;
}

public UMLAbstractClass getClazz() {
return clazz;
}

public static Import of(UMLImport umlImport, UMLAbstractClass clazz, Version version) {
Class c = Class.of(clazz, version);
return of(umlImport, c);
}

public static Import of(UMLImport umlImport, Class clazz) {
LocationInfo commentLocationInfo = umlImport.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(umlImport.getName());
String identifierExcludeVersion = String.format(
"%s$%s:{%s}",
clazz.getIdentifierIgnoringVersion(),
statementType,
sha512
);
return new Import(umlImport, clazz.getUmlClass(), identifierExcludeVersion, name, commentLocationInfo.getFilePath(), clazz.getVersion());
}

@Override
public LocationInfo getLocation() {
return umlImport.getLocationInfo();
}
}
25 changes: 25 additions & 0 deletions src/main/java/org/codetracker/util/AbstractCodeElementLocator.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
import org.codetracker.element.Block;
import org.codetracker.element.Class;
import org.codetracker.element.Comment;
import org.codetracker.element.Import;
import org.codetracker.element.Method;
import org.codetracker.element.Variable;

import gr.uom.java.xmi.UMLAnonymousClass;
import gr.uom.java.xmi.UMLAttribute;
import gr.uom.java.xmi.UMLClass;
import gr.uom.java.xmi.UMLComment;
import gr.uom.java.xmi.UMLImport;
import gr.uom.java.xmi.UMLModel;
import gr.uom.java.xmi.UMLOperation;
import gr.uom.java.xmi.decomposition.CompositeStatementObject;
Expand Down Expand Up @@ -140,6 +142,11 @@ protected boolean commentPredicate(Comment comment) {
comment.getComment().getLocationInfo().getEndLine() >= lineNumber;
}

protected boolean importPredicate(Import imp) {
return imp.getUmlImport().getLocationInfo().getStartLine() <= lineNumber &&
imp.getUmlImport().getLocationInfo().getEndLine() >= lineNumber;
}

protected boolean blockPredicate(Block block) {
return block.getComposite().getLocationInfo().getStartLine() == lineNumber &&
block.getComposite().getLocationInfo().getEndLine() >= lineNumber;
Expand Down Expand Up @@ -214,6 +221,20 @@ protected static Class getClass(UMLModel umlModel, Version version, String fileP
return null;
}

protected static Import getImport(UMLModel umlModel, Version version, String filePath, Predicate<Import> predicate) {
if (umlModel != null)
for (UMLClass umlClass : umlModel.getClassList()) {
if (umlClass.getSourceFile().equals(filePath)) {
for (UMLImport umlImport : umlClass.getImportedTypes()) {
Import imp = Import.of(umlImport, umlClass, version);
if (predicate.test(imp))
return imp;
}
}
}
return null;
}

protected CodeElement locateWithName(Version version, UMLModel umlModel) throws CodeElementNotFoundException {
Class clazz = getClass(umlModel, version, filePath, this::classPredicateWithName);
if (clazz != null) {
Expand Down Expand Up @@ -287,6 +308,10 @@ protected CodeElement locateWithoutName(Version version, UMLModel umlModel) thro
}
return attribute;
}
Import imp = getImport(umlModel, version, filePath, this::importPredicate);
if (imp != null) {
return imp;
}
Class clazz = getClass(umlModel, version, filePath, this::classPredicateWithoutName);
if (clazz != null) {
Comment comment = clazz.findComment(this::commentPredicate);
Expand Down
19 changes: 19 additions & 0 deletions src/test/java/org/codetracker/util/CodeElementLocatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.codetracker.element.Block;
import org.codetracker.element.Class;
import org.codetracker.element.Comment;
import org.codetracker.element.Import;
import org.codetracker.element.Method;
import org.codetracker.element.Variable;
import org.eclipse.jgit.lib.Repository;
Expand Down Expand Up @@ -749,4 +750,22 @@ public void testCommentInInnerClassMethodLocator() throws Exception {
assertEquals(((Comment)codeElement).getOperation().get().getName(), "startElement");
}
}
// Import tests
@Test
public void testImportLocator() throws Exception {
GitService gitService = new GitServiceImpl();
final String filePath = "src/main/java/com/puppycrawl/tools/checkstyle/Checker.java";
final String commitId = "119fd4fb33bef9f5c66fc950396669af842c21a3";
final int lineNumber = 58;
try (Repository repository = gitService.cloneIfNotExists(FOLDER_TO_CLONE + "checkstyle\\checkstyle",
"https://github.com/checkstyle/checkstyle.git")){
CodeElementLocator locator = new CodeElementLocator(repository, commitId, filePath, lineNumber);
CodeElement codeElement = locator.locate();
assertNotNull(codeElement);
assertEquals(codeElement.getClass(), Import.class);
assertEquals(codeElement.getLocation().getCodeElementType(), CodeElementType.IMPORT_DECLARATION);
assertEquals(codeElement.getLocation().getStartLine(), lineNumber);
assertEquals(((Import)codeElement).getUmlImport().getName(), "com.puppycrawl.tools.checkstyle.utils.CommonUtils");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.codetracker.element.Block;
import org.codetracker.element.Class;
import org.codetracker.element.Comment;
import org.codetracker.element.Import;
import org.codetracker.element.Method;
import org.codetracker.element.Variable;
import static org.junit.jupiter.api.Assertions.*;
Expand Down Expand Up @@ -614,4 +615,19 @@ public void testCommentInInnerClassMethodLocator() throws Exception {
assertEquals(codeElement.getLocation().getStartLine(), lineNumber);
assertEquals(((Comment)codeElement).getOperation().get().getName(), "startElement");
}
// Import tests
@Test
public void testImportLocator() throws Exception {
final String cloneURL = "https://github.com/checkstyle/checkstyle.git";
final String filePath = "src/main/java/com/puppycrawl/tools/checkstyle/Checker.java";
final String commitId = "119fd4fb33bef9f5c66fc950396669af842c21a3";
final int lineNumber = 58;
CodeElementLocatorWithLocalFiles locator = new CodeElementLocatorWithLocalFiles(cloneURL, commitId, filePath, lineNumber);
CodeElement codeElement = locator.locate();
assertNotNull(codeElement);
assertEquals(codeElement.getClass(), Import.class);
assertEquals(codeElement.getLocation().getCodeElementType(), CodeElementType.IMPORT_DECLARATION);
assertEquals(codeElement.getLocation().getStartLine(), lineNumber);
assertEquals(((Import)codeElement).getUmlImport().getName(), "com.puppycrawl.tools.checkstyle.utils.CommonUtils");
}
}

0 comments on commit 3071ac1

Please sign in to comment.