#12 -- Add some parsing of java files

This commit is contained in:
Anton Romanov 2021-03-30 18:07:48 +04:00
parent c5e1ad8f37
commit 2ee965a113
10 changed files with 127 additions and 34 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.
*/
buildscript { buildscript {
ext { ext {
versionSpringBoot = '2.3.9.RELEASE' versionSpringBoot = '2.3.9.RELEASE'
@ -53,8 +58,9 @@ dependencies {
compile group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-hibernate5' compile group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-hibernate5'
compile group: 'org.postgresql', name: 'postgresql', version: '9.4.1212' compile group: 'org.postgresql', name: 'postgresql', version: '9.4.1212'
compile group: 'org.liquibase', name: 'liquibase-core', version: '4.3.1' 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: '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' compile group: 'io.springfox', name: 'springfox-boot-starter', version: '3.0.0'

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.heuristic.api; package ru.ulstu.extractor.heuristic.api;
import ru.ulstu.extractor.heuristic.model.BuildTool; 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.model.StructuralUnit;
import ru.ulstu.extractor.heuristic.service.DetectorService; import ru.ulstu.extractor.heuristic.service.DetectorService;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
public abstract class StructuralUnitIdentifier { public abstract class StructuralUnitIdentifier {
public abstract List<StructuralUnit> getEntityClasses(); public abstract List<StructuralUnit> getEntityClasses(List<File> projectFiles);
public abstract List<StructuralUnit> getBusinessLogicClasses(); public abstract List<StructuralUnit> getBusinessLogicClasses();
@ -21,7 +28,11 @@ public abstract class StructuralUnitIdentifier {
protected abstract ProgrammingLanguage getProgrammingLanguage(); protected abstract ProgrammingLanguage getProgrammingLanguage();
public Optional<ProgrammingLanguage> getMainProgrammingLanguage() { protected abstract boolean isEntityClass(File file) throws FileNotFoundException;
protected abstract boolean isBusinessLogicClass(File file);
protected Optional<ProgrammingLanguage> getMainProgrammingLanguage(List<File> projectFiles) {
String detectedLanguage = getDetectorService().getDetectedLanguage("package ru.ulstu.extractor.heuristic.service;"); String detectedLanguage = getDetectorService().getDetectedLanguage("package ru.ulstu.extractor.heuristic.service;");
ProgrammingLanguage programmingLanguage = getProgrammingLanguage(); ProgrammingLanguage programmingLanguage = getProgrammingLanguage();
return programmingLanguage.getName().equals(detectedLanguage) return programmingLanguage.getName().equals(detectedLanguage)
@ -29,5 +40,5 @@ public abstract class StructuralUnitIdentifier {
: Optional.empty(); : Optional.empty();
} }
public abstract boolean canAppliedToProject(); public abstract boolean canAppliedToProject(List<File> projectFiles);
} }

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.heuristic.controller; package ru.ulstu.extractor.heuristic.controller;
import org.springframework.web.bind.annotation.GetMapping; 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.model.StructuralUnit;
import ru.ulstu.extractor.heuristic.service.StructuralUnitService; import ru.ulstu.extractor.heuristic.service.StructuralUnitService;
import java.io.IOException;
import java.util.List; import java.util.List;
@RestController @RestController
@ -18,7 +24,7 @@ public class StructuralUnitController {
} }
@GetMapping("get-entities") @GetMapping("get-entities")
public List<StructuralUnit> getEntities() { public List<StructuralUnit> getEntities(String repositoryUrl) throws IOException {
return structuralUnitService.getEntities(); return structuralUnitService.getEntities(repositoryUrl);
} }
} }

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 EntityUnit extends StructuralUnit {
public EntityUnit(File file) {
super(file);
}
}

View File

@ -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; package ru.ulstu.extractor.heuristic.model;
import java.io.File;
public abstract class StructuralUnit { public abstract class StructuralUnit {
private String pathToFile; private String pathToFile;
private String moduleName; private String moduleName;
public StructuralUnit(File file) {
this.pathToFile = file.getPath();
}
public String getPathToFile() { public String getPathToFile() {
return pathToFile; return pathToFile;
} }

View File

@ -31,8 +31,8 @@ public class DirectoryService {
* @return список всех файлов * @return список всех файлов
* @throws IOException при возникновении исключения * @throws IOException при возникновении исключения
*/ */
public List<File> getFilesRecursively(@NotNull Path directory) throws IOException { public List<File> getFilesRecursively(@NotNull File directory) throws IOException {
return Files.find(directory, return Files.find(directory.toPath(),
Integer.MAX_VALUE, Integer.MAX_VALUE,
(filePath, fileAttr) -> fileAttr.isRegularFile()) (filePath, fileAttr) -> fileAttr.isRegularFile())
.map(Path::toFile) .map(Path::toFile)

View File

@ -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; 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 org.springframework.stereotype.Service;
import ru.ulstu.extractor.heuristic.api.StructuralUnitIdentifier; import ru.ulstu.extractor.heuristic.api.StructuralUnitIdentifier;
import ru.ulstu.extractor.heuristic.model.BuildTool; 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.JavaProgrammingLanguage;
import ru.ulstu.extractor.heuristic.model.ProgrammingLanguage; import ru.ulstu.extractor.heuristic.model.ProgrammingLanguage;
import ru.ulstu.extractor.heuristic.model.StructuralUnit; import ru.ulstu.extractor.heuristic.model.StructuralUnit;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
@Service @Service
public class JavaIdentifier extends StructuralUnitIdentifier { public class JavaIdentifier extends StructuralUnitIdentifier {
private final DetectorService detectorService; private final DetectorService detectorService;
public JavaIdentifier(DetectorService detectorService) { public JavaIdentifier(DetectorService detectorService) {
@ -19,14 +30,17 @@ public class JavaIdentifier extends StructuralUnitIdentifier {
} }
@Override @Override
public boolean canAppliedToProject() { public boolean canAppliedToProject(List<File> projectFiles) {
return /*getBuildTool() instanceof GradleBuildTool return /*getBuildTool() instanceof GradleBuildTool
&&*/ getMainProgrammingLanguage().orElse(null) instanceof JavaProgrammingLanguage; &&*/ getMainProgrammingLanguage(projectFiles).orElse(null) instanceof JavaProgrammingLanguage;
} }
@Override @Override
public List<StructuralUnit> getEntityClasses() { public List<StructuralUnit> getEntityClasses(List<File> projectFiles) {
return null; return projectFiles.stream()
.filter(this::isEntityClass)
.map(EntityUnit::new)
.collect(Collectors.toList());
} }
@Override @Override
@ -53,4 +67,29 @@ public class JavaIdentifier extends StructuralUnitIdentifier {
protected ProgrammingLanguage getProgrammingLanguage() { protected ProgrammingLanguage getProgrammingLanguage() {
return new JavaProgrammingLanguage(); 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<CompilationUnit> 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;
}
} }

View File

@ -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; package ru.ulstu.extractor.heuristic.service;
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.model.StructuralUnit; 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; import java.util.List;
@Service @Service
public class StructuralUnitService { public class StructuralUnitService {
private final GitRepositoryService gitRepositoryService;
private final DirectoryService directoryService;
private final List<StructuralUnitIdentifier> structuralUnitIdentifiers; private final List<StructuralUnitIdentifier> structuralUnitIdentifiers;
public StructuralUnitService(List<StructuralUnitIdentifier> structuralUnitIdentifiers) { public StructuralUnitService(GitRepositoryService gitRepositoryService,
DirectoryService directoryService,
List<StructuralUnitIdentifier> structuralUnitIdentifiers) {
this.gitRepositoryService = gitRepositoryService;
this.directoryService = directoryService;
this.structuralUnitIdentifiers = structuralUnitIdentifiers; this.structuralUnitIdentifiers = structuralUnitIdentifiers;
} }
public List<StructuralUnit> getEntities() { public List<StructuralUnit> getEntities(String repositoryUrl) throws IOException {
File rootPath = gitRepositoryService.getProjectDirectoryFile(repositoryUrl);
List<File> projectFiles = directoryService.getFilesRecursively(rootPath);
StructuralUnitIdentifier selectedIdentifier = structuralUnitIdentifiers.stream() StructuralUnitIdentifier selectedIdentifier = structuralUnitIdentifiers.stream()
.filter(StructuralUnitIdentifier::canAppliedToProject) .filter(structuralUnitIdentifier -> structuralUnitIdentifier.canAppliedToProject(projectFiles))
.findAny() .findAny()
.orElseThrow(() -> new RuntimeException("Identifier not found")); .orElseThrow(() -> new RuntimeException("Identifier not found"));
return selectedIdentifier.getEntityClasses(); return selectedIdentifier.getEntityClasses(projectFiles);
} }
} }

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.service; package ru.ulstu.extractor.service;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
@ -63,7 +68,7 @@ public class GitRepositoryService {
return getProjectDirectory(url) + "/.git"; return getProjectDirectory(url) + "/.git";
} }
private File getProjectDirectoryFile(String url) { public File getProjectDirectoryFile(String url) {
return Path.of(getProjectDirectory(url)) return Path.of(getProjectDirectory(url))
.toFile(); .toFile();
} }

View File

@ -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());
}
}