From 6b9889deabc136f846e4a5d5c700bb9944f8ad2d Mon Sep 17 00:00:00 2001 From: Francisco Javier Tirado Sarti <65240126+fjtirado@users.noreply.github.com> Date: Thu, 6 Jul 2023 17:28:09 +0200 Subject: [PATCH] [KOGITO-9525] Add tag if description is null (#3101) * [KOGITO-9525] Add tag if description is null * [KOGITO-9525] Adding tag import to springboot Probably it wont work, but worth trying * [KOGITO-9525] Disable tag aggregration for springboot * [KOGITO-9525] Using codegen Also failed * Revert "[KOGITO-9525] Using codegen" This reverts commit 8e4a39293f3b962ca8fd749324ac04b91b30197a. * Revert "[KOGITO-9525] Disable tag aggregration for springboot" This reverts commit 5d3a9a970fa33d707ed455fc19dd0a56bcbe658a. * Revert "[KOGITO-9525] Adding tag import to springboot" This reverts commit 287c9e31dbcb613f24cf604de67d14de83f0042a. * [KOGITO-9525] Dependency annotator approach * [KOGITO-8525] Making plain Java test work --- .../api/di/DependencyInjectionAnnotator.java | 5 +++ .../impl/CDIDependencyInjectionAnnotator.java | 6 +++ .../process/ProcessResourceGenerator.java | 2 +- .../codegen/process/TagResourceGenerator.java | 39 ++++++++++--------- 4 files changed, 32 insertions(+), 20 deletions(-) diff --git a/kogito-codegen-modules/kogito-codegen-api/src/main/java/org/kie/kogito/codegen/api/di/DependencyInjectionAnnotator.java b/kogito-codegen-modules/kogito-codegen-api/src/main/java/org/kie/kogito/codegen/api/di/DependencyInjectionAnnotator.java index 29ebcf1a4d0..eb8c3126158 100644 --- a/kogito-codegen-modules/kogito-codegen-api/src/main/java/org/kie/kogito/codegen/api/di/DependencyInjectionAnnotator.java +++ b/kogito-codegen-modules/kogito-codegen-api/src/main/java/org/kie/kogito/codegen/api/di/DependencyInjectionAnnotator.java @@ -21,6 +21,7 @@ import com.github.javaparser.ast.NodeList; import com.github.javaparser.ast.expr.ArrayInitializerExpr; import com.github.javaparser.ast.expr.Expression; +import com.github.javaparser.ast.expr.MemberValuePair; import com.github.javaparser.ast.expr.MethodCallExpr; import com.github.javaparser.ast.expr.Name; import com.github.javaparser.ast.expr.NameExpr; @@ -259,4 +260,8 @@ default Expression getOptionalInstance(String fieldName) { * @param node node to be annotated */ > T withFactoryMethod(T node); + + default > T withTagAnnotation(T node, NodeList attributes) { + return node; + } } diff --git a/kogito-codegen-modules/kogito-codegen-api/src/main/java/org/kie/kogito/codegen/api/di/impl/CDIDependencyInjectionAnnotator.java b/kogito-codegen-modules/kogito-codegen-api/src/main/java/org/kie/kogito/codegen/api/di/impl/CDIDependencyInjectionAnnotator.java index ec8ccb9839d..e3fe75a4c7e 100644 --- a/kogito-codegen-modules/kogito-codegen-api/src/main/java/org/kie/kogito/codegen/api/di/impl/CDIDependencyInjectionAnnotator.java +++ b/kogito-codegen-modules/kogito-codegen-api/src/main/java/org/kie/kogito/codegen/api/di/impl/CDIDependencyInjectionAnnotator.java @@ -176,4 +176,10 @@ public > T withFactoryMethod(T node) { node.addAnnotation("javax.enterprise.inject.Produces"); return node; } + + @Override + public > T withTagAnnotation(T node, NodeList attributes) { + node.addAnnotation(new NormalAnnotationExpr(new Name("org.eclipse.microprofile.openapi.annotations.tags.Tag"), attributes)); + return node; + } } diff --git a/kogito-codegen-modules/kogito-codegen-processes/src/main/java/org/kie/kogito/codegen/process/ProcessResourceGenerator.java b/kogito-codegen-modules/kogito-codegen-processes/src/main/java/org/kie/kogito/codegen/process/ProcessResourceGenerator.java index 646e5803ef1..8f594759b5c 100644 --- a/kogito-codegen-modules/kogito-codegen-processes/src/main/java/org/kie/kogito/codegen/process/ProcessResourceGenerator.java +++ b/kogito-codegen-modules/kogito-codegen-processes/src/main/java/org/kie/kogito/codegen/process/ProcessResourceGenerator.java @@ -258,7 +258,7 @@ public String generate() { template.findAll(StringLiteralExpr.class).forEach(this::interpolateStrings); template.findAll(ClassOrInterfaceType.class).forEach(cls -> interpolateTypes(cls, typeInterpolations)); - TagResourceGenerator.addTags(clazz, process); + TagResourceGenerator.addTags(clazz, process, context); template.findAll(MethodDeclaration.class).forEach(this::interpolateMethods); diff --git a/kogito-codegen-modules/kogito-codegen-processes/src/main/java/org/kie/kogito/codegen/process/TagResourceGenerator.java b/kogito-codegen-modules/kogito-codegen-processes/src/main/java/org/kie/kogito/codegen/process/TagResourceGenerator.java index 4574f9c1703..0f5deecfb86 100644 --- a/kogito-codegen-modules/kogito-codegen-processes/src/main/java/org/kie/kogito/codegen/process/TagResourceGenerator.java +++ b/kogito-codegen-modules/kogito-codegen-processes/src/main/java/org/kie/kogito/codegen/process/TagResourceGenerator.java @@ -20,14 +20,13 @@ import java.util.Set; import org.jbpm.ruleflow.core.Metadata; +import org.kie.kogito.codegen.api.context.KogitoBuildContext; import org.kie.kogito.internal.process.runtime.KogitoWorkflowProcess; import com.github.javaparser.ast.CompilationUnit; import com.github.javaparser.ast.NodeList; import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration; import com.github.javaparser.ast.expr.MemberValuePair; -import com.github.javaparser.ast.expr.Name; -import com.github.javaparser.ast.expr.NormalAnnotationExpr; import com.github.javaparser.ast.expr.StringLiteralExpr; /** @@ -44,30 +43,32 @@ private TagResourceGenerator() { * @param compilationUnit the compilation unit to add the {@code org.eclipse.microprofile.openapi.annotations.tags.Tag} annotations to * @param process the {@link KogitoWorkflowProcess} to get the tags from */ - static void addTags(CompilationUnit compilationUnit, KogitoWorkflowProcess process) { - Map metadata = process.getMetaData(); - @SuppressWarnings("unchecked") - Collection tags = (Collection) metadata.getOrDefault(Metadata.TAGS, Set.of()); - String description = (String) metadata.get(Metadata.DESCRIPTION); - compilationUnit.findAll(ClassOrInterfaceDeclaration.class) - .forEach(cls -> addTags(process, tags, description, cls)); + static void addTags(CompilationUnit compilationUnit, KogitoWorkflowProcess process, KogitoBuildContext context) { + if (context.hasDI()) { + Map metadata = process.getMetaData(); + @SuppressWarnings("unchecked") + Collection tags = (Collection) metadata.getOrDefault(Metadata.TAGS, Set.of()); + String description = (String) metadata.get(Metadata.DESCRIPTION); + compilationUnit.findAll(ClassOrInterfaceDeclaration.class) + .forEach(cls -> addTags(process, tags, description, cls, context)); + } } - private static void addTags(KogitoWorkflowProcess process, Collection tags, String description, ClassOrInterfaceDeclaration cls) { - tags.forEach(tag -> addTag(cls, tag)); - if (description != null) { - addDescription(process, description, cls); - } + private static void addTags(KogitoWorkflowProcess process, Collection tags, String description, ClassOrInterfaceDeclaration cls, KogitoBuildContext context) { + tags.forEach(tag -> addTag(cls, tag, context)); + addDescription(process, description, cls, context); } - private static void addDescription(KogitoWorkflowProcess process, String description, ClassOrInterfaceDeclaration cls) { + private static void addDescription(KogitoWorkflowProcess process, String description, ClassOrInterfaceDeclaration cls, KogitoBuildContext context) { NodeList attributes = attributes(process.getId()); - attributes.add(new MemberValuePair("description", new StringLiteralExpr(description))); - cls.addAnnotation(new NormalAnnotationExpr(new Name("Tag"), attributes)); + if (description != null) { + attributes.add(new MemberValuePair("description", new StringLiteralExpr(description))); + } + context.getDependencyInjectionAnnotator().withTagAnnotation(cls, attributes); } - private static void addTag(ClassOrInterfaceDeclaration cls, String tag) { - cls.addAnnotation(new NormalAnnotationExpr(new Name("Tag"), attributes(tag))); + private static void addTag(ClassOrInterfaceDeclaration cls, String tag, KogitoBuildContext context) { + context.getDependencyInjectionAnnotator().withTagAnnotation(cls, attributes(tag)); } private static NodeList attributes(String tag) {