You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@brooklyn.apache.org by he...@apache.org on 2021/05/14 14:38:28 UTC

[brooklyn-server] branch master updated: refactor to make findConfigKeys shareable

This is an automated email from the ASF dual-hosted git repository.

heneveld pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/brooklyn-server.git


The following commit(s) were added to refs/heads/master by this push:
     new 0f2c4ca  refactor to make findConfigKeys shareable
0f2c4ca is described below

commit 0f2c4ca6fc4146f0e601d2b865380f206888bfc5
Author: Alex Heneveld <al...@cloudsoftcorp.com>
AuthorDate: Fri May 14 15:38:11 2021 +0100

    refactor to make findConfigKeys shareable
---
 .../core/entity/internal/ConfigUtilsInternal.java  | 55 +++++++++++++++++++
 .../org/apache/brooklyn/core/objs/AdjunctType.java | 62 ++--------------------
 .../brooklyn/core/objs/BrooklynDynamicType.java    |  3 ++
 3 files changed, 62 insertions(+), 58 deletions(-)

diff --git a/core/src/main/java/org/apache/brooklyn/core/entity/internal/ConfigUtilsInternal.java b/core/src/main/java/org/apache/brooklyn/core/entity/internal/ConfigUtilsInternal.java
index fb3a0e0..d967faf 100644
--- a/core/src/main/java/org/apache/brooklyn/core/entity/internal/ConfigUtilsInternal.java
+++ b/core/src/main/java/org/apache/brooklyn/core/entity/internal/ConfigUtilsInternal.java
@@ -18,13 +18,19 @@
  */
 package org.apache.brooklyn.core.entity.internal;
 
+import com.google.common.collect.Maps;
+import java.lang.reflect.Field;
+import java.lang.reflect.Modifier;
 import java.util.LinkedHashMap;
 import java.util.Map;
 
 import org.apache.brooklyn.api.objs.Configurable;
 import org.apache.brooklyn.config.ConfigKey;
+import org.apache.brooklyn.config.ConfigKey.HasConfigKey;
 import org.apache.brooklyn.util.collections.MutableMap;
+import org.apache.brooklyn.util.exceptions.Exceptions;
 import org.apache.brooklyn.util.guava.Maybe;
+import org.apache.brooklyn.util.javalang.Reflections;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -105,4 +111,53 @@ public class ConfigUtilsInternal {
             }
         }
     }
+
+    public static <T> Map<String,ConfigKey<?>> findConfigKeys(Class<? extends T> clazz, T optionalInstance) {
+        try {
+            Map<String,ConfigKey<?>> result = Maps.newLinkedHashMap();
+            Map<String, Field> configFields = Maps.newLinkedHashMap();
+            for (Field f : clazz.getFields()) {
+                boolean isConfigKey = ConfigKey.class.isAssignableFrom(f.getType());
+                if (!isConfigKey) {
+                    if (!HasConfigKey.class.isAssignableFrom(f.getType())) {
+                        // neither ConfigKey nor HasConfigKey
+                        continue;
+                    }
+                }
+                if (!Modifier.isStatic(f.getModifiers())) {
+                    // require it to be static or we have an instance
+                    LOG.warn("Discouraged use of non-static config key "+f+" defined in " + (optionalInstance!=null ? optionalInstance : clazz));
+                    if (optionalInstance==null) continue;
+                }
+                ConfigKey<?> k = isConfigKey ? (ConfigKey<?>) f.get(optionalInstance) :
+                        ((HasConfigKey<?>)f.get(optionalInstance)).getConfigKey();
+
+                Field alternativeField = configFields.get(k.getName());
+                // Allow overriding config keys (e.g. to set default values) when there is an assignable-from relationship between classes
+                Field definitiveField = alternativeField != null ? Reflections.inferSubbestField(alternativeField, f) : f;
+                boolean skip = false;
+                if (definitiveField != f) {
+                    // If they refer to the _same_ instance, just keep the one we already have
+                    if (alternativeField.get(optionalInstance) == f.get(optionalInstance)) skip = true;
+                }
+                if (skip) {
+                    //nothing
+                } else if (definitiveField == f) {
+                    result.put(k.getName(), k);
+                    configFields.put(k.getName(), f);
+                } else if (definitiveField != null) {
+                    if (LOG.isDebugEnabled()) LOG.debug("multiple definitions for config key {} on {}; preferring that in sub-class: {} to {}", new Object[] {
+                            k.getName(), optionalInstance!=null ? optionalInstance : clazz, alternativeField, f});
+                } else if (definitiveField == null) {
+                    LOG.warn("multiple definitions for config key {} on {}; preferring {} to {}", new Object[] {
+                            k.getName(), optionalInstance!=null ? optionalInstance : clazz, alternativeField, f});
+                }
+            }
+
+            return result;
+        } catch (IllegalAccessException e) {
+            throw Exceptions.propagate(e);
+        }
+    }
+
 }
