#91 -- fix ts classes

This commit is contained in:
Anton Romanov 2023-05-08 00:52:35 +04:00
parent 23b41ba283
commit 422864e489
6 changed files with 82 additions and 67 deletions

View File

@ -9,8 +9,8 @@ public class LineChange extends BaseEntity {
private Boolean added = false;
private Boolean removed = false;
private String lineFrom;
private String lineTo;
private String lineFrom = "";
private String lineTo = "";
public LineChange() {

View File

@ -377,56 +377,31 @@ public class GitRepositoryService {
}
private FileChange getChange(FileChange fileChange, List<String> fileContent) {
int addedLine = 0;
int removedLine = 0;
StringBuilder builder = new StringBuilder();
boolean isRemoved = false;
boolean isAdded = false;
for (String line : fileContent) {
int i = 0;
int added = 0;
int removed = 0;
while (i < fileContent.size()) {
LineChange lineChange = new LineChange();
if (line.startsWith("-")) {
isRemoved = true;
if (isAdded) {
isAdded = false;
lineChange = setAdded(lineChange, builder);
builder.setLength(0);
}
builder.append(line).append("\n");
removedLine++;
} else if (line.startsWith("+")) {
isAdded = true;
if (isRemoved) {
isRemoved = false;
lineChange = setRemoved(lineChange, builder);
builder.setLength(0);
}
builder.append(line).append("\n");
addedLine++;
} else {
if (isRemoved) {
lineChange = setRemoved(lineChange, builder);
builder.setLength(0);
} else if (isAdded) {
lineChange = setAdded(lineChange, builder);
builder.setLength(0);
}
}
}
fileChange.setRemoved(removedLine == fileContent.size() - 1);
fileChange.setAdded(addedLine == fileContent.size() - 1);
return fileChange;
}
private LineChange setRemoved(LineChange lineChange, StringBuilder builder) {
lineChange.setLineFrom(builder.toString());
while ((i < fileContent.size()) && fileContent.get(i).startsWith("-")) {
lineChange.setRemoved(true);
return lineChange;
lineChange.setLineFrom(lineChange.getLineFrom() + "\n" + fileContent.get(i).replaceFirst("\\-", ""));
removed++;
i++;
}
private LineChange setAdded(LineChange lineChange, StringBuilder builder) {
lineChange.setLineTo(builder.toString());
while ((i < fileContent.size()) && fileContent.get(i).startsWith("+")) {
lineChange.setAdded(true);
return lineChange;
lineChange.setLineTo(lineChange.getLineTo() + "\n" + fileContent.get(i).replaceFirst("\\+", ""));
added++;
i++;
}
if (!lineChange.getLineTo().isEmpty() || !lineChange.getLineFrom().isEmpty()) {
fileChange.getLineChanges().add(lineChange);
}
i++;
}
fileChange.setRemoved(removed == fileContent.size());
fileChange.setAdded(added == fileContent.size());
return fileChange;
}
public Page<GitRepository> findAll(Pageable pageable) {

View File

@ -107,4 +107,6 @@ public abstract class StructuralUnitIdentifier {
public abstract boolean isResourceClass(String sourceCode);
protected abstract boolean isResourceClass(File file);
public abstract List<String> getClasses(String sourceCode);
}

View File

@ -6,6 +6,7 @@ import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.FieldDeclaration;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.body.TypeDeclaration;
import com.github.javaparser.ast.nodeTypes.NodeWithSimpleName;
import org.springframework.stereotype.Service;
import ru.ulstu.extractor.heuristic.api.StructuralUnitIdentifier;
import ru.ulstu.extractor.heuristic.component.BuildTool;
@ -19,6 +20,7 @@ import ru.ulstu.extractor.util.StringUtils;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
@ -52,6 +54,9 @@ public class JavaIdentifier extends StructuralUnitIdentifier {
}
public boolean canAppliedToCode(String sourceCode) {
if (sourceCode == null || sourceCode.isEmpty()) {
return false;
}
return getMainProgrammingLanguage(sourceCode).orElse(null) instanceof JavaProgrammingLanguage;
}
@ -169,12 +174,7 @@ public class JavaIdentifier extends StructuralUnitIdentifier {
}
private boolean sourceCodeContainsClass(String sourceCode) {
JavaParser parser = new JavaParser();
ParseResult<CompilationUnit> parseResult = parser.parse(sourceCode);
if (parseResult.getResult().isPresent() && parseResult.getResult().get().findCompilationUnit().isPresent()) {
return parseResult.getResult().get().getTypes().stream().findAny().isPresent();
}
return false;
return getClasses(sourceCode).size() > 0;
}
@Override
@ -243,4 +243,14 @@ public class JavaIdentifier extends StructuralUnitIdentifier {
}
return false;
}
@Override
public List<String> getClasses(String sourceCode) {
JavaParser parser = new JavaParser();
ParseResult<CompilationUnit> parseResult = parser.parse(sourceCode);
if (parseResult.getResult().isPresent() && parseResult.getResult().get().findCompilationUnit().isPresent()) {
return parseResult.getResult().get().getTypes().stream().map(NodeWithSimpleName::getNameAsString).collect(Collectors.toList());
}
return new ArrayList<>();
}
}

View File

@ -3,36 +3,64 @@ package ru.ulstu.extractor.ts.creator.db;
import org.springframework.stereotype.Component;
import ru.ulstu.extractor.branch.model.Branch;
import ru.ulstu.extractor.branch.service.BranchService;
import ru.ulstu.extractor.commit.model.Commit;
import ru.ulstu.extractor.commit.service.CommitService;
import ru.ulstu.extractor.gitrepository.model.FileChange;
import ru.ulstu.extractor.gitrepository.model.LineChange;
import ru.ulstu.extractor.heuristic.api.StructuralUnitIdentifier;
import ru.ulstu.extractor.ts.model.TimeSeries;
import ru.ulstu.extractor.ts.model.TimeSeriesType;
import ru.ulstu.extractor.ts.model.TimeSeriesValue;
import ru.ulstu.extractor.ts.service.TimeSeriesService;
import ru.ulstu.extractor.ts.util.Dummy;
import java.util.Collections;
import java.util.ArrayList;
import java.util.List;
@Component
public class ClassTS extends DBTimeSeriesCreator {
private final TimeSeriesService timeSeriesService;
private final CommitService commitService;
private final BranchService branchService;
private final StructuralUnitIdentifier structuralUnitIdentifier;
public ClassTS(TimeSeriesService timeSeriesService,
BranchService branchService) {
CommitService commitService,
BranchService branchService,
StructuralUnitIdentifier structuralUnitIdentifier) {
this.timeSeriesService = timeSeriesService;
this.commitService = commitService;
this.branchService = branchService;
this.structuralUnitIdentifier = structuralUnitIdentifier;
}
@Override
public List<TimeSeries> getTimeSeries(Branch branch) {
return Collections.singletonList(
new TimeSeries(
List<TimeSeries> timeSeriesResult = new ArrayList<>();
List<Commit> commits = new ArrayList<>(commitService.findByRepositoryIdAndName(branch.getGitRepository().getId(),
branch.getName()));
TimeSeries timeSeries = new TimeSeries(
String.format("%s %s %s",
branch.getGitRepository().getName(),
branch.getName(),
getTimeSeriesType().getDescription()),
branchService.findByRepositoryAndName(branch.getGitRepository(), branch.getName()),
getTimeSeriesType(),
Dummy.getDefaultTimeSeries()));
branchService.findByRepositoryAndName(branch.getGitRepository(),
branch.getName()),
getTimeSeriesType());
for (Commit commit : commits) {
double value = 0;
for (FileChange fileChange : commit.getFileChanges()) {
for (LineChange lineChange : fileChange.getLineChanges()) {
if (lineChange.getLineTo() != null && !lineChange.getLineTo().isEmpty()) {
value += structuralUnitIdentifier.getClasses(lineChange.getLineTo()).size();
}
}
}
timeSeries.getValues().add(new TimeSeriesValue(commit.getDate(), value));
}
if (!timeSeries.getValues().isEmpty()) {
timeSeriesResult.add(timeSeries);
}
return timeSeriesResult;
}
@Override

View File

@ -4,7 +4,7 @@ public enum TimeSeriesType {
COMMITS("Временной ряд коммитов"),
AUTHOR_COMMITS("Временной ряд коммитов авторов"),
BRANCHES("Временной ряд веток"),
CLASSES("Временной ряд классов"),
CLASSES("Временной ряд измененных классов"),
DEPENDENCIES("Временной ряд зависимостей"),
ENTITIES("Временной ряд сущностей"),
FILES("Временной файлов"),