Search in sources :

Example 1 with ExplicitConstructorInvocationStmt

use of com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt in project drools by kiegroup.

the class KiePMMLModelFactoryUtils method setConstructorSuperNameInvocation.

/**
 * Set the <b>name</b> parameter on <b>super</b> invocation
 * @param generatedClassName
 * @param constructorDeclaration
 * @param name
 */
public static void setConstructorSuperNameInvocation(final String generatedClassName, final ConstructorDeclaration constructorDeclaration, final String name) {
    constructorDeclaration.setName(generatedClassName);
    final BlockStmt body = constructorDeclaration.getBody();
    final ExplicitConstructorInvocationStmt superStatement = CommonCodegenUtils.getExplicitConstructorInvocationStmt(body).orElseThrow(() -> new KiePMMLException(String.format(MISSING_CONSTRUCTOR_IN_BODY, body)));
    CommonCodegenUtils.setExplicitConstructorInvocationStmtArgument(superStatement, "name", String.format("\"%s\"", name));
}
Also used : BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) ExplicitConstructorInvocationStmt(com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt)

Example 2 with ExplicitConstructorInvocationStmt

use of com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt in project drools by kiegroup.

the class KiePMMLModelFactoryUtilsTest method initTest.

@Before
public void initTest() {
    CompilationUnit clonedCompilationUnit = compilationUnit.clone();
    classOrInterfaceDeclaration = clonedCompilationUnit.getClassByName(TEMPLATE_CLASS_NAME).orElseThrow(() -> new RuntimeException("Failed to retrieve ClassOrInterfaceDeclaration " + TEMPLATE_CLASS_NAME + "  from " + TEMPLATE_SOURCE)).clone();
    constructorDeclaration = classOrInterfaceDeclaration.getDefaultConstructor().orElseThrow(() -> new RuntimeException("Failed to retrieve default constructor from " + TEMPLATE_SOURCE));
    assertNotNull(constructorDeclaration);
    assertNotNull(constructorDeclaration.getBody());
    staticGetterMethod = classOrInterfaceDeclaration.getMethodsByName(GET_MODEL).get(0);
    Optional<ExplicitConstructorInvocationStmt> optSuperInvocation = CommonCodegenUtils.getExplicitConstructorInvocationStmt(constructorDeclaration.getBody());
    assertTrue(optSuperInvocation.isPresent());
    superInvocation = optSuperInvocation.get();
    // as in the original template
    assertEquals("Template", constructorDeclaration.getName().asString());
    // as in
    assertEquals("super(name, Collections.emptyList(), operator, second);", superInvocation.toString());
    // the original template
    assertTrue(clonedCompilationUnit.getClassByName(TEMPLATE_CLASS_NAME).isPresent());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ExplicitConstructorInvocationStmt(com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt) Before(org.junit.Before)

Example 3 with ExplicitConstructorInvocationStmt

use of com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt in project drools by kiegroup.

the class CommonCodegenUtils method setConstructorDeclarationParameterArgument.

/**
 * Set the <b>value</b> of the given <b>parameterName</b> in the given <code>ConstructorDeclaration</code>
 * @param constructorDeclaration
 * @param parameterName
 * @param value
 * @throws KiePMMLException if the given parameter is not found
 */
public static void setConstructorDeclarationParameterArgument(final ConstructorDeclaration constructorDeclaration, final String parameterName, final String value) {
    final BlockStmt body = constructorDeclaration.getBody();
    final ExplicitConstructorInvocationStmt superStatement = CommonCodegenUtils.getExplicitConstructorInvocationStmt(body).orElseThrow(() -> new KiePMMLException(String.format(MISSING_CONSTRUCTOR_IN_BODY, body)));
    final NameExpr parameterExpr = getExplicitConstructorInvocationParameter(superStatement, parameterName).orElseThrow(() -> new KiePMMLException(String.format(MISSING_PARAMETER_IN_CONSTRUCTOR_INVOCATION, parameterName, constructorDeclaration)));
    if (value != null) {
        parameterExpr.setName(value);
    } else {
        superStatement.getArguments().replace(parameterExpr, new NullLiteralExpr());
    }
}
Also used : NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) NameExpr(com.github.javaparser.ast.expr.NameExpr) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) ExplicitConstructorInvocationStmt(com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt)

