Search in sources :

Example 11 with GeneratedFile

use of org.kie.kogito.codegen.api.GeneratedFile in project kogito-runtimes by kiegroup.

the class PersistenceGenerator method fileSystemBasedPersistence.

protected Collection<GeneratedFile> fileSystemBasedPersistence() {
    ClassOrInterfaceDeclaration persistenceProviderClazz = new ClassOrInterfaceDeclaration().setName(KOGITO_PROCESS_INSTANCE_FACTORY_IMPL).setModifiers(Modifier.Keyword.PUBLIC).addExtendedType(KOGITO_PROCESS_INSTANCE_FACTORY_PACKAGE);
    Optional<GeneratedFile> generatedClientFile = Optional.empty();
    if (context().hasDI()) {
        context().getDependencyInjectionAnnotator().withApplicationComponent(persistenceProviderClazz);
        FieldDeclaration pathField = new FieldDeclaration().addVariable(new VariableDeclarator().setType(new ClassOrInterfaceType(null, new SimpleName(Optional.class.getCanonicalName()), NodeList.nodeList(new ClassOrInterfaceType(null, String.class.getCanonicalName())))).setName(PATH_NAME));
        context().getDependencyInjectionAnnotator().withConfigInjection(pathField, KOGITO_PERSISTENCE_FILESYSTEM_PATH);
        // allow to inject path for the file system storage
        BlockStmt pathMethodBody = new BlockStmt();
        pathMethodBody.addStatement(new ReturnStmt(new MethodCallExpr(new NameExpr(PATH_NAME), OR_ELSE).addArgument(new StringLiteralExpr("/tmp"))));
        MethodDeclaration pathMethod = new MethodDeclaration().addModifier(Keyword.PUBLIC).setName(PATH_NAME).setType(String.class).setBody(pathMethodBody);
        persistenceProviderClazz.addMember(pathField);
        persistenceProviderClazz.addMember(pathMethod);
        generatedClientFile = generatePersistenceProviderClazz(persistenceProviderClazz, new CompilationUnit(KOGITO_PROCESS_INSTANCE_PACKAGE).addType(persistenceProviderClazz));
    }
    Collection<GeneratedFile> generatedFiles = new ArrayList<>();
    generatedClientFile.ifPresent(generatedFiles::add);
    return generatedFiles;
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) Optional(java.util.Optional) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) SimpleName(com.github.javaparser.ast.expr.SimpleName) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) NameExpr(com.github.javaparser.ast.expr.NameExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) ArrayList(java.util.ArrayList) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) GeneratedFile(org.kie.kogito.codegen.api.GeneratedFile) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 12 with GeneratedFile

use of org.kie.kogito.codegen.api.GeneratedFile in project kogito-runtimes by kiegroup.

the class KogitoAssetsProcessor method generateModel.

@BuildStep
public List<KogitoGeneratedClassesBuildItem> generateModel(KogitoGeneratedSourcesBuildItem sources, List<KogitoAddonsGeneratedSourcesBuildItem> extraSources, KogitoBuildContextBuildItem contextBuildItem, BuildProducer<GeneratedBeanBuildItem> generatedBeans, BuildProducer<GeneratedJaxRsResourceBuildItem> jaxrsProducer, BuildProducer<AdditionalStaticResourceBuildItem> staticResProducer, BuildProducer<NativeImageResourceBuildItem> resource, BuildProducer<ReflectiveClassBuildItem> reflectiveClass, BuildProducer<GeneratedResourceBuildItem> genResBI) throws IOException {
    final KogitoBuildContext context = contextBuildItem.getKogitoBuildContext();
    Collection<GeneratedFile> generatedFiles = collectGeneratedFiles(sources, extraSources);
    // dump files to disk
    dumpFilesToDisk(context.getAppPaths(), generatedFiles);
    // build Java source code and register the generated beans
    Optional<KogitoGeneratedClassesBuildItem> optionalIndex = compileAndIndexJavaSources(context, generatedFiles, generatedBeans, jaxrsProducer, liveReload.isLiveReload());
    registerDataEventsForReflection(optionalIndex.map(KogitoGeneratedClassesBuildItem::getIndexedClasses), context, reflectiveClass);
    registerKogitoIncubationAPI(reflectiveClass);
    registerResources(generatedFiles, staticResProducer, resource, genResBI);
    return optionalIndex.map(Collections::singletonList).orElse(Collections.emptyList());
}
Also used : KogitoBuildContext(org.kie.kogito.codegen.api.context.KogitoBuildContext) GeneratedFile(org.kie.kogito.codegen.api.GeneratedFile) BuildStep(io.quarkus.deployment.annotations.BuildStep)

Example 13 with GeneratedFile

use of org.kie.kogito.codegen.api.GeneratedFile in project kogito-runtimes by kiegroup.

the class InMemoryCompiler method compile.

/**
 * Compiles the given {@link GeneratedFile}s and returns the compilation results.
 * It throws an {@link IllegalStateException} if there are errors.
 */
public CompilationResult compile(Collection<GeneratedFile> generatedFiles) {
    MemoryFileSystem srcMfs = new MemoryFileSystem();
    String[] sources = new String[generatedFiles.size()];
    int index = 0;
    for (GeneratedFile entry : generatedFiles) {
        String relativePath = entry.relativePath();
        logger.trace("Relative path {}", relativePath);
        // verify if this is still needed https://issues.redhat.com/browse/KOGITO-3085
        String generatedClassFile = relativePath.replace("src/main/java/", "");
        logger.trace("generatedClassFile {}", generatedClassFile);
        String fileName = toRuntimeSource(toClassName(generatedClassFile));
        logger.trace("fileName {}", fileName);
        sources[index++] = fileName;
        srcMfs.write(fileName, entry.contents());
    }
    CompilationResult result = javaCompiler.compile(sources, srcMfs, trgMfs, Thread.currentThread().getContextClassLoader(), compilerSettings);
    if (result.getErrors().length > 0) {
        StringBuilder errorInfo = new StringBuilder();
        for (CompilationProblem compilationProblem : result.getErrors()) {
            errorInfo.append(compilationProblem.toString());
            errorInfo.append("\n");
            logger.error(compilationProblem.toString());
        }
        Arrays.stream(result.getErrors()).forEach(cp -> errorInfo.append(cp.toString()));
        throw new IllegalStateException(errorInfo.toString());
    }
    return result;
}
Also used : MemoryFileSystem(org.drools.compiler.compiler.io.memory.MemoryFileSystem) CompilationResult(org.kie.memorycompiler.CompilationResult) CompilationProblem(org.kie.memorycompiler.CompilationProblem) GeneratedFile(org.kie.kogito.codegen.api.GeneratedFile)

Aggregations

GeneratedFile (org.kie.kogito.codegen.api.GeneratedFile)13 ArrayList (java.util.ArrayList)8 CompilationUnit (com.github.javaparser.ast.CompilationUnit)7 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)7 NameExpr (com.github.javaparser.ast.expr.NameExpr)7 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)6 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)6 ConstructorDeclaration (com.github.javaparser.ast.body.ConstructorDeclaration)5 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)5 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)5 ExplicitConstructorInvocationStmt (com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt)5 ReturnStmt (com.github.javaparser.ast.stmt.ReturnStmt)5 KogitoBuildContext (org.kie.kogito.codegen.api.context.KogitoBuildContext)5 FieldDeclaration (com.github.javaparser.ast.body.FieldDeclaration)4 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)4 SimpleName (com.github.javaparser.ast.expr.SimpleName)4 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)4 Optional (java.util.Optional)4 Parameter (com.github.javaparser.ast.body.Parameter)3 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)3