#12 -- Fix components for spring

This commit is contained in:
Anton Romanov 2021-04-10 11:22:01 +04:00
parent cbd77dfb24
commit 03e2b7ac07
6 changed files with 96 additions and 24 deletions

View File

@ -9,10 +9,10 @@ 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.heuristic.service.ProgrammingLanguageService;
import ru.ulstu.extractor.util.StringUtils; import ru.ulstu.extractor.util.StringUtils;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.util.HashMap; import java.util.HashMap;
@ -20,15 +20,16 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import static ru.ulstu.extractor.heuristic.service.DetectorService.LangDetectScrupulousness.HIGH;
import static ru.ulstu.extractor.heuristic.service.DetectorService.LangDetectScrupulousness.LOW;
public abstract class StructuralUnitIdentifier { public abstract class StructuralUnitIdentifier {
public List<StructuralUnit> getEntityClasses(String projectPath, List<File> projectFiles, List<File> rootProjectFiles) { public List<StructuralUnit> getEntityClasses(String projectPath, List<File> projectFiles, List<File> rootProjectFiles) {
String subDirectory = getSourceDirectory(rootProjectFiles); String subDirectory = getSourceDirectory(rootProjectFiles);
return getEntityClasses(projectPath, subDirectory, projectFiles); return getEntityClasses(projectPath, subDirectory, projectFiles);
} }
protected abstract String getSourceDirectory(List<File> rootProjectFiles); public abstract boolean isEntityClass(File file);
public abstract List<StructuralUnit> getEntityClasses(String projectPath, String subDirectory, List<File> projectFiles);
public abstract List<StructuralUnit> getBusinessLogicClasses(); public abstract List<StructuralUnit> getBusinessLogicClasses();
@ -36,32 +37,33 @@ public abstract class StructuralUnitIdentifier {
public abstract Optional<BuildTool> getBuildTool(List<File> rootDirectoryFiles); public abstract Optional<BuildTool> getBuildTool(List<File> rootDirectoryFiles);
public abstract boolean canAppliedToProject(String projectPath, List<File> projectFiles, List<File> rootProjectFiles);
public abstract boolean canAppliedToFile(File projectFile);
protected abstract List<StructuralUnit> getEntityClasses(String projectPath, String subDirectory, List<File> projectFiles);
protected abstract String getSourceDirectory(List<File> rootProjectFiles);
protected abstract DetectorService getDetectorService(); protected abstract DetectorService getDetectorService();
protected abstract ProgrammingLanguage getProgrammingLanguage(); protected abstract ProgrammingLanguageService getProgrammingLanguageService();
protected abstract boolean isEntityClass(File file) throws FileNotFoundException; protected abstract ProgrammingLanguage getProgrammingLanguage();
protected abstract boolean isBusinessLogicClass(File file); protected abstract boolean isBusinessLogicClass(File file);
protected Optional<ProgrammingLanguage> getMainProgrammingLanguage(String projectPath, List<File> projectFiles, List<File> rootProjectFiles) { protected Optional<ProgrammingLanguage> getMainProgrammingLanguage(String projectPath, List<File> projectFiles, List<File> rootProjectFiles) {
String subDirectory = getSourceDirectory(rootProjectFiles); String subDirectory = getSourceDirectory(rootProjectFiles);
Map<String, Integer> projectFileLanguageFrequency = new HashMap<>(); Map<ProgrammingLanguage, Integer> projectFileLanguageFrequency = new HashMap<>();
projectFiles.stream() projectFiles.stream()
.filter(file -> StringUtils.fileInSubdirectory(file.getPath(), projectPath, subDirectory)) .filter(file -> StringUtils.fileInSubdirectory(file.getPath(), projectPath, subDirectory))
.forEach(projectFile -> { .forEach(projectFile -> {
try { Optional<ProgrammingLanguage> detectedLanguage = getMainProgrammingLanguage(projectFile, LOW);
String detectedLanguage = getDetectorService().getDetectedLanguage( detectedLanguage.ifPresent(programmingLanguage -> projectFileLanguageFrequency.put(programmingLanguage, projectFileLanguageFrequency.getOrDefault(programmingLanguage, 0) + 1));
projectFile.getName(),
new String(Files.readAllBytes(projectFile.toPath())),
DetectorService.LangDetectScrupulousness.LOW);
projectFileLanguageFrequency.put(detectedLanguage, projectFileLanguageFrequency.getOrDefault(detectedLanguage, 0) + 1);
} catch (IOException e) {
e.printStackTrace();
}
}); });
Optional<Map.Entry<String, Integer>> mostFrequentLanguageEntry = projectFileLanguageFrequency Optional<Map.Entry<ProgrammingLanguage, Integer>> mostFrequentLanguageEntry = projectFileLanguageFrequency
.entrySet() .entrySet()
.stream() .stream()
.max(Map.Entry.comparingByValue()); .max(Map.Entry.comparingByValue());
@ -69,11 +71,21 @@ public abstract class StructuralUnitIdentifier {
return Optional.empty(); return Optional.empty();
} }
ProgrammingLanguage programmingLanguage = getProgrammingLanguage(); return Optional.of(mostFrequentLanguageEntry.get().getKey());
return programmingLanguage.getName().equals(mostFrequentLanguageEntry.get().getKey())
? Optional.of(programmingLanguage)
: Optional.empty();
} }
public abstract boolean canAppliedToProject(String projectPath, List<File> projectFiles, List<File> rootProjectFiles); protected Optional<ProgrammingLanguage> getMainProgrammingLanguage(File projectFile, DetectorService.LangDetectScrupulousness scrupulousness) {
String fileContent = "";
if (scrupulousness == HIGH) {
try {
fileContent = new String(Files.readAllBytes(projectFile.toPath()));
} catch (IOException e) {
e.printStackTrace();
}
}
return getProgrammingLanguageService().getByName(getDetectorService().getDetectedLanguage(
projectFile.getName(),
fileContent,
scrupulousness));
}
} }

