From 422864e489ba75a17019e9dc713658c3b12335fa Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Mon, 8 May 2023 00:52:35 +0400 Subject: [PATCH] #91 -- fix ts classes --- .../gitrepository/model/LineChange.java | 4 +- .../service/GitRepositoryService.java | 67 ++++++------------- .../api/StructuralUnitIdentifier.java | 2 + .../heuristic/service/JavaIdentifier.java | 22 ++++-- .../extractor/ts/creator/db/ClassTS.java | 52 ++++++++++---- .../extractor/ts/model/TimeSeriesType.java | 2 +- 6 files changed, 82 insertions(+), 67 deletions(-) diff --git a/src/main/java/ru/ulstu/extractor/gitrepository/model/LineChange.java b/src/main/java/ru/ulstu/extractor/gitrepository/model/LineChange.java index 00dbdfc..317db4f 100644 --- a/src/main/java/ru/ulstu/extractor/gitrepository/model/LineChange.java +++ b/src/main/java/ru/ulstu/extractor/gitrepository/model/LineChange.java @@ -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() { diff --git a/src/main/java/ru/ulstu/extractor/gitrepository/service/GitRepositoryService.java b/src/main/java/ru/ulstu/extractor/gitrepository/service/GitRepositoryService.java index e400670..391a9a8 100644 --- a/src/main/java/ru/ulstu/extractor/gitrepository/service/GitRepositoryService.java +++ b/src/main/java/ru/ulstu/extractor/gitrepository/service/GitRepositoryService.java @@ -377,58 +377,33 @@ public class GitRepositoryService { } private FileChange getChange(FileChange fileChange, List 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); - } + while ((i < fileContent.size()) && fileContent.get(i).startsWith("-")) { + lineChange.setRemoved(true); + lineChange.setLineFrom(lineChange.getLineFrom() + "\n" + fileContent.get(i).replaceFirst("\\-", "")); + removed++; + i++; } + while ((i < fileContent.size()) && fileContent.get(i).startsWith("+")) { + lineChange.setAdded(true); + 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(removedLine == fileContent.size() - 1); - fileChange.setAdded(addedLine == fileContent.size() - 1); + fileChange.setRemoved(removed == fileContent.size()); + fileChange.setAdded(added == fileContent.size()); return fileChange; } - private LineChange setRemoved(LineChange lineChange, StringBuilder builder) { - lineChange.setLineFrom(builder.toString()); - lineChange.setRemoved(true); - return lineChange; - } - - private LineChange setAdded(LineChange lineChange, StringBuilder builder) { - lineChange.setLineTo(builder.toString()); - lineChange.setAdded(true); - return lineChange; - } - public Page findAll(Pageable pageable) { return gitRepositoryRepository.findAll(pageable); } diff --git a/src/main/java/ru/ulstu/extractor/heuristic/api/StructuralUnitIdentifier.java b/src/main/java/ru/ulstu/extractor/heuristic/api/StructuralUnitIdentifier.java index 549e7e4..c65fed7 100644 --- a/src/main/java/ru/ulstu/extractor/heuristic/api/StructuralUnitIdentifier.java +++ b/src/main/java/ru/ulstu/extractor/heuristic/api/StructuralUnitIdentifier.java @@ -107,4 +107,6 @@ public abstract class StructuralUnitIdentifier { public abstract boolean isResourceClass(String sourceCode); protected abstract boolean isResourceClass(File file); + + public abstract List getClasses(String sourceCode); } diff --git a/src/main/java/ru/ulstu/extractor/heuristic/service/JavaIdentifier.java b/src/main/java/ru/ulstu/extractor/heuristic/service/JavaIdentifier.java index 178c304..d4247f0 100644 --- a/src/main/java/ru/ulstu/extractor/heuristic/service/JavaIdentifier.java +++ b/src/main/java/ru/ulstu/extractor/heuristic/service/JavaIdentifier.java @@ -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 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 getClasses(String sourceCode) { + JavaParser parser = new JavaParser(); + ParseResult 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<>(); + } } diff --git a/src/main/java/ru/ulstu/extractor/ts/creator/db/ClassTS.java b/src/main/java/ru/ulstu/extractor/ts/creator/db/ClassTS.java index 904238c..446f1d7 100644 --- a/src/main/java/ru/ulstu/extractor/ts/creator/db/ClassTS.java +++ b/src/main/java/ru/ulstu/extractor/ts/creator/db/ClassTS.java @@ -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 getTimeSeries(Branch branch) { - return Collections.singletonList( - new TimeSeries( - String.format("%s %s %s", - branch.getGitRepository().getName(), - branch.getName(), - getTimeSeriesType().getDescription()), - branchService.findByRepositoryAndName(branch.getGitRepository(), branch.getName()), - getTimeSeriesType(), - Dummy.getDefaultTimeSeries())); + List timeSeriesResult = new ArrayList<>(); + List 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()); + 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 diff --git a/src/main/java/ru/ulstu/extractor/ts/model/TimeSeriesType.java b/src/main/java/ru/ulstu/extractor/ts/model/TimeSeriesType.java index 27487b7..6b422e6 100644 --- a/src/main/java/ru/ulstu/extractor/ts/model/TimeSeriesType.java +++ b/src/main/java/ru/ulstu/extractor/ts/model/TimeSeriesType.java @@ -4,7 +4,7 @@ public enum TimeSeriesType { COMMITS("Временной ряд коммитов"), AUTHOR_COMMITS("Временной ряд коммитов авторов"), BRANCHES("Временной ряд веток"), - CLASSES("Временной ряд классов"), + CLASSES("Временной ряд измененных классов"), DEPENDENCIES("Временной ряд зависимостей"), ENTITIES("Временной ряд сущностей"), FILES("Временной файлов"),