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 2019/05/23 19:30:57 UTC

[camel] 11/18: CAMEL-13557: Add property binding support to make it convenient to configure components and whatnot.

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

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

commit 8213ae4add7a91111b10c06b4d45c3c0b894b170
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Thu May 23 10:25:07 2019 +0200

    CAMEL-13557: Add property binding support to make it convenient to configure components and whatnot.
---
 .../camel/support/PropertyBindingSupportTest.java  | 33 +++++++++++++---------
 .../camel/support/PropertyBindingSupport.java      | 28 ++++++++++++------
 2 files changed, 40 insertions(+), 21 deletions(-)

diff --git a/core/camel-core/src/test/java/org/apache/camel/support/PropertyBindingSupportTest.java b/core/camel-core/src/test/java/org/apache/camel/support/PropertyBindingSupportTest.java
index 35bfadf..c372e39 100644
--- a/core/camel-core/src/test/java/org/apache/camel/support/PropertyBindingSupportTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/support/PropertyBindingSupportTest.java
@@ -228,32 +228,39 @@ public class PropertyBindingSupportTest extends ContextTestSupport {
             return age;
         }
 
-        public void setAge(int age) {
-            this.age = age;
-        }
-
         public boolean isRider() {
             return rider;
         }
 
-        public void setRider(boolean rider) {
-            this.rider = rider;
-        }
-
         public Company getWork() {
             return work;
         }
 
-        public void setWork(Company work) {
-            this.work = work;
-        }
-
         public boolean isGoldCustomer() {
             return goldCustomer;
         }
 
-        public void setGoldCustomer(boolean goldCustomer) {
+        // this has no setter but only builders
+        // and mix the builders with both styles (with as prefix and no prefix at all)
+
+        public Bar withAge(int age) {
+            this.age = age;
+            return this;
+        }
+
+        public Bar withRider(boolean rider) {
+            this.rider = rider;
+            return this;
+        }
+
+        public Bar work(Company work) {
+            this.work = work;
+            return this;
+        }
+
+        public Bar goldCustomer(boolean goldCustomer) {
             this.goldCustomer = goldCustomer;
+            return this;
         }
     }
 
diff --git a/core/camel-support/src/main/java/org/apache/camel/support/PropertyBindingSupport.java b/core/camel-support/src/main/java/org/apache/camel/support/PropertyBindingSupport.java
index 453e5b0..d4a6980 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/PropertyBindingSupport.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/PropertyBindingSupport.java
@@ -140,10 +140,8 @@ public final class PropertyBindingSupport {
                 Object prop = getOrElseProperty(newTarget, part, null);
                 if (prop == null) {
                     // okay is there a setter so we can create a new instance and set it automatic
-                    Set<Method> newSetters = findSetterMethods(newClass, part, true);
-                    // TODO: you may have setter + fluent builder at the same time, so grab setter first, and fallback to fluent builder afterwards
-                    if (newSetters.size() == 1) {
-                        Method method = newSetters.iterator().next();
+                    Method method = findBestSetterMethod(newClass, part);
+                    if (method != null) {
                         Class<?> parameterType = method.getParameterTypes()[0];
                         if (parameterType != null && org.apache.camel.util.ObjectHelper.hasDefaultPublicNoArgConstructor(parameterType)) {
                             Object instance = context.getInjector().newInstance(parameterType);
@@ -184,10 +182,8 @@ public final class PropertyBindingSupport {
                 }
             } else if (value.toString().equals("#autowire")) {
                 // we should get the type from the setter
-                // TODO: you may have setter + fluent builder at the same time, so grab setter first, and fallback to fluent builder afterwards
-                Set<Method> newSetters = findSetterMethods(target.getClass(), name, true);
-                if (newSetters.size() == 1) {
-                    Method method = newSetters.iterator().next();
+                Method method = findBestSetterMethod(target.getClass(), name);
+                if (method != null) {
                     Class<?> parameterType = method.getParameterTypes()[0];
                     if (parameterType != null) {
                         Set<?> types = context.getRegistry().findByType(parameterType);
@@ -210,4 +206,20 @@ public final class PropertyBindingSupport {
         return IntrospectionSupport.setProperty(context, context.getTypeConverter(), target, name, value, refName, true);
     }
 
+    private static Method findBestSetterMethod(Class clazz, String name) {
+        // is there a direct setter?
+        Set<Method> candidates = findSetterMethods(clazz, name, false);
+        if (candidates.size() == 1) {
+            return candidates.iterator().next();
+        }
+
+        // okay now try with builder pattern
+        candidates = findSetterMethods(clazz, name, true);
+        if (candidates.size() == 1) {
+            return candidates.iterator().next();
+        }
+
+        return null;
+    }
+
 }