Search in sources :

Example 1 with ReflectionMarshallerGenerator

use of org.kie.kogito.codegen.process.persistence.marshaller.ReflectionMarshallerGenerator in project kogito-runtimes by kiegroup.

the class MongoDBPersistenceGeneratorTest method test.

@ParameterizedTest
@MethodSource("persistenceTestContexts")
void test(KogitoBuildContext context) {
    context.setApplicationProperty(KOGITO_PERSISTENCE_TYPE, persistenceType());
    ReflectionProtoGenerator protoGenerator = ReflectionProtoGenerator.builder().build(Collections.singleton(GeneratedPOJO.class));
    PersistenceGenerator persistenceGenerator = new PersistenceGenerator(context, protoGenerator, new ReflectionMarshallerGenerator(context));
    Collection<GeneratedFile> generatedFiles = persistenceGenerator.generate();
    if (context.hasDI()) {
        Optional<GeneratedFile> generatedCLASSFile = generatedFiles.stream().filter(gf -> gf.category() == GeneratedFileType.SOURCE.category()).filter(f -> PERSISTENCE_FILE_PATH.equals(f.relativePath())).findAny();
        assertTrue(generatedCLASSFile.isPresent());
        GeneratedFile classFile = generatedCLASSFile.get();
        assertEquals(PERSISTENCE_FILE_PATH, classFile.relativePath());
        final CompilationUnit compilationUnit = parse(new ByteArrayInputStream(classFile.contents()));
        final ClassOrInterfaceDeclaration classDeclaration = compilationUnit.findFirst(ClassOrInterfaceDeclaration.class).orElseThrow(() -> new NoSuchElementException("Compilation unit doesn't contain a class or interface declaration!"));
        assertNotNull(classDeclaration);
        final MethodDeclaration methodDeclaration = classDeclaration.findFirst(MethodDeclaration.class, d -> d.getName().getIdentifier().equals("dbName")).orElseThrow(() -> new NoSuchElementException("Class declaration doesn't contain a method named \"dbName\"!"));
        assertNotNull(methodDeclaration);
        assertTrue(methodDeclaration.getBody().isPresent());
        final BlockStmt body = methodDeclaration.getBody().get();
        assertThat(body.getStatements().size()).isOne();
        assertTrue(body.getStatements().get(0).isReturnStmt());
        final ReturnStmt returnStmt = (ReturnStmt) body.getStatements().get(0);
        assertThat(returnStmt.toString()).contains("kogito");
        final MethodDeclaration transactionMethodDeclaration = classDeclaration.findFirst(MethodDeclaration.class, d -> "transactionManager".equals(d.getName().getIdentifier())).orElseThrow(() -> new NoSuchElementException("Class declaration doesn't contain a method named \"transactionManager\"!"));
        assertNotNull(transactionMethodDeclaration);
        assertTrue(transactionMethodDeclaration.getBody().isPresent());
        final BlockStmt transactionMethodBody = transactionMethodDeclaration.getBody().get();
        assertThat(transactionMethodBody.getStatements().size()).isOne();
        assertTrue(transactionMethodBody.getStatements().get(0).isReturnStmt());
        final ReturnStmt transactionReturnStmt = (ReturnStmt) transactionMethodBody.getStatements().get(0);
        assertThat(transactionReturnStmt.toString()).contains("transactionManager");
        Optional<GeneratedFile> generatedTransactionCLASSFile = generatedFiles.stream().filter(gf -> gf.category() == GeneratedFileType.SOURCE.category()).filter(f -> TRANSACTION_FILE_PATH.equals(f.relativePath())).findAny();
        assertTrue(generatedTransactionCLASSFile.isPresent());
        final CompilationUnit transactionCompilationUnit = parse(new ByteArrayInputStream(generatedTransactionCLASSFile.get().contents()));
        final ClassOrInterfaceDeclaration transactionClassDeclaration = transactionCompilationUnit.findFirst(ClassOrInterfaceDeclaration.class).orElseThrow(() -> new NoSuchElementException("Compilation unit doesn't contain a class or interface declaration!"));
        assertNotNull(transactionClassDeclaration);
        final List<ConstructorDeclaration> constructorDeclarations = transactionCompilationUnit.findAll(ConstructorDeclaration.class);
        assertEquals(2, constructorDeclarations.size());
        Optional<ConstructorDeclaration> annotatedConstructorDeclaration = constructorDeclarations.stream().filter(c -> c.isAnnotationPresent(injectionAnnotation(context))).findAny();
        assertTrue(annotatedConstructorDeclaration.isPresent());
        final MethodDeclaration transactionEnabledMethodDeclaration = transactionClassDeclaration.findFirst(MethodDeclaration.class, d -> d.getName().getIdentifier().equals("enabled")).orElseThrow(() -> new NoSuchElementException("Class declaration doesn't contain a method named \"enabled\"!"));
        assertNotNull(transactionEnabledMethodDeclaration);
        assertTrue(transactionEnabledMethodDeclaration.getBody().isPresent());
        final BlockStmt enabledMethodBody = transactionEnabledMethodDeclaration.getBody().get();
        assertThat(enabledMethodBody.getStatements().size()).isOne();
        assertTrue(enabledMethodBody.getStatements().get(0).isReturnStmt());
        final ReturnStmt enabledReturnStmt = (ReturnStmt) enabledMethodBody.getStatements().get(0);
        assertThat(enabledReturnStmt.toString()).contains("enabled");
        final FieldDeclaration transactionEnabledFieldDeclaration = transactionClassDeclaration.findFirst(FieldDeclaration.class, f -> f.getVariable(0).getName().getIdentifier().equals("enabled")).orElseThrow(() -> new NoSuchElementException("Class declaration doesn't contain a field named \"enabled\"!"));
        assertNotNull(transactionEnabledFieldDeclaration);
        final AnnotationExpr transactionEnabledAnnotationDeclaration = transactionEnabledFieldDeclaration.findFirst(AnnotationExpr.class, a -> ((Name) a.getChildNodes().get(0)).getIdentifier().equals(configInjectionAnnotation(context))).orElseThrow(() -> new NoSuchElementException("Field declaration doesn't contain an annotation  named \"" + configInjectionAnnotation(context) + "\"!"));
        assertNotNull(transactionEnabledAnnotationDeclaration);
        assertThat(transactionEnabledAnnotationDeclaration.getChildNodes().get(1).toString()).contains("kogito.persistence.transaction.enabled");
    }
}
Also used : ReflectionProtoGenerator(org.kie.kogito.codegen.process.persistence.proto.ReflectionProtoGenerator) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) SpringBootKogitoBuildContext(org.kie.kogito.codegen.api.context.impl.SpringBootKogitoBuildContext) Name(com.github.javaparser.ast.expr.Name) StaticJavaParser.parse(com.github.javaparser.StaticJavaParser.parse) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AnnotationExpr(com.github.javaparser.ast.expr.AnnotationExpr) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) MONGODB_PERSISTENCE_TYPE(org.kie.kogito.codegen.process.persistence.PersistenceGenerator.MONGODB_PERSISTENCE_TYPE) ByteArrayInputStream(java.io.ByteArrayInputStream) CompilationUnit(com.github.javaparser.ast.CompilationUnit) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) NoSuchElementException(java.util.NoSuchElementException) MethodSource(org.junit.jupiter.params.provider.MethodSource) KogitoBuildContext(org.kie.kogito.codegen.api.context.KogitoBuildContext) GeneratedFileType(org.kie.kogito.codegen.api.GeneratedFileType) Collection(java.util.Collection) ReflectionMarshallerGenerator(org.kie.kogito.codegen.process.persistence.marshaller.ReflectionMarshallerGenerator) GeneratedPOJO(org.kie.kogito.codegen.data.GeneratedPOJO) List(java.util.List) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) QuarkusKogitoBuildContext(org.kie.kogito.codegen.api.context.impl.QuarkusKogitoBuildContext) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) KOGITO_PERSISTENCE_TYPE(org.kie.kogito.codegen.process.persistence.PersistenceGenerator.KOGITO_PERSISTENCE_TYPE) Optional(java.util.Optional) GeneratedFile(org.kie.kogito.codegen.api.GeneratedFile) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) Collections(java.util.Collections) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) CompilationUnit(com.github.javaparser.ast.CompilationUnit) AnnotationExpr(com.github.javaparser.ast.expr.AnnotationExpr) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) GeneratedFile(org.kie.kogito.codegen.api.GeneratedFile) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) ByteArrayInputStream(java.io.ByteArrayInputStream) ReflectionProtoGenerator(org.kie.kogito.codegen.process.persistence.proto.ReflectionProtoGenerator) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) ReflectionMarshallerGenerator(org.kie.kogito.codegen.process.persistence.marshaller.ReflectionMarshallerGenerator) GeneratedPOJO(org.kie.kogito.codegen.data.GeneratedPOJO) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) NoSuchElementException(java.util.NoSuchElementException) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 2 with ReflectionMarshallerGenerator

