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

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new 4f503fbff33 camel-java-joor-dsl - Do not swallow if a compiled class 
cannot be created as instance due to bean post processing failure.
4f503fbff33 is described below

commit 4f503fbff334d9fd437dc8b45f3287aed6d5357b
Author: Claus Ibsen <[email protected]>
AuthorDate: Wed May 18 14:09:31 2022 +0200

    camel-java-joor-dsl - Do not swallow if a compiled class cannot be created 
as instance due to bean post processing failure.
---
 .../java/org/apache/camel/util/ObjectHelper.java   | 16 +++++++++++
 .../dsl/java/joor/JavaRoutesBuilderLoader.java     | 32 ++++++++++++++--------
 2 files changed, 36 insertions(+), 12 deletions(-)

diff --git 
a/core/camel-util/src/main/java/org/apache/camel/util/ObjectHelper.java 
b/core/camel-util/src/main/java/org/apache/camel/util/ObjectHelper.java
index 173b44ab0ed..10f7feb6203 100644
--- a/core/camel-util/src/main/java/org/apache/camel/util/ObjectHelper.java
+++ b/core/camel-util/src/main/java/org/apache/camel/util/ObjectHelper.java
@@ -24,6 +24,7 @@ import java.lang.reflect.Array;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.Field;
 import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
 import java.net.URL;
 import java.nio.charset.Charset;
 import java.util.ArrayList;
@@ -995,6 +996,21 @@ public final class ObjectHelper {
         return false;
     }
 
+    /**
+     * Does the given class have a default no-arg constructor (public or 
inherited).
+     */
+    public static boolean hasDefaultNoArgConstructor(Class<?> type) {
+        if (hasDefaultPublicNoArgConstructor(type)) {
+            return true;
+        }
+        for (Constructor<?> ctr : type.getDeclaredConstructors()) {
+            if (!Modifier.isPrivate(ctr.getModifiers()) && 
ctr.getParameterCount() == 0) {
+                return true;
+            }
+        }
+        return false;
+    }
+
     /**
      * Returns the type of the given object or null if the value is null
      */
diff --git 
a/dsl/camel-java-joor-dsl/src/main/java/org/apache/camel/dsl/java/joor/JavaRoutesBuilderLoader.java
 
b/dsl/camel-java-joor-dsl/src/main/java/org/apache/camel/dsl/java/joor/JavaRoutesBuilderLoader.java
index 238721e66eb..5df707f7032 100644
--- 
a/dsl/camel-java-joor-dsl/src/main/java/org/apache/camel/dsl/java/joor/JavaRoutesBuilderLoader.java
+++ 
b/dsl/camel-java-joor-dsl/src/main/java/org/apache/camel/dsl/java/joor/JavaRoutesBuilderLoader.java
@@ -20,6 +20,7 @@ import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.InputStream;
+import java.lang.reflect.Modifier;
 import java.nio.file.Files;
 import java.util.ArrayList;
 import java.util.Collection;
@@ -32,6 +33,7 @@ import java.util.regex.Pattern;
 import org.apache.camel.CamelContextAware;
 import org.apache.camel.ExtendedCamelContext;
 import org.apache.camel.RoutesBuilder;
+import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.api.management.ManagedResource;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.dsl.support.ExtendedRouteBuilderLoaderSupport;
@@ -44,6 +46,7 @@ import org.apache.camel.support.ResourceHelper;
 import org.apache.camel.support.RouteWatcherReloadStrategy;
 import org.apache.camel.util.FileUtil;
 import org.apache.camel.util.IOHelper;
+import org.apache.camel.util.ObjectHelper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -114,18 +117,23 @@ public class JavaRoutesBuilderLoader extends 
ExtendedRouteBuilderLoaderSupport {
 
             Class<?> clazz = result.getClass(className);
             if (clazz != null) {
-                try {
-                    // requires a default no-arg constructor otherwise we skip 
the class
-                    obj = getCamelContext().getInjector().newInstance(clazz);
-                } catch (Exception e) {
-                    LOG.debug("Compiled class: " + className + " must have a 
default no-arg constructor. Skipping.");
-                }
-                if (obj != null) {
-                    LOG.debug("Compiled: {} -> {}", className, obj);
-
-                    // inject context and resource
-                    CamelContextAware.trySetCamelContext(obj, 
getCamelContext());
-                    ResourceAware.trySetResource(obj, 
nameToResource.get(className));
+                boolean skip = clazz.isInterface() || 
Modifier.isAbstract(clazz.getModifiers()) || 
Modifier.isPrivate(clazz.getModifiers());
+                // must have a default no-arg constructor to be able to create 
an instance
+                boolean ctr = ObjectHelper.hasDefaultNoArgConstructor(clazz);
+                if (ctr && !skip) {
+                    // create a new instance of the class
+                    try {
+                        obj = 
getCamelContext().getInjector().newInstance(clazz);
+                        if (obj != null) {
+                            LOG.debug("Compiled: {} -> {}", className, obj);
+
+                            // inject context and resource
+                            CamelContextAware.trySetCamelContext(obj, 
getCamelContext());
+                            ResourceAware.trySetResource(obj, 
nameToResource.get(className));
+                        }
+                    } catch (Exception e) {
+                        throw new RuntimeCamelException("Cannot create 
instance of class: " + className, e);
+                    }
                 }
             }
 

Reply via email to