private CompilationUnit compilationUnitOutput()

in jbpm/jbpm-flow-builder/src/main/java/org/jbpm/compiler/canonical/WorkItemModelMetaData.java [274:356]


    private CompilationUnit compilationUnitOutput() {
        CompilationUnit compilationUnit = parse(this.getClass().getResourceAsStream("/class-templates/TaskOutputTemplate.java"));
        compilationUnit.setPackageDeclaration(packageName);
        ClassOrInterfaceDeclaration modelClass = compilationUnit.findFirst(ClassOrInterfaceDeclaration.class, sl1 -> true).orElseThrow(this::cannotFindClass);
        addComment(compilationUnit, "Task output");
        addUserTaskAnnotation(modelClass);
        modelClass.setName(outputModelClassSimpleName);

        // setup of the toMap method body
        BlockStmt toMapBody = new BlockStmt();
        BlockStmt fromMapBody = new BlockStmt();
        NameExpr params = new NameExpr(PARAMS);
        final String fromMapReturnName = "result";
        NameExpr fromMapReturn = new NameExpr(fromMapReturnName);
        fromMapBody.addStatement(new AssignExpr(new VariableDeclarationExpr(parseClassOrInterfaceType(
                outputModelClassName), fromMapReturnName),
                new ObjectCreationExpr().setType(outputModelClassSimpleName), Operator.ASSIGN));

        ClassOrInterfaceType toMap = new ClassOrInterfaceType(null, new SimpleName(Map.class.getSimpleName()), NodeList
                .nodeList(new ClassOrInterfaceType(null, String.class.getSimpleName()), new ClassOrInterfaceType(null,
                        Object.class.getSimpleName())));

        VariableDeclarationExpr paramsField = new VariableDeclarationExpr(toMap, PARAMS);
        toMapBody.addStatement(new AssignExpr(paramsField, new ObjectCreationExpr(null, new ClassOrInterfaceType(null,
                HashMap.class.getSimpleName() + "<>"), NodeList.nodeList()), AssignExpr.Operator.ASSIGN));

        // map is task output -> context variable / process variable
        Map<String, String> outputTypes = workItemNode.getIoSpecification().getOutputTypes();
        for (Entry<String, String> entry : workItemNode.getIoSpecification().getOutputMappingBySources().entrySet()) {
            if (entry.getValue() == null || HumanTaskNode.TASK_PARAMETERS.contains(entry.getKey())) {
                continue;
            }

            Variable variable = Optional.ofNullable(variableScope.findVariable(entry.getValue()))
                    .orElse(processVariableScope.findVariable(entry.getValue()));

            if (variable == null) {
                variable = new Variable();
                variable.setName(entry.getKey());
                DataType type = DataTypeResolver.fromType(outputTypes.get(entry.getKey()), Thread.currentThread().getContextClassLoader());
                variable.setType(type);
                if (!PatternConstants.PARAMETER_MATCHER.matcher(entry.getValue()).find()) {
                    variable.setValue(type.readValue(entry.getValue()));
                }
            }

            FieldDeclaration fd = new FieldDeclaration().addVariable(
                    new VariableDeclarator()
                            .setType(variable.getType().getStringType())
                            .setName(entry.getKey()))
                    .addModifier(Modifier.Keyword.PRIVATE);
            modelClass.addMember(fd);
            addUserTaskParamAnnotation(fd, UserTaskParam.ParamType.OUTPUT);

            fd.createGetter();
            fd.createSetter();

            // toMap method body
            MethodCallExpr putVariable = new MethodCallExpr(params, "put");
            putVariable.addArgument(new StringLiteralExpr(entry.getKey()));
            putVariable.addArgument(new FieldAccessExpr(new ThisExpr(), entry.getKey()));
            toMapBody.addStatement(putVariable);

            // fromMap method body
            fromMapBody.addStatement(new AssignExpr(new FieldAccessExpr(fromMapReturn, entry.getKey()),
                    new CastExpr(new ClassOrInterfaceType(null, variable.getType().getStringType()),
                            new MethodCallExpr(params, "get").addArgument(new StringLiteralExpr(entry.getKey()))),
                    Operator.ASSIGN));

        }

        toMapBody.addStatement(new ReturnStmt(params));
        fromMapBody.addStatement(new ReturnStmt(fromMapReturn));
        modelClass.findFirst(MethodDeclaration.class, sl -> sl.getName().asString().equals("toMap")).ifPresent(
                methodDeclaration -> methodDeclaration.setBody(toMapBody));
        modelClass.findFirst(MethodDeclaration.class, sl -> sl.getName().asString().equals("fromMap")).ifPresent(
                methodDeclaration -> {
                    methodDeclaration.setBody(fromMapBody);
                    methodDeclaration.setType(outputModelClassSimpleName);
                });

        return compilationUnit;
    }