Search in sources :

Example 1 with TemplatedGenerator

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

the class DecisionContainerGenerator method compilationUnit.

@Override
public CompilationUnit compilationUnit() {
    CompilationUnit compilationUnit = templatedGenerator.compilationUnitOrThrow("Invalid Template: No CompilationUnit");
    ClassOrInterfaceType applicationClass = StaticJavaParser.parseClassOrInterfaceType(applicationCanonicalName);
    final InitializerDeclaration staticDeclaration = compilationUnit.findFirst(InitializerDeclaration.class).orElseThrow(() -> new InvalidTemplateException(templatedGenerator, "Missing static block"));
    final MethodCallExpr initMethod = staticDeclaration.findFirst(MethodCallExpr.class, mtd -> "init".equals(mtd.getNameAsString())).orElseThrow(() -> new InvalidTemplateException(templatedGenerator, "Missing init() method"));
    setupPmmlIfAvailable(initMethod);
    setupExecIdSupplierVariable(initMethod);
    setupDecisionModelTransformerVariable(initMethod);
    for (CollectedResource resource : resources) {
        Optional<String> encoding = determineEncoding(resource);
        MethodCallExpr getResAsStream = getReadResourceMethod(applicationClass, resource);
        MethodCallExpr isr = new MethodCallExpr("readResource").addArgument(getResAsStream);
        encoding.map(StringLiteralExpr::new).ifPresent(isr::addArgument);
        initMethod.addArgument(isr);
    }
    return compilationUnit;
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) KogitoBuildContext(org.kie.kogito.codegen.api.context.KogitoBuildContext) InvalidTemplateException(org.kie.kogito.codegen.api.template.InvalidTemplateException) Logger(org.slf4j.Logger) AbstractApplicationSection(org.kie.kogito.codegen.core.AbstractApplicationSection) TemplatedGenerator(org.kie.kogito.codegen.api.template.TemplatedGenerator) Collection(java.util.Collection) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) LoggerFactory(org.slf4j.LoggerFactory) ReadResourceUtil.getReadResourceMethod(org.kie.kogito.codegen.decision.ReadResourceUtil.getReadResourceMethod) DmnExecutionIdSupplier(org.kie.kogito.dmn.DmnExecutionIdSupplier) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) ArrayList(java.util.ArrayList) InitializerDeclaration(com.github.javaparser.ast.body.InitializerDeclaration) List(java.util.List) StaticJavaParser(com.github.javaparser.StaticJavaParser) NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) CollectedResource(org.kie.kogito.codegen.api.io.CollectedResource) Optional(java.util.Optional) Expression(com.github.javaparser.ast.expr.Expression) BufferedReader(java.io.BufferedReader) CompilationUnit(com.github.javaparser.ast.CompilationUnit) CodegenUtils.newObject(org.kie.kogito.codegen.core.CodegenUtils.newObject) CollectedResource(org.kie.kogito.codegen.api.io.CollectedResource) InitializerDeclaration(com.github.javaparser.ast.body.InitializerDeclaration) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) InvalidTemplateException(org.kie.kogito.codegen.api.template.InvalidTemplateException) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 2 with TemplatedGenerator

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

the class PersistenceGenerator method kafkaBasedPersistence.

