Search in sources :

Example 6 with FieldDefinition

use of org.drools.modelcompiler.builder.generator.declaredtype.api.FieldDefinition in project drools by kiegroup.

the class GeneratedClassDeclaration method generateFullClass.

private ClassOrInterfaceDeclaration generateFullClass(String generatedClassName, Collection<FieldDefinition> inheritedFields) {
    boolean hasSuper = typeDefinition.getSuperTypeName().isPresent();
    generatedHashcode = new GeneratedHashcode(hasSuper);
    generatedToString = new GeneratedToString(generatedClassName);
    generatedEqualsMethod = new GeneratedEqualsMethod(generatedClassName, hasSuper);
    List<? extends FieldDefinition> typeFields = typeDefinition.getFields();
    GeneratedConstructor fullArgumentConstructor = GeneratedConstructor.factory(generatedClass, typeFields);
    typeDefinition.getMethods().forEach(this::addMethod);
    for (FieldDefinition tf : typeFields) {
        processTypeField(tf);
    }
    List<FieldDefinition> keyFields = typeDefinition.getKeyFields();
    fullArgumentConstructor.generateConstructor(inheritedFields, keyFields);
    if (!keyFields.isEmpty()) {
        generatedClass.addMember(generatedEqualsMethod.method());
        generatedClass.addMember(generatedHashcode.method());
    }
    generatedClass.addMember(generatedToString.method());
    return generatedClass;
}
Also used : FieldDefinition(org.drools.modelcompiler.builder.generator.declaredtype.api.FieldDefinition)

Example 7 with FieldDefinition

use of org.drools.modelcompiler.builder.generator.declaredtype.api.FieldDefinition in project drools by kiegroup.

the class NoConstructor method generateConstructor.

@Override
public void generateConstructor(Collection<FieldDefinition> inheritedFields, List<FieldDefinition> keyFields) {
    NodeList<Modifier> modifiers = publicConstructor ? new NodeList<>(Modifier.publicModifier()) : new NodeList<Modifier>();
    ConstructorDeclaration constructor = new ConstructorDeclaration(modifiers, generatedClass.getNameAsString());
    NodeList<Statement> fieldAssignStatement = nodeList();
    MethodCallExpr superCall = new MethodCallExpr(null, "super");
    for (FieldDefinition typeField : inheritedFields) {
        if (typeField.isStatic()) {
            continue;
        }
        String fieldName = typeField.getFieldName();
        addConstructorArgument(constructor, typeField.getObjectType(), fieldName);
        superCall.addArgument(fieldName);
        if (typeField.isKeyField()) {
            keyFields.add(typeField);
        }
    }
    if (shouldCallSuper) {
        fieldAssignStatement.add(new ExpressionStmt(superCall));
    }
    int nonStaticFields = 0;
    for (FieldDefinition fieldDefinition : typeDeclarationFields) {
        if (fieldDefinition.isStatic() || fieldDefinition.isOverride()) {
            continue;
        }
        nonStaticFields++;
        String fieldName = fieldDefinition.getFieldName();
        Type returnType = toClassOrInterfaceType(fieldDefinition.getObjectType());
        addConstructorArgument(constructor, returnType, fieldName);
        fieldAssignStatement.add(fieldAssignment(fieldName));
    }
    constructor.setBody(new BlockStmt(fieldAssignStatement));
    if (constructor.getParameters().isNonEmpty()) {
        generatedClass.addMember(constructor);
    }
    if (!keyFields.isEmpty() && keyFields.size() != inheritedFields.size() + nonStaticFields) {
        generateKieFieldsConstructor(keyFields);
    }
}
Also used : StaticJavaParser.parseStatement(com.github.javaparser.StaticJavaParser.parseStatement) Statement(com.github.javaparser.ast.stmt.Statement) FieldDefinition(org.drools.modelcompiler.builder.generator.declaredtype.api.FieldDefinition) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) DrlxParseUtil.toClassOrInterfaceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType) Type(com.github.javaparser.ast.type.Type) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) Modifier(com.github.javaparser.ast.Modifier) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 8 with FieldDefinition

