@Test public void addMethod() { final MethodDeclaration methodTemplate = new MethodDeclaration(); methodTemplate.setName("methodTemplate"); final BlockStmt body = new BlockStmt(); methodTemplate.setBody(body); final String methodName = "METHOD_NAME"; final ClassOrInterfaceDeclaration classOrInterfaceDeclaration = new ClassOrInterfaceDeclaration(); assertTrue(classOrInterfaceDeclaration.getMethodsByName(methodName).isEmpty()); CommonCodegenUtils.addMethod(methodTemplate, classOrInterfaceDeclaration, methodName); assertEquals(1, classOrInterfaceDeclaration.getMethodsByName(methodName).size()); assertEquals(body, classOrInterfaceDeclaration.getMethodsByName(methodName).get(0).getBody().get()); }
public static MethodDeclaration addMethod(final MethodDeclaration methodTemplate, final ClassOrInterfaceDeclaration tableTemplate, final String methodName) { final BlockStmt body = methodTemplate.getBody().orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_BODY_TEMPLATE, methodTemplate.getName()))); final MethodDeclaration toReturn = tableTemplate.addMethod(methodName).setBody(body); toReturn.setModifiers(methodTemplate.getModifiers()); methodTemplate.getParameters().forEach(toReturn::addParameter); toReturn.setType(methodTemplate.getType()); return toReturn; }
CommonCodegenUtils { public static MethodDeclaration addMethod(final MethodDeclaration methodTemplate, final ClassOrInterfaceDeclaration tableTemplate, final String methodName) { final BlockStmt body = methodTemplate.getBody().orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_BODY_TEMPLATE, methodTemplate.getName()))); final MethodDeclaration toReturn = tableTemplate.addMethod(methodName).setBody(body); toReturn.setModifiers(methodTemplate.getModifiers()); methodTemplate.getParameters().forEach(toReturn::addParameter); toReturn.setType(methodTemplate.getType()); return toReturn; } }
CommonCodegenUtils { public static MethodDeclaration addMethod(final MethodDeclaration methodTemplate, final ClassOrInterfaceDeclaration tableTemplate, final String methodName) { final BlockStmt body = methodTemplate.getBody().orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_BODY_TEMPLATE, methodTemplate.getName()))); final MethodDeclaration toReturn = tableTemplate.addMethod(methodName).setBody(body); toReturn.setModifiers(methodTemplate.getModifiers()); methodTemplate.getParameters().forEach(toReturn::addParameter); toReturn.setType(methodTemplate.getType()); return toReturn; } private  CommonCodegenUtils(); }
CommonCodegenUtils { public static MethodDeclaration addMethod(final MethodDeclaration methodTemplate, final ClassOrInterfaceDeclaration tableTemplate, final String methodName) { final BlockStmt body = methodTemplate.getBody().orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_BODY_TEMPLATE, methodTemplate.getName()))); final MethodDeclaration toReturn = tableTemplate.addMethod(methodName).setBody(body); toReturn.setModifiers(methodTemplate.getModifiers()); methodTemplate.getParameters().forEach(toReturn::addParameter); toReturn.setType(methodTemplate.getType()); return toReturn; } 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); }
CommonCodegenUtils { public static MethodDeclaration addMethod(final MethodDeclaration methodTemplate, final ClassOrInterfaceDeclaration tableTemplate, final String methodName) { final BlockStmt body = methodTemplate.getBody().orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_BODY_TEMPLATE, methodTemplate.getName()))); final MethodDeclaration toReturn = tableTemplate.addMethod(methodName).setBody(body); toReturn.setModifiers(methodTemplate.getModifiers()); methodTemplate.getParameters().forEach(toReturn::addParameter); toReturn.setType(methodTemplate.getType()); return toReturn; } 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; }