#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 added = false;
private Boolean removed = false; private Boolean removed = false;
private String lineFrom; private String lineFrom = "";
private String lineTo; private String lineTo = "";
public LineChange() { public LineChange() {

View File

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

View File

@ -107,4 +107,6 @@ public abstract class StructuralUnitIdentifier {
public abstract boolean isResourceClass(String sourceCode); public abstract boolean isResourceClass(String sourceCode);
protected abstract boolean isResourceClass(File file); 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.FieldDeclaration;
import com.github.javaparser.ast.body.MethodDeclaration; import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.body.TypeDeclaration; import com.github.javaparser.ast.body.TypeDeclaration;
import com.github.javaparser.ast.nodeTypes.NodeWithSimpleName;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import ru.ulstu.extractor.heuristic.api.StructuralUnitIdentifier; import ru.ulstu.extractor.heuristic.api.StructuralUnitIdentifier;
import ru.ulstu.extractor.heuristic.component.BuildTool; import ru.ulstu.extractor.heuristic.component.BuildTool;
@ -19,6 +20,7 @@ import ru.ulstu.extractor.util.StringUtils;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -52,6 +54,9 @@ public class JavaIdentifier extends StructuralUnitIdentifier {
} }
public boolean canAppliedToCode(String sourceCode) { public boolean canAppliedToCode(String sourceCode) {
if (sourceCode == null || sourceCode.isEmpty()) {
return false;
}
return getMainProgrammingLanguage(sourceCode).orElse(null) instanceof JavaProgrammingLanguage; return getMainProgrammingLanguage(sourceCode).orElse(null) instanceof JavaProgrammingLanguage;
} }
@ -169,12 +174,7 @@ public class JavaIdentifier extends StructuralUnitIdentifier {
} }
private boolean sourceCodeContainsClass(String sourceCode) { private boolean sourceCodeContainsClass(String sourceCode) {
JavaParser parser = new JavaParser(); return getClasses(sourceCode).size() > 0;
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;
} }
@Override @Override
@ -243,4 +243,14 @@ public class JavaIdentifier extends StructuralUnitIdentifier {
} }
return false; 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 org.springframework.stereotype.Component;
import ru.ulstu.extractor.branch.model.Branch; import ru.ulstu.extractor.branch.model.Branch;
import ru.ulstu.extractor.branch.service.BranchService; 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.TimeSeries;
import ru.ulstu.extractor.ts.model.TimeSeriesType; 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.service.TimeSeriesService;
import ru.ulstu.extractor.ts.util.Dummy;
import java.util.Collections; import java.util.ArrayList;
import java.util.List; import java.util.List;
@Component @Component
public class ClassTS extends DBTimeSeriesCreator { public class ClassTS extends DBTimeSeriesCreator {
private final TimeSeriesService timeSeriesService; private final TimeSeriesService timeSeriesService;
private final CommitService commitService;
private final BranchService branchService; private final BranchService branchService;
private final StructuralUnitIdentifier structuralUnitIdentifier;
public ClassTS(TimeSeriesService timeSeriesService, public ClassTS(TimeSeriesService timeSeriesService,
BranchService branchService) { CommitService commitService,
BranchService branchService,
StructuralUnitIdentifier structuralUnitIdentifier) {
this.timeSeriesService = timeSeriesService; this.timeSeriesService = timeSeriesService;
this.commitService = commitService;
this.branchService = branchService; this.branchService = branchService;
this.structuralUnitIdentifier = structuralUnitIdentifier;
} }
@Override @Override
public List<TimeSeries> getTimeSeries(Branch branch) { public List<TimeSeries> getTimeSeries(Branch branch) {
return Collections.singletonList( List<TimeSeries> timeSeriesResult = new ArrayList<>();
new TimeSeries( List<Commit> commits = new ArrayList<>(commitService.findByRepositoryIdAndName(branch.getGitRepository().getId(),
branch.getName()));
TimeSeries timeSeries = new TimeSeries(
String.format("%s %s %s", String.format("%s %s %s",
branch.getGitRepository().getName(), branch.getGitRepository().getName(),
branch.getName(), branch.getName(),
getTimeSeriesType().getDescription()), getTimeSeriesType().getDescription()),
branchService.findByRepositoryAndName(branch.getGitRepository(), branch.getName()), branchService.findByRepositoryAndName(branch.getGitRepository(),
getTimeSeriesType(), branch.getName()),
Dummy.getDefaultTimeSeries())); 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 @Override

View File

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