io.robe.crud.helper.ClassVisitor Maven / Gradle / Ivy
package io.robe.crud.helper;
import japa.parser.ast.CompilationUnit;
import japa.parser.ast.Node;
import japa.parser.ast.body.BodyDeclaration;
import japa.parser.ast.body.ClassOrInterfaceDeclaration;
import japa.parser.ast.body.FieldDeclaration;
import japa.parser.ast.body.VariableDeclarator;
import japa.parser.ast.expr.AnnotationExpr;
import japa.parser.ast.expr.MemberValuePair;
import japa.parser.ast.visitor.VoidVisitorAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ClassVisitor extends VoidVisitorAdapter {
public static List classes = new ArrayList();
public static Map> models = new HashMap>();
public static Map> allColumns = new HashMap>();
public static Map> uniqueColumns = new HashMap>();
public static Map imports = new HashMap();
@Override
public void visit(ClassOrInterfaceDeclaration n, Object arg) {
List list = n.getAnnotations();
if (list != null) {
for (AnnotationExpr annotationExpr : list) {
if (annotationExpr.toString().equals("@Entity")) {
List body = n.getMembers();
List allList = new ArrayList();
List uniqueList = new ArrayList();
imports.put(n.getName(), ((CompilationUnit) n.getParentNode()).getPackage().getName().toString());
List model = new ArrayList();
for (BodyDeclaration bodyDeclaration : body) {
if (bodyDeclaration instanceof FieldDeclaration) {
FieldDeclaration fieldDeclaration = (FieldDeclaration) bodyDeclaration;
List fieldExp = fieldDeclaration.getAnnotations();
VariableDeclarator variableDeclarator = fieldDeclaration.getVariables().get(0);
Model m = new Model();
m.setName(variableDeclarator.getId().toString());
//set default value
m.setNullable(true);
m.setLength("255");
m.setType("string");
String fieldType = fieldDeclaration.getType().toString().toLowerCase().replaceAll("\"", "");
if (fieldType.equals("boolean")) {
m.setType("boolean");
} else if (fieldType.equals("integer") || fieldType.equals("int") || fieldType.equals("bigdecimal") || fieldType.equals("double") || fieldType.equals("long")) {
m.setType("number");
}
if (fieldExp != null) {
for (AnnotationExpr expr : fieldExp) {
if (expr.getName().toString().equals("Column")) {
allList.add(variableDeclarator.getId().toString());
List nodes = expr.getChildrenNodes();
for (Node node : nodes) {
if (node instanceof MemberValuePair) {
MemberValuePair memberValuePair = (MemberValuePair) node;
if (memberValuePair.getName().equals("nullable")) {
Boolean nullable = Boolean.valueOf(memberValuePair.getValue().toString());
m.setNullable(nullable);
} else if ((memberValuePair.getName().equals("length"))) {
m.setLength(memberValuePair.getValue().toString());
}
if (memberValuePair.getName().equals("unique")) {
if (Boolean.valueOf(memberValuePair.getValue().toString())) {
uniqueList.add(variableDeclarator.getId().toString());
}
}
}
}
model.add(m);
}
}
allColumns.put(n.getName(), allList);
uniqueColumns.put(n.getName(), uniqueList);
models.put(n.getName(), model);
}
}
}
classes.add(n.getName());
}
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy