use of org.drools.javaparser.ast.stmt.Statement in project drools by kiegroup.
the class POJOGenerator method toClassDeclaration.
/**
* @param packageDescr
*/
public static ClassOrInterfaceDeclaration toClassDeclaration(TypeDeclarationDescr typeDeclaration, PackageDescr packageDescr) {
EnumSet<Modifier> classModifiers = EnumSet.of(Modifier.PUBLIC);
String generatedClassName = typeDeclaration.getTypeName();
ClassOrInterfaceDeclaration generatedClass = new ClassOrInterfaceDeclaration(classModifiers, false, generatedClassName);
generatedClass.addImplementedType(GeneratedFact.class.getName());
// Ref: {@link org.drools.core.factmodel.DefaultBeanClassBuilder} by default always receive is Serializable.
generatedClass.addImplementedType(Serializable.class.getName());
boolean hasSuper = typeDeclaration.getSuperTypeName() != null;
if (hasSuper) {
generatedClass.addExtendedType(typeDeclaration.getSuperTypeName());
}
List<AnnotationDescr> softAnnotations = new ArrayList<>();
for (AnnotationDescr ann : typeDeclaration.getAnnotations()) {
final String annFqn = Optional.ofNullable(ann.getFullyQualifiedName()).orElse(Optional.ofNullable(predefinedClassLevelAnnotation.get(ann.getName())).map(Class::getCanonicalName).orElse(null));
if (annFqn != null) {
NormalAnnotationExpr annExpr = generatedClass.addAndGetAnnotation(annFqn);
ann.getValueMap().forEach((k, v) -> annExpr.addPair(k, getAnnotationValue(annFqn, k, v.toString())));
} else {
softAnnotations.add(ann);
}
}
if (softAnnotations.size() > 0) {
String softAnnDictionary = softAnnotations.stream().map(a -> "<dt>" + a.getName() + "</dt><dd>" + a.getValuesAsString() + "</dd>").collect(Collectors.joining());
JavadocComment generatedClassJavadoc = new JavadocComment("<dl>" + softAnnDictionary + "</dl>");
generatedClass.setJavadocComment(generatedClassJavadoc);
}
// No-args ctor
generatedClass.addConstructor(Modifier.PUBLIC);
List<Statement> equalsFieldStatement = new ArrayList<>();
List<Statement> hashCodeFieldStatement = new ArrayList<>();
List<String> toStringFieldStatement = new ArrayList<>();
List<TypeFieldDescr> keyFields = new ArrayList<>();
Collection<TypeFieldDescr> inheritedFields = findInheritedDeclaredFields(typeDeclaration, packageDescr);
Collection<TypeFieldDescr> typeFields = typeDeclaration.getFields().values();
if (!inheritedFields.isEmpty() || !typeDeclaration.getFields().isEmpty()) {
ConstructorDeclaration fullArgumentsCtor = generatedClass.addConstructor(Modifier.PUBLIC);
NodeList<Statement> ctorFieldStatement = NodeList.nodeList();
MethodCallExpr superCall = new MethodCallExpr(null, "super");
for (TypeFieldDescr typeFieldDescr : inheritedFields) {
String fieldName = typeFieldDescr.getFieldName();
addCtorArg(fullArgumentsCtor, typeFieldDescr.getPattern().getObjectType(), fieldName);
superCall.addArgument(fieldName);
if (typeFieldDescr.getAnnotation("key") != null) {
keyFields.add(typeFieldDescr);
}
}
ctorFieldStatement.add(new ExpressionStmt(superCall));
int position = inheritedFields.size();
for (TypeFieldDescr typeFieldDescr : typeFields) {
String fieldName = typeFieldDescr.getFieldName();
Type returnType = addCtorArg(fullArgumentsCtor, typeFieldDescr.getPattern().getObjectType(), fieldName);
FieldDeclaration field = generatedClass.addField(returnType, fieldName, Modifier.PRIVATE);
field.createSetter();
field.addAndGetAnnotation(Position.class.getName()).addPair("value", "" + position++);
MethodDeclaration getter = field.createGetter();
equalsFieldStatement.add(generateEqualsForField(getter, fieldName));
hashCodeFieldStatement.addAll(generateHashCodeForField(getter, fieldName));
ctorFieldStatement.add(replaceFieldName(parseStatement("this.__fieldName = __fieldName;"), fieldName));
toStringFieldStatement.add(format("+ {0}+{1}", quote(fieldName + "="), fieldName));
if (typeFieldDescr.getAnnotation("key") != null) {
keyFields.add(typeFieldDescr);
}
}
fullArgumentsCtor.setBody(new BlockStmt(ctorFieldStatement));
if (!keyFields.isEmpty() && keyFields.size() != inheritedFields.size() + typeFields.size()) {
ConstructorDeclaration keyArgumentsCtor = generatedClass.addConstructor(Modifier.PUBLIC);
NodeList<Statement> ctorKeyFieldStatement = NodeList.nodeList();
MethodCallExpr keySuperCall = new MethodCallExpr(null, "super");
ctorKeyFieldStatement.add(new ExpressionStmt(keySuperCall));
for (TypeFieldDescr typeFieldDescr : keyFields) {
String fieldName = typeFieldDescr.getFieldName();
addCtorArg(keyArgumentsCtor, typeFieldDescr.getPattern().getObjectType(), fieldName);
if (typeDeclaration.getFields().get(fieldName) != null) {
ctorKeyFieldStatement.add(replaceFieldName(parseStatement("this.__fieldName = __fieldName;"), fieldName));
} else {
keySuperCall.addArgument(fieldName);
}
}
keyArgumentsCtor.setBody(new BlockStmt(ctorKeyFieldStatement));
}
if (hasSuper) {
generatedClass.addMember(generateEqualsMethod(generatedClassName, equalsFieldStatement));
generatedClass.addMember(generateHashCodeMethod(hashCodeFieldStatement));
}
}
generatedClass.addMember(generateToStringMethod(generatedClassName, toStringFieldStatement));
return generatedClass;
}
use of org.drools.javaparser.ast.stmt.Statement in project drools by kiegroup.
the class POJOGenerator method generateToStringMethod.
private static MethodDeclaration generateToStringMethod(String generatedClassName, List<String> toStringFieldStatement) {
final String header = format("return {0} + {1}", quote(generatedClassName), quote("( "));
final String body = String.join(format("+ {0}", quote(", ")), toStringFieldStatement);
final String close = format("+{0};", quote(" )"));
final Statement toStringStatement = parseStatement(header + body + close);
final Type returnType = JavaParser.parseType(String.class.getSimpleName());
final MethodDeclaration equals = new MethodDeclaration(EnumSet.of(Modifier.PUBLIC), returnType, TO_STRING);
equals.addAnnotation("Override");
equals.setBody(new BlockStmt(NodeList.nodeList(toStringStatement)));
return equals;
}
use of org.drools.javaparser.ast.stmt.Statement in project drools by kiegroup.
the class POJOGenerator method classCastStatement.
private static Statement classCastStatement(String className) {
Statement statement = parseStatement("__className that = (__className) o;");
statement.findAll(ClassOrInterfaceType.class).stream().filter(n1 -> n1.getName().toString().equals("__className")).forEach(n -> n.replace(JavaParser.parseClassOrInterfaceType(className)));
return statement;
}
use of org.drools.javaparser.ast.stmt.Statement in project drools by kiegroup.
the class POJOGenerator method generateEqualsForField.
private static Statement generateEqualsForField(MethodDeclaration getter, String fieldName) {
Type type = getter.getType();
Statement statement;
if (type instanceof ClassOrInterfaceType) {
statement = parseStatement(" if( __fieldName != null ? !__fieldName.equals(that.__fieldName) : that.__fieldName != null) { return false; }");
} else if (type instanceof PrimitiveType) {
statement = parseStatement(" if( __fieldName != that.__fieldName) { return false; }");
} else {
throw new RuntimeException("Unknown type");
}
return replaceFieldName(statement, fieldName);
}
use of org.drools.javaparser.ast.stmt.Statement in project drools by kiegroup.
the class AccumulateVisitor method writeAccumulateMethod.
void writeAccumulateMethod(List<String> contextFieldNames, Type singleAccumulateType, MethodDeclaration accumulateMethod, BlockStmt actionBlock) {
for (Statement stmt : actionBlock.getStatements()) {
for (ExpressionStmt eStmt : stmt.findAll(ExpressionStmt.class)) {
forceCastForName(accumulateMethod.getParameter(1).getNameAsString(), singleAccumulateType, eStmt.getExpression());
rescopeNamesToNewScope(new NameExpr("data"), contextFieldNames, eStmt.getExpression());
}
accumulateMethod.getBody().get().addStatement(stmt);
}
}
Aggregations