use of org.kie.kogito.codegen.process.persistence.marshaller.ReflectionMarshallerGenerator in project kogito-runtimes by kiegroup.

the class AbstractPersistenceGeneratorTest method persistenceGeneratorSanityCheck.

@ParameterizedTest
@MethodSource("persistenceTestContexts")
void persistenceGeneratorSanityCheck(KogitoBuildContext context) {
    context.setApplicationProperty(KOGITO_PERSISTENCE_TYPE, persistenceType());
    ReflectionProtoGenerator protoGenerator = ReflectionProtoGenerator.builder().build(Collections.singleton(GeneratedPOJO.class));
    PersistenceGenerator persistenceGenerator = new PersistenceGenerator(context, protoGenerator, new ReflectionMarshallerGenerator(context, null));
    Collection<GeneratedFile> generatedFiles = persistenceGenerator.generate();
    int expectedDataIndexProto = hasDataIndexProto(context) ? 2 : 0;
    int expectedListDataIndexProto = hasDataIndexProto(context) ? 1 : 0;
    assertThat(generatedFiles.stream().filter(gf -> gf.type().equals(ProtoGenerator.PROTO_TYPE)).count()).isEqualTo(expectedDataIndexProto);
    assertThat(generatedFiles.stream().filter(gf -> gf.type().equals(ProtoGenerator.PROTO_TYPE) && gf.relativePath().endsWith(".json")).count()).isEqualTo(expectedListDataIndexProto);
    int expectedProtoMarshaller = hasProtoMarshaller(context) ? 10 : 0;
    assertThat(generatedFiles.stream().filter(gf -> gf.type().equals(GeneratedFileType.SOURCE) && gf.relativePath().endsWith("Marshaller.java"))).hasSize(expectedProtoMarshaller);
}
Also used : AddonsConfig(org.kie.kogito.codegen.api.AddonsConfig) ReflectionProtoGenerator(org.kie.kogito.codegen.process.persistence.proto.ReflectionProtoGenerator) KogitoBuildContext(org.kie.kogito.codegen.api.context.KogitoBuildContext) GeneratedFileType(org.kie.kogito.codegen.api.GeneratedFileType) KogitoContextTestUtils.contextBuilders(org.kie.kogito.codegen.api.utils.KogitoContextTestUtils.contextBuilders) PersistenceGenerator.hasDataIndexProto(org.kie.kogito.codegen.process.persistence.PersistenceGenerator.hasDataIndexProto) Collection(java.util.Collection) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ReflectionMarshallerGenerator(org.kie.kogito.codegen.process.persistence.marshaller.ReflectionMarshallerGenerator) Arguments(org.junit.jupiter.params.provider.Arguments) GeneratedPOJO(org.kie.kogito.codegen.data.GeneratedPOJO) KOGITO_PERSISTENCE_DATA_INDEX_PROTO_GENERATION(org.kie.kogito.codegen.process.persistence.PersistenceGenerator.KOGITO_PERSISTENCE_DATA_INDEX_PROTO_GENERATION) ProtoGenerator(org.kie.kogito.codegen.process.persistence.proto.ProtoGenerator) File(java.io.File) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Stream(java.util.stream.Stream) PersistenceGenerator.hasProtoMarshaller(org.kie.kogito.codegen.process.persistence.PersistenceGenerator.hasProtoMarshaller) KOGITO_PERSISTENCE_TYPE(org.kie.kogito.codegen.process.persistence.PersistenceGenerator.KOGITO_PERSISTENCE_TYPE) GeneratedFile(org.kie.kogito.codegen.api.GeneratedFile) KOGITO_PERSISTENCE_PROTO_MARSHALLER(org.kie.kogito.codegen.process.persistence.PersistenceGenerator.KOGITO_PERSISTENCE_PROTO_MARSHALLER) Collections(java.util.Collections) MethodSource(org.junit.jupiter.params.provider.MethodSource) ReflectionProtoGenerator(org.kie.kogito.codegen.process.persistence.proto.ReflectionProtoGenerator) ReflectionMarshallerGenerator(org.kie.kogito.codegen.process.persistence.marshaller.ReflectionMarshallerGenerator) GeneratedPOJO(org.kie.kogito.codegen.data.GeneratedPOJO) GeneratedFile(org.kie.kogito.codegen.api.GeneratedFile) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 3 with ReflectionMarshallerGenerator

