From 995e2b0dd6b89ab37d207e9593dc18cf068373f5 Mon Sep 17 00:00:00 2001 From: Gabriele-Cardosi Date: Wed, 18 Sep 2024 16:11:14 +0200 Subject: [PATCH] [incubator-kie-issues#1473] Refactoring DependencyInjectionAnnotator. Add "Patch" annotation to RestAnnotators --- .../di/DependencyInjectionAnnotator.java | 7 +++- .../impl/CDIDependencyInjectionAnnotator.java | 19 +++++---- .../SpringDependencyInjectionAnnotator.java | 39 ++++++++++++------- .../common/rest/impl/CDIRestAnnotator.java | 2 +- .../common/rest/impl/SpringRestAnnotator.java | 2 +- 5 files changed, 44 insertions(+), 25 deletions(-) diff --git a/drools-model/drools-codegen-common/src/main/java/org/drools/codegen/common/di/DependencyInjectionAnnotator.java b/drools-model/drools-codegen-common/src/main/java/org/drools/codegen/common/di/DependencyInjectionAnnotator.java index 29fe2ca0ee7..1446895fe55 100644 --- a/drools-model/drools-codegen-common/src/main/java/org/drools/codegen/common/di/DependencyInjectionAnnotator.java +++ b/drools-model/drools-codegen-common/src/main/java/org/drools/codegen/common/di/DependencyInjectionAnnotator.java @@ -165,7 +165,12 @@ default > T withInjection(T node) { * * @param node node to be annotated */ - > T withTransactional(T node); + default > T withTransactional(T node) { + node.addAnnotation(getTransactionalAnnotation()); + return node; + } + + String getTransactionalAnnotation(); /** * Annotates and enhances method used to produce messages diff --git a/drools-model/drools-codegen-common/src/main/java/org/drools/codegen/common/di/impl/CDIDependencyInjectionAnnotator.java b/drools-model/drools-codegen-common/src/main/java/org/drools/codegen/common/di/impl/CDIDependencyInjectionAnnotator.java index fc44fe35a47..425b84e0f7d 100644 --- a/drools-model/drools-codegen-common/src/main/java/org/drools/codegen/common/di/impl/CDIDependencyInjectionAnnotator.java +++ b/drools-model/drools-codegen-common/src/main/java/org/drools/codegen/common/di/impl/CDIDependencyInjectionAnnotator.java @@ -45,7 +45,8 @@ public > T withProduces(T node, boolean isDefau @Override public > T withNamed(T node, String name) { - node.addAnnotation(new SingleMemberAnnotationExpr(new Name("jakarta.inject.Named"), new StringLiteralExpr(name))); + node.addAnnotation(new SingleMemberAnnotationExpr(new Name("jakarta.inject.Named"), + new StringLiteralExpr(name))); return node; } @@ -89,13 +90,17 @@ public > T withOptionalInjection(T node) { @Override public > T withIncomingMessage(T node, String channel) { - node.addAnnotation(new SingleMemberAnnotationExpr(new Name("org.eclipse.microprofile.reactive.messaging.Incoming"), new StringLiteralExpr(channel))); + node.addAnnotation(new SingleMemberAnnotationExpr(new Name("org.eclipse.microprofile.reactive.messaging" + + ".Incoming"), + new StringLiteralExpr(channel))); return node; } @Override public > T withOutgoingMessage(T node, String channel) { - node.addAnnotation(new SingleMemberAnnotationExpr(new Name("org.eclipse.microprofile.reactive.messaging.Channel"), new StringLiteralExpr(channel))); + node.addAnnotation(new SingleMemberAnnotationExpr(new Name("org.eclipse.microprofile.reactive.messaging" + + ".Channel"), + new StringLiteralExpr(channel))); return node; } @@ -179,14 +184,14 @@ public > T withFactoryMethod(T node) { } @Override - public > T withTransactional(T node) { - node.addAnnotation("jakarta.transaction.Transactional"); - return node; + public String getTransactionalAnnotation() { + return "jakarta.transaction.Transactional"; } @Override public > T withTagAnnotation(T node, NodeList attributes) { - node.addAnnotation(new NormalAnnotationExpr(new Name("org.eclipse.microprofile.openapi.annotations.tags.Tag"), attributes)); + node.addAnnotation(new NormalAnnotationExpr(new Name("org.eclipse.microprofile.openapi.annotations.tags.Tag") + , attributes)); return node; } } diff --git a/drools-model/drools-codegen-common/src/main/java/org/drools/codegen/common/di/impl/SpringDependencyInjectionAnnotator.java b/drools-model/drools-codegen-common/src/main/java/org/drools/codegen/common/di/impl/SpringDependencyInjectionAnnotator.java index f30381275cf..c9cf6687ab7 100644 --- a/drools-model/drools-codegen-common/src/main/java/org/drools/codegen/common/di/impl/SpringDependencyInjectionAnnotator.java +++ b/drools-model/drools-codegen-common/src/main/java/org/drools/codegen/common/di/impl/SpringDependencyInjectionAnnotator.java @@ -18,6 +18,10 @@ */ package org.drools.codegen.common.di.impl; +import java.util.Collection; +import java.util.Collections; +import java.util.Optional; + import com.github.javaparser.ast.NodeList; import com.github.javaparser.ast.expr.BinaryExpr; import com.github.javaparser.ast.expr.BooleanLiteralExpr; @@ -36,10 +40,6 @@ import com.github.javaparser.ast.type.ClassOrInterfaceType; import org.drools.codegen.common.di.DependencyInjectionAnnotator; -import java.util.Collection; -import java.util.Collections; -import java.util.Optional; - public class SpringDependencyInjectionAnnotator implements DependencyInjectionAnnotator { @Override @@ -53,7 +53,8 @@ public > T withProduces(T node, boolean isDefau @Override public > T withNamed(T node, String name) { - node.addAnnotation(new SingleMemberAnnotationExpr(new Name("org.springframework.beans.factory.annotation.Qualifier"), new StringLiteralExpr(name))); + node.addAnnotation(new SingleMemberAnnotationExpr(new Name("org.springframework.beans.factory.annotation" + + ".Qualifier"), new StringLiteralExpr(name))); return node; } @@ -65,7 +66,8 @@ public > T withApplicationComponent(T node) { @Override public > T withNamedApplicationComponent(T node, String name) { - node.addAnnotation(new SingleMemberAnnotationExpr(new Name("org.springframework.stereotype.Component"), new StringLiteralExpr(name))); + node.addAnnotation(new SingleMemberAnnotationExpr(new Name("org.springframework.stereotype.Component"), + new StringLiteralExpr(name))); return node; } @@ -96,14 +98,18 @@ public > T withNamedInjection(T node, String na @Override public > T withOptionalInjection(T node) { node.addAnnotation( - new NormalAnnotationExpr(new Name("org.springframework.beans.factory.annotation.Autowired"), NodeList.nodeList(new MemberValuePair("required", new BooleanLiteralExpr(false))))); + new NormalAnnotationExpr(new Name("org.springframework.beans.factory.annotation.Autowired"), + NodeList.nodeList(new MemberValuePair("required", + new BooleanLiteralExpr(false))))); node.addAnnotation("org.springframework.context.annotation.Lazy"); return node; } @Override public > T withIncomingMessage(T node, String channel) { - node.addAnnotation(new NormalAnnotationExpr(new Name("org.springframework.kafka.annotation.KafkaListener"), NodeList.nodeList(new MemberValuePair("topics", new StringLiteralExpr(channel))))); + node.addAnnotation(new NormalAnnotationExpr(new Name("org.springframework.kafka.annotation.KafkaListener"), + NodeList.nodeList(new MemberValuePair("topics", + new StringLiteralExpr(channel))))); return node; } @@ -139,7 +145,8 @@ public Expression getMultiInstance(String fieldName) { return new ConditionalExpr( new BinaryExpr(new NameExpr(fieldName), new NullLiteralExpr(), BinaryExpr.Operator.NOT_EQUALS), new NameExpr(fieldName), - new MethodCallExpr(new TypeExpr(new ClassOrInterfaceType(null, Collections.class.getCanonicalName())), "emptyList")); + new MethodCallExpr(new TypeExpr(new ClassOrInterfaceType(null, Collections.class.getCanonicalName())) + , "emptyList")); } @Override @@ -154,19 +161,22 @@ public String emitterType(String dataType) { @Override public > T withConfigInjection(T node, String configKey) { - node.addAnnotation(new SingleMemberAnnotationExpr(new Name("org.springframework.beans.factory.annotation.Value"), new StringLiteralExpr("${" + configKey + ":#{null}}"))); + node.addAnnotation(new SingleMemberAnnotationExpr(new Name("org.springframework.beans.factory.annotation" + + ".Value"), + new StringLiteralExpr("${" + configKey + ":#{null}}"))); return node; } @Override public > T withConfigInjection(T node, String configKey, String defaultValue) { - node.addAnnotation(new SingleMemberAnnotationExpr(new Name("org.springframework.beans.factory.annotation.Value"), new StringLiteralExpr("${" + configKey + ":" + defaultValue + "}"))); + node.addAnnotation(new SingleMemberAnnotationExpr(new Name("org.springframework.beans.factory.annotation" + + ".Value"), + new StringLiteralExpr("${" + configKey + ":" + defaultValue + "}"))); return node; } /** * no-op, Spring beans are not lazy by default. - * * @param node node to be annotated * @return */ @@ -182,9 +192,8 @@ public > T withFactoryClass(T node) { } @Override - public > T withTransactional(T node) { - node.addAnnotation("org.springframework.transaction.annotation.Transactional"); - return node; + public String getTransactionalAnnotation() { + return "org.springframework.transaction.annotation.Transactional"; } @Override diff --git a/drools-model/drools-codegen-common/src/main/java/org/drools/codegen/common/rest/impl/CDIRestAnnotator.java b/drools-model/drools-codegen-common/src/main/java/org/drools/codegen/common/rest/impl/CDIRestAnnotator.java index 600eff5f833..7ce162e5854 100644 --- a/drools-model/drools-codegen-common/src/main/java/org/drools/codegen/common/rest/impl/CDIRestAnnotator.java +++ b/drools-model/drools-codegen-common/src/main/java/org/drools/codegen/common/rest/impl/CDIRestAnnotator.java @@ -29,7 +29,7 @@ public class CDIRestAnnotator implements RestAnnotator { @Override public > boolean isRestAnnotated(T node) { - return Stream.of("POST", "GET", "PUT", "DELETE") + return Stream.of("POST", "GET", "PUT", "DELETE", "PATCH") .map(node::getAnnotationByName) .anyMatch(Optional::isPresent); } diff --git a/drools-model/drools-codegen-common/src/main/java/org/drools/codegen/common/rest/impl/SpringRestAnnotator.java b/drools-model/drools-codegen-common/src/main/java/org/drools/codegen/common/rest/impl/SpringRestAnnotator.java index 1258b4d9d5c..7d480c5f321 100644 --- a/drools-model/drools-codegen-common/src/main/java/org/drools/codegen/common/rest/impl/SpringRestAnnotator.java +++ b/drools-model/drools-codegen-common/src/main/java/org/drools/codegen/common/rest/impl/SpringRestAnnotator.java @@ -29,7 +29,7 @@ public class SpringRestAnnotator implements RestAnnotator { @Override public > boolean isRestAnnotated(T node) { - return Stream.of("PostMapping", "GetMapping", "PutMapping", "DeleteMapping") + return Stream.of("PostMapping", "GetMapping", "PutMapping", "DeleteMapping", "PatchMapping") .map(node::getAnnotationByName) .anyMatch(Optional::isPresent); }