|
|
@ -8,6 +8,9 @@ package ru.ulstu.extractor.heuristic.service;
|
|
|
|
import com.github.javaparser.JavaParser;
|
|
|
|
import com.github.javaparser.JavaParser;
|
|
|
|
import com.github.javaparser.ParseResult;
|
|
|
|
import com.github.javaparser.ParseResult;
|
|
|
|
import com.github.javaparser.ast.CompilationUnit;
|
|
|
|
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 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;
|
|
|
@ -15,6 +18,7 @@ import ru.ulstu.extractor.heuristic.component.JavaProgrammingLanguage;
|
|
|
|
import ru.ulstu.extractor.heuristic.component.ProgrammingLanguage;
|
|
|
|
import ru.ulstu.extractor.heuristic.component.ProgrammingLanguage;
|
|
|
|
import ru.ulstu.extractor.heuristic.model.BusinessLogicUnit;
|
|
|
|
import ru.ulstu.extractor.heuristic.model.BusinessLogicUnit;
|
|
|
|
import ru.ulstu.extractor.heuristic.model.EntityUnit;
|
|
|
|
import ru.ulstu.extractor.heuristic.model.EntityUnit;
|
|
|
|
|
|
|
|
import ru.ulstu.extractor.heuristic.model.ResourceUnit;
|
|
|
|
import ru.ulstu.extractor.util.StringUtils;
|
|
|
|
import ru.ulstu.extractor.util.StringUtils;
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.File;
|
|
|
@ -106,10 +110,28 @@ public class JavaIdentifier extends StructuralUnitIdentifier {
|
|
|
|
return new JavaProgrammingLanguage();
|
|
|
|
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
|
|
|
|
@Override
|
|
|
|
public boolean isEntityClass(File file) {
|
|
|
|
public boolean isEntityClass(File file) {
|
|
|
|
try {
|
|
|
|
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) {
|
|
|
|
} catch (Exception ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -148,6 +170,7 @@ public class JavaIdentifier extends StructuralUnitIdentifier {
|
|
|
|
.anyMatch(clazz -> clazz.getAnnotations().stream().anyMatch(annotation -> annotation.toString().startsWith(annotationDeclaration)));
|
|
|
|
.anyMatch(clazz -> clazz.getAnnotations().stream().anyMatch(annotation -> annotation.toString().startsWith(annotationDeclaration)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private boolean sourceCodeContainsClass(String sourceCode) {
|
|
|
|
private boolean sourceCodeContainsClass(String sourceCode) {
|
|
|
@ -175,4 +198,54 @@ public class JavaIdentifier extends StructuralUnitIdentifier {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
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;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|