use of org.kie.kogito.codegen.process.persistence.marshaller.ReflectionMarshallerGenerator in project kogito-runtimes by kiegroup.

the class InfinispanPersistenceGeneratorTest method test.

@ParameterizedTest
@MethodSource("persistenceTestContexts")
void test(KogitoBuildContext context) {
    context.setApplicationProperty(KOGITO_PERSISTENCE_TYPE, persistenceType());
    ReflectionProtoGenerator protoGenerator = ReflectionProtoGenerator.builder().build(Collections.singleton(GeneratedPOJO.class));
    PersistenceGenerator persistenceGenerator = new PersistenceGenerator(context, protoGenerator, new ReflectionMarshallerGenerator(context));
    Collection<GeneratedFile> generatedFiles = persistenceGenerator.generate();
    if (context.hasDI()) {
        Optional<GeneratedFile> persistenceFactoryImpl = generatedFiles.stream().filter(gf -> gf.relativePath().equals("org/kie/kogito/persistence/KogitoProcessInstancesFactoryImpl.java")).findFirst();
        if (hasDataIndexProto(context)) {
            List<GeneratedFile> marshallerFiles = generatedFiles.stream().filter(gf -> gf.relativePath().endsWith("MessageMarshaller.java")).collect(Collectors.toList());
            String expectedMarshaller = "PersonMessageMarshaller";
            assertThat(persistenceFactoryImpl).isNotEmpty();
            assertThat(marshallerFiles.size()).isEqualTo(1);
            assertThat(marshallerFiles.get(0).relativePath()).endsWith(expectedMarshaller + ".java");
        }
        final CompilationUnit compilationUnit = parse(new ByteArrayInputStream(persistenceFactoryImpl.get().contents()));
        compilationUnit.findFirst(ClassOrInterfaceDeclaration.class).orElseThrow(() -> new NoSuchElementException("Compilation unit doesn't contain a class or interface declaration!"));
    }
}
Also used : ReflectionProtoGenerator(org.kie.kogito.codegen.process.persistence.proto.ReflectionProtoGenerator) KogitoBuildContext(org.kie.kogito.codegen.api.context.KogitoBuildContext) PersistenceGenerator.hasDataIndexProto(org.kie.kogito.codegen.process.persistence.PersistenceGenerator.hasDataIndexProto) Collection(java.util.Collection) StaticJavaParser.parse(com.github.javaparser.StaticJavaParser.parse) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ReflectionMarshallerGenerator(org.kie.kogito.codegen.process.persistence.marshaller.ReflectionMarshallerGenerator) GeneratedPOJO(org.kie.kogito.codegen.data.GeneratedPOJO) Collectors(java.util.stream.Collectors) List(java.util.List) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) ByteArrayInputStream(java.io.ByteArrayInputStream) KOGITO_PERSISTENCE_TYPE(org.kie.kogito.codegen.process.persistence.PersistenceGenerator.KOGITO_PERSISTENCE_TYPE) Optional(java.util.Optional) GeneratedFile(org.kie.kogito.codegen.api.GeneratedFile) CompilationUnit(com.github.javaparser.ast.CompilationUnit) INFINISPAN_PERSISTENCE_TYPE(org.kie.kogito.codegen.process.persistence.PersistenceGenerator.INFINISPAN_PERSISTENCE_TYPE) NoSuchElementException(java.util.NoSuchElementException) Collections(java.util.Collections) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) MethodSource(org.junit.jupiter.params.provider.MethodSource) CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) GeneratedFile(org.kie.kogito.codegen.api.GeneratedFile) ByteArrayInputStream(java.io.ByteArrayInputStream) ReflectionProtoGenerator(org.kie.kogito.codegen.process.persistence.proto.ReflectionProtoGenerator) ReflectionMarshallerGenerator(org.kie.kogito.codegen.process.persistence.marshaller.ReflectionMarshallerGenerator) GeneratedPOJO(org.kie.kogito.codegen.data.GeneratedPOJO) NoSuchElementException(java.util.NoSuchElementException) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 4 with ReflectionMarshallerGenerator