protected Collection<GeneratedFile> kafkaBasedPersistence() {
    ClassOrInterfaceDeclaration persistenceProviderClazz = new ClassOrInterfaceDeclaration().setName(KOGITO_PROCESS_INSTANCE_FACTORY_IMPL).setModifiers(Modifier.Keyword.PUBLIC).addExtendedType(KOGITO_PROCESS_INSTANCE_FACTORY_PACKAGE);
    if (context().hasDI()) {
        context().getDependencyInjectionAnnotator().withApplicationComponent(persistenceProviderClazz);
    }
    Collection<GeneratedFile> generatedFiles = new ArrayList<>();
    TemplatedGenerator generator = TemplatedGenerator.builder().withTemplateBasePath(CLASS_TEMPLATES_PERSISTENCE).withFallbackContext(JavaKogitoBuildContext.CONTEXT_NAME).withPackageName(KOGITO_PROCESS_INSTANCE_PACKAGE).build(context(), "KafkaStreamsTopologyProducer");
    CompilationUnit parsedClazzFile = generator.compilationUnitOrThrow();
    ClassOrInterfaceDeclaration producer = parsedClazzFile.findFirst(ClassOrInterfaceDeclaration.class).orElseThrow(() -> new InvalidTemplateException(generator, "Failed to find template for KafkaStreamsTopologyProducer"));
    MethodCallExpr asListOfProcesses = new MethodCallExpr(new NameExpr("java.util.Arrays"), "asList");
    protoGenerator.getProcessIds().forEach(p -> asListOfProcesses.addArgument(new StringLiteralExpr(p)));
    producer.getFieldByName("processes").orElseThrow(() -> new InvalidTemplateException(generator, "Failed to find field 'processes' in KafkaStreamsTopologyProducer template")).getVariable(0).setInitializer(asListOfProcesses);
    String clazzName = KOGITO_PROCESS_INSTANCE_PACKAGE + "." + producer.getName().asString();
    generatedFiles.add(new GeneratedFile(GeneratedFileType.SOURCE, clazzName.replace('.', '/') + JAVA, parsedClazzFile.toString()));
    CompilationUnit compilationUnit = new CompilationUnit(KOGITO_PROCESS_INSTANCE_PACKAGE);
    compilationUnit.getTypes().add(persistenceProviderClazz);
    generatePersistenceProviderClazz(persistenceProviderClazz, compilationUnit).ifPresent(generatedFiles::add);
    return generatedFiles;
}
Also used : TemplatedGenerator(org.kie.kogito.codegen.api.template.TemplatedGenerator) CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) ArrayList(java.util.ArrayList) NameExpr(com.github.javaparser.ast.expr.NameExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) GeneratedFile(org.kie.kogito.codegen.api.GeneratedFile) InvalidTemplateException(org.kie.kogito.codegen.api.template.InvalidTemplateException) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 3 with TemplatedGenerator

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

the class PersistenceGenerator method generateProtoMarshaller.

