tamaya-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From anat...@apache.org
Subject incubator-tamaya git commit: TAMAYA-108: Fixed injection on not configurable types/fields/methods.
Date Thu, 24 Sep 2015 23:54:24 GMT
Repository: incubator-tamaya
Updated Branches:
  refs/heads/master f7aa8a480 -> be66a9a2f


TAMAYA-108: Fixed injection on not configurable types/fields/methods.


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

Branch: refs/heads/master
Commit: be66a9a2f9346b2ab36766d43e04d782c7066c4f
Parents: f7aa8a4
Author: anatole <anatole@apache.org>
Authored: Fri Sep 25 01:53:15 2015 +0200
Committer: anatole <anatole@apache.org>
Committed: Fri Sep 25 01:54:05 2015 +0200

----------------------------------------------------------------------
 .../tamaya/inject/internal/ConfiguredType.java  | 76 ++++++++++++--------
 .../internal/DefaultConfigurationInjector.java  | 45 +++++++++++-
 .../tamaya/inject/internal/InjectionUtils.java  |  2 +-
 3 files changed, 92 insertions(+), 31 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/be66a9a2/modules/injection/src/main/java/org/apache/tamaya/inject/internal/ConfiguredType.java
----------------------------------------------------------------------
diff --git a/modules/injection/src/main/java/org/apache/tamaya/inject/internal/ConfiguredType.java
b/modules/injection/src/main/java/org/apache/tamaya/inject/internal/ConfiguredType.java
index cba90e1..c5de8f9 100644
--- a/modules/injection/src/main/java/org/apache/tamaya/inject/internal/ConfiguredType.java
+++ b/modules/injection/src/main/java/org/apache/tamaya/inject/internal/ConfiguredType.java
@@ -64,11 +64,19 @@ public class ConfiguredType {
 
     public ConfiguredType(Class type) {
         this.type = Objects.requireNonNull(type);
-        initFields(type);
-        initMethods(type);
+        org.apache.tamaya.inject.ConfiguredType confType = (org.apache.tamaya.inject.ConfiguredType)
+                type.getAnnotation(org.apache.tamaya.inject.ConfiguredType.class);
+        if(confType!=null){
+            initFields(type, confType.autoConfigure());
+            initMethods(type, confType.autoConfigure());
+        }
+        else{
+            initFields(type, false);
+            initMethods(type, false);
+        }
     }
 
-    private void initFields(Class type) {
+    private void initFields(Class type, boolean autoConfigure) {
         for (Field f : type.getDeclaredFields()) {
             if (f.isAnnotationPresent(NoConfig.class)) {
                 LOG.finest("Ignored @NoConfig annotated field " + f.getClass().getName()
+ "#" +
@@ -86,10 +94,12 @@ public class ConfiguredType {
                 continue;
             }
             try {
-                ConfiguredField configuredField = new ConfiguredField(f);
-                configuredFields.add(configuredField);
-                LOG.finer("Registered field " + f.getClass().getName() + "#" +
-                        f.toGenericString());
+                if(isConfiguredField(f) || autoConfigure) {
+                    ConfiguredField configuredField = new ConfiguredField(f);
+                    configuredFields.add(configuredField);
+                    LOG.finer("Registered field " + f.getClass().getName() + "#" +
+                            f.toGenericString());
+                }
             } catch (Exception e) {
                 throw new ConfigException("Failed to initialized configured field: " +
                         f.getDeclaringClass().getName() + '.' + f.getName(), e);
@@ -97,7 +107,7 @@ public class ConfiguredType {
         }
     }
 
-    private void initMethods(Class type) {
+    private void initMethods(Class type, boolean autoConfigure) {
         // TODO revisit this logic here...
         for (Method m : type.getDeclaredMethods()) {
             if (m.isAnnotationPresent(NoConfig.class)) {
@@ -110,26 +120,28 @@ public class ConfiguredType {
                         m.toGenericString());
                 continue;
             }
-            ObservesConfigChange observesAnnot = m.getAnnotation(ObservesConfigChange.class);
-            ConfiguredProperty propAnnot = m.getAnnotation(ConfiguredProperty.class);
-            if (type.isInterface()) {
-                // it is a template
-                if (observesAnnot != null && m.isDefault()) {
-                    if(addObserverMethod(m)){
-                        LOG.finer("Added configured observer for template: " + m.getClass().getName()
+ "#" +
-                                m.toGenericString());
-                    }
-                }
-            } else {
-                if (observesAnnot != null) {
-                    if(addObserverMethod(m)){
-                        LOG.finer("Added configured observer: " + m.getClass().getName()
+ "#" +
-                                m.toGenericString());
+            if(isConfiguredMethod(m) || autoConfigure) {
+                ObservesConfigChange observesAnnot = m.getAnnotation(ObservesConfigChange.class);
+                ConfiguredProperty propAnnot = m.getAnnotation(ConfiguredProperty.class);
+                if (type.isInterface()) {
+                    // it is a template
+                    if (observesAnnot != null && m.isDefault()) {
+                        if (addObserverMethod(m)) {
+                            LOG.finer("Added configured observer for template: " + m.getClass().getName()
+ "#" +
+                                    m.toGenericString());
+                        }
                     }
                 } else {
-                    if (addPropertySetter(m, propAnnot)) {
-                        LOG.finer("Added configured setter: " + m.getClass().getName() +
"#" +
-                                m.toGenericString());
+                    if (observesAnnot != null) {
+                        if (addObserverMethod(m)) {
+                            LOG.finer("Added configured observer: " + m.getClass().getName()
+ "#" +
+                                    m.toGenericString());
+                        }
+                    } else {
+                        if (addPropertySetter(m, propAnnot)) {
+                            LOG.finer("Added configured setter: " + m.getClass().getName()
+ "#" +
+                                    m.toGenericString());
+                        }
                     }
                 }
             }
@@ -204,19 +216,27 @@ public class ConfiguredType {
         }
         // if no class level annotation is there we might have field level annotations only
         for (Field field : type.getDeclaredFields()) {
-            if (field.isAnnotationPresent(ConfiguredProperty.class)) {
+            if (isConfiguredField(field)) {
                 return true;
             }
         }
         // if no class level annotation is there we might have method level annotations only
         for (Method method : type.getDeclaredMethods()) {
-            if (method.isAnnotationPresent(ConfiguredProperty.class)) {
+            if(isConfiguredMethod(method)) {
                 return true;
             }
         }
         return false;
     }
 
+    public static boolean isConfiguredField(Field field) {
+        return field.isAnnotationPresent(ConfiguredProperty.class);
+    }
+
+    public static boolean isConfiguredMethod(Method method) {
+        return method.isAnnotationPresent(ConfiguredProperty.class);
+    }
+
     public Class getType() {
         return this.type;
     }

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/be66a9a2/modules/injection/src/main/java/org/apache/tamaya/inject/internal/DefaultConfigurationInjector.java
----------------------------------------------------------------------
diff --git a/modules/injection/src/main/java/org/apache/tamaya/inject/internal/DefaultConfigurationInjector.java
b/modules/injection/src/main/java/org/apache/tamaya/inject/internal/DefaultConfigurationInjector.java
index 9cdfe1a..1dbaa7d 100644
--- a/modules/injection/src/main/java/org/apache/tamaya/inject/internal/DefaultConfigurationInjector.java
+++ b/modules/injection/src/main/java/org/apache/tamaya/inject/internal/DefaultConfigurationInjector.java
@@ -19,15 +19,21 @@
 package org.apache.tamaya.inject.internal;
 
 import org.apache.tamaya.ConfigurationProvider;
+import org.apache.tamaya.event.ObservesConfigChange;
 import org.apache.tamaya.inject.ConfigurationInjector;
 
 import javax.annotation.Priority;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
 import java.lang.reflect.Proxy;
 import java.util.Map;
 import java.util.Objects;
 import java.util.concurrent.ConcurrentHashMap;
+import java.util.logging.Logger;
 
 import org.apache.tamaya.inject.ConfiguredItemSupplier;
+import org.apache.tamaya.inject.ConfiguredProperty;
+import org.apache.tamaya.inject.NoConfig;
 
 /**
  * Simple injector singleton that also registers instances configured using weak references.
@@ -37,6 +43,8 @@ public final class DefaultConfigurationInjector implements ConfigurationInjector
 
     private Map<Class<?>, ConfiguredType> configuredTypes = new ConcurrentHashMap<>();
 
+    private static final Logger LOG = Logger.getLogger(DefaultConfigurationInjector.class.getName());
+
     /**
      * Extract the configuration annotation config and registers it per class, for later
reuse.
      *
@@ -45,7 +53,7 @@ public final class DefaultConfigurationInjector implements ConfigurationInjector
      */
     public ConfiguredType registerType(Class<?> type) {
         ConfiguredType confType = configuredTypes.get(type);
-        if (confType == null) {
+        if (confType == null && isConfigured(type)) {
             confType = new ConfiguredType(type);
             ModelPopulator.register(confType);
             configuredTypes.put(type, confType);
@@ -55,6 +63,34 @@ public final class DefaultConfigurationInjector implements ConfigurationInjector
     }
 
     /**
+     * CHecks if type is eligible for configuration injection.
+     * @param type the target type, not null.
+     * @return true, if the type, a method or field has Tamaya config annotation on it.
+     */
+    private boolean isConfigured(Class<?> type) {
+        if(type.getClass().isAnnotationPresent(org.apache.tamaya.inject.ConfiguredType.class)){
+            return true;
+        }
+        for (Field f : type.getDeclaredFields()) {
+            if (f.isAnnotationPresent(NoConfig.class)) {
+                return true;
+            }
+            if (f.isAnnotationPresent(ObservesConfigChange.class) || f.isAnnotationPresent(ConfiguredProperty.class))
{
+                return true;
+            }
+        }
+        for (Method m : type.getDeclaredMethods()) {
+            if (m.isAnnotationPresent(NoConfig.class)) {
+                return true;
+            }
+            if (m.isAnnotationPresent(ObservesConfigChange.class) || m.isAnnotationPresent(ConfiguredProperty.class))
{
+                return true;
+            }
+        }
+        return false;
+    }
+
+    /**
      * Configured the current instance and reigsterd necessary listener to forward config
change events as
      * defined by the current annotations in place.
      *
@@ -64,7 +100,12 @@ public final class DefaultConfigurationInjector implements ConfigurationInjector
     public <T> T configure(T instance) {
         Class<?> type = Objects.requireNonNull(instance).getClass();
         ConfiguredType configuredType = registerType(type);
-        Objects.requireNonNull(configuredType).configure(instance);
+        if(configuredType!=null){
+            configuredType.configure(instance);
+        }
+        else{
+            LOG.info("Instance passed is not annotated for configuration: " + instance);
+        }
         return instance;
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/be66a9a2/modules/injection/src/main/java/org/apache/tamaya/inject/internal/InjectionUtils.java
----------------------------------------------------------------------
diff --git a/modules/injection/src/main/java/org/apache/tamaya/inject/internal/InjectionUtils.java
b/modules/injection/src/main/java/org/apache/tamaya/inject/internal/InjectionUtils.java
index c8b52da..3dddc20 100644
--- a/modules/injection/src/main/java/org/apache/tamaya/inject/internal/InjectionUtils.java
+++ b/modules/injection/src/main/java/org/apache/tamaya/inject/internal/InjectionUtils.java
@@ -86,7 +86,7 @@ final class InjectionUtils {
                 // absolute key, strip away brackets, take key as is
                 iterator.set(next.substring(1, next.length() - 1));
             } else {
-                if (areasAnnot != null) {
+                if (areasAnnot != null && areasAnnot.defaultSections().length>0)
{
                     // Remove original entry, since it will be replaced with prefixed entries
                     iterator.remove();
                     // Add prefixed entries, including absolute (root) entry for "" area
keys.


Mime
View raw message