use of org.kie.kogito.codegen.process.persistence.marshaller.ReflectionMarshallerGenerator in project kogito-runtimes by kiegroup.

the class KafkaPersistenceGeneratorTest method test.

@ParameterizedTest
@MethodSource("persistenceTestContexts")
void test(KogitoBuildContext context) {
    context.setApplicationProperty(KOGITO_PERSISTENCE_TYPE, persistenceType());
    ReflectionProtoGenerator protoGenerator = ReflectionProtoGenerator.builder().build(Collections.singleton(GeneratedPOJO.class));
    PersistenceGenerator persistenceGenerator = new PersistenceGenerator(context, protoGenerator, new ReflectionMarshallerGenerator(context));
    Collection<GeneratedFile> generatedFiles = persistenceGenerator.generate();
    if (context.hasDI()) {
        Optional<GeneratedFile> persistenceFactoryImpl = generatedFiles.stream().filter(gf -> gf.relativePath().equals("org/kie/kogito/persistence/KogitoProcessInstancesFactoryImpl.java")).findFirst();
        if (hasProtoMarshaller(context)) {
            List<GeneratedFile> marshallerFiles = generatedFiles.stream().filter(gf -> gf.relativePath().endsWith("MessageMarshaller.java")).collect(Collectors.toList());
            String expectedMarshaller = "PersonMessageMarshaller";
            assertThat(persistenceFactoryImpl).isNotEmpty();
            assertThat(marshallerFiles.size()).isEqualTo(1);
            assertThat(marshallerFiles.get(0).relativePath()).endsWith(expectedMarshaller + ".java");
        }
        final CompilationUnit compilationUnit = parse(new ByteArrayInputStream(persistenceFactoryImpl.get().contents()));
        compilationUnit.findFirst(ClassOrInterfaceDeclaration.class).orElseThrow(() -> new NoSuchElementException("Compilation unit doesn't contain a class or interface declaration!"));
    }
}
Also used : ReflectionProtoGenerator(org.kie.kogito.codegen.process.persistence.proto.ReflectionProtoGenerator) KogitoBuildContext(org.kie.kogito.codegen.api.context.KogitoBuildContext) Collection(java.util.Collection) StaticJavaParser.parse(com.github.javaparser.StaticJavaParser.parse) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ReflectionMarshallerGenerator(org.kie.kogito.codegen.process.persistence.marshaller.ReflectionMarshallerGenerator) GeneratedPOJO(org.kie.kogito.codegen.data.GeneratedPOJO) KAFKA_PERSISTENCE_TYPE(org.kie.kogito.codegen.process.persistence.PersistenceGenerator.KAFKA_PERSISTENCE_TYPE) Collectors(java.util.stream.Collectors) List(java.util.List) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) ByteArrayInputStream(java.io.ByteArrayInputStream) PersistenceGenerator.hasProtoMarshaller(org.kie.kogito.codegen.process.persistence.PersistenceGenerator.hasProtoMarshaller) KOGITO_PERSISTENCE_TYPE(org.kie.kogito.codegen.process.persistence.PersistenceGenerator.KOGITO_PERSISTENCE_TYPE) Optional(java.util.Optional) GeneratedFile(org.kie.kogito.codegen.api.GeneratedFile) CompilationUnit(com.github.javaparser.ast.CompilationUnit) NoSuchElementException(java.util.NoSuchElementException) Collections(java.util.Collections) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) MethodSource(org.junit.jupiter.params.provider.MethodSource) CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) GeneratedFile(org.kie.kogito.codegen.api.GeneratedFile) ByteArrayInputStream(java.io.ByteArrayInputStream) ReflectionProtoGenerator(org.kie.kogito.codegen.process.persistence.proto.ReflectionProtoGenerator) ReflectionMarshallerGenerator(org.kie.kogito.codegen.process.persistence.marshaller.ReflectionMarshallerGenerator) GeneratedPOJO(org.kie.kogito.codegen.data.GeneratedPOJO) NoSuchElementException(java.util.NoSuchElementException) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 5 with ReflectionMarshallerGenerator

