Search in sources :

Example 1 with ProtoField

use of org.kie.kogito.codegen.process.persistence.proto.ProtoField 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

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 ProtoField (org.kie.kogito.codegen.process.persistence.proto.ProtoField)1 ProtoMessage (org.kie.kogito.codegen.process.persistence.proto.ProtoMessage)1