You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2023/04/01 08:57:13 UTC

[camel] 01/04: CAMEL-19104: camel-bean - Method matching for class types now require to specify .class, eg String -> String.class. This optimizes to avoid unnessarry class resolver, and also makes it explicit its a class and not a parameter value.

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

commit cc58a9ac1df9d4235b6c6200eb976a75f3566756
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Sat Apr 1 10:31:05 2023 +0200

    CAMEL-19104: camel-bean - Method matching for class types now require to specify .class, eg String -> String.class. This optimizes to avoid unnessarry class resolver, and also makes it explicit its a class and not a parameter value.
---
 .../camel-bean/src/main/docs/bean-language.adoc     |  2 +-
 .../camel-bean/src/main/docs/class-component.adoc   | 21 ++++++++++++---------
 .../org/apache/camel/component/bean/BeanHelper.java | 14 +++++++++++---
 .../bean/BeanOverloadedCovariantMethodTest.java     |  2 +-
 .../component/bean/BeanOverloadedMethodFQNTest.java | 16 ++++++++--------
 .../BeanOverloadedMethodParameterValueTest.java     |  4 ++--
 .../component/bean/BeanOverloadedMethodTest.java    | 16 ++++++++--------
 7 files changed, 43 insertions(+), 32 deletions(-)

diff --git a/components/camel-bean/src/main/docs/bean-language.adoc b/components/camel-bean/src/main/docs/bean-language.adoc
index f0209a7a148..8c9b2e47a48 100644
--- a/components/camel-bean/src/main/docs/bean-language.adoc
+++ b/components/camel-bean/src/main/docs/bean-language.adoc
@@ -37,7 +37,7 @@ from("activemq:topic:OrdersTopic")
 ----
 
 TIP: It is also possible to omit the method name, then Camel would have to choose the best suitable
-method to use; this process is a little bit complex, so it is good practice to specify the method name,
+method to use; this process is a complex, so it is good practice to specify the method name,
 
 And in XML DSL
 
diff --git a/components/camel-bean/src/main/docs/class-component.adoc b/components/camel-bean/src/main/docs/class-component.adoc
index 7e3cf80419f..28ae99eb7ce 100644
--- a/components/camel-bean/src/main/docs/class-component.adoc
+++ b/components/camel-bean/src/main/docs/class-component.adoc
@@ -54,7 +54,9 @@ component but by specifying the fully qualified classname instead. +
 
 [source,java]
 -------------------------------------------------------------------------------------------------
-    from("direct:start").to("class:org.apache.camel.component.bean.MyFooBean").to("mock:result");
+from("direct:start")
+    .to("class:org.apache.camel.component.bean.MyFooBean")
+    .to("mock:result");
 -------------------------------------------------------------------------------------------------
 
 You can also specify which method to invoke on the `MyFooBean`, for
@@ -62,7 +64,9 @@ example `hello`:
 
 [source,java]
 --------------------------------------------------------------------------------------------------------------
-    from("direct:start").to("class:org.apache.camel.component.bean.MyFooBean?method=hello").to("mock:result");
+from("direct:start")
+    .to("class:org.apache.camel.component.bean.MyFooBean?method=hello")
+    .to("mock:result");
 --------------------------------------------------------------------------------------------------------------
 
 == Setting properties on the created instance
@@ -72,9 +76,9 @@ instance, for example if it has a `setPrefix` method:
 
 [source,java]
 ---------------------------------------------------------------------------------
-   from("direct:start")
-        .to("class:org.apache.camel.component.bean.MyPrefixBean?bean.prefix=Bye")
-        .to("mock:result");
+from("direct:start")
+    .to("class:org.apache.camel.component.bean.MyPrefixBean?bean.prefix=Bye")
+    .to("mock:result");
 ---------------------------------------------------------------------------------
 
 And you can also use the `#` syntax to refer to properties to be looked
@@ -82,9 +86,9 @@ up in the Registry.
 
 [source,java]
 --------------------------------------------------------------------------------
