Merge branch '20-detect-bl' into 'master'

Resolve "Определять классы, содержащие бизнес-логику"

Closes #20

See merge request romanov73/git-extractor!17
This commit is contained in:
Anton Romanov 2021-04-19 09:47:05 +00:00
commit 4fd8a510f3
14 changed files with 131 additions and 17 deletions

View File

@ -1,3 +1,8 @@
/*
* Copyright (C) 2021 Anton Romanov - All Rights Reserved
* You may use, distribute and modify this code, please write to: romanov73@gmail.com.
*/
package ru.ulstu.extractor.config;
import org.springframework.http.HttpStatus;
@ -6,10 +11,12 @@ import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.NoHandlerFoundException;
import springfox.documentation.annotations.ApiIgnore;
import javax.servlet.http.HttpServletRequest;
@ControllerAdvice
@ApiIgnore
class GlobalDefaultExceptionHandler {
public static final String DEFAULT_ERROR_VIEW = "error";

View File

@ -11,10 +11,12 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import ru.ulstu.extractor.repository.BranchRepository;
import ru.ulstu.extractor.repository.RepositoryRepository;
import springfox.documentation.annotations.ApiIgnore;
import static ru.ulstu.extractor.controller.Route.LIST_REPOSITORY_BRANCHES;
@Controller
@ApiIgnore
public class BranchController {
private final RepositoryRepository repositoryRepository;
private final BranchRepository branchRepository;

View File

@ -15,6 +15,7 @@ import ru.ulstu.extractor.model.Commit;
import ru.ulstu.extractor.model.OffsetablePageRequest;
import ru.ulstu.extractor.model.mvc.FilterForm;
import ru.ulstu.extractor.service.FilteringService;
import springfox.documentation.annotations.ApiIgnore;
import java.util.List;
import java.util.Optional;
@ -24,6 +25,7 @@ import java.util.stream.IntStream;
import static ru.ulstu.extractor.controller.Route.FILTER_COMMITS;
@Controller
@ApiIgnore
public class GitFilteringController {
private final static int DEFAULT_PAGE_SIZE = 20;
private final FilteringService filteringService;

View File

@ -20,6 +20,7 @@ import ru.ulstu.extractor.model.mvc.FilterForm;
import ru.ulstu.extractor.model.mvc.RepoForm;
import ru.ulstu.extractor.service.GitRepositoryService;
import ru.ulstu.extractor.service.IndexService;
import springfox.documentation.annotations.ApiIgnore;
import java.io.IOException;
import java.util.List;
@ -28,6 +29,7 @@ import static ru.ulstu.extractor.controller.Route.FILTER_COMMITS;
import static ru.ulstu.extractor.controller.Route.INDEXING_NEW_REPOSITORY;
@Controller
@ApiIgnore
public class GitIndexingController {
private final static Logger LOG = LoggerFactory.getLogger(GitIndexingController.class);
private final GitRepositoryService gitRepositoryService;

View File

@ -9,10 +9,12 @@ import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import ru.ulstu.extractor.repository.RepositoryRepository;
import springfox.documentation.annotations.ApiIgnore;
import static ru.ulstu.extractor.controller.Route.LIST_INDEXED_REPOSITORIES;
@Controller
@ApiIgnore
public class RepositoryController {
private final RepositoryRepository repositoryRepository;

View File

@ -9,6 +9,7 @@ import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import ru.ulstu.extractor.repository.CommitRepository;
import springfox.documentation.annotations.ApiIgnore;
import java.util.List;
import java.util.stream.Collectors;
@ -16,6 +17,7 @@ import java.util.stream.Collectors;
import static ru.ulstu.extractor.controller.Route.STATISTIC;
@Controller
@ApiIgnore
public class StatisticController {
private final CommitRepository commitRepository;

View File

@ -7,7 +7,8 @@ package ru.ulstu.extractor.heuristic.api;
import ru.ulstu.extractor.heuristic.component.BuildTool;
import ru.ulstu.extractor.heuristic.component.ProgrammingLanguage;
import ru.ulstu.extractor.heuristic.model.StructuralUnit;
import ru.ulstu.extractor.heuristic.model.BusinessLogicUnit;
import ru.ulstu.extractor.heuristic.model.EntityUnit;
import ru.ulstu.extractor.heuristic.service.DetectorService;
import ru.ulstu.extractor.heuristic.service.ProgrammingLanguageService;
import ru.ulstu.extractor.util.StringUtils;
@ -24,11 +25,16 @@ import static ru.ulstu.extractor.heuristic.service.DetectorService.LangDetectScr
import static ru.ulstu.extractor.heuristic.service.DetectorService.LangDetectScrupulousness.LOW;
public abstract class StructuralUnitIdentifier {
public List<StructuralUnit> getEntityClasses(String projectPath, List<File> projectFiles, List<File> rootProjectFiles) {
public List<EntityUnit> getEntityClasses(String projectPath, List<File> projectFiles, List<File> rootProjectFiles) {
String subDirectory = getSourceDirectory(rootProjectFiles);
return getEntityClasses(projectPath, subDirectory, projectFiles);
}
public List<BusinessLogicUnit> getBusinessLogicClasses(String projectPath, List<File> projectFiles, List<File> rootProjectFiles) {
String subDirectory = getSourceDirectory(rootProjectFiles);
return getBusinessLogicClasses(projectPath, subDirectory, projectFiles);
}
public abstract boolean canAppliedToCode(String sourceCode);
public abstract boolean canAppliedToFile(File projectFile);
@ -39,15 +45,17 @@ public abstract class StructuralUnitIdentifier {
public abstract boolean isEntityClass(String sourceCode);
public abstract boolean isBusinessLogicClass(String sourceCode);
protected abstract boolean isBusinessLogicClass(File file);
public abstract List<StructuralUnit> getBusinessLogicClasses();
public abstract List<BusinessLogicUnit> getBusinessLogicClasses(String projectPath, String subDirectory, List<File> projectFiles);
public abstract boolean isMultiModuleProject();
public abstract Optional<BuildTool> getBuildTool(List<File> rootDirectoryFiles);
protected abstract List<StructuralUnit> getEntityClasses(String projectPath, String subDirectory, List<File> projectFiles);
protected abstract List<EntityUnit> getEntityClasses(String projectPath, String subDirectory, List<File> projectFiles);
protected abstract String getSourceDirectory(List<File> rootProjectFiles);

View File

@ -8,7 +8,8 @@ package ru.ulstu.extractor.heuristic.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import ru.ulstu.extractor.heuristic.model.StructuralUnit;
import ru.ulstu.extractor.heuristic.model.BusinessLogicUnit;
import ru.ulstu.extractor.heuristic.model.EntityUnit;
import ru.ulstu.extractor.heuristic.service.StructuralUnitService;
import ru.ulstu.extractor.service.GitRepositoryService;
@ -29,8 +30,14 @@ public class StructuralUnitController {
}
@GetMapping("get-entities")
public List<StructuralUnit> getEntities(String repositoryUrl) throws IOException {
public List<EntityUnit> getEntities(String repositoryUrl) throws IOException {
File rootPath = gitRepositoryService.getProjectDirectoryFile(repositoryUrl);
return structuralUnitService.getEntities(rootPath);
}
@GetMapping("get-business-logic")
public List<BusinessLogicUnit> getBusinessLogic(String repositoryUrl) throws IOException {
File rootPath = gitRepositoryService.getProjectDirectoryFile(repositoryUrl);
return structuralUnitService.getBusinessLogic(rootPath);
}
}

View File

@ -0,0 +1,15 @@
/*
* Copyright (C) 2021 Anton Romanov - All Rights Reserved
* You may use, distribute and modify this code, please write to: romanov73@gmail.com.
*/
package ru.ulstu.extractor.heuristic.model;
import java.io.File;
public class BusinessLogicUnit extends StructuralUnit {
public BusinessLogicUnit(String projectPath, File file) {
super(projectPath, file);
}
}

View File

@ -13,8 +13,8 @@ import ru.ulstu.extractor.heuristic.api.StructuralUnitIdentifier;
import ru.ulstu.extractor.heuristic.component.BuildTool;
import ru.ulstu.extractor.heuristic.component.JavaProgrammingLanguage;
import ru.ulstu.extractor.heuristic.component.ProgrammingLanguage;
import ru.ulstu.extractor.heuristic.model.BusinessLogicUnit;
import ru.ulstu.extractor.heuristic.model.EntityUnit;
import ru.ulstu.extractor.heuristic.model.StructuralUnit;
import ru.ulstu.extractor.util.StringUtils;
import java.io.File;
@ -29,6 +29,7 @@ import static ru.ulstu.extractor.heuristic.service.DetectorService.LangDetectScr
@Service
public class JavaIdentifier extends StructuralUnitIdentifier {
private static final String ENTITY_ANNOTATION = "@Entity";
private static final String SERVICE_ANNOTATION = "@Service";
private final DetectorService detectorService;
private final BuildToolService buildToolService;
private final ProgrammingLanguageService programmingLanguageService;
@ -56,7 +57,7 @@ public class JavaIdentifier extends StructuralUnitIdentifier {
}
@Override
public List<StructuralUnit> getEntityClasses(String projectPath, String subDirectory, List<File> projectFiles) {
public List<EntityUnit> getEntityClasses(String projectPath, String subDirectory, List<File> projectFiles) {
return projectFiles.stream()
.filter(file -> StringUtils.fileInSubdirectory(file.getPath(), projectPath, subDirectory))
.filter(this::isEntityClass)
@ -64,15 +65,14 @@ public class JavaIdentifier extends StructuralUnitIdentifier {
.collect(Collectors.toList());
}
protected Optional<ProgrammingLanguage> getMainProgrammingLanguage(String sourceCode) {
return sourceCodeContainsClass(sourceCode)
? Optional.of(getProgrammingLanguage())
: Optional.empty();
}
@Override
public List<StructuralUnit> getBusinessLogicClasses() {
return null;
public List<BusinessLogicUnit> getBusinessLogicClasses(String projectPath, String subDirectory, List<File> projectFiles) {
return projectFiles.stream()
.filter(file -> StringUtils.fileInSubdirectory(file.getPath(), projectPath, subDirectory))
.filter(this::isBusinessLogicClass)
.map(file -> new BusinessLogicUnit(projectPath, file))
.collect(Collectors.toList());
}
@Override
@ -85,6 +85,12 @@ public class JavaIdentifier extends StructuralUnitIdentifier {
return buildToolService.getProjectBuildTool(rootDirectoryFiles);
}
protected Optional<ProgrammingLanguage> getMainProgrammingLanguage(String sourceCode) {
return sourceCodeContainsClass(sourceCode)
? Optional.of(getProgrammingLanguage())
: Optional.empty();
}
@Override
protected DetectorService getDetectorService() {
return detectorService;
@ -120,6 +126,16 @@ public class JavaIdentifier extends StructuralUnitIdentifier {
return false;
}
@Override
public boolean isBusinessLogicClass(String sourceCode) {
try {
return classContainsAnnotation(sourceCode, SERVICE_ANNOTATION);
} catch (Exception ex) {
ex.printStackTrace();
}
return false;
}
private boolean classContainsAnnotation(File file, String annotationDeclaration) throws IOException {
return classContainsAnnotation(new String(Files.readAllBytes(file.toPath())), annotationDeclaration);
}
@ -152,6 +168,11 @@ public class JavaIdentifier extends StructuralUnitIdentifier {
@Override
protected boolean isBusinessLogicClass(File file) {
try {
return file.getName().endsWith("java") && classContainsAnnotation(file, SERVICE_ANNOTATION);
} catch (Exception ex) {
ex.printStackTrace();
}
return false;
}
}

View File

@ -7,7 +7,8 @@ package ru.ulstu.extractor.heuristic.service;
import org.springframework.stereotype.Service;
import ru.ulstu.extractor.heuristic.api.StructuralUnitIdentifier;
import ru.ulstu.extractor.heuristic.model.StructuralUnit;
import ru.ulstu.extractor.heuristic.model.BusinessLogicUnit;
import ru.ulstu.extractor.heuristic.model.EntityUnit;
import java.io.File;
import java.io.IOException;
@ -26,7 +27,7 @@ public class StructuralUnitService {
this.structuralUnitIdentifiers = structuralUnitIdentifiers;
}
public List<StructuralUnit> getEntities(File rootPath) throws IOException {
public List<EntityUnit> getEntities(File rootPath) throws IOException {
List<File> projectFiles = directoryService.getFilesRecursively(rootPath);
List<File> rootProjectFiles = directoryService.getDirectoryFiles(rootPath.toPath());
return getStructuralUnitIdentifier(
@ -38,6 +39,18 @@ public class StructuralUnitService {
.getEntityClasses(rootPath.getPath(), projectFiles, rootProjectFiles);
}
public List<BusinessLogicUnit> getBusinessLogic(File rootPath) throws IOException {
List<File> projectFiles = directoryService.getFilesRecursively(rootPath);
List<File> rootProjectFiles = directoryService.getDirectoryFiles(rootPath.toPath());
return getStructuralUnitIdentifier(
structuralUnitIdentifier -> structuralUnitIdentifier.canAppliedToProject(
rootPath.getPath(),
projectFiles,
rootProjectFiles))
.orElseThrow(() -> new RuntimeException("Identifier not found"))
.getBusinessLogicClasses(rootPath.getPath(), projectFiles, rootProjectFiles);
}
public boolean containsEntity(File projectFile) {
return getStructuralUnitIdentifier(
structuralUnitIdentifier -> structuralUnitIdentifier.canAppliedToFile(projectFile))
@ -57,4 +70,11 @@ public class StructuralUnitService {
.filter(predicate)
.findAny();
}
public boolean containsBusinessLogic(String sourceCode) {
return getStructuralUnitIdentifier(
structuralUnitIdentifier -> structuralUnitIdentifier.canAppliedToCode(sourceCode))
.map(identifier -> identifier.isBusinessLogicClass(sourceCode))
.orElse(false);
}
}

View File

@ -27,6 +27,8 @@ public class FileChange extends BaseEntity {
private Boolean containsEntity;
private Boolean containsBusinessLogic;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "file_change_id", unique = true)
@Fetch(FetchMode.SUBSELECT)
@ -86,4 +88,20 @@ public class FileChange extends BaseEntity {
public void setContainsEntity(boolean containsEntity) {
this.containsEntity = containsEntity;
}
public Boolean getContainsEntity() {
return containsEntity;
}
public void setContainsEntity(Boolean containsEntity) {
this.containsEntity = containsEntity;
}
public Boolean getContainsBusinessLogic() {
return containsBusinessLogic;
}
public void setContainsBusinessLogic(Boolean containsBusinessLogic) {
this.containsBusinessLogic = containsBusinessLogic;
}
}

View File

@ -206,6 +206,9 @@ public class GitRepositoryService {
fileChange.setContainsEntity(
structuralUnitService.containsEntity(getContent(repository, commit, maybeFileName.get()))
);
fileChange.setContainsBusinessLogic(
structuralUnitService.containsBusinessLogic(getContent(repository, commit, maybeFileName.get()))
);
/// вытащить другие изменения из коммита
changes.add(fileChange);
}

View File

@ -12,4 +12,9 @@
<column name="contains_entity" type="boolean"/>
</addColumn>
</changeSet>
<changeSet author="orion" id="20210419-100000-1">
<addColumn tableName="file_change">
<column name="contains_business_logic" type="boolean"/>
</addColumn>
</changeSet>
</databaseChangeLog>