diff --git a/core/src/main/java/org/apache/brooklyn/core/objs/AdjunctType.java b/core/src/main/java/org/apache/brooklyn/core/objs/AdjunctType.java
index 2366a79..d952770 100644
--- a/core/src/main/java/org/apache/brooklyn/core/objs/AdjunctType.java
+++ b/core/src/main/java/org/apache/brooklyn/core/objs/AdjunctType.java
@@ -28,6 +28,8 @@ import java.util.Set;
 import org.apache.brooklyn.api.objs.EntityAdjunct;
 import org.apache.brooklyn.config.ConfigKey;
 import org.apache.brooklyn.config.ConfigKey.HasConfigKey;
+import org.apache.brooklyn.core.config.ConfigUtils;
+import org.apache.brooklyn.core.entity.internal.ConfigUtilsInternal;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -112,63 +114,7 @@ public class AdjunctType implements Serializable {
      */
     // TODO Remove duplication from EntityDynamicType
     protected static Map<String,ConfigKey<?>> findConfigKeys(Class<? extends EntityAdjunct> clazz, EntityAdjunct optionalInstance) {
-        try {
-            Map<String,ConfigKey<?>> result = Maps.newLinkedHashMap();
-            Map<String,Field> configFields = Maps.newLinkedHashMap();
-            for (Field f : clazz.getFields()) {
-                boolean isConfigKey = ConfigKey.class.isAssignableFrom(f.getType());
-                if (!isConfigKey) {
-                    if (!HasConfigKey.class.isAssignableFrom(f.getType())) {
-                        // neither ConfigKey nor HasConfigKey
-                        continue;
-                    }
-                }
-                if (!Modifier.isStatic(f.getModifiers())) {
-                    // require it to be static or we have an instance
-                    LOG.warn("Discouraged use of non-static config key "+f+" defined in " + (optionalInstance!=null ? optionalInstance : clazz));
-                    if (optionalInstance==null) continue;
-                }
-                ConfigKey<?> k = isConfigKey ? (ConfigKey<?>) f.get(optionalInstance) : 
-                    ((HasConfigKey<?>)f.get(optionalInstance)).getConfigKey();
-
-                Field alternativeField = configFields.get(k.getName());
-                // Allow overriding config keys (e.g. to set default values) when there is an assignable-from relationship between classes
-                Field definitiveField = alternativeField != null ? inferSubbestField(alternativeField, f) : f;
-                boolean skip = false;
-                if (definitiveField != f) {
-                    // If they refer to the _same_ instance, just keep the one we already have
-                    if (alternativeField.get(optionalInstance) == f.get(optionalInstance)) skip = true;
-                }
-                if (skip) {
-                    //nothing
-                } else if (definitiveField == f) {
-                    result.put(k.getName(), k);
-                    configFields.put(k.getName(), f);
-                } else if (definitiveField != null) {
-                    if (LOG.isDebugEnabled()) LOG.debug("multiple definitions for config key {} on {}; preferring that in sub-class: {} to {}", new Object[] {
-                            k.getName(), optionalInstance!=null ? optionalInstance : clazz, alternativeField, f});
-                } else if (definitiveField == null) {
-                    LOG.warn("multiple definitions for config key {} on {}; preferring {} to {}", new Object[] {
-                            k.getName(), optionalInstance!=null ? optionalInstance : clazz, alternativeField, f});
-                }
-            }
-            
-            return result;
-        } catch (IllegalAccessException e) {
-            throw Throwables.propagate(e);
-        }
-    }
-    
-    /**
-     * Gets the field that is in the sub-class; or null if one field does not come from a sub-class of the other field's class
-     */
-    // TODO Remove duplication from EntityDynamicType
-    private static Field inferSubbestField(Field f1, Field f2) {
-        Class<?> c1 = f1.getDeclaringClass();
-        Class<?> c2 = f2.getDeclaringClass();
-        boolean isSuper1 = c1.isAssignableFrom(c2);
-        boolean isSuper2 = c2.isAssignableFrom(c1);
-        return (isSuper1) ? (isSuper2 ? null : f2) : (isSuper2 ? f1 : null);
+        return ConfigUtilsInternal.findConfigKeys(clazz, optionalInstance);
     }
-    
+
 }
diff --git a/core/src/main/java/org/apache/brooklyn/core/objs/BrooklynDynamicType.java b/core/src/main/java/org/apache/brooklyn/core/objs/BrooklynDynamicType.java
index 69f8ec2..4891812 100644
--- a/core/src/main/java/org/apache/brooklyn/core/objs/BrooklynDynamicType.java
+++ b/core/src/main/java/org/apache/brooklyn/core/objs/BrooklynDynamicType.java
@@ -160,6 +160,9 @@ public abstract class BrooklynDynamicType<T extends BrooklynObject, AbstractT ex
      */
     protected static void buildConfigKeys(Class<? extends BrooklynObject> clazz, AbstractBrooklynObject optionalInstance, 
             Map<String, FieldAndValue<ConfigKey<?>>> configKeys) {
+
+        // TODO remove or document overlap with ConfigUtilsInternal.findConfigKeys
+
         ListMultimap<String,FieldAndValue<ConfigKey<?>>> configKeysAll = 
                 ArrayListMultimap.<String, FieldAndValue<ConfigKey<?>>>create();