From d6837ae86d45ca30816656da9d8f9eab100c205f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Crocquesel?= <88554524+scrocquesel@users.noreply.github.com> Date: Wed, 13 Nov 2024 20:08:44 +0100 Subject: [PATCH] feat: enable constructor injection of NamedDynamoDbTable (#1469) --- .../DynamodbEnhancedDbTableProcessor.java | 64 ++++++++++++------- ...DBEnhancedDbTableConstructorInjection.java | 21 ++++++ .../DynamoDbEnhancedDbTableTest.java | 5 ++ .../enhanced/runtime/NamedDynamoDbTable.java | 3 +- 4 files changed, 70 insertions(+), 23 deletions(-) create mode 100644 dynamodb-enhanced/deployment/src/test/java/io/quarkus/amazon/dynamodb/enhanced/deployment/DynamoDBEnhancedDbTableConstructorInjection.java diff --git a/dynamodb-enhanced/deployment/src/main/java/io/quarkus/amazon/dynamodb/enhanced/deployment/DynamodbEnhancedDbTableProcessor.java b/dynamodb-enhanced/deployment/src/main/java/io/quarkus/amazon/dynamodb/enhanced/deployment/DynamodbEnhancedDbTableProcessor.java index d23f83eea..a50c7c188 100644 --- a/dynamodb-enhanced/deployment/src/main/java/io/quarkus/amazon/dynamodb/enhanced/deployment/DynamodbEnhancedDbTableProcessor.java +++ b/dynamodb-enhanced/deployment/src/main/java/io/quarkus/amazon/dynamodb/enhanced/deployment/DynamodbEnhancedDbTableProcessor.java @@ -15,9 +15,10 @@ import org.jboss.jandex.ClassInfo; import org.jboss.jandex.ClassType; import org.jboss.jandex.DotName; -import org.jboss.jandex.FieldInfo; import org.jboss.jandex.IndexView; +import org.jboss.jandex.MethodParameterInfo; import org.jboss.jandex.ParameterizedType; +import org.jboss.jandex.Type; import io.quarkus.amazon.common.deployment.RequireAmazonClientInjectionBuildItem; import io.quarkus.amazon.common.runtime.ClientUtil; @@ -72,31 +73,50 @@ void discoverDynamoDbTable(CombinedIndexBuildItem combinedIndexBuildItem, IndexView index = combinedIndexBuildItem.getIndex(); Collection ais = index.getAnnotations(DotNames.DYNAMODB_NAMED_TABLE); for (AnnotationInstance ano : ais) { + String tableName = ano.value().asString(); + Type targetType = null; + if (ano.target().kind().equals(Kind.FIELD)) { - FieldInfo field = ano.target().asField(); - String tableName = ano.value().asString(); - DotName beanClassName = field.type().asParameterizedType().arguments().get(0).name(); - - ClassInfo beanClass = index.getClassByName(beanClassName); - if (beanClass.annotation(DotNames.DYNAMODB_ENHANCED_BEAN) == null - && beanClass.annotation(DotNames.DYNAMODB_ENHANCED_IMMUTABLE) == null) { - throw new DeploymentException(String - .format("'%s' must be bean annotated with @DynamoDbBean or @DynamoDbImmutable", beanClassName)); + targetType = ano.target().asField().type(); + } else if (ano.target().kind().equals(Kind.METHOD_PARAMETER)) { + MethodParameterInfo mpi = ano.target().asMethodParameter(); + if (mpi.method().isConstructor()) { + targetType = mpi.type(); } + } + + if (targetType == null) { + continue; + } + + DotName beanClassName = targetType.asParameterizedType().arguments().get(0).name(); + DotName dbTableClassName = targetType.name(); + + ClassInfo beanClass = index.getClassByName(beanClassName); - if (DotNames.DYNAMODB_TABLE.equals(field.type().name())) { - if (syncSeen.add(Map.entry(tableName, beanClassName))) { - tables.produce(new DynamodbEnhancedTableBuildItem(tableName, beanClassName, - DotNames.DYNAMODB_ENHANCED_CLIENT, DYNAMODB_ENHANCED_CLIENT_TABLE_METHOD, - DotNames.DYNAMODB_TABLE)); - } + if (beanClass == null) { + throw new DeploymentException(String + .format("'%s' is not in the Jandex index", beanClassName)); + } + + if (beanClass.annotation(DotNames.DYNAMODB_ENHANCED_BEAN) == null + && beanClass.annotation(DotNames.DYNAMODB_ENHANCED_IMMUTABLE) == null) { + throw new DeploymentException(String + .format("'%s' must be bean annotated with @DynamoDbBean or @DynamoDbImmutable", beanClassName)); + } + + if (DotNames.DYNAMODB_TABLE.equals(dbTableClassName)) { + if (syncSeen.add(Map.entry(tableName, beanClassName))) { + tables.produce(new DynamodbEnhancedTableBuildItem(tableName, beanClassName, + DotNames.DYNAMODB_ENHANCED_CLIENT, DYNAMODB_ENHANCED_CLIENT_TABLE_METHOD, + DotNames.DYNAMODB_TABLE)); } - if (DotNames.DYNAMODB_ASYNC_TABLE.equals(field.type().name())) { - if (asyncSeen.add(Map.entry(tableName, beanClassName))) { - tables.produce(new DynamodbEnhancedTableBuildItem(tableName, beanClassName, - DotNames.DYNAMODB_ENHANCED_ASYNC_CLIENT, DYNAMODB_ENHANCED_ASYNC_CLIENT_TABLE_METHOD, - DotNames.DYNAMODB_ASYNC_TABLE)); - } + } + if (DotNames.DYNAMODB_ASYNC_TABLE.equals(dbTableClassName)) { + if (asyncSeen.add(Map.entry(tableName, beanClassName))) { + tables.produce(new DynamodbEnhancedTableBuildItem(tableName, beanClassName, + DotNames.DYNAMODB_ENHANCED_ASYNC_CLIENT, DYNAMODB_ENHANCED_ASYNC_CLIENT_TABLE_METHOD, + DotNames.DYNAMODB_ASYNC_TABLE)); } } } diff --git a/dynamodb-enhanced/deployment/src/test/java/io/quarkus/amazon/dynamodb/enhanced/deployment/DynamoDBEnhancedDbTableConstructorInjection.java b/dynamodb-enhanced/deployment/src/test/java/io/quarkus/amazon/dynamodb/enhanced/deployment/DynamoDBEnhancedDbTableConstructorInjection.java new file mode 100644 index 000000000..b4741c240 --- /dev/null +++ b/dynamodb-enhanced/deployment/src/test/java/io/quarkus/amazon/dynamodb/enhanced/deployment/DynamoDBEnhancedDbTableConstructorInjection.java @@ -0,0 +1,21 @@ +package io.quarkus.amazon.dynamodb.enhanced.deployment; + +import jakarta.enterprise.context.ApplicationScoped; + +import io.quarkus.amazon.dynamodb.enhanced.runtime.NamedDynamoDbTable; +import software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable; + +@ApplicationScoped +public class DynamoDBEnhancedDbTableConstructorInjection { + + private DynamoDbTable syncTable; + + DynamoDBEnhancedDbTableConstructorInjection( + @NamedDynamoDbTable("sync") DynamoDbTable syncTable) { + this.syncTable = syncTable; + } + + public DynamoDbTable getSyncTable() { + return syncTable; + } +} diff --git a/dynamodb-enhanced/deployment/src/test/java/io/quarkus/amazon/dynamodb/enhanced/deployment/DynamoDbEnhancedDbTableTest.java b/dynamodb-enhanced/deployment/src/test/java/io/quarkus/amazon/dynamodb/enhanced/deployment/DynamoDbEnhancedDbTableTest.java index 2abd6c2a2..15b35b752 100644 --- a/dynamodb-enhanced/deployment/src/test/java/io/quarkus/amazon/dynamodb/enhanced/deployment/DynamoDbEnhancedDbTableTest.java +++ b/dynamodb-enhanced/deployment/src/test/java/io/quarkus/amazon/dynamodb/enhanced/deployment/DynamoDbEnhancedDbTableTest.java @@ -38,10 +38,14 @@ public class DynamoDbEnhancedDbTableTest { @Inject DynamoDbAsyncTable asyncTableOther; + @Inject + DynamoDBEnhancedDbTableConstructorInjection constructorInjection; + @RegisterExtension static final QuarkusUnitTest config = new QuarkusUnitTest() .withApplicationRoot((jar) -> jar .addClass(DynamoDBExampleTableEntry.class) + .addClass(DynamoDBEnhancedDbTableConstructorInjection.class) .addAsResource("full-config.properties", "application.properties")); @Test @@ -53,5 +57,6 @@ public void test() { assertEquals("async", asyncTable.tableName()); assertEquals("async", asyncTableDuplicate.tableName()); assertEquals("async-other", asyncTableOther.tableName()); + assertEquals("sync", constructorInjection.getSyncTable().tableName()); } } diff --git a/dynamodb-enhanced/runtime/src/main/java/io/quarkus/amazon/dynamodb/enhanced/runtime/NamedDynamoDbTable.java b/dynamodb-enhanced/runtime/src/main/java/io/quarkus/amazon/dynamodb/enhanced/runtime/NamedDynamoDbTable.java index 117ca5335..81c712e75 100644 --- a/dynamodb-enhanced/runtime/src/main/java/io/quarkus/amazon/dynamodb/enhanced/runtime/NamedDynamoDbTable.java +++ b/dynamodb-enhanced/runtime/src/main/java/io/quarkus/amazon/dynamodb/enhanced/runtime/NamedDynamoDbTable.java @@ -1,6 +1,7 @@ package io.quarkus.amazon.dynamodb.enhanced.runtime; import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.ElementType.PARAMETER; import static java.lang.annotation.RetentionPolicy.RUNTIME; import java.lang.annotation.Documented; @@ -12,7 +13,7 @@ /** * Specification of DynamoDb table to be injected. */ -@Target({ FIELD }) +@Target({ FIELD, PARAMETER }) @Retention(RUNTIME) @Qualifier @Documented