tamaya-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From anat...@apache.org
Subject [17/20] incubator-tamaya-extensions git commit: TAMAYA-358 Possible fix for failing injection of Optional.
Date Sun, 18 Nov 2018 21:20:35 GMT
TAMAYA-358 Possible fix for failing injection of Optional.


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

Branch: refs/heads/master
Commit: e729bd0f7d8a3510a353674f45ae159101029a85
Parents: c8ba9c4
Author: Anatole Tresch <atsticks@gmail.com>
Authored: Fri Nov 16 01:06:46 2018 +0100
Committer: Anatole Tresch <atsticks@gmail.com>
Committed: Fri Nov 16 01:06:46 2018 +0100

----------------------------------------------------------------------
 .../org/apache/tamaya/cdi/ConfigurationProducer.java |  2 +-
 .../java/org/apache/tamaya/cdi/ConfiguredBTest.java  |  5 +++++
 .../java/org/apache/tamaya/cdi/ConfiguredClass.java  | 15 +++++++++++++++
 .../tamaya/inject/internal/ConfiguredFieldImpl.java  |  2 +-
 .../tamaya/inject/internal/InjectionHelper.java      | 10 +++++-----
 5 files changed, 27 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/e729bd0f/modules/injection/cdi/src/main/java/org/apache/tamaya/cdi/ConfigurationProducer.java