protected Collection<GeneratedFile> generateProtoMarshaller() {
    if (!hasProtoMarshaller(context())) {
        // TODO implement a validation check to verify that data classes implement Serializable
        LOGGER.debug("Proto marshaller generation is skipped because " + KOGITO_PERSISTENCE_PROTO_MARSHALLER + "=false");
        return Collections.emptyList();
    }
    Proto proto = protoGenerator.protoOfDataClasses(context().getPackageName(), "import \"kogito-types.proto\";");
    List<String> variableMarshallers = new ArrayList<>();
    String protoContent = proto.serialize();
    List<CompilationUnit> marshallers;
    try {
        marshallers = marshallerGenerator.generate(protoContent);
    } catch (IOException e) {
        throw new UncheckedIOException("Impossible to obtain marshaller CompilationUnits", e);
    }
    Collection<GeneratedFile> protoFiles = new ArrayList<>();
    try {
        String typesURI = "META-INF/kogito-types.proto";
        protoFiles.add(new GeneratedFile(GeneratedFileType.INTERNAL_RESOURCE, typesURI, IOUtils.toString(context().getClassLoader().getResourceAsStream(typesURI))));
    } catch (IOException e) {
        throw new UncheckedIOException("Cannot find kogito types protobuf!", e);
    }
    // generate proto files leads to problems as it has a reverse dependency of kogito-index
    String typesURI = "META-INF/application-types.proto";
    protoFiles.add(new GeneratedFile(GeneratedFileType.INTERNAL_RESOURCE, typesURI, protoContent));
    Collection<GeneratedFile> generatedFiles = new ArrayList<>(protoFiles);
    if (!marshallers.isEmpty()) {
        List<CompilationUnit> files = new ArrayList<>(marshallers);
        variableMarshallers.add("org.kie.kogito.persistence.StringProtostreamBaseMarshaller");
        variableMarshallers.add("org.kie.kogito.persistence.BooleanProtostreamBaseMarshaller");
        variableMarshallers.add("org.kie.kogito.persistence.DateProtostreamBaseMarshaller");
        variableMarshallers.add("org.kie.kogito.persistence.DoubleProtostreamBaseMarshaller");
        variableMarshallers.add("org.kie.kogito.persistence.FloatProtostreamBaseMarshaller");
        variableMarshallers.add("org.kie.kogito.persistence.IntegerProtostreamBaseMarshaller");
        variableMarshallers.add("org.kie.kogito.persistence.LongProtostreamBaseMarshaller");
        variableMarshallers.add("org.kie.kogito.persistence.InstantProtostreamBaseMarshaller");
        variableMarshallers.add("org.kie.kogito.persistence.SerializableProtostreamBaseMarshaller");
        for (CompilationUnit unit : files) {
            String packageName = unit.getPackageDeclaration().map(pd -> pd.getName().toString()).orElse("");
            Optional<ClassOrInterfaceDeclaration> clazz = unit.findFirst(ClassOrInterfaceDeclaration.class);
            clazz.ifPresent(c -> {
                String clazzName = packageName + "." + c.getName().toString();
                variableMarshallers.add(clazzName);
                generatedFiles.add(new GeneratedFile(GeneratedFileType.SOURCE, clazzName.replace('.', '/') + JAVA, unit.toString()));
            });
        }
        // we build the marshaller for protostream
        TemplatedGenerator generatorProtostreamSerialization = TemplatedGenerator.builder().withTemplateBasePath(CLASS_TEMPLATES_PERSISTENCE).withFallbackContext(JavaKogitoBuildContext.CONTEXT_NAME).withPackageName(KOGITO_PROCESS_INSTANCE_PACKAGE).build(context(), "ProtostreamObjectMarshaller");
        CompilationUnit parsedClazzFile = generatorProtostreamSerialization.compilationUnitOrThrow();
        String packageName = parsedClazzFile.getPackageDeclaration().map(pd -> pd.getName().toString()).orElse("");
        ClassOrInterfaceDeclaration clazz = parsedClazzFile.findFirst(ClassOrInterfaceDeclaration.class).orElseThrow(() -> new InvalidTemplateException(generatorProtostreamSerialization, "Failed to find template for ProtostreamObjectMarshaller"));
        ConstructorDeclaration constructor = clazz.getDefaultConstructor().orElseThrow(() -> new InvalidTemplateException(generatorProtostreamSerialization, "Failed to find default constructor in template for ProtostreamObjectMarshaller"));
        // register protofiles and marshallers
        BlockStmt body = new BlockStmt();
        Expression newFileDescriptorSource = new ObjectCreationExpr(null, new ClassOrInterfaceType(null, FileDescriptorSource.class.getCanonicalName()), NodeList.nodeList());
        Expression getClassLoader = new MethodCallExpr(new MethodCallExpr(null, "getClass", NodeList.nodeList()), "getClassLoader", NodeList.nodeList());
        Expression chainExpression = newFileDescriptorSource;
        for (GeneratedFile generatedFile : protoFiles) {
            String path = generatedFile.relativePath();
            String name = generatedFile.path().getFileName().toString();
            if (!name.endsWith(".proto")) {
                continue;
            }
            Expression getISKogito = new MethodCallExpr(getClassLoader, "getResourceAsStream", NodeList.nodeList(new StringLiteralExpr(path)));
            chainExpression = new MethodCallExpr(new EnclosedExpr(chainExpression), "addProtoFile", NodeList.nodeList(new StringLiteralExpr(name), getISKogito));
        }
        body.addStatement(new MethodCallExpr(new NameExpr("context"), "registerProtoFiles", NodeList.nodeList(chainExpression)));
        for (String baseMarshallers : variableMarshallers) {
            Expression newMarshallerExpr = new ObjectCreationExpr(null, new ClassOrInterfaceType(null, baseMarshallers), NodeList.nodeList());
            body.addStatement(new MethodCallExpr(new NameExpr("context"), "registerMarshaller", NodeList.nodeList(newMarshallerExpr)));
        }
        CatchClause catchClause = new CatchClause(new Parameter().setType(IOException.class).setName("e"), new BlockStmt());
        TryStmt tryStmt = new TryStmt(body, NodeList.nodeList(catchClause), null);
        constructor.getBody().addStatement(tryStmt);
        String fqnProtoStreamMarshaller = packageName + "." + clazz.getName().toString();
        generatedFiles.add(new GeneratedFile(GeneratedFileType.SOURCE, fqnProtoStreamMarshaller.replace('.', '/') + JAVA, parsedClazzFile.toString()));
        String objectMarshallerStrategyServiceDescriptor = "";
        try {
            // try to find an existing ObjectMarshallerStrategy descriptor in the classpath to be appended to the ProtoStream generated one
            objectMarshallerStrategyServiceDescriptor = IOUtils.toString(getClass().getResourceAsStream("/META-INF/services/org.kie.kogito.serialization.process.ObjectMarshallerStrategy"), "UTF-8");
        } catch (Exception e) {
            LOGGER.warn("No existing ObjectMarshallerStrategy found the the classpath to be included with the ProtoS generated one for SPI.");
        }
        objectMarshallerStrategyServiceDescriptor += "\n" + fqnProtoStreamMarshaller + "\n";
        generatedFiles.add(new GeneratedFile(GeneratedFileType.INTERNAL_RESOURCE, "META-INF/services/org.kie.kogito.serialization.process.ObjectMarshallerStrategy", objectMarshallerStrategyServiceDescriptor));
    }
    return generatedFiles;
}
Also used : ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) LoggerFactory(org.slf4j.LoggerFactory) ProtoGenerator(org.kie.kogito.codegen.process.persistence.proto.ProtoGenerator) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) Expression(com.github.javaparser.ast.expr.Expression) CompilationUnit(com.github.javaparser.ast.CompilationUnit) NodeList(com.github.javaparser.ast.NodeList) SimpleName(com.github.javaparser.ast.expr.SimpleName) KogitoBuildContext(org.kie.kogito.codegen.api.context.KogitoBuildContext) GeneratedFileType(org.kie.kogito.codegen.api.GeneratedFileType) Collection(java.util.Collection) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) ApplicationSection(org.kie.kogito.codegen.api.ApplicationSection) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) EnclosedExpr(com.github.javaparser.ast.expr.EnclosedExpr) ExplicitConstructorInvocationStmt(com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt) Modifier(com.github.javaparser.ast.Modifier) UncheckedIOException(java.io.UncheckedIOException) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) StaticJavaParser(com.github.javaparser.StaticJavaParser) QuarkusKogitoBuildContext(org.kie.kogito.codegen.api.context.impl.QuarkusKogitoBuildContext) TryStmt(com.github.javaparser.ast.stmt.TryStmt) Optional(java.util.Optional) GeneratedFile(org.kie.kogito.codegen.api.GeneratedFile) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) ConditionalExpr(com.github.javaparser.ast.expr.ConditionalExpr) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) SpringBootKogitoBuildContext(org.kie.kogito.codegen.api.context.impl.SpringBootKogitoBuildContext) Parameter(com.github.javaparser.ast.body.Parameter) BodyDeclarationComparator(org.kie.kogito.codegen.core.BodyDeclarationComparator) ArrayList(java.util.ArrayList) Proto(org.kie.kogito.codegen.process.persistence.proto.Proto) CatchClause(com.github.javaparser.ast.stmt.CatchClause) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) AbstractGenerator(org.kie.kogito.codegen.core.AbstractGenerator) Keyword(com.github.javaparser.ast.Modifier.Keyword) MarshallerGenerator(org.kie.kogito.codegen.process.persistence.marshaller.MarshallerGenerator) FileDescriptorSource(org.infinispan.protostream.FileDescriptorSource) InvalidTemplateException(org.kie.kogito.codegen.api.template.InvalidTemplateException) Logger(org.slf4j.Logger) JavaKogitoBuildContext(org.kie.kogito.codegen.api.context.impl.JavaKogitoBuildContext) BooleanLiteralExpr(com.github.javaparser.ast.expr.BooleanLiteralExpr) TemplatedGenerator(org.kie.kogito.codegen.api.template.TemplatedGenerator) IOException(java.io.IOException) NameExpr(com.github.javaparser.ast.expr.NameExpr) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) Collections(java.util.Collections) TemplatedGenerator(org.kie.kogito.codegen.api.template.TemplatedGenerator) ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) ArrayList(java.util.ArrayList) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) UncheckedIOException(java.io.UncheckedIOException) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) GeneratedFile(org.kie.kogito.codegen.api.GeneratedFile) Proto(org.kie.kogito.codegen.process.persistence.proto.Proto) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) CompilationUnit(com.github.javaparser.ast.CompilationUnit) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) TryStmt(com.github.javaparser.ast.stmt.TryStmt) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) CatchClause(com.github.javaparser.ast.stmt.CatchClause) InvalidTemplateException(org.kie.kogito.codegen.api.template.InvalidTemplateException) UncheckedIOException(java.io.UncheckedIOException) InvalidTemplateException(org.kie.kogito.codegen.api.template.InvalidTemplateException) IOException(java.io.IOException) Expression(com.github.javaparser.ast.expr.Expression) Parameter(com.github.javaparser.ast.body.Parameter) EnclosedExpr(com.github.javaparser.ast.expr.EnclosedExpr) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 4 with TemplatedGenerator

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

