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

doebele pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/empire-db.git


The following commit(s) were added to refs/heads/master by this push:
     new 6216e8c6 EMPIREDB-410 ClassUtils newInstance added
6216e8c6 is described below

commit 6216e8c68fd465d288c3ff4b8857aadbc75fc56f
Author: Rainer Döbele <[email protected]>
AuthorDate: Thu Apr 18 15:36:20 2024 +0200

    EMPIREDB-410
    ClassUtils newInstance added
---
 .../java/org/apache/empire/commons/ClassUtils.java | 37 ++++++++++++++++++++++
 1 file changed, 37 insertions(+)

diff --git a/empire-db/src/main/java/org/apache/empire/commons/ClassUtils.java 
b/empire-db/src/main/java/org/apache/empire/commons/ClassUtils.java
index 60621c4d..33fdb1ef 100644
--- a/empire-db/src/main/java/org/apache/empire/commons/ClassUtils.java
+++ b/empire-db/src/main/java/org/apache/empire/commons/ClassUtils.java
@@ -343,6 +343,43 @@ public final class ClassUtils
         }
     }
 
+    /**
+     * Creates a new Object instance
+     * @param typeClass the class of the object to instantiate
+     * @return the instance
+     */
+    public static <T> T newInstance(Class<T> typeClass)
+    {
+        try
+        {
+            return typeClass.newInstance();
+        }
+        catch (InstantiationException | IllegalAccessException e)
+        {
+            throw new InternalException(e);
+        }
+    }
+
+    /**
+     * Creates a new Object instance
+     * @param typeConstructor the constructor of the object to instantiate
+     * @return the instance
+     */
+    public static <T> T newInstance(Constructor<T> typeConstructor, Object... 
params)
+    {
+        try
+        {   // Check number of arguments
+            if (typeConstructor.getParameterCount()!=params.length)
+                throw new InvalidArgumentException("typeConstructor|params", 
new Object[] { typeConstructor.getParameterCount(), params.length });
+            // create
+            return typeConstructor.newInstance(params);
+        }
+        catch (InstantiationException | IllegalAccessException | 
IllegalArgumentException | InvocationTargetException e)
+        {
+            throw new InternalException(e);
+        }
+    }
+
     /**
      * copied from org.apache.commons.beanutils.ConstructorUtils since it's 
private there
      * @param <T> the class type

Reply via email to