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

struberg pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/deltaspike.git

commit b7c4f46c1ef893d37f02ca97ef5b5d4bce93e24a
Author: Mark Struberg <strub...@apache.org>
AuthorDate: Sat Jul 9 19:46:12 2022 +0200

    DELTASPIKE-1455 call setProperties via reflection
    
    Only Weld3 and beyond has a setProperties method.
    Thus we only support passing properties for those containers and have
    to use reflection to invoke that method.
---
 .../cdise/weld/WeldContainerControl.java           | 62 +++++++++++++++++++++-
 .../apache/deltaspike/core/util/ClassUtils.java    | 28 ++++++++--
 2 files changed, 85 insertions(+), 5 deletions(-)

diff --git 
a/deltaspike/cdictrl/impl-weld/src/main/java/org/apache/deltaspike/cdise/weld/WeldContainerControl.java
 
b/deltaspike/cdictrl/impl-weld/src/main/java/org/apache/deltaspike/cdise/weld/WeldContainerControl.java
index 10578294..ea1157e4 100644
--- 
a/deltaspike/cdictrl/impl-weld/src/main/java/org/apache/deltaspike/cdise/weld/WeldContainerControl.java
+++ 
b/deltaspike/cdictrl/impl-weld/src/main/java/org/apache/deltaspike/cdise/weld/WeldContainerControl.java
@@ -29,6 +29,8 @@ import javax.enterprise.context.spi.CreationalContext;
 import javax.enterprise.inject.spi.Bean;
 import javax.enterprise.inject.spi.BeanManager;
 
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
 import java.util.Map;
 import java.util.Set;
 import java.util.logging.Logger;
@@ -77,10 +79,65 @@ public class WeldContainerControl implements CdiContainer
     public void boot(Map<?, ?> properties)
     {
         weld = new Weld();
-        weld.setProperties(convertProperties(properties));
+
+        // setProperties only exists from Weld-2.x onwards
+        setProperties(weld, convertProperties(properties));
+
         weldContainer = weld.initialize();
     }
 
+    private void setProperties(Weld weld, Map<String, Object> properties)
+    {
+        if (properties == null || properties.isEmpty())
+        {
+            return;
+        }
+
+        Method setPropertiesMethod = extractMethod(Weld.class, 
"setProperties", Map.class);
+        if (setPropertiesMethod != null)
+        {
+            if (!setPropertiesMethod.isAccessible())
+            {
+                setPropertiesMethod.setAccessible(true);
+            }
+
+            try
+            {
+                setPropertiesMethod.invoke(weld, properties);
+            }
+            catch (IllegalAccessException | InvocationTargetException e)
+            {
+                throw new RuntimeException(e);
+            }
+        }
+        else
+        {
+            throw new IllegalStateException("No setProperties method available 
on this version of Weld!");
+        }
+    }
+
+    /**
+     * Extract a method with the given name and parameterTypes.
+     * Return {@code null} if no such visible method exists on the given class.
+     *
+     * @param clazz
+     * @param methodName
+     * @param parameterTypes
+     * @return
+     */
+    private static Method extractMethod(Class<?> clazz, String methodName, 
Class<?>... parameterTypes)
+    {
+        try
+        {
+            return clazz != null ? clazz.getMethod(methodName, parameterTypes) 
: null;
+        }
+        catch (NoSuchMethodException e)
+        {
+            return null;
+        }
+    }
+
+
     @Override
     public synchronized  void shutdown()
     {
@@ -148,7 +205,8 @@ public class WeldContainerControl implements CdiContainer
         return "WeldContainerControl [Weld " + 
Formats.version(Container.class.getPackage()) + ']';
     }
     
-    private static Map<String, Object> convertProperties(final Map<?, ?> map) {
+    private static Map<String, Object> convertProperties(final Map<?, ?> map)
+    {
         return map.entrySet().stream()
                 .collect(Collectors.toMap(
                         entry -> String.valueOf(entry.getKey()),
diff --git 
a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/util/ClassUtils.java
 
b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/util/ClassUtils.java
index 9347900c..670aad6d 100644
--- 
a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/util/ClassUtils.java
+++ 
b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/util/ClassUtils.java
@@ -31,9 +31,10 @@ import java.util.jar.Attributes;
 import java.net.URL;
 
 /**
- * Util methods for classes, {@link ClassLoader} and {@link Manifest} handling
+ * Util methods for classes, {@link ClassLoader} and {@link Manifest} handling.
+ *
+ * Abstract to not get picked up as CDI bean on old containers.
  */
-//X TODO quite a few of this methods needs merging with Seam Solder and a few 
can get dropped at all.
 @Typed()
 public abstract class ClassUtils
 {
@@ -398,7 +399,7 @@ public abstract class ClassUtils
         String classLocation = 
targetClass.getResource(targetClass.getSimpleName() + ".class").toString();
         return classLocation.substring(0, classLocation.indexOf(classFilePath) 
- 1) + manifestFilePath;
     }
-    
+
     /**
      * Checks if the given class contains a method with the same signature.
      * 
@@ -431,6 +432,27 @@ public abstract class ClassUtils
         }
     }
 
+    /**
+     * Extract a method with the given name and parameterTypes.
+     * Return {@code null} if no such visible method exists on the given class.
+     *
+     * @param clazz
+     * @param methodName
+     * @param parameterTypes
+     * @return
+     */
+    public static Method extractMethod(Class<?> clazz, String methodName, 
Class<?>... parameterTypes)
+    {
+        try
+        {
+            return clazz != null ? clazz.getMethod(methodName, parameterTypes) 
: null;
+        }
+        catch (NoSuchMethodException e)
+        {
+            return null;
+        }
+    }
+
     /**
      * Checks if the given class has a method with the same signature, taking 
in to account generic types
      * @param targetClass

Reply via email to