#12 -- Fix directory methods

This commit is contained in:
Anton Romanov 2021-03-31 13:10:44 +04:00
parent 8d3e7f4e2e
commit f83d78e763
3 changed files with 19 additions and 27 deletions

View File

@ -9,6 +9,7 @@ import ru.ulstu.extractor.heuristic.component.BuildTool;
import ru.ulstu.extractor.heuristic.component.ProgrammingLanguage; import ru.ulstu.extractor.heuristic.component.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 ru.ulstu.extractor.util.StringUtils;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
@ -43,9 +44,12 @@ public abstract class StructuralUnitIdentifier {
protected abstract boolean isBusinessLogicClass(File file); protected abstract boolean isBusinessLogicClass(File file);
protected Optional<ProgrammingLanguage> getMainProgrammingLanguage(List<File> projectFiles) { protected Optional<ProgrammingLanguage> getMainProgrammingLanguage(String projectPath, List<File> projectFiles, List<File> rootProjectFiles) {
String subDirectory = getSourceDirectory(rootProjectFiles);
Map<String, Integer> projectFileLanguageFrequency = new HashMap<>(); Map<String, Integer> projectFileLanguageFrequency = new HashMap<>();
projectFiles.forEach(projectFile -> { projectFiles.stream()
.filter(file -> StringUtils.fileInSubdirectory(file.getPath(), projectPath, subDirectory))
.forEach(projectFile -> {
try { try {
String detectedLanguage = getDetectorService().getDetectedLanguage(new String(Files.readAllBytes(projectFile.toPath()))); String detectedLanguage = getDetectorService().getDetectedLanguage(new String(Files.readAllBytes(projectFile.toPath())));
projectFileLanguageFrequency.put(detectedLanguage, projectFileLanguageFrequency.getOrDefault(detectedLanguage, 0) + 1); projectFileLanguageFrequency.put(detectedLanguage, projectFileLanguageFrequency.getOrDefault(detectedLanguage, 0) + 1);
@ -68,5 +72,5 @@ public abstract class StructuralUnitIdentifier {
: Optional.empty(); : Optional.empty();
} }
public abstract boolean canAppliedToProject(List<File> projectFiles); public abstract boolean canAppliedToProject(String projectPath, List<File> projectFiles, List<File> rootProjectFiles);
} }

View File

@ -11,9 +11,7 @@ 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.component.BuildTool; import ru.ulstu.extractor.heuristic.component.BuildTool;
import ru.ulstu.extractor.heuristic.component.GradleBuildTool;
import ru.ulstu.extractor.heuristic.component.JavaProgrammingLanguage; import ru.ulstu.extractor.heuristic.component.JavaProgrammingLanguage;
import ru.ulstu.extractor.heuristic.component.MavenBuildTool;
import ru.ulstu.extractor.heuristic.component.ProgrammingLanguage; import ru.ulstu.extractor.heuristic.component.ProgrammingLanguage;
import ru.ulstu.extractor.heuristic.model.EntityUnit; import ru.ulstu.extractor.heuristic.model.EntityUnit;
import ru.ulstu.extractor.heuristic.model.StructuralUnit; import ru.ulstu.extractor.heuristic.model.StructuralUnit;
@ -37,9 +35,9 @@ public class JavaIdentifier extends StructuralUnitIdentifier {
} }
@Override @Override
public boolean canAppliedToProject(List<File> projectFiles) { public boolean canAppliedToProject(String projectPath, List<File> projectFiles, List<File> rootProjectFiles) {
return /*getBuildTool() instanceof GradleBuildTool return /*getBuildTool() instanceof GradleBuildTool
&&*/ getMainProgrammingLanguage(projectFiles).orElse(null) instanceof JavaProgrammingLanguage; &&*/ getMainProgrammingLanguage(projectPath, projectFiles, rootProjectFiles).orElse(null) instanceof JavaProgrammingLanguage;
} }
@Override @Override
@ -62,18 +60,8 @@ public class JavaIdentifier extends StructuralUnitIdentifier {
} }
@Override @Override
public Optional getBuildTool(List<File> rootDirectoryFiles) { public Optional<BuildTool> getBuildTool(List<File> rootDirectoryFiles) {
//Todo: refactoring is highly recommended return buildToolService.getProjectBuildTool(rootDirectoryFiles);
Optional<GradleBuildTool> maybeGradleBuildTool = rootDirectoryFiles.stream()
.filter(file -> file.getName().equals("build.gradle"))
.findAny().map(file -> new GradleBuildTool());
if (maybeGradleBuildTool.isPresent()) {
return maybeGradleBuildTool;
}
return rootDirectoryFiles.stream()
.filter(file -> file.getName().equals("pom.xml"))
.findAny().map(file -> new MavenBuildTool());
} }
@Override @Override

View File

@ -33,7 +33,7 @@ public class StructuralUnitService {
List<File> projectFiles = directoryService.getFilesRecursively(rootPath); List<File> projectFiles = directoryService.getFilesRecursively(rootPath);
List<File> rootProjectFiles = directoryService.getDirectoryFiles(rootPath.toPath()); List<File> rootProjectFiles = directoryService.getDirectoryFiles(rootPath.toPath());
StructuralUnitIdentifier selectedIdentifier = structuralUnitIdentifiers.stream() StructuralUnitIdentifier selectedIdentifier = structuralUnitIdentifiers.stream()
.filter(structuralUnitIdentifier -> structuralUnitIdentifier.canAppliedToProject(projectFiles)) .filter(structuralUnitIdentifier -> structuralUnitIdentifier.canAppliedToProject(rootPath.getPath(), projectFiles, rootProjectFiles))
.findAny() .findAny()
.orElseThrow(() -> new RuntimeException("Identifier not found")); .orElseThrow(() -> new RuntimeException("Identifier not found"));
return selectedIdentifier.getEntityClasses(rootPath.getPath(), projectFiles, rootProjectFiles); return selectedIdentifier.getEntityClasses(rootPath.getPath(), projectFiles, rootProjectFiles);