View File

@ -5,6 +5,9 @@
package ru.ulstu.extractor.heuristic.component; package ru.ulstu.extractor.heuristic.component;
import org.springframework.stereotype.Component;
@Component
public class JavaProgrammingLanguage extends ProgrammingLanguage { public class JavaProgrammingLanguage extends ProgrammingLanguage {
public JavaProgrammingLanguage() { public JavaProgrammingLanguage() {
super("java"); super("java");

View File

@ -5,6 +5,8 @@
package ru.ulstu.extractor.heuristic.component; package ru.ulstu.extractor.heuristic.component;
import java.util.Locale;
public abstract class ProgrammingLanguage { public abstract class ProgrammingLanguage {
private final String name; private final String name;
@ -15,4 +17,12 @@ public abstract class ProgrammingLanguage {
public String getName() { public String getName() {
return name; return name;
} }
public boolean canMappedByName(String programmingLanguageName) {
if (programmingLanguageName == null || programmingLanguageName.isBlank()) {
return false;
}
return getName().toLowerCase(Locale.ROOT).equals(programmingLanguageName.toLowerCase(Locale.ROOT))
|| programmingLanguageName.toLowerCase(Locale.ROOT).contains(getName().toLowerCase(Locale.ROOT));
}
} }

View File

@ -23,15 +23,20 @@ import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static ru.ulstu.extractor.heuristic.service.DetectorService.LangDetectScrupulousness.LOW;
@Service @Service
public class JavaIdentifier extends StructuralUnitIdentifier { public class JavaIdentifier extends StructuralUnitIdentifier {
private final DetectorService detectorService; private final DetectorService detectorService;
private final BuildToolService buildToolService; private final BuildToolService buildToolService;
private final ProgrammingLanguageService programmingLanguageService;
public JavaIdentifier(DetectorService detectorService, public JavaIdentifier(DetectorService detectorService,
BuildToolService buildToolService) { BuildToolService buildToolService,
ProgrammingLanguageService programmingLanguageService) {
this.detectorService = detectorService; this.detectorService = detectorService;
this.buildToolService = buildToolService; this.buildToolService = buildToolService;
this.programmingLanguageService = programmingLanguageService;
} }
@Override @Override
@ -40,6 +45,10 @@ public class JavaIdentifier extends StructuralUnitIdentifier {
&&*/ getMainProgrammingLanguage(projectPath, projectFiles, rootProjectFiles).orElse(null) instanceof JavaProgrammingLanguage; &&*/ getMainProgrammingLanguage(projectPath, projectFiles, rootProjectFiles).orElse(null) instanceof JavaProgrammingLanguage;
} }
public boolean canAppliedToFile(File projectFile) {
return getMainProgrammingLanguage(projectFile, LOW).orElse(null) instanceof JavaProgrammingLanguage;
}
@Override @Override
public List<StructuralUnit> getEntityClasses(String projectPath, String subDirectory, List<File> projectFiles) { public List<StructuralUnit> getEntityClasses(String projectPath, String subDirectory, List<File> projectFiles) {
return projectFiles.stream() return projectFiles.stream()
@ -69,13 +78,18 @@ public class JavaIdentifier extends StructuralUnitIdentifier {
return detectorService; return detectorService;
} }
@Override
protected ProgrammingLanguageService getProgrammingLanguageService() {
return programmingLanguageService;
}
@Override @Override
protected ProgrammingLanguage getProgrammingLanguage() { protected ProgrammingLanguage getProgrammingLanguage() {
return new JavaProgrammingLanguage(); return new JavaProgrammingLanguage();
} }
@Override @Override
protected boolean isEntityClass(File file) { public boolean isEntityClass(File file) {
try { try {
return file.getName().endsWith("java") && classContainsAnnotation(file, "@Entity"); return file.getName().endsWith("java") && classContainsAnnotation(file, "@Entity");
} catch (Exception ex) { } catch (Exception ex) {

View File

@ -0,0 +1,25 @@
/*
* 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.component.ProgrammingLanguage;
import java.util.List;
import java.util.Optional;
@Service
public class ProgrammingLanguageService {
private final List<ProgrammingLanguage> programmingLanguages;
public ProgrammingLanguageService(List<ProgrammingLanguage> programmingLanguages) {
this.programmingLanguages = programmingLanguages;
}
public Optional<ProgrammingLanguage> getByName(String programmingLanguageName) {
return programmingLanguages.stream().filter(lang -> lang.canMappedByName(programmingLanguageName)).findAny();
}
}

View File

@ -38,4 +38,12 @@ public class StructuralUnitService {
.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);
} }
public boolean containsEntity(File projectFile) {
StructuralUnitIdentifier selectedIdentifier = structuralUnitIdentifiers.stream()
.filter(structuralUnitIdentifier -> structuralUnitIdentifier.canAppliedToFile(projectFile))
.findAny()
.orElseThrow(() -> new RuntimeException("Identifier not found"));
return selectedIdentifier.isEntityClass(projectFile);
}
} }