Search in sources :

Example 1 with ProtoMessage

use of org.kie.kogito.codegen.process.persistence.proto.ProtoMessage in project kogito-runtimes by kiegroup.

the class JandexProtoGenerator method generateModelClassProto.

@Override
protected Optional<GeneratedFile> generateModelClassProto(ClassInfo modelClazz) {
    String processId = getReferenceOfModel(modelClazz, "reference");
    String name = getReferenceOfModel(modelClazz, "name");
    if (processId != null) {
        Proto modelProto = generate("@Indexed", INDEX_COMMENT, modelClazz.name().prefix().toString() + "." + processId, modelClazz, "import \"kogito-index.proto\";", "import \"kogito-types.proto\";", "option kogito_model = \"" + name + "\";", "option kogito_id = \"" + processId + "\";");
        if (modelProto.getMessages().isEmpty()) {
            // no messages, nothing to do
            return Optional.empty();
        }
        ProtoMessage modelMessage = modelProto.getMessages().stream().filter(msg -> msg.getName().equals(name)).findFirst().orElseThrow(() -> new IllegalStateException("Unable to find model message"));
        modelMessage.addField("optional", "org.kie.kogito.index.model.KogitoMetadata", "metadata").setComment(INDEX_COMMENT);
        return Optional.of(generateProtoFiles(processId, modelProto));
    }
    return Optional.empty();
}
Also used : Proto(org.kie.kogito.codegen.process.persistence.proto.Proto) ProtoMessage(org.kie.kogito.codegen.process.persistence.proto.ProtoMessage)

Example 2 with ProtoMessage

use of org.kie.kogito.codegen.process.persistence.proto.ProtoMessage in project kogito-runtimes by kiegroup.

the class JandexProtoGenerator method messageFromClass.

@Override
protected ProtoMessage messageFromClass(Proto proto, Set<String> alreadyGenerated, ClassInfo clazz, String messageComment, String fieldComment) throws Exception {
    Optional<String> optionalName = extractName(clazz);
    if (!optionalName.isPresent()) {
        // if name cannot be extracted let skip the object
        return null;
    }
    String name = optionalName.get();
    ProtoMessage message = new ProtoMessage(name, clazz.name().prefix().toString());
    for (FieldInfo pd : extractAllFields(clazz)) {
        // ignore static and/or transient fields
        if (Modifier.isStatic(pd.flags()) || Modifier.isTransient(pd.flags())) {
            continue;
        }
        // By default, only index id field from Model generated class
        String completeFieldComment = "id".equals(pd.name()) && clazz.interfaceTypes().stream().anyMatch(t -> t.name().equals(modelClazz)) ? fieldComment.replace("Index.NO", "Index.YES") : fieldComment;
        AnnotationInstance variableInfo = pd.annotation(variableInfoAnnotation);
        if (variableInfo != null) {
            completeFieldComment = fieldComment + "\n @VariableInfo(tags=\"" + variableInfo.value("tags").asString() + "\")";
        }
        String fieldTypeString = pd.type().name().toString();
        DotName fieldType = pd.type().name();
        String protoType;
        if (isArray(pd)) {
            fieldTypeString = "Array";
            fieldType = pd.type().asArrayType().component().name();
            protoType = protoType(fieldType.toString());
        } else if (isCollection(pd)) {
            fieldTypeString = "Collection";
            List<Type> typeParameters = pd.type().asParameterizedType().arguments();
            if (typeParameters.isEmpty()) {
                throw new IllegalArgumentException("Field " + pd.name() + " of class " + clazz.name().toString() + " uses collection without type information");
            }
            fieldType = typeParameters.get(0).name();
            protoType = protoType(fieldType.toString());
        } else {
            protoType = protoType(fieldTypeString);
        }
        if (protoType == null) {
            ClassInfo classInfo = index.getClassByName(fieldType);
            if (classInfo == null) {
                throw new IllegalStateException("Cannot find class info in jandex index for " + fieldType);
            }
            // recursive call to visit the type
            Optional<String> optionalProtoType = internalGenerate(proto, alreadyGenerated, messageComment, fieldComment, classInfo);
            if (!optionalProtoType.isPresent()) {
                return message;
            }
            protoType = optionalProtoType.get();
        }
        ProtoField protoField = message.addField(applicabilityByType(fieldTypeString), protoType, pd.name());
        protoField.setComment(completeFieldComment);
        if (KOGITO_SERIALIZABLE.equals(protoType)) {
            protoField.setOption(format("[(%s) = \"%s\"]", KOGITO_JAVA_CLASS_OPTION, fieldTypeString));
        }
    }
    message.setComment(messageComment);
    proto.addMessage(message);
    return message;
}
Also used : ProtoMessage(org.kie.kogito.codegen.process.persistence.proto.ProtoMessage) ProtoField(org.kie.kogito.codegen.process.persistence.proto.ProtoField) ArrayList(java.util.ArrayList) List(java.util.List) DotName(org.jboss.jandex.DotName) FieldInfo(org.jboss.jandex.FieldInfo) AnnotationInstance(org.jboss.jandex.AnnotationInstance) ClassInfo(org.jboss.jandex.ClassInfo)

Aggregations

ProtoMessage (org.kie.kogito.codegen.process.persistence.proto.ProtoMessage)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 AnnotationInstance (org.jboss.jandex.AnnotationInstance)1 ClassInfo (org.jboss.jandex.ClassInfo)1 DotName (org.jboss.jandex.DotName)1 FieldInfo (org.jboss.jandex.FieldInfo)1 Proto (org.kie.kogito.codegen.process.persistence.proto.Proto)1 ProtoField (org.kie.kogito.codegen.process.persistence.proto.ProtoField)1