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

asf-gitbox-commits pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cayenne.git


The following commit(s) were added to refs/heads/master by this push:
     new 721adadea AdhocObjectFactory to allow object creation from class 
instances, not just Strings
721adadea is described below

commit 721adadea0241bd9785bc992cb28bf0b58a961c6
Author: Andrus Adamchik <[email protected]>
AuthorDate: Sun Aug 16 15:35:09 2026 -0400

    AdhocObjectFactory to allow object creation from class instances, not just 
Strings
---
 .../java/org/apache/cayenne/di/AdhocObjectFactory.java     |  8 ++++++++
 .../apache/cayenne/di/spi/DefaultAdhocObjectFactory.java   | 14 +++++++++-----
 2 files changed, 17 insertions(+), 5 deletions(-)

diff --git 
a/cayenne-di/src/main/java/org/apache/cayenne/di/AdhocObjectFactory.java 
b/cayenne-di/src/main/java/org/apache/cayenne/di/AdhocObjectFactory.java
index 1a151a6ed..31ea5a84d 100644
--- a/cayenne-di/src/main/java/org/apache/cayenne/di/AdhocObjectFactory.java
+++ b/cayenne-di/src/main/java/org/apache/cayenne/di/AdhocObjectFactory.java
@@ -41,6 +41,14 @@ public interface AdhocObjectFactory {
      */
     <T> T newInstance(Class<? super T> superType, String className, boolean 
skipInjection);
 
+    /**
+     * Returns an instance of "type" that implements "superType", injecting 
dependencies from the registry into it
+     * if requested
+     *
+     * @since 5.0
+     */
+    <T> T newInstance(Class<? super T> superType, Class<? extends T> type, 
boolean skipInjection);
+
     /**
      * Returns a Java class loaded using ClassLoader returned from
      * {@link ClassLoaderManager#getClassLoader(String)} for a given class 
name.
diff --git 
a/cayenne-di/src/main/java/org/apache/cayenne/di/spi/DefaultAdhocObjectFactory.java
 
b/cayenne-di/src/main/java/org/apache/cayenne/di/spi/DefaultAdhocObjectFactory.java
index 4c6beb327..a4314b0ac 100644
--- 
a/cayenne-di/src/main/java/org/apache/cayenne/di/spi/DefaultAdhocObjectFactory.java
+++ 
b/cayenne-di/src/main/java/org/apache/cayenne/di/spi/DefaultAdhocObjectFactory.java
@@ -45,21 +45,25 @@ public class DefaultAdhocObjectFactory implements 
AdhocObjectFactory {
         this.classLoaderManager = classLoaderManager;
     }
 
+    @SuppressWarnings("unchecked")
     @Override
     public <T> T newInstance(Class<? super T> superType, String className, 
boolean skipInjection) {
+        return newInstance(superType, (Class<? extends T>) 
getJavaClass(className), skipInjection);
+    }
+
+    @Override
+    public <T> T newInstance(Class<? super T> superType, Class<? extends T> 
type, boolean skipInjection) {
 
         if (superType == null) {
             throw new NullPointerException("Null superType");
         }
 
-        if (className == null) {
-            throw new NullPointerException("Null className");
+        if (type == null) {
+            throw new NullPointerException("Null type");
         }
 
-        Class<T> type = (Class<T>) getJavaClass(className);
-
         if (!superType.isAssignableFrom(type)) {
-            throw new DIRuntimeException("Class %s is not assignable to %s", 
className, superType.getName());
+            throw new DIRuntimeException("Class %s is not assignable to %s", 
type.getName(), superType.getName());
         }
 
         return skipInjection

Reply via email to