Merge branch '24-change-entity-methods' into 'master'
Resolve "Дополнить методы работающие с сущностью" Closes #24 See merge request romanov73/git-extractor!22
This commit is contained in:
commit
4b4194e188
@ -9,6 +9,7 @@ import ru.ulstu.extractor.heuristic.component.BuildTool;
|
||||
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.ResourceUnit;
|
||||
import ru.ulstu.extractor.heuristic.service.DetectorService;
|
||||
import ru.ulstu.extractor.heuristic.service.ProgrammingLanguageService;
|
||||
import ru.ulstu.extractor.util.StringUtils;
|
||||
@ -35,6 +36,11 @@ public abstract class StructuralUnitIdentifier {
|
||||
return getBusinessLogicClasses(projectPath, subDirectory, projectFiles);
|
||||
}
|
||||
|
||||
public List<ResourceUnit> getResourceClasses(String projectPath, List<File> projectFiles, List<File> rootProjectFiles) {
|
||||
String subDirectory = getSourceDirectory(rootProjectFiles);
|
||||
return getResourceClasses(projectPath, subDirectory, projectFiles);
|
||||
}
|
||||
|
||||
public abstract boolean canAppliedToCode(String sourceCode);
|
||||
|
||||
public abstract boolean canAppliedToFile(File projectFile);
|
||||
@ -100,4 +106,10 @@ public abstract class StructuralUnitIdentifier {
|
||||
fileContent,
|
||||
scrupulousness));
|
||||
}
|
||||
|
||||
protected abstract List<ResourceUnit> getResourceClasses(String projectPath, String subDirectory, List<File> projectFiles);
|
||||
|
||||
public abstract boolean isResourceClass(String sourceCode);
|
||||
|
||||
protected abstract boolean isResourceClass(File file);
|
||||
}
|
||||
|
@ -10,6 +10,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import ru.ulstu.extractor.heuristic.model.BusinessLogicUnit;
|
||||
import ru.ulstu.extractor.heuristic.model.EntityUnit;
|
||||
import ru.ulstu.extractor.heuristic.model.ResourceUnit;
|
||||
import ru.ulstu.extractor.heuristic.service.StructuralUnitService;
|
||||
import ru.ulstu.extractor.service.GitRepositoryService;
|
||||
|
||||
@ -40,4 +41,10 @@ public class StructuralUnitController {
|
||||
File rootPath = gitRepositoryService.getProjectDirectoryFile(repositoryUrl);
|
||||
return structuralUnitService.getBusinessLogic(rootPath);
|
||||
}
|
||||
|
||||
@GetMapping("get-resources")
|
||||
public List<ResourceUnit> getResource(String repositoryUrl) throws IOException {
|
||||
File rootPath = gitRepositoryService.getProjectDirectoryFile(repositoryUrl);
|
||||
return structuralUnitService.getResource(rootPath);
|
||||
}
|
||||
}
|
||||
|
@ -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 ResourceUnit extends StructuralUnit {
|
||||
|
||||
public ResourceUnit(String projectPath, File file) {
|
||||
super(projectPath, file);
|
||||
}
|
||||
}
|
@ -8,6 +8,9 @@ package ru.ulstu.extractor.heuristic.service;
|
||||
import com.github.javaparser.JavaParser;
|
||||
import com.github.javaparser.ParseResult;
|
||||
import com.github.javaparser.ast.CompilationUnit;
|
||||
import com.github.javaparser.ast.body.FieldDeclaration;
|
||||
import com.github.javaparser.ast.body.MethodDeclaration;
|
||||
import com.github.javaparser.ast.body.TypeDeclaration;
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.ulstu.extractor.heuristic.api.StructuralUnitIdentifier;
|
||||
import ru.ulstu.extractor.heuristic.component.BuildTool;
|
||||
@ -15,6 +18,7 @@ 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.ResourceUnit;
|
||||
import ru.ulstu.extractor.util.StringUtils;
|
||||
|
||||
import java.io.File;
|
||||
@ -106,10 +110,28 @@ public class JavaIdentifier extends StructuralUnitIdentifier {
|
||||
return new JavaProgrammingLanguage();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected List<ResourceUnit> getResourceClasses(String projectPath, String subDirectory, List<File> projectFiles) {
|
||||
return projectFiles.stream()
|
||||
.filter(file -> StringUtils.fileInSubdirectory(file.getPath(), projectPath, subDirectory))
|
||||
.filter(this::isResourceClass)
|
||||
.map(file -> new ResourceUnit(projectPath, file))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isResourceClass(String sourceCode) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEntityClass(File file) {
|
||||
try {
|
||||
return file.getName().endsWith("java") && classContainsAnnotation(file, ENTITY_ANNOTATION);
|
||||
return file.getName().endsWith("java")
|
||||
&& (classContainsGetSet(file)
|
||||
|| classContainsAnnotation(file, ENTITY_ANNOTATION)
|
||||
|| packageContains(file, "model")
|
||||
|| packageContains(file, "entity"));
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
@ -148,6 +170,7 @@ public class JavaIdentifier extends StructuralUnitIdentifier {
|
||||
.anyMatch(clazz -> clazz.getAnnotations().stream().anyMatch(annotation -> annotation.toString().startsWith(annotationDeclaration)));
|
||||
}
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
private boolean sourceCodeContainsClass(String sourceCode) {
|
||||
@ -175,4 +198,54 @@ public class JavaIdentifier extends StructuralUnitIdentifier {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean classContainsGetSet(File file) throws IOException {
|
||||
return classContainsGetSet(new String(Files.readAllBytes(file.toPath())));
|
||||
}
|
||||
|
||||
private boolean classContainsGetSet(String sourceCode) {
|
||||
JavaParser parser = new JavaParser();
|
||||
ParseResult<CompilationUnit> parseResult = parser.parse(sourceCode);
|
||||
if (parseResult.getResult().isPresent() && parseResult.getResult().get().findCompilationUnit().isPresent()) {
|
||||
for (TypeDeclaration<?> clazz : parseResult.getResult().get().getTypes()) {
|
||||
int countGetters = 0;
|
||||
int countSetters = 0;
|
||||
for (FieldDeclaration fieldDeclaration : clazz.getFields()) {
|
||||
for (MethodDeclaration methodDeclaration : clazz.getMethods()) {
|
||||
String fieldName = fieldDeclaration.getVariables().getFirst().map(v -> v.getName().toString()).orElse("").toUpperCase();
|
||||
if (("GET" + fieldName).equals(methodDeclaration.getName().toString().toUpperCase())) {
|
||||
countGetters++;
|
||||
}
|
||||
if (("SET" + fieldName).equals(methodDeclaration.getName().toString().toUpperCase())) {
|
||||
countSetters++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return clazz.getFields().size() == countGetters || clazz.getFields().size() == countSetters;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean packageContains(File file, String packageName) throws IOException {
|
||||
return packageContains(new String(Files.readAllBytes(file.toPath())), packageName);
|
||||
}
|
||||
|
||||
private boolean packageContains(String sourceCode, String packageName) {
|
||||
JavaParser parser = new JavaParser();
|
||||
ParseResult<CompilationUnit> parseResult = parser.parse(sourceCode);
|
||||
if (parseResult.getResult().isPresent() && parseResult.getResult().get().findCompilationUnit().isPresent()) {
|
||||
return parseResult.getResult().isPresent() && parseResult.getResult().get().getPackageDeclaration().isPresent() && parseResult.getResult().get().getPackageDeclaration().get().getName().toString().contains(packageName);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
protected boolean isResourceClass(File file) {
|
||||
try {
|
||||
return file.getName().endsWith("html") || file.getName().endsWith("css");
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -9,6 +9,7 @@ import org.springframework.stereotype.Service;
|
||||
import ru.ulstu.extractor.heuristic.api.StructuralUnitIdentifier;
|
||||
import ru.ulstu.extractor.heuristic.model.BusinessLogicUnit;
|
||||
import ru.ulstu.extractor.heuristic.model.EntityUnit;
|
||||
import ru.ulstu.extractor.heuristic.model.ResourceUnit;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
@ -77,4 +78,16 @@ public class StructuralUnitService {
|
||||
.map(identifier -> identifier.isBusinessLogicClass(sourceCode))
|
||||
.orElse(false);
|
||||
}
|
||||
|
||||
public List<ResourceUnit> getResource(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"))
|
||||
.getResourceClasses(rootPath.getPath(), projectFiles, rootProjectFiles);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user