Skip to content

Commit

Permalink
Updated CodeElementLocator tests to work without name
Browse files Browse the repository at this point in the history
  • Loading branch information
tsantalis committed Jun 27, 2024
1 parent c049d06 commit f41d4f8
Show file tree
Hide file tree
Showing 7 changed files with 208 additions and 172 deletions.
19 changes: 17 additions & 2 deletions src/main/java/org/codetracker/element/Attribute.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import gr.uom.java.xmi.UMLAnonymousClass;
import gr.uom.java.xmi.UMLAttribute;
import gr.uom.java.xmi.UMLOperation;
import gr.uom.java.xmi.LocationInfo.CodeElementType;
import gr.uom.java.xmi.decomposition.AbstractCodeFragment;
import gr.uom.java.xmi.decomposition.CompositeStatementObject;
import gr.uom.java.xmi.decomposition.LambdaExpressionObject;
Expand All @@ -14,6 +15,8 @@
import static org.codetracker.util.Util.annotationsToString;
import static org.codetracker.util.Util.getPath;

import java.util.LinkedHashSet;
import java.util.Set;
import java.util.function.Predicate;

public class Attribute extends BaseCodeElement {
Expand Down Expand Up @@ -56,12 +59,18 @@ public Block findBlockWithoutName(Predicate<Block> equalOperator) {
}
}
}
Set<Block> matches = new LinkedHashSet<Block>();
for (CompositeStatementObject composite : operation.getBody().getCompositeStatement().getInnerNodes()) {
Block block = Block.of(composite, this);
if (block != null && equalOperator.test(block)) {
return block;
matches.add(block);
}
}
for (Block match : matches) {
if (!match.getLocation().getCodeElementType().equals(CodeElementType.BLOCK)) {
return match;
}
}
}
}
}
Expand All @@ -76,12 +85,18 @@ public Block findBlockWithoutName(Predicate<Block> equalOperator) {
}
}
}
Set<Block> matches = new LinkedHashSet<Block>();
for (CompositeStatementObject composite : lambda.getBody().getCompositeStatement().getInnerNodes()) {
Block block = Block.of(composite, this);
if (block != null && equalOperator.test(block)) {
return block;
matches.add(block);
}
}
for (Block match : matches) {
if (!match.getLocation().getCodeElementType().equals(CodeElementType.BLOCK)) {
return match;
}
}
}
}
return null;
Expand Down
25 changes: 22 additions & 3 deletions src/main/java/org/codetracker/element/Method.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.codetracker.element;

import gr.uom.java.xmi.*;
import gr.uom.java.xmi.LocationInfo.CodeElementType;
import gr.uom.java.xmi.decomposition.AbstractCodeFragment;
import gr.uom.java.xmi.decomposition.AbstractStatement;
import gr.uom.java.xmi.decomposition.CompositeStatementObject;
Expand Down Expand Up @@ -118,12 +119,18 @@ public Block findBlockWithoutName(Predicate<Block> equalOperator) {
}
}
}
Set<Block> matches = new LinkedHashSet<Block>();
for (CompositeStatementObject composite : umlOperation.getBody().getCompositeStatement().getInnerNodes()) {
Block block = Block.of(composite, this);
if (block != null && equalOperator.test(block)) {
return block;
matches.add(block);
}
}
for (Block match : matches) {
if (!match.getLocation().getCodeElementType().equals(CodeElementType.BLOCK)) {
return match;
}
}
}
for (UMLAnonymousClass anonymousClass : umlOperation.getAnonymousClassList()) {
for (UMLOperation operation : anonymousClass.getOperations()) {
Expand All @@ -137,12 +144,18 @@ public Block findBlockWithoutName(Predicate<Block> equalOperator) {
}
}
}
Set<Block> matches = new LinkedHashSet<Block>();
for (CompositeStatementObject composite : operation.getBody().getCompositeStatement().getInnerNodes()) {
Block block = Block.of(composite, this);
if (block != null && equalOperator.test(block)) {
return block;
matches.add(block);
}
}
for (Block match : matches) {
if (!match.getLocation().getCodeElementType().equals(CodeElementType.BLOCK)) {
return match;
}
}
}
}
}
Expand All @@ -157,12 +170,18 @@ public Block findBlockWithoutName(Predicate<Block> equalOperator) {
}
}
}
Set<Block> matches = new LinkedHashSet<Block>();
for (CompositeStatementObject composite : lambda.getBody().getCompositeStatement().getInnerNodes()) {
Block block = Block.of(composite, this);
if (block != null && equalOperator.test(block)) {
return block;
matches.add(block);
}
}
for (Block match : matches) {
if (!match.getLocation().getCodeElementType().equals(CodeElementType.BLOCK)) {
return match;
}
}
}
}
return null;
Expand Down
108 changes: 86 additions & 22 deletions src/main/java/org/codetracker/util/AbstractCodeElementLocator.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.function.Predicate;

