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));
}
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());
}
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());
}
}
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());
}
}
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);
}
Aggregations