From 9fcc14ae6f15429b2db28a2c6fc3e3589e679ade Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Mon, 12 Apr 2021 12:12:24 +0400 Subject: [PATCH 1/3] #20 -- Get business logic classes --- .../api/StructuralUnitIdentifier.java | 14 ++++++--- .../controller/StructuralUnitController.java | 11 +++++-- .../heuristic/model/BusinessLogicUnit.java | 15 ++++++++++ .../heuristic/service/JavaIdentifier.java | 29 +++++++++++++------ .../service/StructuralUnitService.java | 17 +++++++++-- 5 files changed, 69 insertions(+), 17 deletions(-) create mode 100644 src/main/java/ru/ulstu/extractor/heuristic/model/BusinessLogicUnit.java 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 5956542..9ea3355 100644 --- a/src/main/java/ru/ulstu/extractor/heuristic/api/StructuralUnitIdentifier.java +++ b/src/main/java/ru/ulstu/extractor/heuristic/api/StructuralUnitIdentifier.java @@ -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 getEntityClasses(String projectPath, List projectFiles, List rootProjectFiles) { + public List getEntityClasses(String projectPath, List projectFiles, List rootProjectFiles) { String subDirectory = getSourceDirectory(rootProjectFiles); return getEntityClasses(projectPath, subDirectory, projectFiles); } + public List getBusinessLogicClasses(String projectPath, List projectFiles, List rootProjectFiles) { + String subDirectory = getSourceDirectory(rootProjectFiles); + return getBusinessLogicClasses(projectPath, subDirectory, projectFiles); + } + public abstract boolean canAppliedToCode(String sourceCode); public abstract boolean canAppliedToFile(File projectFile); @@ -41,13 +47,13 @@ public abstract class StructuralUnitIdentifier { protected abstract boolean isBusinessLogicClass(File file); - public abstract List getBusinessLogicClasses(); + public abstract List getBusinessLogicClasses(String projectPath, String subDirectory, List projectFiles); public abstract boolean isMultiModuleProject(); public abstract Optional getBuildTool(List rootDirectoryFiles); - protected abstract List getEntityClasses(String projectPath, String subDirectory, List projectFiles); + protected abstract List getEntityClasses(String projectPath, String subDirectory, List projectFiles); protected abstract String getSourceDirectory(List rootProjectFiles); diff --git a/src/main/java/ru/ulstu/extractor/heuristic/controller/StructuralUnitController.java b/src/main/java/ru/ulstu/extractor/heuristic/controller/StructuralUnitController.java index ff9b7fa..7024d56 100644 --- a/src/main/java/ru/ulstu/extractor/heuristic/controller/StructuralUnitController.java +++ b/src/main/java/ru/ulstu/extractor/heuristic/controller/StructuralUnitController.java @@ -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 getEntities(String repositoryUrl) throws IOException { + public List getEntities(String repositoryUrl) throws IOException { File rootPath = gitRepositoryService.getProjectDirectoryFile(repositoryUrl); return structuralUnitService.getEntities(rootPath); } + + @GetMapping("get-business-logic") + public List getBusinessLogic(String repositoryUrl) throws IOException { + File rootPath = gitRepositoryService.getProjectDirectoryFile(repositoryUrl); + return structuralUnitService.getBusinessLogic(rootPath); + } } diff --git a/src/main/java/ru/ulstu/extractor/heuristic/model/BusinessLogicUnit.java b/src/main/java/ru/ulstu/extractor/heuristic/model/BusinessLogicUnit.java new file mode 100644 index 0000000..9d8e132 --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/heuristic/model/BusinessLogicUnit.java @@ -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); + } +} 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 438ed33..ef2a70a 100644 --- a/src/main/java/ru/ulstu/extractor/heuristic/service/JavaIdentifier.java +++ b/src/main/java/ru/ulstu/extractor/heuristic/service/JavaIdentifier.java @@ -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 getEntityClasses(String projectPath, String subDirectory, List projectFiles) { + public List getEntityClasses(String projectPath, String subDirectory, List 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 getMainProgrammingLanguage(String sourceCode) { - return sourceCodeContainsClass(sourceCode) - ? Optional.of(getProgrammingLanguage()) - : Optional.empty(); - } @Override - public List getBusinessLogicClasses() { - return null; + public List getBusinessLogicClasses(String projectPath, String subDirectory, List 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 getMainProgrammingLanguage(String sourceCode) { + return sourceCodeContainsClass(sourceCode) + ? Optional.of(getProgrammingLanguage()) + : Optional.empty(); + } + @Override protected DetectorService getDetectorService() { return detectorService; @@ -152,6 +158,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; } } diff --git a/src/main/java/ru/ulstu/extractor/heuristic/service/StructuralUnitService.java b/src/main/java/ru/ulstu/extractor/heuristic/service/StructuralUnitService.java index c7bdfcc..83dc150 100644 --- a/src/main/java/ru/ulstu/extractor/heuristic/service/StructuralUnitService.java +++ b/src/main/java/ru/ulstu/extractor/heuristic/service/StructuralUnitService.java @@ -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 getEntities(File rootPath) throws IOException { + public List getEntities(File rootPath) throws IOException { List projectFiles = directoryService.getFilesRecursively(rootPath); List rootProjectFiles = directoryService.getDirectoryFiles(rootPath.toPath()); return getStructuralUnitIdentifier( @@ -38,6 +39,18 @@ public class StructuralUnitService { .getEntityClasses(rootPath.getPath(), projectFiles, rootProjectFiles); } + public List getBusinessLogic(File rootPath) throws IOException { + List projectFiles = directoryService.getFilesRecursively(rootPath); + List 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)) From 7a4e6d8fa1f0929d3543b537d0a3f1e607d27dcf Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Mon, 19 Apr 2021 13:24:49 +0400 Subject: [PATCH 2/3] #20 -- Fix docs --- .../extractor/config/GlobalDefaultExceptionHandler.java | 7 +++++++ .../ru/ulstu/extractor/controller/BranchController.java | 2 ++ .../ulstu/extractor/controller/GitFilteringController.java | 2 ++ .../ulstu/extractor/controller/GitIndexingController.java | 2 ++ .../ulstu/extractor/controller/RepositoryController.java | 2 ++ .../ru/ulstu/extractor/controller/StatisticController.java | 2 ++ 6 files changed, 17 insertions(+) diff --git a/src/main/java/ru/ulstu/extractor/config/GlobalDefaultExceptionHandler.java b/src/main/java/ru/ulstu/extractor/config/GlobalDefaultExceptionHandler.java index 49f1501..c2df5cd 100644 --- a/src/main/java/ru/ulstu/extractor/config/GlobalDefaultExceptionHandler.java +++ b/src/main/java/ru/ulstu/extractor/config/GlobalDefaultExceptionHandler.java @@ -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"; diff --git a/src/main/java/ru/ulstu/extractor/controller/BranchController.java b/src/main/java/ru/ulstu/extractor/controller/BranchController.java index 031dc48..4006edd 100644 --- a/src/main/java/ru/ulstu/extractor/controller/BranchController.java +++ b/src/main/java/ru/ulstu/extractor/controller/BranchController.java @@ -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; diff --git a/src/main/java/ru/ulstu/extractor/controller/GitFilteringController.java b/src/main/java/ru/ulstu/extractor/controller/GitFilteringController.java index e172698..b58c1fc 100644 --- a/src/main/java/ru/ulstu/extractor/controller/GitFilteringController.java +++ b/src/main/java/ru/ulstu/extractor/controller/GitFilteringController.java @@ -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; diff --git a/src/main/java/ru/ulstu/extractor/controller/GitIndexingController.java b/src/main/java/ru/ulstu/extractor/controller/GitIndexingController.java index fb6f694..ce2433f 100644 --- a/src/main/java/ru/ulstu/extractor/controller/GitIndexingController.java +++ b/src/main/java/ru/ulstu/extractor/controller/GitIndexingController.java @@ -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; diff --git a/src/main/java/ru/ulstu/extractor/controller/RepositoryController.java b/src/main/java/ru/ulstu/extractor/controller/RepositoryController.java index 9401504..31befc0 100644 --- a/src/main/java/ru/ulstu/extractor/controller/RepositoryController.java +++ b/src/main/java/ru/ulstu/extractor/controller/RepositoryController.java @@ -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; diff --git a/src/main/java/ru/ulstu/extractor/controller/StatisticController.java b/src/main/java/ru/ulstu/extractor/controller/StatisticController.java index 55f1f33..20910fa 100644 --- a/src/main/java/ru/ulstu/extractor/controller/StatisticController.java +++ b/src/main/java/ru/ulstu/extractor/controller/StatisticController.java @@ -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; From 37839920c0b565e4b0908fd35855c5cd1fce4913 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Mon, 19 Apr 2021 13:45:21 +0400 Subject: [PATCH 3/3] #20 -- Add database business logic field --- .../api/StructuralUnitIdentifier.java | 2 ++ .../heuristic/service/JavaIdentifier.java | 10 ++++++++++ .../service/StructuralUnitService.java | 7 +++++++ .../ru/ulstu/extractor/model/FileChange.java | 18 ++++++++++++++++++ .../service/GitRepositoryService.java | 3 +++ .../db/changelog-20210412_100000-schema.xml | 5 +++++ 6 files changed, 45 insertions(+) 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 9ea3355..cfb7188 100644 --- a/src/main/java/ru/ulstu/extractor/heuristic/api/StructuralUnitIdentifier.java +++ b/src/main/java/ru/ulstu/extractor/heuristic/api/StructuralUnitIdentifier.java @@ -45,6 +45,8 @@ 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 getBusinessLogicClasses(String projectPath, String subDirectory, List projectFiles); 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 ef2a70a..c446fd1 100644 --- a/src/main/java/ru/ulstu/extractor/heuristic/service/JavaIdentifier.java +++ b/src/main/java/ru/ulstu/extractor/heuristic/service/JavaIdentifier.java @@ -126,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); } diff --git a/src/main/java/ru/ulstu/extractor/heuristic/service/StructuralUnitService.java b/src/main/java/ru/ulstu/extractor/heuristic/service/StructuralUnitService.java index 83dc150..4070c10 100644 --- a/src/main/java/ru/ulstu/extractor/heuristic/service/StructuralUnitService.java +++ b/src/main/java/ru/ulstu/extractor/heuristic/service/StructuralUnitService.java @@ -70,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); + } } diff --git a/src/main/java/ru/ulstu/extractor/model/FileChange.java b/src/main/java/ru/ulstu/extractor/model/FileChange.java index 2a34ff3..80d1b8f 100644 --- a/src/main/java/ru/ulstu/extractor/model/FileChange.java +++ b/src/main/java/ru/ulstu/extractor/model/FileChange.java @@ -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; + } } diff --git a/src/main/java/ru/ulstu/extractor/service/GitRepositoryService.java b/src/main/java/ru/ulstu/extractor/service/GitRepositoryService.java index 0cc4f15..de16237 100644 --- a/src/main/java/ru/ulstu/extractor/service/GitRepositoryService.java +++ b/src/main/java/ru/ulstu/extractor/service/GitRepositoryService.java @@ -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); } diff --git a/src/main/resources/db/changelog-20210412_100000-schema.xml b/src/main/resources/db/changelog-20210412_100000-schema.xml index 38ffdc7..83f9844 100644 --- a/src/main/resources/db/changelog-20210412_100000-schema.xml +++ b/src/main/resources/db/changelog-20210412_100000-schema.xml @@ -12,4 +12,9 @@ + + + + + \ No newline at end of file