Search in sources :

Example 21 with ClassOrInterfaceDeclaration

use of com.github.javaparser.ast.body.ClassOrInterfaceDeclaration in project drools by kiegroup.

the class GeneratedClassDeclaration method createBasicDeclaredClass.

private ClassOrInterfaceDeclaration createBasicDeclaredClass(String generatedClassName) {
    ClassOrInterfaceDeclaration basicDeclaredClass = new ClassOrInterfaceDeclaration(nodeList(Modifier.publicModifier()), false, generatedClassName);
    // Ref: {@link org.drools.core.factmodel.DefaultBeanClassBuilder} by default always receive is Serializable.
    basicDeclaredClass.addImplementedType(Serializable.class.getName());
    markerInterfaceAnnotations.stream().map(Class::getCanonicalName).forEach(basicDeclaredClass::addImplementedType);
    // No-args ctor
    basicDeclaredClass.addConstructor(Modifier.publicModifier().getKeyword());
    return basicDeclaredClass;
}
Also used : Serializable(java.io.Serializable) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)

Example 22 with ClassOrInterfaceDeclaration

use of com.github.javaparser.ast.body.ClassOrInterfaceDeclaration in project drools by kiegroup.

the class CommonCodegenUtilsTest method getMethodDeclaration.

@Test
public void getMethodDeclaration() {
    final String methodName = "METHOD_NAME";
    final ClassOrInterfaceDeclaration classOrInterfaceDeclaration = new ClassOrInterfaceDeclaration();
    assertFalse(CommonCodegenUtils.getMethodDeclaration(classOrInterfaceDeclaration, methodName).isPresent());
    classOrInterfaceDeclaration.addMethod("NOT_METHOD");
    assertFalse(CommonCodegenUtils.getMethodDeclaration(classOrInterfaceDeclaration, methodName).isPresent());
    classOrInterfaceDeclaration.addMethod(methodName);
    assertTrue(CommonCodegenUtils.getMethodDeclaration(classOrInterfaceDeclaration, methodName).isPresent());
}
Also used : ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) Test(org.junit.Test)

Example 23 with ClassOrInterfaceDeclaration

use of com.github.javaparser.ast.body.ClassOrInterfaceDeclaration in project drools by kiegroup.

the class CodegenTestUtils method commonValidateCompilation.

public static void commonValidateCompilation(BlockStmt body, List<Parameter> parameters) {
    ClassOrInterfaceDeclaration classOrInterfaceType = new ClassOrInterfaceDeclaration();
    classOrInterfaceType.setName("CommCodeTest");
    MethodDeclaration toAdd = new MethodDeclaration();
    toAdd.setType("void");
    toAdd.setName("TestingMethod");
    toAdd.setParameters(NodeList.nodeList(parameters));
    toAdd.setBody(body);
    classOrInterfaceType.addMember(toAdd);
    CompilationUnit compilationUnit = StaticJavaParser.parse("");
    compilationUnit.setPackageDeclaration("org.kie.pmml.compiler.commons.utils");
    compilationUnit.addType(classOrInterfaceType);
    Map<String, String> sourcesMap = Collections.singletonMap("org.kie.pmml.compiler.commons.utils.CommCodeTest", compilationUnit.toString());
    try {
        KieMemoryCompiler.compile(sourcesMap, Thread.currentThread().getContextClassLoader());
    } catch (Exception e) {
        fail(e.getMessage());
    }
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration)

Example 24 with ClassOrInterfaceDeclaration

use of com.github.javaparser.ast.body.ClassOrInterfaceDeclaration in project drools by kiegroup.

the class PMMLRuleMapperFactory method getPMMLRuleMapperSource.

public static String getPMMLRuleMapperSource(final String fullRuleName) {
    final String packageName = fullRuleName.contains(".") ? fullRuleName.substring(0, fullRuleName.lastIndexOf('.')) : "";
    CompilationUnit cloneCU = JavaParserUtils.getKiePMMLModelCompilationUnit(KIE_PMML_RULE_MAPPER_CLASS_NAME, packageName, KIE_PMML_RULE_MAPPER_TEMPLATE_JAVA, KIE_PMML_RULE_MAPPER_CLASS_NAME);
    ClassOrInterfaceDeclaration typeDeclaration = (ClassOrInterfaceDeclaration) cloneCU.getTypes().get(0);
    FieldDeclaration ruleNameField = typeDeclaration.getFieldByName("model").orElseThrow(() -> new RuntimeException("The template " + KIE_PMML_RULE_MAPPER_TEMPLATE_JAVA + " has been modified."));
    ObjectCreationExpr objectCreationExpr = new ObjectCreationExpr();
    objectCreationExpr.setType(fullRuleName);
    ruleNameField.getVariables().get(0).setInitializer(objectCreationExpr);
    return cloneCU.toString();
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration)

Example 25 with ClassOrInterfaceDeclaration

use of com.github.javaparser.ast.body.ClassOrInterfaceDeclaration in project drools by kiegroup.

the class KiePMMLMiningModelFactory method getKiePMMLMiningModelSourcesMapCommon.

static Map<String, String> getKiePMMLMiningModelSourcesMapCommon(final MiningModelCompilationDTO compilationDTO, final Map<String, String> toReturn) {
    logger.trace("getKiePMMLMiningModelSourcesMap {} {} {}", compilationDTO.getFields(), compilationDTO.getModel(), compilationDTO.getPackageName());
    if (!toReturn.containsKey(compilationDTO.getSegmentationCanonicalClassName())) {
        throw new KiePMMLException("Expected generated class " + compilationDTO.getSegmentationCanonicalClassName() + " not " + "found");
    }
    CompilationUnit cloneCU = JavaParserUtils.getKiePMMLModelCompilationUnit(compilationDTO.getSimpleClassName(), compilationDTO.getPackageName(), KIE_PMML_MINING_MODEL_TEMPLATE_JAVA, KIE_PMML_MINING_MODEL_TEMPLATE);
    ClassOrInterfaceDeclaration modelTemplate = cloneCU.getClassByName(compilationDTO.getSimpleClassName()).orElseThrow(() -> new KiePMMLException(MAIN_CLASS_NOT_FOUND + ": " + compilationDTO.getSimpleClassName()));
    setConstructor(compilationDTO, modelTemplate);
    toReturn.put(getFullClassName(cloneCU), cloneCU.toString());
    return toReturn;
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException)

Aggregations

ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)261 Test (org.junit.Test)174 CompilationUnit (com.github.javaparser.ast.CompilationUnit)164 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)84 ReflectionTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver)79 AbstractResolutionTest (com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest)71 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)52 Context (com.github.javaparser.symbolsolver.core.resolution.Context)39 ClassOrInterfaceDeclarationContext (com.github.javaparser.symbolsolver.javaparsermodel.contexts.ClassOrInterfaceDeclarationContext)38 CompilationUnitContext (com.github.javaparser.symbolsolver.javaparsermodel.contexts.CompilationUnitContext)37 ResolvedType (com.github.javaparser.resolution.types.ResolvedType)36 Expression (com.github.javaparser.ast.expr.Expression)33 TypeSolver (com.github.javaparser.symbolsolver.model.resolution.TypeSolver)28 JavaParserFacade (com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade)27 MemoryTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.MemoryTypeSolver)24 HashMap (java.util.HashMap)21 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)20 ResolvedMethodDeclaration (com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration)20 KiePMMLException (org.kie.pmml.api.exceptions.KiePMMLException)20 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)19