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