use of org.kie.kogito.codegen.process.persistence.marshaller.ReflectionMarshallerGenerator in project kogito-runtimes by kiegroup.

the class ProcessClassesMojo method execute.

@Override
public void execute() throws MojoExecutionException {
    try {
        JavaCompilerSettings settings = new JavaCompilerSettings();
        List<URL> pathUrls = new ArrayList<>();
        for (String path : project.getRuntimeClasspathElements()) {
            File pathFile = new File(path);
            pathUrls.add(pathFile.toURI().toURL());
            settings.addClasspath(pathFile);
        }
        URL[] urlsForClassLoader = pathUrls.toArray(new URL[pathUrls.size()]);
        // need to define parent classloader which knows all dependencies of the plugin
        try (URLClassLoader cl = new URLClassLoader(urlsForClassLoader, Thread.currentThread().getContextClassLoader())) {
            ConfigurationBuilder builder = new ConfigurationBuilder();
            builder.addUrls(cl.getURLs());
            builder.addClassLoader(cl);
            Reflections reflections = new Reflections(builder);
            @SuppressWarnings({ "rawtype", "unchecked" }) Set<Class<?>> modelClasses = (Set) reflections.getSubTypesOf(Model.class);
            // collect constructor parameters so the generated class can create constructor with injection
            Class<?> persistenceClass = reflections.getSubTypesOf(ProcessInstancesFactory.class).stream().filter(c -> !c.isInterface()).findFirst().orElse(null);
            ReflectionProtoGenerator protoGenerator = ReflectionProtoGenerator.builder().withPersistenceClass(persistenceClass).build(modelClasses);
            KogitoBuildContext context = discoverKogitoRuntimeContext(cl);
            // Generate persistence files
            PersistenceGenerator persistenceGenerator = new PersistenceGenerator(context, protoGenerator, new ReflectionMarshallerGenerator(context, protoGenerator.getDataClasses()));
            Collection<GeneratedFile> persistenceFiles = persistenceGenerator.generate();
            validateGeneratedFileTypes(persistenceFiles, asList(GeneratedFileType.Category.SOURCE, GeneratedFileType.Category.INTERNAL_RESOURCE, GeneratedFileType.Category.STATIC_HTTP_RESOURCE));
            Collection<GeneratedFile> generatedClasses = persistenceFiles.stream().filter(x -> x.category().equals(GeneratedFileType.Category.SOURCE)).collect(Collectors.toList());
            Collection<GeneratedFile> generatedResources = persistenceFiles.stream().filter(x -> x.category().equals(GeneratedFileType.Category.INTERNAL_RESOURCE) || x.category().equals(GeneratedFileType.Category.STATIC_HTTP_RESOURCE)).collect(Collectors.toList());
            // Compile and write persistence files
            compileAndWriteClasses(generatedClasses, cl, settings);
            // Dump resources
            generatedResources.forEach(this::writeGeneratedFile);
            // Json schema generation
            Stream<Class<?>> processClassStream = reflections.getTypesAnnotatedWith(ProcessInput.class).stream();
            generateJsonSchema(processClassStream).forEach(this::writeGeneratedFile);
            Stream<Class<?>> userTaskClassStream = reflections.getTypesAnnotatedWith(UserTask.class).stream();
            generateJsonSchema(userTaskClassStream).forEach(this::writeGeneratedFile);
        }
    } catch (Exception e) {
        throw new MojoExecutionException("Error during processing model classes", e);
    }
}
Also used : ReflectionProtoGenerator(org.kie.kogito.codegen.process.persistence.proto.ReflectionProtoGenerator) KiePath(org.kie.memorycompiler.resources.KiePath) Arrays(java.util.Arrays) URL(java.net.URL) CompilationResult(org.kie.memorycompiler.CompilationResult) MemoryFileSystem(org.drools.compiler.compiler.io.memory.MemoryFileSystem) UserTask(org.kie.kogito.UserTask) Parameter(org.apache.maven.plugins.annotations.Parameter) Reflections(org.reflections.Reflections) JavaCompilerFactory(org.kie.memorycompiler.JavaCompilerFactory) ArrayList(java.util.ArrayList) GeneratedFileValidation.validateGeneratedFileTypes(org.kie.kogito.codegen.core.utils.GeneratedFileValidation.validateGeneratedFileTypes) Mojo(org.apache.maven.plugins.annotations.Mojo) URLClassLoader(java.net.URLClassLoader) Arrays.asList(java.util.Arrays.asList) JsonSchemaGenerator(org.kie.kogito.codegen.json.JsonSchemaGenerator) ProcessInput(org.kie.kogito.ProcessInput) LifecyclePhase(org.apache.maven.plugins.annotations.LifecyclePhase) ResolutionScope(org.apache.maven.plugins.annotations.ResolutionScope) JavaCompiler(org.kie.memorycompiler.JavaCompiler) ConfigurationBuilder(org.reflections.util.ConfigurationBuilder) PersistenceGenerator(org.kie.kogito.codegen.process.persistence.PersistenceGenerator) KogitoBuildContext(org.kie.kogito.codegen.api.context.KogitoBuildContext) GeneratedFileType(org.kie.kogito.codegen.api.GeneratedFileType) Collection(java.util.Collection) Set(java.util.Set) ReflectionMarshallerGenerator(org.kie.kogito.codegen.process.persistence.marshaller.ReflectionMarshallerGenerator) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Collectors(java.util.stream.Collectors) File(java.io.File) MojoFailureException(org.apache.maven.plugin.MojoFailureException) List(java.util.List) Stream(java.util.stream.Stream) Model(org.kie.kogito.Model) GeneratedFile(org.kie.kogito.codegen.api.GeneratedFile) JavaCompilerSettings(org.kie.memorycompiler.JavaCompilerSettings) ProcessInstancesFactory(org.kie.kogito.process.ProcessInstancesFactory) JavaConfiguration(org.kie.memorycompiler.JavaConfiguration) ConfigurationBuilder(org.reflections.util.ConfigurationBuilder) Set(java.util.Set) ArrayList(java.util.ArrayList) URL(java.net.URL) GeneratedFile(org.kie.kogito.codegen.api.GeneratedFile) ReflectionProtoGenerator(org.kie.kogito.codegen.process.persistence.proto.ReflectionProtoGenerator) ProcessInput(org.kie.kogito.ProcessInput) Reflections(org.reflections.Reflections) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) PersistenceGenerator(org.kie.kogito.codegen.process.persistence.PersistenceGenerator) JavaCompilerSettings(org.kie.memorycompiler.JavaCompilerSettings) UserTask(org.kie.kogito.UserTask) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) URLClassLoader(java.net.URLClassLoader) Model(org.kie.kogito.Model) ReflectionMarshallerGenerator(org.kie.kogito.codegen.process.persistence.marshaller.ReflectionMarshallerGenerator) KogitoBuildContext(org.kie.kogito.codegen.api.context.KogitoBuildContext) File(java.io.File) GeneratedFile(org.kie.kogito.codegen.api.GeneratedFile)

