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 53da9592c18 CAMEL-18800: camel-jbang - Maven downloader backwards 
compatible with javax.inject annotations that are in use by Maven 3.8.x and 
Maven resolver.
53da9592c18 is described below

commit 53da9592c18e78b30c1aac8f87b187a548952580
Author: Claus Ibsen <[email protected]>
AuthorDate: Wed Jan 11 14:40:50 2023 +0100

    CAMEL-18800: camel-jbang - Maven downloader backwards compatible with 
javax.inject annotations that are in use by Maven 3.8.x and Maven resolver.
---
 .../apache/camel/main/injection/DIRegistry.java    | 50 ++++++++++++++++++++--
 1 file changed, 47 insertions(+), 3 deletions(-)

diff --git 
a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/injection/DIRegistry.java
 
b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/injection/DIRegistry.java
index b8b98a336bd..e229ff0a211 100644
--- 
a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/injection/DIRegistry.java
+++ 
b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/injection/DIRegistry.java
@@ -40,6 +40,7 @@ import jakarta.inject.Inject;
 import jakarta.inject.Named;
 
 import org.apache.camel.support.SupplierRegistry;
+import org.apache.camel.util.StringHelper;
 import org.apache.camel.util.function.Suppliers;
 
 /**
@@ -67,6 +68,49 @@ public class DIRegistry extends SupplierRegistry {
         bind(type, type);
     }
 
+    private static boolean hasInjectAnnotation(Constructor<?> ctr) {
+        if (ctr.getAnnotation(Inject.class) != null) {
+            return true;
+        }
+
+        for (Annotation a : ctr.getAnnotations()) {
+            String s = a.annotationType().getName();
+            if ("javax.inject.Inject".equals(s)) {
+                return true;
+            }
+        }
+
+        return false;
+    }
+
+    private static boolean isNamedAnnotation(Annotation ann) {
+        if (Named.class == ann.annotationType()) {
+            return true;
+        }
+
+        // backwards comp
+        String s = ann.annotationType().getName();
+        return "javax.inject.Named".equals(s);
+    }
+
+    private static String getNamedAnnotationValue(Class<?> type) {
+        Named ann = type.getAnnotation(Named.class);
+        if (ann != null) {
+            return ann.value();
+        }
+
+        for (Annotation a : type.getAnnotations()) {
+            if (isNamedAnnotation(a)) {
+                String s = a.toString();
+                // @javax.inject.Named("file-lock")
+                String name = StringHelper.between(s, "(\"", "\")");
+                return name;
+            }
+        }
+
+        return null;
+    }
+
     /**
      * Main "registration" method, where {@code beanClass} is expected to be a 
pojo class with non-default constructor
      * annotated with {@link Inject}. The class may be annotated with {@link 
Named}. (Maybe supporting
@@ -78,8 +122,8 @@ public class DIRegistry extends SupplierRegistry {
     public void bind(Class<?> key, Class<?> type) {
         String name = key.getName();
         for (Annotation ann : type.getAnnotations()) {
-            if (Named.class == ann.annotationType()) {
-                name = type.getAnnotation(Named.class).value();
+            if (isNamedAnnotation(ann)) {
+                name = getNamedAnnotationValue(type);
                 if (name == null || "".equals(name.trim())) {
                     name = key.getName();
                 }
@@ -94,7 +138,7 @@ public class DIRegistry extends SupplierRegistry {
             if (ctr.getParameterCount() == 0) {
                 defaultConstructor = ctr;
             } else {
-                if (ctr.getAnnotation(Inject.class) != null) {
+                if (hasInjectAnnotation(ctr)) {
                     constructors.add(ctr);
                 }
             }

Reply via email to