CommonCodegenUtils { public static void addMapPopulation(final Map<String, MethodDeclaration> toAdd, final BlockStmt body, final String mapName) { toAdd.forEach((s, methodDeclaration) -> { MethodReferenceExpr methodReferenceExpr = new MethodReferenceExpr(); methodReferenceExpr.setScope(new ThisExpr()); methodReferenceExpr.setIdentifier(methodDeclaration.getNameAsString()); NodeList<Expression> expressions = NodeList.nodeList(new StringLiteralExpr(s), methodReferenceExpr); body.addStatement(new MethodCallExpr(new NameExpr(mapName), "put", expressions)); }); } private  CommonCodegenUtils(); static void populateMethodDeclarations(final ClassOrInterfaceDeclaration toPopulate,
                                                  final Collection<MethodDeclaration> methodDeclarations); static ExpressionStmt getFilteredKiePMMLNameValueExpression(final String kiePMMLNameValueListParam,
                                                                       final String fieldNameToRef,
                                                                       boolean stringLiteralComparison); static void addMapPopulation(final Map<String, MethodDeclaration> toAdd,
                                        final BlockStmt body,
                                        final String mapName); static MethodDeclaration getMethodDeclaration(final String methodName,
                                                         final Map<String, ClassOrInterfaceType> parameterNameTypeMap); static MethodDeclaration getMethodDeclaration(final String methodName); static ReturnStmt getReturnStmt(final String returnedVariableName); static ClassOrInterfaceType getTypedClassOrInterfaceType(final String className,
                                                                    final List<String> typesName ); static void setAssignExpressionValue( final BlockStmt body, final String assignExpressionName, final Expression value); static Optional<AssignExpr> getAssignExpression(final BlockStmt body, final String assignExpressionName); static Optional<ExplicitConstructorInvocationStmt> getExplicitConstructorInvocationStmt(final BlockStmt body); static void setExplicitConstructorInvocationArgument(final ExplicitConstructorInvocationStmt constructorInvocationStmt, final String parameterName, final String value); static Optional<NameExpr> getExplicitConstructorInvocationParameter(final ExplicitConstructorInvocationStmt constructorInvocationStmt, final String parameterName); static Optional<MethodDeclaration> getMethodDeclaration(final ClassOrInterfaceDeclaration classOrInterfaceDeclaration, final String methodName); static MethodDeclaration addMethod(final MethodDeclaration methodTemplate,
                                                 final ClassOrInterfaceDeclaration tableTemplate,
                                                 final String methodName); static Optional<VariableDeclarator> getVariableDeclarator(final BlockStmt body, final String variableName); static String OPTIONAL_FILTERED_KIEPMMLNAMEVALUE_NAME; }
@Test public void addMapPopulation() { final Map<String, MethodDeclaration> toAdd = IntStream.range(0, 5).boxed().collect(Collectors.toMap(index -> "KEY_" + index, index -> getMethodDeclaration("METHOD_" + index))); BlockStmt body = new BlockStmt(); String mapName = "MAP_NAME"; CommonCodegenUtils.addMapPopulation(toAdd, body, mapName); NodeList<Statement> statements = body.getStatements(); assertEquals(toAdd.size(), statements.size()); for (Statement statement : statements) { assertTrue(statement instanceof ExpressionStmt); ExpressionStmt expressionStmt = (ExpressionStmt) statement; com.github.javaparser.ast.expr.Expression expression = expressionStmt.getExpression(); assertTrue(expression instanceof MethodCallExpr); MethodCallExpr methodCallExpr = (MethodCallExpr) expression; final NodeList<com.github.javaparser.ast.expr.Expression> arguments = methodCallExpr.getArguments(); assertEquals(2, arguments.size()); assertTrue(arguments.get(0) instanceof StringLiteralExpr); assertTrue(arguments.get(1) instanceof MethodReferenceExpr); MethodReferenceExpr methodReferenceExpr = (MethodReferenceExpr) arguments.get(1); assertTrue(methodReferenceExpr.getScope() instanceof ThisExpr); final com.github.javaparser.ast.expr.Expression scope = methodCallExpr.getScope().orElse(null); assertNotNull(scope); assertTrue(scope instanceof NameExpr); assertEquals(mapName, ((NameExpr) scope).getNameAsString()); } for (Map.Entry<String, MethodDeclaration> entry : toAdd.entrySet()) { int matchingDeclarations = (int) statements.stream().filter(statement -> { ExpressionStmt expressionStmt = (ExpressionStmt) statement; com.github.javaparser.ast.expr.Expression expression = expressionStmt.getExpression(); MethodCallExpr methodCallExpr = (MethodCallExpr) expression; final NodeList<com.github.javaparser.ast.expr.Expression> arguments = methodCallExpr.getArguments(); if (!entry.getKey().equals(((StringLiteralExpr) arguments.get(0)).getValue())) { return false; } MethodReferenceExpr methodReferenceExpr = (MethodReferenceExpr) arguments.get(1); return entry.getValue().getName().asString().equals(methodReferenceExpr.getIdentifier()); }).count(); assertEquals(1, matchingDeclarations); } }