You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tamaya.apache.org by an...@apache.org on 2017/10/12 23:21:35 UTC

incubator-tamaya-sandbox git commit: TAMAYA-260 Fixed Microprofile Optional injection for String values.

Repository: incubator-tamaya-sandbox
Updated Branches:
  refs/heads/master a4e2df547 -> fdc5a1644


TAMAYA-260 Fixed Microprofile Optional injection for String values.


Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/commit/fdc5a164
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/tree/fdc5a164
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/diff/fdc5a164

Branch: refs/heads/master
Commit: fdc5a1644f800a25043e2b75b010f57c62383918
Parents: a4e2df5
Author: Anatole Tresch <an...@apache.org>
Authored: Fri Oct 13 01:21:19 2017 +0200
Committer: Anatole Tresch <an...@apache.org>
Committed: Fri Oct 13 01:21:19 2017 +0200

----------------------------------------------------------------------
 .../cdi/MicroprofileCDIExtension.java           | 17 +++++++++++++++++
 .../cdi/MicroprofileConfigurationProducer.java  | 20 ++++++++++++++++++--
 2 files changed, 35 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/fdc5a164/microprofile/src/main/java/org/apache/tamaya/microprofile/cdi/MicroprofileCDIExtension.java
----------------------------------------------------------------------
diff --git a/microprofile/src/main/java/org/apache/tamaya/microprofile/cdi/MicroprofileCDIExtension.java b/microprofile/src/main/java/org/apache/tamaya/microprofile/cdi/MicroprofileCDIExtension.java
index 0e30b78..310b72c 100644
--- a/microprofile/src/main/java/org/apache/tamaya/microprofile/cdi/MicroprofileCDIExtension.java
+++ b/microprofile/src/main/java/org/apache/tamaya/microprofile/cdi/MicroprofileCDIExtension.java
@@ -28,6 +28,8 @@ import javax.enterprise.inject.spi.InjectionPoint;
 import javax.enterprise.inject.spi.ProcessBean;
 import javax.enterprise.inject.spi.ProcessProducerMethod;
 import javax.inject.Provider;
+import java.lang.reflect.AnnotatedType;
+import java.lang.reflect.Method;
 import java.lang.reflect.ParameterizedType;
 import java.lang.reflect.Type;
 import java.util.HashSet;
@@ -77,6 +79,21 @@ public class MicroprofileCDIExtension implements Extension {
                 configured = true;
                 LOG.finest(() -> "Enabling Tamaya Microprofile Configuration on bean: " + configuredType.getName());
                 configuredType.addConfiguredMember(injectionPoint, key);
+            }else if(injectionPoint.getMember() instanceof Method){
+                Method method = (Method)injectionPoint.getMember();
+                for(AnnotatedType paramType: method.getAnnotatedParameterTypes()){
+                    if(paramType.isAnnotationPresent(ConfigProperty.class)) {
+                        LOG.fine("Configuring method: " + injectionPoint);
+                        final ConfigProperty annotation = paramType.getAnnotation(ConfigProperty.class);
+                        String key = !annotation.name().isEmpty() ? annotation.name() : MicroprofileConfigurationProducer.getDefaultKey(injectionPoint);
+                        Type originalType = paramType.getType();
+                        Type convertedType = unwrapType(originalType);
+                        types.add(convertedType);
+                        configured = true;
+                        LOG.finest(() -> "Enabling Tamaya Microprofile Configuration on bean: " + configuredType.getName());
+                        configuredType.addConfiguredMember(injectionPoint, key);
+                    }
+                }
             }
         }
         if(configured) {

http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/fdc5a164/microprofile/src/main/java/org/apache/tamaya/microprofile/cdi/MicroprofileConfigurationProducer.java
----------------------------------------------------------------------
diff --git a/microprofile/src/main/java/org/apache/tamaya/microprofile/cdi/MicroprofileConfigurationProducer.java b/microprofile/src/main/java/org/apache/tamaya/microprofile/cdi/MicroprofileConfigurationProducer.java
index d52f7b6..bf61117 100644
--- a/microprofile/src/main/java/org/apache/tamaya/microprofile/cdi/MicroprofileConfigurationProducer.java
+++ b/microprofile/src/main/java/org/apache/tamaya/microprofile/cdi/MicroprofileConfigurationProducer.java
@@ -35,9 +35,11 @@ import javax.enterprise.inject.spi.AnnotatedType;
 import javax.enterprise.inject.spi.InjectionPoint;
 import javax.inject.Provider;
 import java.lang.reflect.AnnotatedElement;
+import java.lang.reflect.Method;
 import java.lang.reflect.ParameterizedType;
 import java.lang.reflect.Type;
 import java.util.List;
+import java.util.Optional;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
@@ -94,7 +96,20 @@ public class MicroprofileConfigurationProducer {
             }
         }
         if (injectionPoint.getMember() instanceof AnnotatedElement) {
-            builder.setAnnotatedElement((AnnotatedElement) injectionPoint.getMember());
+            AnnotatedElement annotated = (AnnotatedElement)injectionPoint.getMember();
+            if(annotated.isAnnotationPresent(ConfigProperty.class)) {
+                builder.setAnnotatedElement(annotated);
+            }
+        }else if(injectionPoint.getMember() instanceof Method){
+            Method method = (Method)injectionPoint.getMember();
+            for(Type type:method.getParameterTypes()){
+                if(type instanceof AnnotatedElement){
+                    AnnotatedElement annotated = (AnnotatedElement)type;
+                    if(annotated.isAnnotationPresent(ConfigProperty.class)) {
+                        builder.setAnnotatedElement(annotated);
+                    }
+                }
+            }
         }
         return builder.build();
     }
@@ -106,7 +121,8 @@ public class MicroprofileConfigurationProducer {
         if(String.class.equals(context.getTargetType().getRawType())){
             value = textValue;
         }
-        if (textValue != null) {
+        if (textValue != null || Optional.class.equals(context.getTargetType().getRawType())) {
+            LOGGER.log(Level.FINEST, () -> "Converting KEY: " + context.getKey() + "("+context.getTargetType()+"), textValue: " + textValue);
             List<PropertyConverter> converters = ConfigurationProvider.getConfiguration().getContext()
                     .getPropertyConverters((TypeLiteral)context.getTargetType());
             for (PropertyConverter<Object> converter : converters) {