the class AbstractMarshallerGenerator method generate.

public List<CompilationUnit> generate(FileDescriptorSource proto) throws IOException {
    List<CompilationUnit> units = new ArrayList<>();
    TemplatedGenerator generator = TemplatedGenerator.builder().withFallbackContext(JavaKogitoBuildContext.CONTEXT_NAME).withTemplateBasePath(TEMPLATE_PERSISTENCE_FOLDER).build(context, "MessageMarshaller");
    Predicate<String> typeExclusions = ExclusionTypeUtils.createTypeExclusions();
    // filter types that don't require to create a marshaller
    Predicate<Descriptor> packagePredicate = (msg) -> !msg.getFileDescriptor().getPackage().equals("kogito");
    Predicate<Descriptor> jacksonPredicate = (msg) -> !typeExclusions.test(packageFromOption(msg.getFileDescriptor(), msg) + "." + msg.getName());
    Predicate<Descriptor> predicate = packagePredicate.and(jacksonPredicate);
    CompilationUnit parsedClazzFile = generator.compilationUnitOrThrow();
    SerializationContext serializationContext = new SerializationContextImpl(Configuration.builder().build());
    FileDescriptorSource kogitoTypesDescriptor = new FileDescriptorSource().addProtoFile("kogito-types.proto", context.getClassLoader().getResourceAsStream("META-INF/kogito-types.proto"));
    serializationContext.registerProtoFiles(kogitoTypesDescriptor);
    serializationContext.registerProtoFiles(proto);
    Map<String, FileDescriptor> descriptors = serializationContext.getFileDescriptors();
    for (Entry<String, FileDescriptor> entry : descriptors.entrySet()) {
        FileDescriptor d = entry.getValue();
        List<Descriptor> messages = d.getMessageTypes().stream().filter(predicate).collect(Collectors.toList());
        for (Descriptor msg : messages) {
            CompilationUnit clazzFile = parsedClazzFile.clone();
            units.add(clazzFile);
            String javaType = packageFromOption(d, msg) + "." + msg.getName();
            clazzFile.setPackageDeclaration(d.getPackage());
            ClassOrInterfaceDeclaration clazz = clazzFile.findFirst(ClassOrInterfaceDeclaration.class, sl -> true).orElseThrow(() -> new InvalidTemplateException(generator, "No class found"));
            clazz.setName(msg.getName() + "MessageMarshaller");
            clazz.getImplementedTypes(0).setTypeArguments(NodeList.nodeList(new ClassOrInterfaceType(null, javaType)));
            MethodDeclaration getJavaClassMethod = clazz.findFirst(MethodDeclaration.class, md -> md.getNameAsString().equals("getJavaClass")).orElseThrow(() -> new InvalidTemplateException(generator, "No getJavaClass method found"));
            getJavaClassMethod.setType(new ClassOrInterfaceType(null, new SimpleName(Class.class.getName()), NodeList.nodeList(new ClassOrInterfaceType(null, javaType))));
            BlockStmt getJavaClassMethodBody = new BlockStmt();
            getJavaClassMethodBody.addStatement(new ReturnStmt(new NameExpr(javaType + ".class")));
            getJavaClassMethod.setBody(getJavaClassMethodBody);
            MethodDeclaration getTypeNameMethod = clazz.findFirst(MethodDeclaration.class, md -> md.getNameAsString().equals("getTypeName")).orElseThrow(() -> new InvalidTemplateException(generator, "No getTypeName method found"));
            BlockStmt getTypeNameMethodBody = new BlockStmt();
            getTypeNameMethodBody.addStatement(new ReturnStmt(new StringLiteralExpr(msg.getFullName())));
            getTypeNameMethod.setBody(getTypeNameMethodBody);
            MethodDeclaration readFromMethod = clazz.findFirst(MethodDeclaration.class, md -> md.getNameAsString().equals("readFrom")).orElseThrow(() -> new InvalidTemplateException(generator, "No readFrom method found"));
            readFromMethod.setType(javaType);
            readFromMethod.setBody(new BlockStmt());
            MethodDeclaration writeToMethod = clazz.findFirst(MethodDeclaration.class, md -> md.getNameAsString().equals("writeTo")).orElseThrow(() -> new InvalidTemplateException(generator, "No writeTo method found"));
            writeToMethod.getParameter(1).setType(javaType);
            writeToMethod.setBody(new BlockStmt());
            ClassOrInterfaceType classType = new ClassOrInterfaceType(null, javaType);
            // read method
            VariableDeclarationExpr instance = new VariableDeclarationExpr(new VariableDeclarator(classType, "value", new ObjectCreationExpr(null, classType, NodeList.nodeList())));
            readFromMethod.getBody().ifPresent(b -> b.addStatement(instance));
            for (FieldDescriptor field : msg.getFields()) {
                String protoStreamMethodType = protoStreamMethodType(field.getTypeName());
                Expression write = null;
                Expression read = null;
                if (protoStreamMethodType != null && !field.isRepeated()) {
                    // has a mapped type
                    read = new MethodCallExpr(new NameExpr("reader"), "read" + protoStreamMethodType).addArgument(new StringLiteralExpr(field.getName()));
                    String accessor = protoStreamMethodType.equals("Boolean") ? "is" : "get";
                    write = new MethodCallExpr(new NameExpr("writer"), "write" + protoStreamMethodType).addArgument(new StringLiteralExpr(field.getName())).addArgument(new MethodCallExpr(new NameExpr("t"), accessor + StringUtils.ucFirst(field.getName())));
                } else {
                    // custom types
                    String customTypeName = javaTypeForMessage(d, field.getTypeName(), serializationContext);
                    if (field.isRepeated()) {
                        if (null == customTypeName || customTypeName.isEmpty()) {
                            customTypeName = primaryTypeClassName(field.getTypeName());
                        }
                        String writeMethod;
                        if (isArray(javaType, field)) {
                            writeMethod = "writeArray";
                            read = new MethodCallExpr(new NameExpr("reader"), "readArray").addArgument(new StringLiteralExpr(field.getName())).addArgument(new NameExpr(customTypeName + ".class"));
                        } else {
                            writeMethod = "writeCollection";
                            read = new MethodCallExpr(new NameExpr("reader"), "readCollection").addArgument(new StringLiteralExpr(field.getName())).addArgument(new ObjectCreationExpr(null, new ClassOrInterfaceType(null, ArrayList.class.getCanonicalName()), NodeList.nodeList())).addArgument(new NameExpr(customTypeName + ".class"));
                        }
                        write = new MethodCallExpr(new NameExpr("writer"), writeMethod).addArgument(new StringLiteralExpr(field.getName())).addArgument(new MethodCallExpr(new NameExpr("t"), "get" + StringUtils.ucFirst(field.getName()))).addArgument(new NameExpr(customTypeName + ".class"));
                    } else {
                        read = new MethodCallExpr(new NameExpr("reader"), "readObject").addArgument(new StringLiteralExpr(field.getName())).addArgument(new NameExpr(customTypeName + ".class"));
                        write = new MethodCallExpr(new NameExpr("writer"), "writeObject").addArgument(new StringLiteralExpr(field.getName())).addArgument(new MethodCallExpr(new NameExpr("t"), "get" + StringUtils.ucFirst(field.getName()))).addArgument(new NameExpr(customTypeName + ".class"));
                    }
                    if (customTypeName.equals(Serializable.class.getName())) {
                        String fieldClazz = (String) field.getOptionByName(KOGITO_JAVA_CLASS_OPTION);
                        if (fieldClazz == null) {
                            throw new IllegalArgumentException(format("Serializable proto field '%s' is missing value for option %s", field.getName(), KOGITO_JAVA_CLASS_OPTION));
                        } else {
                            read = new CastExpr().setExpression(new EnclosedExpr(read)).setType(fieldClazz);
                        }
                    }
                }
                MethodCallExpr setter = new MethodCallExpr(new NameExpr("value"), "set" + StringUtils.ucFirst(field.getName())).addArgument(read);
                readFromMethod.getBody().ifPresent(b -> b.addStatement(setter));
                // write method
                writeToMethod.getBody().orElseThrow(() -> new NoSuchElementException("A method declaration doesn't contain a body!")).addStatement(write);
            }
            readFromMethod.getBody().ifPresent(b -> b.addStatement(new ReturnStmt(new NameExpr("value"))));
            clazz.getMembers().sort(new BodyDeclarationComparator());
        }
        for (EnumDescriptor msg : d.getEnumTypes()) {
            CompilationUnit compilationUnit = new CompilationUnit();
            units.add(compilationUnit);
            String javaType = packageFromOption(d, msg) + "." + msg.getName();
            ClassOrInterfaceDeclaration classDeclaration = compilationUnit.setPackageDeclaration(d.getPackage()).addClass(msg.getName() + "EnumMarshaller").setPublic(true);
            classDeclaration.addImplementedType(EnumMarshaller.class).getImplementedTypes(0).setTypeArguments(NodeList.nodeList(new ClassOrInterfaceType(null, javaType)));
            classDeclaration.addMethod("getTypeName", PUBLIC).setType(String.class).setBody(new BlockStmt().addStatement(new ReturnStmt(new StringLiteralExpr(msg.getFullName()))));
            classDeclaration.addMethod("getJavaClass", PUBLIC).setType(new ClassOrInterfaceType(null, new SimpleName(Class.class.getName()), NodeList.nodeList(new ClassOrInterfaceType(null, javaType)))).setBody(new BlockStmt().addStatement(new ReturnStmt(new ClassExpr(new ClassOrInterfaceType(null, javaType)))));
            BlockStmt encodeBlock = new BlockStmt().addStatement(new IfStmt(new BinaryExpr(new NullLiteralExpr(), new NameExpr(STATE_PARAM), EQUALS), new ThrowStmt(new ObjectCreationExpr(null, new ClassOrInterfaceType(null, IllegalArgumentException.class.getName()), NodeList.nodeList(new StringLiteralExpr("Invalid value provided to enum")))), null)).addStatement(new ReturnStmt(new MethodCallExpr(new NameExpr(STATE_PARAM), "ordinal")));
            classDeclaration.addMethod("encode", PUBLIC).setType("int").addParameter(javaType, STATE_PARAM).setBody(encodeBlock);
            MethodDeclaration decode = classDeclaration.addMethod("decode", PUBLIC).setType(javaType).addParameter("int", "value");
            SwitchStmt decodeSwitch = new SwitchStmt().setSelector(new NameExpr("value"));
            msg.getValues().forEach(v -> {
                SwitchEntry dEntry = new SwitchEntry();
                dEntry.getLabels().add(new IntegerLiteralExpr(v.getNumber()));
                dEntry.addStatement(new ReturnStmt(new NameExpr(javaType + "." + v.getName())));
                decodeSwitch.getEntries().add(dEntry);
            });
            decodeSwitch.getEntries().add(new SwitchEntry().addStatement(new ThrowStmt(new ObjectCreationExpr(null, new ClassOrInterfaceType(null, IllegalArgumentException.class.getName()), NodeList.nodeList(new StringLiteralExpr("Invalid value provided to enum"))))));
            decode.setBody(new BlockStmt().addStatement(decodeSwitch));
        }
    }
    return units;
}
Also used : TemplatedGenerator(org.kie.kogito.codegen.api.template.TemplatedGenerator) ClassExpr(com.github.javaparser.ast.expr.ClassExpr) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) EQUALS(com.github.javaparser.ast.expr.BinaryExpr.Operator.EQUALS) ExclusionTypeUtils(org.kie.kogito.codegen.process.persistence.ExclusionTypeUtils) ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) Map(java.util.Map) SerializationContextImpl(org.infinispan.protostream.impl.SerializationContextImpl) Expression(com.github.javaparser.ast.expr.Expression) CompilationUnit(com.github.javaparser.ast.CompilationUnit) BinaryExpr(com.github.javaparser.ast.expr.BinaryExpr) NodeList(com.github.javaparser.ast.NodeList) SimpleName(com.github.javaparser.ast.expr.SimpleName) KogitoBuildContext(org.kie.kogito.codegen.api.context.KogitoBuildContext) Predicate(java.util.function.Predicate) Collection(java.util.Collection) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) KOGITO_JAVA_CLASS_OPTION(org.kie.kogito.codegen.process.persistence.proto.ProtoGenerator.KOGITO_JAVA_CLASS_OPTION) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor) EnclosedExpr(com.github.javaparser.ast.expr.EnclosedExpr) String.format(java.lang.String.format) Serializable(java.io.Serializable) VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) ThrowStmt(com.github.javaparser.ast.stmt.ThrowStmt) List(java.util.List) Entry(java.util.Map.Entry) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) StringUtils(org.drools.core.util.StringUtils) SwitchEntry(com.github.javaparser.ast.stmt.SwitchEntry) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) EnumMarshaller(org.infinispan.protostream.EnumMarshaller) Descriptor(org.infinispan.protostream.descriptors.Descriptor) BodyDeclarationComparator(org.kie.kogito.codegen.core.BodyDeclarationComparator) CastExpr(com.github.javaparser.ast.expr.CastExpr) PUBLIC(com.github.javaparser.ast.Modifier.Keyword.PUBLIC) ArrayList(java.util.ArrayList) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) EnumDescriptor(org.infinispan.protostream.descriptors.EnumDescriptor) Option(org.infinispan.protostream.descriptors.Option) SwitchStmt(com.github.javaparser.ast.stmt.SwitchStmt) NoSuchElementException(java.util.NoSuchElementException) FileDescriptorSource(org.infinispan.protostream.FileDescriptorSource) FileDescriptor(org.infinispan.protostream.descriptors.FileDescriptor) InvalidTemplateException(org.kie.kogito.codegen.api.template.InvalidTemplateException) JavaKogitoBuildContext(org.kie.kogito.codegen.api.context.impl.JavaKogitoBuildContext) TemplatedGenerator(org.kie.kogito.codegen.api.template.TemplatedGenerator) IOException(java.io.IOException) NameExpr(com.github.javaparser.ast.expr.NameExpr) IfStmt(com.github.javaparser.ast.stmt.IfStmt) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) Configuration(org.infinispan.protostream.config.Configuration) Collections(java.util.Collections) SerializationContext(org.infinispan.protostream.SerializationContext) IntegerLiteralExpr(com.github.javaparser.ast.expr.IntegerLiteralExpr) SerializationContext(org.infinispan.protostream.SerializationContext) ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) IntegerLiteralExpr(com.github.javaparser.ast.expr.IntegerLiteralExpr) Serializable(java.io.Serializable) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) SimpleName(com.github.javaparser.ast.expr.SimpleName) ArrayList(java.util.ArrayList) NameExpr(com.github.javaparser.ast.expr.NameExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor) SerializationContextImpl(org.infinispan.protostream.impl.SerializationContextImpl) CastExpr(com.github.javaparser.ast.expr.CastExpr) BodyDeclarationComparator(org.kie.kogito.codegen.core.BodyDeclarationComparator) SwitchEntry(com.github.javaparser.ast.stmt.SwitchEntry) FileDescriptorSource(org.infinispan.protostream.FileDescriptorSource) CompilationUnit(com.github.javaparser.ast.CompilationUnit) VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) SwitchStmt(com.github.javaparser.ast.stmt.SwitchStmt) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) BinaryExpr(com.github.javaparser.ast.expr.BinaryExpr) EnumDescriptor(org.infinispan.protostream.descriptors.EnumDescriptor) FileDescriptor(org.infinispan.protostream.descriptors.FileDescriptor) InvalidTemplateException(org.kie.kogito.codegen.api.template.InvalidTemplateException) NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) IfStmt(com.github.javaparser.ast.stmt.IfStmt) Expression(com.github.javaparser.ast.expr.Expression) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor) Descriptor(org.infinispan.protostream.descriptors.Descriptor) EnumDescriptor(org.infinispan.protostream.descriptors.EnumDescriptor) FileDescriptor(org.infinispan.protostream.descriptors.FileDescriptor) ClassExpr(com.github.javaparser.ast.expr.ClassExpr) EnclosedExpr(com.github.javaparser.ast.expr.EnclosedExpr) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) ThrowStmt(com.github.javaparser.ast.stmt.ThrowStmt) NoSuchElementException(java.util.NoSuchElementException) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 5 with TemplatedGenerator

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