import org.codetracker.api.CodeElement;
import org.codetracker.api.CodeElementNotFoundException;
import org.codetracker.api.Version;
import org.codetracker.element.Attribute;
import org.codetracker.element.Block;
Expand Down Expand Up @@ -36,15 +37,17 @@ public AbstractCodeElementLocator(String commitId, String filePath, int lineNumb

public abstract CodeElement locate() throws Exception;

protected static Method getMethod(UMLModel umlModel, Version version, Predicate<Method> predicate) {
protected static Method getMethod(UMLModel umlModel, Version version, String filePath, Predicate<Method> predicate) {
if (umlModel != null)
for (UMLClass umlClass : umlModel.getClassList()) {
for (UMLAnonymousClass anonymousClass : umlClass.getAnonymousClassList()) {
Method method = getMethod(version, predicate, anonymousClass.getOperations());
if (method != null) return method;
}
Method method = getMethod(version, predicate, umlClass.getOperations());
if (method != null) return method;
if (umlClass.getSourceFile().equals(filePath)) {
for (UMLAnonymousClass anonymousClass : umlClass.getAnonymousClassList()) {
Method method = getMethod(version, predicate, anonymousClass.getOperations());
if (method != null) return method;
}
Method method = getMethod(version, predicate, umlClass.getOperations());
if (method != null) return method;
}
}
return null;
}
Expand Down Expand Up @@ -100,19 +103,21 @@ private static Method getMethod(Version version, Predicate<Method> predicate, Li
return null;
}

protected static Attribute getAttribute(UMLModel umlModel, Version version, Predicate<Attribute> predicate) {
protected static Attribute getAttribute(UMLModel umlModel, Version version, String filePath, Predicate<Attribute> predicate) {
if (umlModel != null)
for (UMLClass umlClass : umlModel.getClassList()) {
for (UMLAnonymousClass anonymousClass : umlClass.getAnonymousClassList()) {
Attribute attribute = getAttribute(version, predicate, anonymousClass.getAttributes());
if (attribute != null) return attribute;
attribute = getAttribute(version, predicate, anonymousClass.getEnumConstants());
if (attribute != null) return attribute;
}
Attribute attribute = getAttribute(version, predicate, umlClass.getAttributes());
if (attribute != null) return attribute;
attribute = getAttribute(version, predicate, umlClass.getEnumConstants());
if (attribute != null) return attribute;
if (umlClass.getSourceFile().equals(filePath)) {
for (UMLAnonymousClass anonymousClass : umlClass.getAnonymousClassList()) {
Attribute attribute = getAttribute(version, predicate, anonymousClass.getAttributes());
if (attribute != null) return attribute;
attribute = getAttribute(version, predicate, anonymousClass.getEnumConstants());
if (attribute != null) return attribute;
}
Attribute attribute = getAttribute(version, predicate, umlClass.getAttributes());
if (attribute != null) return attribute;
attribute = getAttribute(version, predicate, umlClass.getEnumConstants());
if (attribute != null) return attribute;
}
}
return null;
}
Expand All @@ -126,13 +131,72 @@ private static Attribute getAttribute(Version version, Predicate<Attribute> pred
return null;
}

protected static Class getClass(UMLModel umlModel, Version version, Predicate<Class> predicate) {
protected static Class getClass(UMLModel umlModel, Version version, String filePath, Predicate<Class> predicate) {
if (umlModel != null)
for (UMLClass umlClass : umlModel.getClassList()) {
Class clazz = Class.of(umlClass, version);
if (predicate.test(clazz))
return clazz;
if (umlClass.getSourceFile().equals(filePath)) {
Class clazz = Class.of(umlClass, version);
if (predicate.test(clazz))
return clazz;
}
}
return null;
}

protected CodeElement locateWithName(Version version, UMLModel umlModel) throws CodeElementNotFoundException {
Class clazz = getClass(umlModel, version, filePath, this::classPredicateWithName);
if (clazz != null) {
return clazz;
}
Attribute attribute = getAttribute(umlModel, version, filePath, this::attributePredicateWithName);
if (attribute != null) {
return attribute;
}
Method method = getMethod(umlModel, version, filePath, this::methodPredicateWithName);
if (method != null) {
return method;
}
else {
method = getMethod(umlModel, version, filePath, this::methodPredicateWithoutName);
if (method != null) {
Variable variable = method.findVariable(this::variablePredicate);
if (variable != null) {
return variable;
}
Block block = method.findBlock(this::blockPredicate);
if (block != null) {
return block;
}
}
}
throw new CodeElementNotFoundException(filePath, name, lineNumber);
}

protected CodeElement locateWithoutName(Version version, UMLModel umlModel) throws CodeElementNotFoundException {
Method method = getMethod(umlModel, version, filePath, this::methodPredicateWithoutName);
if (method != null) {
Block block = method.findBlockWithoutName(this::blockPredicate);
if (block != null) {
return block;
}
Attribute attribute = getAttribute(umlModel, version, filePath, this::attributePredicateWithoutName);
if (attribute != null) {
return attribute;
}
return method;
}
Attribute attribute = getAttribute(umlModel, version, filePath, this::attributePredicateWithoutName);
if (attribute != null) {
Block block = attribute.findBlockWithoutName(this::blockPredicate);
if (block != null) {
return block;
}
return attribute;
}
Class clazz = getClass(umlModel, version, filePath, this::classPredicateWithoutName);
if (clazz != null) {
return clazz;
}
throw new CodeElementNotFoundException(filePath, name, lineNumber);
}
}
52 changes: 2 additions & 50 deletions src/main/java/org/codetracker/util/CodeElementLocator.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import gr.uom.java.xmi.UMLModel;
import org.codetracker.api.CodeElement;
import org.codetracker.api.CodeElementNotFoundException;
import org.codetracker.api.Version;
import org.codetracker.element.*;
import org.codetracker.element.Class;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevWalk;
Expand Down Expand Up @@ -35,58 +32,13 @@ public CodeElement locate() throws Exception {
}
Version version = gitRepository.getVersion(commitId);
UMLModel umlModel = getUMLModel(repository, commitId, Collections.singleton(filePath));
Class clazz = getClass(umlModel, version, this::classPredicateWithName);
if (clazz != null) {
return clazz;
}
Attribute attribute = getAttribute(umlModel, version, this::attributePredicateWithName);
if (attribute != null) {
return attribute;
}
Method method = getMethod(umlModel, version, this::methodPredicateWithName);
if (method != null) {
return method;
}
else {
method = getMethod(umlModel, version, this::methodPredicateWithoutName);
if (method != null) {
Variable variable = method.findVariable(this::variablePredicate);
if (variable != null) {
return variable;
}
Block block = method.findBlock(this::blockPredicate);
if (block != null) {
return block;
}
}
}
throw new CodeElementNotFoundException(filePath, name, lineNumber);
return locateWithName(version, umlModel);
}

private CodeElement locateWithoutName() throws Exception {
Version version = gitRepository.getVersion(commitId);
UMLModel umlModel = getUMLModel(repository, commitId, Collections.singleton(filePath));
Method method = getMethod(umlModel, version, this::methodPredicateWithoutName);
if (method != null) {
Block block = method.findBlockWithoutName(this::blockPredicate);
if (block != null) {
return block;
}
return method;
}
Attribute attribute = getAttribute(umlModel, version, this::attributePredicateWithoutName);
if (attribute != null) {
Block block = attribute.findBlockWithoutName(this::blockPredicate);
if (block != null) {
return block;
}
return attribute;
}
Class clazz = getClass(umlModel, version, this::classPredicateWithoutName);
if (clazz != null) {
return clazz;
}
throw new CodeElementNotFoundException(filePath, name, lineNumber);
return locateWithoutName(version, umlModel);
}

private static UMLModel getUMLModel(Repository repository, String commitId, Set<String> fileNames) throws Exception {
Expand Down
Loading

0 comments on commit f41d4f8

Please sign in to comment.