PredictionRuleMappersGenerator { public static String getPredictionRuleMappersSource(final String packageName, final List<String> generatedRuleMappers) { CompilationUnit clazz = StaticJavaParser.parse(PredictionRuleMappersGenerator.class.getResourceAsStream(TEMPLATE_JAVA)).clone(); if (!packageName.isEmpty()) { clazz.setPackageDeclaration(packageName); } ClassOrInterfaceDeclaration typeDeclaration = (ClassOrInterfaceDeclaration) clazz.getTypes().get(0); FieldDeclaration ruleNameField = typeDeclaration.getFieldByName("predictionRuleMappers").orElseThrow(() -> new RuntimeException("The template " + TEMPLATE_JAVA + " has been modified.")); final List<Expression> nodeList = generatedRuleMappers.stream() .map(generatedRuleMapper -> { ObjectCreationExpr objectCreationExpr = new ObjectCreationExpr(); objectCreationExpr.setType(generatedRuleMapper); return objectCreationExpr; }) .collect(Collectors.toList()); NodeList<Expression> expressions = NodeList.nodeList(nodeList); MethodCallExpr methodCallExpr = new MethodCallExpr(new NameExpr("Arrays"), "asList", expressions); ruleNameField.getVariables().get(0).setInitializer(methodCallExpr); return clazz.toString(); } private  PredictionRuleMappersGenerator(); static String getPredictionRuleMappersSource(final String packageName, final List<String> generatedRuleMappers);  }
@Test void getPredictionRuleMapperSource() { final String predictionRuleMapper = "PredictionRuleMapperImpl"; final String packageName = "PACKAGE"; final List<String> generatedRuleMappers = IntStream.range(0, 4).mapToObj(index -> packageName + "." + "subPack" + index + "." + predictionRuleMapper).collect(Collectors.toList()); String retrieved = PredictionRuleMappersGenerator.getPredictionRuleMappersSource(packageName, generatedRuleMappers); assertNotNull(retrieved); String expected = String.format("package %s;", packageName); assertTrue(retrieved.contains(expected)); List<String> mod = generatedRuleMappers.stream().map(gen -> "new " + gen + "()").collect(Collectors.toList()); expected = "Arrays.asList(" + String.join(", ", mod) + ");"; assertTrue(retrieved.contains(expected)); }