From 2ee965a11373d32c321018542678b159b39d7651 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Tue, 30 Mar 2021 18:07:48 +0400 Subject: [PATCH] #12 -- Add some parsing of java files --- build.gradle | 8 ++- .../api/StructuralUnitIdentifier.java | 17 +++++-- .../controller/StructuralUnitController.java | 10 +++- .../extractor/heuristic/model/EntityUnit.java | 15 ++++++ .../heuristic/model/StructuralUnit.java | 11 +++++ .../heuristic/service/DirectoryService.java | 4 +- .../heuristic/service/JavaIdentifier.java | 49 +++++++++++++++++-- .../service/StructuralUnitService.java | 24 +++++++-- .../service/GitRepositoryService.java | 7 ++- .../ru/ulstu/test/HeuristicServiceTest.java | 16 ------ 10 files changed, 127 insertions(+), 34 deletions(-) create mode 100644 src/main/java/ru/ulstu/extractor/heuristic/model/EntityUnit.java delete mode 100644 src/test/java/ru/ulstu/test/HeuristicServiceTest.java diff --git a/build.gradle b/build.gradle index bd1edfe..f1c0e32 100644 --- a/build.gradle +++ b/build.gradle @@ -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. + */ + buildscript { ext { versionSpringBoot = '2.3.9.RELEASE' @@ -53,8 +58,9 @@ dependencies { compile group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-hibernate5' compile group: 'org.postgresql', name: 'postgresql', version: '9.4.1212' compile group: 'org.liquibase', name: 'liquibase-core', version: '4.3.1' - implementation group: 'commons-io', name: 'commons-io', version: '2.6' + compile group: 'commons-io', name: 'commons-io', version: '2.6' compile group: 'net.sourceforge.htmlunit', name: 'htmlunit', version: '2.35.0' + compile group: 'com.github.javaparser', name: 'javaparser-core', version: '3.20.2' compile group: 'io.springfox', name: 'springfox-boot-starter', version: '3.0.0' 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 7c3d359..2f95002 100644 --- a/src/main/java/ru/ulstu/extractor/heuristic/api/StructuralUnitIdentifier.java +++ b/src/main/java/ru/ulstu/extractor/heuristic/api/StructuralUnitIdentifier.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.heuristic.api; import ru.ulstu.extractor.heuristic.model.BuildTool; @@ -5,11 +10,13 @@ import ru.ulstu.extractor.heuristic.model.ProgrammingLanguage; import ru.ulstu.extractor.heuristic.model.StructuralUnit; import ru.ulstu.extractor.heuristic.service.DetectorService; +import java.io.File; +import java.io.FileNotFoundException; import java.util.List; import java.util.Optional; public abstract class StructuralUnitIdentifier { - public abstract List getEntityClasses(); + public abstract List getEntityClasses(List projectFiles); public abstract List getBusinessLogicClasses(); @@ -21,7 +28,11 @@ public abstract class StructuralUnitIdentifier { protected abstract ProgrammingLanguage getProgrammingLanguage(); - public Optional getMainProgrammingLanguage() { + protected abstract boolean isEntityClass(File file) throws FileNotFoundException; + + protected abstract boolean isBusinessLogicClass(File file); + + protected Optional getMainProgrammingLanguage(List projectFiles) { String detectedLanguage = getDetectorService().getDetectedLanguage("package ru.ulstu.extractor.heuristic.service;"); ProgrammingLanguage programmingLanguage = getProgrammingLanguage(); return programmingLanguage.getName().equals(detectedLanguage) @@ -29,5 +40,5 @@ public abstract class StructuralUnitIdentifier { : Optional.empty(); } - public abstract boolean canAppliedToProject(); + public abstract boolean canAppliedToProject(List projectFiles); } 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 2467361..96b9e14 100644 --- a/src/main/java/ru/ulstu/extractor/heuristic/controller/StructuralUnitController.java +++ b/src/main/java/ru/ulstu/extractor/heuristic/controller/StructuralUnitController.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.heuristic.controller; import org.springframework.web.bind.annotation.GetMapping; @@ -6,6 +11,7 @@ import org.springframework.web.bind.annotation.RestController; import ru.ulstu.extractor.heuristic.model.StructuralUnit; import ru.ulstu.extractor.heuristic.service.StructuralUnitService; +import java.io.IOException; import java.util.List; @RestController @@ -18,7 +24,7 @@ public class StructuralUnitController { } @GetMapping("get-entities") - public List getEntities() { - return structuralUnitService.getEntities(); + public List getEntities(String repositoryUrl) throws IOException { + return structuralUnitService.getEntities(repositoryUrl); } } diff --git a/src/main/java/ru/ulstu/extractor/heuristic/model/EntityUnit.java b/src/main/java/ru/ulstu/extractor/heuristic/model/EntityUnit.java new file mode 100644 index 0000000..5cfafad --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/heuristic/model/EntityUnit.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 EntityUnit extends StructuralUnit { + + public EntityUnit(File file) { + super(file); + } +} diff --git a/src/main/java/ru/ulstu/extractor/heuristic/model/StructuralUnit.java b/src/main/java/ru/ulstu/extractor/heuristic/model/StructuralUnit.java index 64ce9b2..4f6baa1 100644 --- a/src/main/java/ru/ulstu/extractor/heuristic/model/StructuralUnit.java +++ b/src/main/java/ru/ulstu/extractor/heuristic/model/StructuralUnit.java @@ -1,9 +1,20 @@ +/* + * 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 abstract class StructuralUnit { private String pathToFile; private String moduleName; + public StructuralUnit(File file) { + this.pathToFile = file.getPath(); + } + public String getPathToFile() { return pathToFile; } diff --git a/src/main/java/ru/ulstu/extractor/heuristic/service/DirectoryService.java b/src/main/java/ru/ulstu/extractor/heuristic/service/DirectoryService.java index 6de3c6c..c2d78e8 100644 --- a/src/main/java/ru/ulstu/extractor/heuristic/service/DirectoryService.java +++ b/src/main/java/ru/ulstu/extractor/heuristic/service/DirectoryService.java @@ -31,8 +31,8 @@ public class DirectoryService { * @return список всех файлов * @throws IOException при возникновении исключения */ - public List getFilesRecursively(@NotNull Path directory) throws IOException { - return Files.find(directory, + public List getFilesRecursively(@NotNull File directory) throws IOException { + return Files.find(directory.toPath(), Integer.MAX_VALUE, (filePath, fileAttr) -> fileAttr.isRegularFile()) .map(Path::toFile) 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 cefa083..1ed34e9 100644 --- a/src/main/java/ru/ulstu/extractor/heuristic/service/JavaIdentifier.java +++ b/src/main/java/ru/ulstu/extractor/heuristic/service/JavaIdentifier.java @@ -1,17 +1,28 @@ +/* + * 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.service; +import com.github.javaparser.JavaParser; +import com.github.javaparser.ParseResult; +import com.github.javaparser.ast.CompilationUnit; import org.springframework.stereotype.Service; import ru.ulstu.extractor.heuristic.api.StructuralUnitIdentifier; import ru.ulstu.extractor.heuristic.model.BuildTool; +import ru.ulstu.extractor.heuristic.model.EntityUnit; import ru.ulstu.extractor.heuristic.model.JavaProgrammingLanguage; import ru.ulstu.extractor.heuristic.model.ProgrammingLanguage; import ru.ulstu.extractor.heuristic.model.StructuralUnit; +import java.io.File; +import java.io.FileNotFoundException; import java.util.List; +import java.util.stream.Collectors; @Service public class JavaIdentifier extends StructuralUnitIdentifier { - private final DetectorService detectorService; public JavaIdentifier(DetectorService detectorService) { @@ -19,14 +30,17 @@ public class JavaIdentifier extends StructuralUnitIdentifier { } @Override - public boolean canAppliedToProject() { + public boolean canAppliedToProject(List projectFiles) { return /*getBuildTool() instanceof GradleBuildTool - &&*/ getMainProgrammingLanguage().orElse(null) instanceof JavaProgrammingLanguage; + &&*/ getMainProgrammingLanguage(projectFiles).orElse(null) instanceof JavaProgrammingLanguage; } @Override - public List getEntityClasses() { - return null; + public List getEntityClasses(List projectFiles) { + return projectFiles.stream() + .filter(this::isEntityClass) + .map(EntityUnit::new) + .collect(Collectors.toList()); } @Override @@ -53,4 +67,29 @@ public class JavaIdentifier extends StructuralUnitIdentifier { protected ProgrammingLanguage getProgrammingLanguage() { return new JavaProgrammingLanguage(); } + + @Override + protected boolean isEntityClass(File file) { + try { + return file.getName().endsWith("java") && classContainsAnnotation(file, "@Entity"); + } catch (Exception ex) { + ex.printStackTrace(); + } + return false; + } + + private boolean classContainsAnnotation(File file, String annotationDeclaration) throws FileNotFoundException { + JavaParser parser = new JavaParser(); + ParseResult parseResult = parser.parse(file); + if (parseResult.getResult().isPresent() && parseResult.getResult().get().findCompilationUnit().isPresent()) { + return parseResult.getResult().get().getTypes().stream() + .anyMatch(clazz -> clazz.getAnnotations().stream().anyMatch(annotation -> annotation.getName().toString().equals(annotationDeclaration))); + } + return false; + } + + @Override + protected boolean isBusinessLogicClass(File file) { + 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 7cbf8dd..1a18899 100644 --- a/src/main/java/ru/ulstu/extractor/heuristic/service/StructuralUnitService.java +++ b/src/main/java/ru/ulstu/extractor/heuristic/service/StructuralUnitService.java @@ -1,24 +1,40 @@ +/* + * 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.service; import org.springframework.stereotype.Service; import ru.ulstu.extractor.heuristic.api.StructuralUnitIdentifier; import ru.ulstu.extractor.heuristic.model.StructuralUnit; +import ru.ulstu.extractor.service.GitRepositoryService; +import java.io.File; +import java.io.IOException; import java.util.List; @Service public class StructuralUnitService { + private final GitRepositoryService gitRepositoryService; + private final DirectoryService directoryService; private final List structuralUnitIdentifiers; - public StructuralUnitService(List structuralUnitIdentifiers) { + public StructuralUnitService(GitRepositoryService gitRepositoryService, + DirectoryService directoryService, + List structuralUnitIdentifiers) { + this.gitRepositoryService = gitRepositoryService; + this.directoryService = directoryService; this.structuralUnitIdentifiers = structuralUnitIdentifiers; } - public List getEntities() { + public List getEntities(String repositoryUrl) throws IOException { + File rootPath = gitRepositoryService.getProjectDirectoryFile(repositoryUrl); + List projectFiles = directoryService.getFilesRecursively(rootPath); StructuralUnitIdentifier selectedIdentifier = structuralUnitIdentifiers.stream() - .filter(StructuralUnitIdentifier::canAppliedToProject) + .filter(structuralUnitIdentifier -> structuralUnitIdentifier.canAppliedToProject(projectFiles)) .findAny() .orElseThrow(() -> new RuntimeException("Identifier not found")); - return selectedIdentifier.getEntityClasses(); + return selectedIdentifier.getEntityClasses(projectFiles); } } diff --git a/src/main/java/ru/ulstu/extractor/service/GitRepositoryService.java b/src/main/java/ru/ulstu/extractor/service/GitRepositoryService.java index 558b179..419817d 100644 --- a/src/main/java/ru/ulstu/extractor/service/GitRepositoryService.java +++ b/src/main/java/ru/ulstu/extractor/service/GitRepositoryService.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.service; import org.apache.commons.io.FileUtils; @@ -63,7 +68,7 @@ public class GitRepositoryService { return getProjectDirectory(url) + "/.git"; } - private File getProjectDirectoryFile(String url) { + public File getProjectDirectoryFile(String url) { return Path.of(getProjectDirectory(url)) .toFile(); } diff --git a/src/test/java/ru/ulstu/test/HeuristicServiceTest.java b/src/test/java/ru/ulstu/test/HeuristicServiceTest.java deleted file mode 100644 index 10f6499..0000000 --- a/src/test/java/ru/ulstu/test/HeuristicServiceTest.java +++ /dev/null @@ -1,16 +0,0 @@ -package ru.ulstu.test; - -import org.junit.Assert; -import org.junit.Test; -import ru.ulstu.extractor.heuristic.model.JavaProgrammingLanguage; -import ru.ulstu.extractor.heuristic.service.DetectorService; -import ru.ulstu.extractor.heuristic.service.JavaIdentifier; - -public class HeuristicServiceTest { - private final JavaIdentifier javaIdentifier = new JavaIdentifier(new DetectorService()); - - @Test - public void testMainProgrammingLanguage() { - Assert.assertEquals(javaIdentifier.getMainProgrammingLanguage().orElse(null), new JavaProgrammingLanguage()); - } -} \ No newline at end of file