the class SampleCodegen method generate.

@Override
public Collection<GeneratedFile> generate() {
    if (isEmpty()) {
        return Collections.emptyList();
    }
    TemplatedGenerator generator = TemplatedGenerator.builder().withFallbackContext(QuarkusKogitoBuildContext.CONTEXT_NAME).build(context(), "SampleRestResource");
    CompilationUnit compilationUnit = generator.compilationUnitOrThrow();
    if (context.hasDI()) {
        compilationUnit.findAll(FieldDeclaration.class, SampleCodegen::isSampleRuntimeField).forEach(fd -> context.getDependencyInjectionAnnotator().withInjection(fd));
    } else {
        compilationUnit.findAll(FieldDeclaration.class, SampleCodegen::isSampleRuntimeField).forEach(SampleCodegen::initializeSampleRuntimeField);
    }
    return context.hasRESTForGenerator(this) ? Collections.singleton(new GeneratedFile(REST_TYPE, generator.generatedFilePath(), compilationUnit.toString())) : Collections.emptyList();
}
Also used : TemplatedGenerator(org.kie.kogito.codegen.api.template.TemplatedGenerator) CompilationUnit(com.github.javaparser.ast.CompilationUnit) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) GeneratedFile(org.kie.kogito.codegen.api.GeneratedFile)

Aggregations

CompilationUnit (com.github.javaparser.ast.CompilationUnit)8 TemplatedGenerator (org.kie.kogito.codegen.api.template.TemplatedGenerator)8 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)6 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)6 Collection (java.util.Collection)6 KogitoBuildContext (org.kie.kogito.codegen.api.context.KogitoBuildContext)6 InvalidTemplateException (org.kie.kogito.codegen.api.template.InvalidTemplateException)6 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)5 ArrayList (java.util.ArrayList)5 Expression (com.github.javaparser.ast.expr.Expression)4 NameExpr (com.github.javaparser.ast.expr.NameExpr)4 ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)4 List (java.util.List)4 GeneratedFile (org.kie.kogito.codegen.api.GeneratedFile)4 StaticJavaParser (com.github.javaparser.StaticJavaParser)3 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)3 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)3 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)3 ReturnStmt (com.github.javaparser.ast.stmt.ReturnStmt)3 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)3