#24-Create new methods

This commit is contained in:
Anton Romanov 2021-04-21 15:23:37 +04:00
parent 0bd1e9e145
commit 942090145f

View 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;
@ -109,7 +112,11 @@ public class JavaIdentifier extends StructuralUnitIdentifier {
@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 +155,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 +183,45 @@ 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;
}
}