Example 4 with ExplicitConstructorInvocationStmt

use of com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt in project drools by kiegroup.

the class CommonCodegenUtils method setConstructorDeclarationReferenceArgument.

/**
 * Set the <b>value</b> of the given <b>parameterName</b> in the given <code>ConstructorDeclaration</code>
 * @param constructorDeclaration
 * @param referenceName
 * @param value
 * @throws KiePMMLException if the given parameter is not found
 */
public static void setConstructorDeclarationReferenceArgument(final ConstructorDeclaration constructorDeclaration, final String referenceName, final String value) {
    final BlockStmt body = constructorDeclaration.getBody();
    final ExplicitConstructorInvocationStmt superStatement = CommonCodegenUtils.getExplicitConstructorInvocationStmt(body).orElseThrow(() -> new KiePMMLException(String.format(MISSING_CONSTRUCTOR_IN_BODY, body)));
    final MethodReferenceExpr methodReferenceExpr = getExplicitConstructorInvocationMethodReference(superStatement, referenceName).orElseThrow(() -> new KiePMMLException(String.format(MISSING_PARAMETER_IN_CONSTRUCTOR_INVOCATION, referenceName, constructorDeclaration)));
    if (value != null) {
        methodReferenceExpr.setScope(new TypeExpr(parseClassOrInterfaceType(value)));
    } else {
        superStatement.getArguments().replace(methodReferenceExpr, new NullLiteralExpr());
    }
}
Also used : NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) ExplicitConstructorInvocationStmt(com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt) MethodReferenceExpr(com.github.javaparser.ast.expr.MethodReferenceExpr) TypeExpr(com.github.javaparser.ast.expr.TypeExpr)

Example 5 with ExplicitConstructorInvocationStmt

use of com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt in project drools by kiegroup.

the class CommonCodegenUtilsTest method getExplicitConstructorInvocationStmt.

@Test
public void getExplicitConstructorInvocationStmt() {
    BlockStmt body = new BlockStmt();
    Optional<ExplicitConstructorInvocationStmt> retrieved = CommonCodegenUtils.getExplicitConstructorInvocationStmt(body);
    assertNotNull(retrieved);
    assertFalse(retrieved.isPresent());
    ExplicitConstructorInvocationStmt explicitConstructorInvocationStmt = new ExplicitConstructorInvocationStmt();
    body.addStatement(explicitConstructorInvocationStmt);
    retrieved = CommonCodegenUtils.getExplicitConstructorInvocationStmt(body);
    assertNotNull(retrieved);
    assertTrue(retrieved.isPresent());
    ExplicitConstructorInvocationStmt retrievedExplicitConstructorInvocationStmt = retrieved.get();
    assertEquals(explicitConstructorInvocationStmt, retrievedExplicitConstructorInvocationStmt);
}
Also used : BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) ExplicitConstructorInvocationStmt(com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt) Test(org.junit.Test)

Aggregations

ExplicitConstructorInvocationStmt (com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt)18 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)10 KiePMMLException (org.kie.pmml.api.exceptions.KiePMMLException)9 NameExpr (com.github.javaparser.ast.expr.NameExpr)6 Test (org.junit.Test)5 ConstructorDeclaration (com.github.javaparser.ast.body.ConstructorDeclaration)4 KiePMMLInternalException (org.kie.pmml.api.exceptions.KiePMMLInternalException)4 Expression (com.github.javaparser.ast.expr.Expression)3 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)3 StaticJavaParser.parseClassOrInterfaceType (com.github.javaparser.StaticJavaParser.parseClassOrInterfaceType)2 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)2 ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)2 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)2 ExpressionStmt (com.github.javaparser.ast.stmt.ExpressionStmt)2 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)2 CompilationUnit (com.github.javaparser.ast.CompilationUnit)1 NodeList (com.github.javaparser.ast.NodeList)1 AssignExpr (com.github.javaparser.ast.expr.AssignExpr)1 DoubleLiteralExpr (com.github.javaparser.ast.expr.DoubleLiteralExpr)1 MethodReferenceExpr (com.github.javaparser.ast.expr.MethodReferenceExpr)1