Aggregations

Collection (java.util.Collection)8 GeneratedFile (org.kie.kogito.codegen.api.GeneratedFile)8 KogitoBuildContext (org.kie.kogito.codegen.api.context.KogitoBuildContext)8 ReflectionMarshallerGenerator (org.kie.kogito.codegen.process.persistence.marshaller.ReflectionMarshallerGenerator)8 ReflectionProtoGenerator (org.kie.kogito.codegen.process.persistence.proto.ReflectionProtoGenerator)8 Collections (java.util.Collections)7 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)7 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)7 MethodSource (org.junit.jupiter.params.provider.MethodSource)7 GeneratedPOJO (org.kie.kogito.codegen.data.GeneratedPOJO)7 KOGITO_PERSISTENCE_TYPE (org.kie.kogito.codegen.process.persistence.PersistenceGenerator.KOGITO_PERSISTENCE_TYPE)7 StaticJavaParser.parse (com.github.javaparser.StaticJavaParser.parse)6 CompilationUnit (com.github.javaparser.ast.CompilationUnit)6 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)6 ByteArrayInputStream (java.io.ByteArrayInputStream)6 Optional (java.util.Optional)6 List (java.util.List)5 NoSuchElementException (java.util.NoSuchElementException)5 PersistenceGenerator.hasProtoMarshaller (org.kie.kogito.codegen.process.persistence.PersistenceGenerator.hasProtoMarshaller)5 Collectors (java.util.stream.Collectors)4