#20 -- Get business logic classes

This commit is contained in:
Anton Romanov 2021-04-12 12:12:24 +04:00
parent b25a1dbab9
commit 9fcc14ae6f
5 changed files with 69 additions and 17 deletions

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);
@ -41,13 +47,13 @@ public abstract class StructuralUnitIdentifier {
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;
@ -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;
}
}

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))