Skip to content

Commit

Permalink
[incubator-kie-issues#1473] Refactoring DependencyInjectionAnnotator.…
Browse files Browse the repository at this point in the history
… Add "Patch" annotation to RestAnnotators
  • Loading branch information
Gabriele-Cardosi committed Sep 18, 2024
1 parent 362ec68 commit 995e2b0
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,12 @@ default <T extends NodeWithAnnotations<?>> T withInjection(T node) {
*
* @param node node to be annotated
*/
<T extends NodeWithAnnotations<?>> T withTransactional(T node);
default <T extends NodeWithAnnotations<?>> T withTransactional(T node) {
node.addAnnotation(getTransactionalAnnotation());
return node;
}

String getTransactionalAnnotation();

/**
* Annotates and enhances method used to produce messages
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ public <T extends NodeWithAnnotations<?>> T withProduces(T node, boolean isDefau

@Override
public <T extends NodeWithAnnotations<?>> 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;
}

Expand Down Expand Up @@ -89,13 +90,17 @@ public <T extends NodeWithAnnotations<?>> T withOptionalInjection(T node) {

@Override
public <T extends NodeWithAnnotations<?>> 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 extends NodeWithAnnotations<?>> 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;
}

Expand Down Expand Up @@ -179,14 +184,14 @@ public <T extends NodeWithAnnotations<?>> T withFactoryMethod(T node) {
}

@Override
public <T extends NodeWithAnnotations<?>> T withTransactional(T node) {
node.addAnnotation("jakarta.transaction.Transactional");
return node;
public String getTransactionalAnnotation() {
return "jakarta.transaction.Transactional";
}

@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));
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 @@ -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;
Expand All @@ -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
Expand All @@ -53,7 +53,8 @@ public <T extends NodeWithAnnotations<?>> T withProduces(T node, boolean isDefau

@Override
public <T extends NodeWithAnnotations<?>> 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;
}

Expand All @@ -65,7 +66,8 @@ public <T extends NodeWithAnnotations<?>> T withApplicationComponent(T node) {

@Override
public <T extends NodeWithAnnotations<?>> 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;
}

Expand Down Expand Up @@ -96,14 +98,18 @@ public <T extends NodeWithAnnotations<?>> T withNamedInjection(T node, String na
@Override
public <T extends NodeWithAnnotations<?>> 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 extends NodeWithAnnotations<?>> 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;
}

Expand Down Expand Up @@ -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
Expand All @@ -154,19 +161,22 @@ public String emitterType(String dataType) {

@Override
public <T extends NodeWithAnnotations<?>> 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 extends NodeWithAnnotations<?>> 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
*/
Expand All @@ -182,9 +192,8 @@ public <T extends NodeWithAnnotations<?>> T withFactoryClass(T node) {
}

@Override
public <T extends NodeWithAnnotations<?>> T withTransactional(T node) {
node.addAnnotation("org.springframework.transaction.annotation.Transactional");
return node;
public String getTransactionalAnnotation() {
return "org.springframework.transaction.annotation.Transactional";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class CDIRestAnnotator implements RestAnnotator {

@Override
public <T extends NodeWithAnnotations<?>> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class SpringRestAnnotator implements RestAnnotator {

@Override
public <T extends NodeWithAnnotations<?>> 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);
}
Expand Down

0 comments on commit 995e2b0

Please sign in to comment.