-    from("direct:start")
-        .to("class:org.apache.camel.component.bean.MyPrefixBean?bean.cool=#foo")
-        .to("mock:result");
+from("direct:start")
+    .to("class:org.apache.camel.component.bean.MyPrefixBean?bean.cool=#foo")
+    .to("mock:result");
 --------------------------------------------------------------------------------
 
 Which will lookup a bean from the Registry with the
@@ -98,5 +102,4 @@ component works in much the same way.
 ====
 
 
-
 include::spring-boot:partial$starter.adoc[]
diff --git a/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanHelper.java b/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanHelper.java
index 5e2aef457c9..25273912640 100644
--- a/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanHelper.java
+++ b/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanHelper.java
@@ -104,18 +104,26 @@ public final class BeanHelper {
      * <p/>
      * This implementation will check if the given parameter type matches the expected type as class using either
      * <ul>
-     * <li>FQN class name - com.foo.MyOrder</li>
-     * <li>Simple class name - MyOrder</li>
+     * <li>FQN class name - com.foo.MyOrder.class</li>
+     * <li>Simple class name - MyOrder.class</li>
      * </ul>
      * If the given parameter type is <b>not</b> a class, then <tt>null</tt> is returned
      *
      * @param  resolver      the class resolver
-     * @param  parameterType the parameter type as a String, can be a FQN or a simple name of the class
+     * @param  parameterType the parameter type as a String, can be a FQN or a simple name of the class (must end with
+     *                       .class)
      * @param  expectedType  the expected type
      * @return               <tt>null</tt> if parameter type is <b>not</b> a class, <tt>true</tt> if parameter type is
      *                       assignable, <tt>false</tt> if not assignable
      */
     public static Boolean isAssignableToExpectedType(ClassResolver resolver, String parameterType, Class<?> expectedType) {
+        if (parameterType == null || !parameterType.endsWith(".class")) {
+            // not a class so return null
+            return null;
+        }
+
+        parameterType = parameterType.substring(0, parameterType.length() - 6); // clip .class
+
         // if its a class, then it should be assignable
         Class<?> parameterClass = resolver.resolveClass(parameterType);
         if (parameterClass == null && parameterType.equals(expectedType.getSimpleName())) {
diff --git a/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanOverloadedCovariantMethodTest.java b/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanOverloadedCovariantMethodTest.java
index 92f98371f89..5639180ebd6 100644
--- a/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanOverloadedCovariantMethodTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanOverloadedCovariantMethodTest.java
@@ -85,7 +85,7 @@ public class BeanOverloadedCovariantMethodTest extends ContextTestSupport {
         context.addRoutes(new RouteBuilder() {
             @Override
             public void configure() throws Exception {
-                from("direct:start").bean(MySuperBean.class, "hello(String)").to("mock:result");
+                from("direct:start").bean(MySuperBean.class, "hello(String.class)").to("mock:result");
 
             }
         });
diff --git a/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanOverloadedMethodFQNTest.java b/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanOverloadedMethodFQNTest.java
index 386ad85f1f0..32b85643c47 100644
--- a/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanOverloadedMethodFQNTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanOverloadedMethodFQNTest.java
@@ -40,7 +40,7 @@ public class BeanOverloadedMethodFQNTest extends ContextTestSupport {
         context.addRoutes(new RouteBuilder() {
             @Override
             public void configure() throws Exception {
-                from("direct:start").bean(MyBean.class, "order(MyOrder)").to("mock:result");
+                from("direct:start").bean(MyBean.class, "order(MyOrder.class)").to("mock:result");
 
             }
         });
@@ -58,7 +58,7 @@ public class BeanOverloadedMethodFQNTest extends ContextTestSupport {
         context.addRoutes(new RouteBuilder() {
             @Override
             public void configure() throws Exception {
-                from("direct:start").bean(MyBean.class, "order(Unknown)").to("mock:result");
+                from("direct:start").bean(MyBean.class, "order(Unknown.class)").to("mock:result");
 
             }
         });
@@ -70,7 +70,7 @@ public class BeanOverloadedMethodFQNTest extends ContextTestSupport {
         } catch (CamelExecutionException e) {
             NoTypeConversionAvailableException cause
                     = assertIsInstanceOf(NoTypeConversionAvailableException.class, e.getCause().getCause());
-            assertEquals("Unknown", cause.getValue());
+            assertEquals("Unknown.class", cause.getValue());
         }
     }
 
@@ -79,7 +79,7 @@ public class BeanOverloadedMethodFQNTest extends ContextTestSupport {
         context.addRoutes(new RouteBuilder() {
             @Override
             public void configure() throws Exception {
-                from("direct:start").bean(MyBean.class, "order(MyOrder,Boolean)").to("mock:result");
+                from("direct:start").bean(MyBean.class, "order(MyOrder.class,Boolean.class)").to("mock:result");
 
             }
         });
@@ -98,7 +98,7 @@ public class BeanOverloadedMethodFQNTest extends ContextTestSupport {
             @Override
             public void configure() throws Exception {
                 from("direct:start")
-                        .bean(MyBean.class, "order(org.apache.camel.component.bean.BeanOverloadedMethodFQNTest$MyOrder)")
+                        .bean(MyBean.class, "order(org.apache.camel.component.bean.BeanOverloadedMethodFQNTest$MyOrder.class)")
                         .to("mock:result");
 
             }
@@ -118,7 +118,7 @@ public class BeanOverloadedMethodFQNTest extends ContextTestSupport {
             @Override
             public void configure() throws Exception {
                 from("direct:start")
-                        .bean(MyBean.class, "order(org.apache.camel.component.bean.BeanOverloadedMethodFQNTest$Unknown)")
+                        .bean(MyBean.class, "order(org.apache.camel.component.bean.BeanOverloadedMethodFQNTest$Unknown.class)")
                         .to("mock:result");
 
             }
@@ -131,7 +131,7 @@ public class BeanOverloadedMethodFQNTest extends ContextTestSupport {
         } catch (CamelExecutionException e) {
             NoTypeConversionAvailableException cause
                     = assertIsInstanceOf(NoTypeConversionAvailableException.class, e.getCause().getCause());
-            assertEquals("org.apache.camel.component.bean.BeanOverloadedMethodFQNTest$Unknown", cause.getValue());
+            assertEquals("org.apache.camel.component.bean.BeanOverloadedMethodFQNTest$Unknown.class", cause.getValue());
         }
     }
 
@@ -142,7 +142,7 @@ public class BeanOverloadedMethodFQNTest extends ContextTestSupport {
             public void configure() throws Exception {
                 from("direct:start")
                         .bean(MyBean.class,
-                                "order(org.apache.camel.component.bean.BeanOverloadedMethodFQNTest$MyOrder,Boolean)")
+                                "order(org.apache.camel.component.bean.BeanOverloadedMethodFQNTest$MyOrder.class,Boolean.class)")
                         .to("mock:result");
 
             }
diff --git a/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanOverloadedMethodParameterValueTest.java b/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanOverloadedMethodParameterValueTest.java
index 9f52978b032..e98ff56e214 100644
--- a/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanOverloadedMethodParameterValueTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanOverloadedMethodParameterValueTest.java
@@ -144,7 +144,7 @@ public class BeanOverloadedMethodParameterValueTest extends ContextTestSupport {
         context.addRoutes(new RouteBuilder() {
             @Override
             public void configure() throws Exception {
-                from("direct:start").bean(MyBean.class, "times(byte[], ${header.times})").to("mock:result");
+                from("direct:start").bean(MyBean.class, "times(byte[].class, ${header.times})").to("mock:result");
 
             }
         });
@@ -162,7 +162,7 @@ public class BeanOverloadedMethodParameterValueTest extends ContextTestSupport {
         context.addRoutes(new RouteBuilder() {
             @Override
             public void configure() throws Exception {
-                from("direct:start").bean(MyBean.class, "times(byte[],${header.times})").to("mock:result");
+                from("direct:start").bean(MyBean.class, "times(byte[].class,${header.times})").to("mock:result");
 
             }
         });
diff --git a/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanOverloadedMethodTest.java b/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanOverloadedMethodTest.java
index d64ed6d1eec..1655eb68e9d 100644
--- a/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanOverloadedMethodTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanOverloadedMethodTest.java
@@ -40,7 +40,7 @@ public class BeanOverloadedMethodTest extends ContextTestSupport {
         context.addRoutes(new RouteBuilder() {
             @Override
             public void configure() throws Exception {
-                from("direct:start").bean(MyBean.class, "hello(String)").to("mock:result");
+                from("direct:start").bean(MyBean.class, "hello(String.class)").to("mock:result");
 
             }
         });
@@ -77,7 +77,7 @@ public class BeanOverloadedMethodTest extends ContextTestSupport {
             @Override
             public void configure() throws Exception {
                 // START SNIPPET: e2
-                from("direct:start").bean(MyBean.class, "hello(String,String)").to("mock:result");
+                from("direct:start").bean(MyBean.class, "hello(String.class, String.class)").to("mock:result");
                 // END SNIPPET: e2
             }
         });
@@ -95,7 +95,7 @@ public class BeanOverloadedMethodTest extends ContextTestSupport {
         context.addRoutes(new RouteBuilder() {
             @Override
             public void configure() throws Exception {
-                from("direct:start").bean(MyBean.class, "hello(*,String)").to("mock:result");
+                from("direct:start").bean(MyBean.class, "hello(*, String.class)").to("mock:result");
 
             }
         });
@@ -150,7 +150,7 @@ public class BeanOverloadedMethodTest extends ContextTestSupport {
         context.addRoutes(new RouteBuilder() {
             @Override
             public void configure() throws Exception {
-                from("direct:start").bean(MyBean.class, "hello(String,String,String)").to("mock:result");
+                from("direct:start").bean(MyBean.class, "hello(String.class,String.class,String.class)").to("mock:result");
 
             }
         });
@@ -170,7 +170,7 @@ public class BeanOverloadedMethodTest extends ContextTestSupport {
         context.addRoutes(new RouteBuilder() {
             @Override
             public void configure() throws Exception {
-                from("direct:start").bean(MyBean.class, "hello(String,int)").to("mock:result");
+                from("direct:start").bean(MyBean.class, "hello(String.class,int.class)").to("mock:result");
 
             }
         });
@@ -190,7 +190,7 @@ public class BeanOverloadedMethodTest extends ContextTestSupport {
         context.addRoutes(new RouteBuilder() {
             @Override
             public void configure() throws Exception {
-                from("direct:start").bean(MyBean.class, "hello(int,String)").to("mock:result");
+                from("direct:start").bean(MyBean.class, "hello(int.class,String.class)").to("mock:result");
 
             }
         });
@@ -210,7 +210,7 @@ public class BeanOverloadedMethodTest extends ContextTestSupport {
         context.addRoutes(new RouteBuilder() {
             @Override
             public void configure() throws Exception {
-                from("direct:start").bean(MyBean.class, "times(String,int)").to("mock:result");
+                from("direct:start").bean(MyBean.class, "times(String.class,int.class)").to("mock:result");
 
             }
         });
@@ -228,7 +228,7 @@ public class BeanOverloadedMethodTest extends ContextTestSupport {
         context.addRoutes(new RouteBuilder() {
             @Override
             public void configure() throws Exception {
-                from("direct:start").bean(MyBean.class, "times(byte[],int)").to("mock:result");
+                from("direct:start").bean(MyBean.class, "times(byte[].class,int.class)").to("mock:result");
 
             }
         });