Skip to content

Commit

Permalink
[KOGITO-9525] Dependency annotator approach
Browse files Browse the repository at this point in the history
  • Loading branch information
fjtirado committed Jul 5, 2023
1 parent 3514584 commit 1dce3c4
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -259,4 +260,8 @@ default Expression getOptionalInstance(String fieldName) {
* @param node node to be annotated
*/
<T extends NodeWithAnnotations<?>> T withFactoryMethod(T node);

default <T extends NodeWithAnnotations<?>> T withTagAnnotation(T node, NodeList<MemberValuePair> attributes) {
return node;
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,10 @@ public <T extends NodeWithAnnotations<?>> T withFactoryMethod(T node) {
node.addAnnotation("javax.enterprise.inject.Produces");
return node;
}

@Override
public <T extends NodeWithAnnotations<?>> T withTagAnnotation(T node, NodeList<MemberValuePair> attributes) {
node.addAnnotation(new NormalAnnotationExpr(new Name("org.eclipse.microprofile.openapi.annotations.tags.Tag"), attributes));
return node;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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<String, Object> metadata = process.getMetaData();
@SuppressWarnings("unchecked")
Collection<String> tags = (Collection<String>) 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<String> tags, String description, ClassOrInterfaceDeclaration cls) {
tags.forEach(tag -> addTag(cls, tag));
addDescription(process, description, cls);
private static void addTags(KogitoWorkflowProcess process, Collection<String> 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<MemberValuePair> 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<MemberValuePair> attributes(String tag) {
Expand Down

0 comments on commit 1dce3c4

Please sign in to comment.