@Test void getPredictionRuleMapperSourceWithoutPackage() { final String fullRuleName = "FULL_RULE_NAME"; String retrieved = PredictionRuleMapperGenerator.getPredictionRuleMapperSource(fullRuleName); assertNotNull(retrieved); String expected = String.format("public final static Model model = new %s();", fullRuleName); assertTrue(retrieved.contains(expected)); }
public static String getPredictionRuleMapperSource(final String fullRuleName) { final String packageName = fullRuleName.contains(".") ? fullRuleName.substring(0, fullRuleName.lastIndexOf('.')) : ""; CompilationUnit clazz = StaticJavaParser.parse(PredictionRuleMapperGenerator.class.getResourceAsStream(TEMPLATE_JAVA)).clone(); if (!packageName.isEmpty()) { clazz.setPackageDeclaration(packageName); } ClassOrInterfaceDeclaration typeDeclaration = (ClassOrInterfaceDeclaration) clazz.getTypes().get(0); FieldDeclaration ruleNameField = typeDeclaration.getFieldByName("model").orElseThrow(() -> new RuntimeException("The template " + TEMPLATE_JAVA + " has been modified.")); ObjectCreationExpr objectCreationExpr = new ObjectCreationExpr(); objectCreationExpr.setType(fullRuleName); ruleNameField.getVariables().get(0).setInitializer(objectCreationExpr); return clazz.toString(); }
PredictionRuleMapperGenerator { public static String getPredictionRuleMapperSource(final String fullRuleName) { final String packageName = fullRuleName.contains(".") ? fullRuleName.substring(0, fullRuleName.lastIndexOf('.')) : ""; CompilationUnit clazz = StaticJavaParser.parse(PredictionRuleMapperGenerator.class.getResourceAsStream(TEMPLATE_JAVA)).clone(); if (!packageName.isEmpty()) { clazz.setPackageDeclaration(packageName); } ClassOrInterfaceDeclaration typeDeclaration = (ClassOrInterfaceDeclaration) clazz.getTypes().get(0); FieldDeclaration ruleNameField = typeDeclaration.getFieldByName("model").orElseThrow(() -> new RuntimeException("The template " + TEMPLATE_JAVA + " has been modified.")); ObjectCreationExpr objectCreationExpr = new ObjectCreationExpr(); objectCreationExpr.setType(fullRuleName); ruleNameField.getVariables().get(0).setInitializer(objectCreationExpr); return clazz.toString(); } }
PredictionRuleMapperGenerator { public static String getPredictionRuleMapperSource(final String fullRuleName) { final String packageName = fullRuleName.contains(".") ? fullRuleName.substring(0, fullRuleName.lastIndexOf('.')) : ""; CompilationUnit clazz = StaticJavaParser.parse(PredictionRuleMapperGenerator.class.getResourceAsStream(TEMPLATE_JAVA)).clone(); if (!packageName.isEmpty()) { clazz.setPackageDeclaration(packageName); } ClassOrInterfaceDeclaration typeDeclaration = (ClassOrInterfaceDeclaration) clazz.getTypes().get(0); FieldDeclaration ruleNameField = typeDeclaration.getFieldByName("model").orElseThrow(() -> new RuntimeException("The template " + TEMPLATE_JAVA + " has been modified.")); ObjectCreationExpr objectCreationExpr = new ObjectCreationExpr(); objectCreationExpr.setType(fullRuleName); ruleNameField.getVariables().get(0).setInitializer(objectCreationExpr); return clazz.toString(); } private  PredictionRuleMapperGenerator(); }
PredictionRuleMapperGenerator { public static String getPredictionRuleMapperSource(final String fullRuleName) { final String packageName = fullRuleName.contains(".") ? fullRuleName.substring(0, fullRuleName.lastIndexOf('.')) : ""; CompilationUnit clazz = StaticJavaParser.parse(PredictionRuleMapperGenerator.class.getResourceAsStream(TEMPLATE_JAVA)).clone(); if (!packageName.isEmpty()) { clazz.setPackageDeclaration(packageName); } ClassOrInterfaceDeclaration typeDeclaration = (ClassOrInterfaceDeclaration) clazz.getTypes().get(0); FieldDeclaration ruleNameField = typeDeclaration.getFieldByName("model").orElseThrow(() -> new RuntimeException("The template " + TEMPLATE_JAVA + " has been modified.")); ObjectCreationExpr objectCreationExpr = new ObjectCreationExpr(); objectCreationExpr.setType(fullRuleName); ruleNameField.getVariables().get(0).setInitializer(objectCreationExpr); return clazz.toString(); } private  PredictionRuleMapperGenerator(); static String getPredictionRuleMapperSource(final String fullRuleName); }
PredictionRuleMapperGenerator { public static String getPredictionRuleMapperSource(final String fullRuleName) { final String packageName = fullRuleName.contains(".") ? fullRuleName.substring(0, fullRuleName.lastIndexOf('.')) : ""; CompilationUnit clazz = StaticJavaParser.parse(PredictionRuleMapperGenerator.class.getResourceAsStream(TEMPLATE_JAVA)).clone(); if (!packageName.isEmpty()) { clazz.setPackageDeclaration(packageName); } ClassOrInterfaceDeclaration typeDeclaration = (ClassOrInterfaceDeclaration) clazz.getTypes().get(0); FieldDeclaration ruleNameField = typeDeclaration.getFieldByName("model").orElseThrow(() -> new RuntimeException("The template " + TEMPLATE_JAVA + " has been modified.")); ObjectCreationExpr objectCreationExpr = new ObjectCreationExpr(); objectCreationExpr.setType(fullRuleName); ruleNameField.getVariables().get(0).setInitializer(objectCreationExpr); return clazz.toString(); } private  PredictionRuleMapperGenerator(); static String getPredictionRuleMapperSource(final String fullRuleName);  }