From 1dce3c42c4667dded147fa5b4b80240399770b71 Mon Sep 17 00:00:00 2001 From: Francisco Javier Tirado Sarti Date: Wed, 5 Jul 2023 18:18:56 +0200 Subject: [PATCH] [KOGITO-9525] Dependency annotator approach --- .../api/di/DependencyInjectionAnnotator.java | 5 +++++ .../impl/CDIDependencyInjectionAnnotator.java | 6 ++++++ .../process/ProcessResourceGenerator.java | 2 +- .../codegen/process/TagResourceGenerator.java | 21 +++++++++---------- 4 files changed, 22 insertions(+), 12 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..d6f19bd3ffa 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 634efd9708e..af0c6a835a6 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,30 @@ 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) { + static void addTags(CompilationUnit compilationUnit, KogitoWorkflowProcess process, KogitoBuildContext context) { 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)); + .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)); - 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()); if (description != null) { attributes.add(new MemberValuePair("description", new StringLiteralExpr(description))); } - cls.addAnnotation(new NormalAnnotationExpr(new Name("Tag"), attributes)); + 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) {