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