use of org.drools.modelcompiler.builder.generator.declaredtype.api.FieldDefinition in project drools by kiegroup.

the class NoConstructor method generateKieFieldsConstructor.

private void generateKieFieldsConstructor(List<FieldDefinition> keyFields) {
    Modifier.Keyword[] modifiers = publicConstructor ? new Modifier.Keyword[] { Modifier.publicModifier().getKeyword() } : new Modifier.Keyword[0];
    ConstructorDeclaration constructor = generatedClass.addConstructor(modifiers);
    NodeList<Statement> fieldStatements = nodeList();
    MethodCallExpr keySuperCall = new MethodCallExpr(null, "super");
    fieldStatements.add(new ExpressionStmt(keySuperCall));
    for (FieldDefinition fieldDefinition : keyFields) {
        String fieldName = fieldDefinition.getFieldName();
        addConstructorArgument(constructor, fieldDefinition.getObjectType(), fieldName);
        Optional<? extends FieldDefinition> typeDefinition = typeDeclarationFields.stream().filter(td -> td.getFieldName().equals(fieldName)).findAny();
        if (typeDefinition.isPresent()) {
            fieldStatements.add(fieldAssignment(fieldName));
        } else {
            keySuperCall.addArgument(fieldName);
        }
    }
    constructor.setBody(new BlockStmt(fieldStatements));
}
Also used : NodeList(com.github.javaparser.ast.NodeList) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) FieldDefinition(org.drools.modelcompiler.builder.generator.declaredtype.api.FieldDefinition) StaticJavaParser.parseStatement(com.github.javaparser.StaticJavaParser.parseStatement) GeneratedClassDeclaration.replaceFieldName(org.drools.modelcompiler.builder.generator.declaredtype.generator.GeneratedClassDeclaration.replaceFieldName) TypeDeclaration(com.github.javaparser.ast.body.TypeDeclaration) Collection(java.util.Collection) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) NodeList.nodeList(com.github.javaparser.ast.NodeList.nodeList) DrlxParseUtil.toClassOrInterfaceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType) Statement(com.github.javaparser.ast.stmt.Statement) Modifier(com.github.javaparser.ast.Modifier) List(java.util.List) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) Type(com.github.javaparser.ast.type.Type) Optional(java.util.Optional) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) StaticJavaParser.parseStatement(com.github.javaparser.StaticJavaParser.parseStatement) Statement(com.github.javaparser.ast.stmt.Statement) FieldDefinition(org.drools.modelcompiler.builder.generator.declaredtype.api.FieldDefinition) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) Modifier(com.github.javaparser.ast.Modifier) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Aggregations

FieldDefinition (org.drools.modelcompiler.builder.generator.declaredtype.api.FieldDefinition)8 List (java.util.List)5 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)4 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)4 ExpressionStmt (com.github.javaparser.ast.stmt.ExpressionStmt)4 Statement (com.github.javaparser.ast.stmt.Statement)4 ArrayList (java.util.ArrayList)4 Collectors (java.util.stream.Collectors)4 MethodDefinition (org.drools.modelcompiler.builder.generator.declaredtype.api.MethodDefinition)4 TypeDefinition (org.drools.modelcompiler.builder.generator.declaredtype.api.TypeDefinition)4 NodeList (com.github.javaparser.ast.NodeList)3 NodeList.nodeList (com.github.javaparser.ast.NodeList.nodeList)3 Collection (java.util.Collection)3 Optional (java.util.Optional)3 StaticJavaParser (com.github.javaparser.StaticJavaParser)2 StaticJavaParser.parseStatement (com.github.javaparser.StaticJavaParser.parseStatement)2 CompilationUnit (com.github.javaparser.ast.CompilationUnit)2 Modifier (com.github.javaparser.ast.Modifier)2 ConstructorDeclaration (com.github.javaparser.ast.body.ConstructorDeclaration)2 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)2