----------------------------------------------------------------------
diff --git a/modules/injection/cdi/src/main/java/org/apache/tamaya/cdi/ConfigurationProducer.java
b/modules/injection/cdi/src/main/java/org/apache/tamaya/cdi/ConfigurationProducer.java
index 16b3608..ee0f592 100644
--- a/modules/injection/cdi/src/main/java/org/apache/tamaya/cdi/ConfigurationProducer.java
+++ b/modules/injection/cdi/src/main/java/org/apache/tamaya/cdi/ConfigurationProducer.java
@@ -85,7 +85,7 @@ public class ConfigurationProducer {
         }
         String keyFound = null;
         for(String key:keys) {
-            textValue = config.get(key);
+            textValue = config.getOrDefault(key, null);
             if(textValue!=null) {
                 keyFound = key;
                 break;

http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/e729bd0f/modules/injection/cdi/src/test/java/org/apache/tamaya/cdi/ConfiguredBTest.java
----------------------------------------------------------------------
diff --git a/modules/injection/cdi/src/test/java/org/apache/tamaya/cdi/ConfiguredBTest.java
b/modules/injection/cdi/src/test/java/org/apache/tamaya/cdi/ConfiguredBTest.java
index abb0721..34bfd95 100644
--- a/modules/injection/cdi/src/test/java/org/apache/tamaya/cdi/ConfiguredBTest.java
+++ b/modules/injection/cdi/src/test/java/org/apache/tamaya/cdi/ConfiguredBTest.java
@@ -34,6 +34,7 @@ import javax.enterprise.inject.spi.CDI;
 import javax.enterprise.inject.spi.Extension;
 
 import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
@@ -50,5 +51,9 @@ public class ConfiguredBTest extends BaseTestConfiguration {
         System.out.println("********************************************");
         double actual = 1234.5678;
         MatcherAssert.assertThat(item.getDoubleValue(), is(actual));
+        assertTrue(item.getExistingDouble()!=null);
+        assertTrue(item.getNonExistingDouble()!=null);
+        assertTrue(item.getExistingDouble().isPresent());
+        assertFalse(item.getNonExistingDouble().isPresent());
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/e729bd0f/modules/injection/cdi/src/test/java/org/apache/tamaya/cdi/ConfiguredClass.java
----------------------------------------------------------------------
diff --git a/modules/injection/cdi/src/test/java/org/apache/tamaya/cdi/ConfiguredClass.java
b/modules/injection/cdi/src/test/java/org/apache/tamaya/cdi/ConfiguredClass.java
index 5d71d5d..a2dc7ce 100644
--- a/modules/injection/cdi/src/test/java/org/apache/tamaya/cdi/ConfiguredClass.java
+++ b/modules/injection/cdi/src/test/java/org/apache/tamaya/cdi/ConfiguredClass.java
@@ -24,6 +24,7 @@ import org.apache.tamaya.inject.api.Config;
 
 import javax.inject.Singleton;
 import java.math.BigDecimal;
+import java.util.Optional;
 
 /**
  * Class to be loaded from CDI to ensure fields are correctly configured using CDI injection
mechanisms.
@@ -61,6 +62,12 @@ public class ConfiguredClass{
     @Config("double1")
     private double doubleValue;
 
+    @Config("double1")
+    private Optional<Double> existingDouble;
+
+    @Config("foo-bar")
+    private Optional<Double> nonExistingDouble;
+
     public String getTestProperty() {
         return testProperty;
     }
@@ -101,6 +108,14 @@ public class ConfiguredClass{
         return doubleValue;
     }
 
+    public Optional<Double> getExistingDouble() {
+        return existingDouble;
+    }
+
+    public Optional<Double> getNonExistingDouble() {
+        return nonExistingDouble;
+    }
+
     @Override
 	public String toString(){
         return super.toString() + ": testProperty="+testProperty+", value1="+value1+", value2="+value2

http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/e729bd0f/modules/injection/standalone/src/main/java/org/apache/tamaya/inject/internal/ConfiguredFieldImpl.java
----------------------------------------------------------------------
diff --git a/modules/injection/standalone/src/main/java/org/apache/tamaya/inject/internal/ConfiguredFieldImpl.java
b/modules/injection/standalone/src/main/java/org/apache/tamaya/inject/internal/ConfiguredFieldImpl.java
index 6655643..32bbb89 100644
--- a/modules/injection/standalone/src/main/java/org/apache/tamaya/inject/internal/ConfiguredFieldImpl.java
+++ b/modules/injection/standalone/src/main/java/org/apache/tamaya/inject/internal/ConfiguredFieldImpl.java
@@ -114,7 +114,7 @@ public class ConfiguredFieldImpl implements ConfiguredField{
 
             // Check for adapter/filter
             Object value = InjectionHelper.adaptValue(this.annotatedField,
-                    TypeLiteral.of(this.annotatedField.getType()), retKey[0], evaluatedValue);
+                    TypeLiteral.of(this.annotatedField.getGenericType()), retKey[0], evaluatedValue);
             AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
                 @Override
                 public Object run() throws Exception {

http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/e729bd0f/modules/injection/standalone/src/main/java/org/apache/tamaya/inject/internal/InjectionHelper.java
----------------------------------------------------------------------
diff --git a/modules/injection/standalone/src/main/java/org/apache/tamaya/inject/internal/InjectionHelper.java
b/modules/injection/standalone/src/main/java/org/apache/tamaya/inject/internal/InjectionHelper.java
index a143498..e207819 100644
--- a/modules/injection/standalone/src/main/java/org/apache/tamaya/inject/internal/InjectionHelper.java
+++ b/modules/injection/standalone/src/main/java/org/apache/tamaya/inject/internal/InjectionHelper.java
@@ -147,7 +147,7 @@ final class InjectionHelper {
     private static String evaluteConfigValue(List<String> keys, String[] retKey, Configuration
config) {
         String configValue = null;
         for (String key : keys) {
-            configValue = config.get(key);
+            configValue = config.getOrDefault(key, null);
             if (configValue != null) {
                 if(retKey!=null && retKey.length>0){
                     retKey[0] = key;
@@ -184,9 +184,6 @@ final class InjectionHelper {
         if (String.class == targetType.getType()) {
             return (T) configValue;
         } else{
-            if(configValue==null) {
-                return null;
-            }
             ConfigurationContext configContext = Configuration.current().getContext();
             List<PropertyConverter<T>> converters = configContext
                     .getPropertyConverters(targetType);
@@ -199,7 +196,10 @@ final class InjectionHelper {
                 }
             }
         }
-        throw new ConfigException("Non convertible property type: " + element);
+        if(configValue!=null) {
+            throw new ConfigException("Non convertible property type: " + element);
+        }
+        return null;
     }
 
     /**


Mime
View raw message