You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by GitBox <gi...@apache.org> on 2020/08/12 08:22:22 UTC

[GitHub] [camel] lburgazzoli opened a new pull request #4086: Inprove PropertyBindingSupport

lburgazzoli opened a new pull request #4086:
URL: https://github.com/apache/camel/pull/4086


   This PR adds support for arrays in PropertyBindingSupport and fix issues when binding to list when properties have gaps.
   
   See tests in:
   - PropertyBindingSupport
   - PropertyBindingSupport
   
   
   [ ] Make sure there is a [JIRA issue](https://issues.apache.org/jira/browse/CAMEL) filed for the change (usually before you start working on it).  Trivial changes like typos do not require a JIRA issue.  Your pull request should address just this issue, without pulling in other changes.
   [ ] Each commit in the pull request should have a meaningful subject line and body.
   [ ] If you're unsure, you can format the pull request title like `[CAMEL-XXX] Fixes bug in camel-file component`, where you replace `CAMEL-XXX` with the appropriate JIRA issue.
   [ ] Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
   [ ] Run `mvn clean install -Psourcecheck` in your module with source check enabled to make sure basic checks pass and there are no checkstyle violations. A more thorough check will be performed on your pull request automatically.
   Below are the contribution guidelines:
   https://github.com/apache/camel/blob/master/CONTRIBUTING.md


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [camel] davsclaus commented on pull request #4086: Inprove PropertyBindingSupport

Posted by GitBox <gi...@apache.org>.
davsclaus commented on pull request #4086:
URL: https://github.com/apache/camel/pull/4086#issuecomment-672738149


   Also would be good to add unit test where you use the "#class:xxx" - as there is only for "#bean:xxx"


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [camel] davsclaus commented on pull request #4086: Inprove PropertyBindingSupport

Posted by GitBox <gi...@apache.org>.
davsclaus commented on pull request #4086:
URL: https://github.com/apache/camel/pull/4086#issuecomment-672792337


   Yeah it would be good to move that out of the main doc and link to it. Lets create a JIRA about this


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [camel] lburgazzoli merged pull request #4086: Inprove PropertyBindingSupport

Posted by GitBox <gi...@apache.org>.
lburgazzoli merged pull request #4086:
URL: https://github.com/apache/camel/pull/4086


   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [camel] davsclaus commented on a change in pull request #4086: Inprove PropertyBindingSupport

Posted by GitBox <gi...@apache.org>.
davsclaus commented on a change in pull request #4086:
URL: https://github.com/apache/camel/pull/4086#discussion_r469099143



##########
File path: core/camel-support/src/main/java/org/apache/camel/support/PropertyBindingSupport.java
##########
@@ -778,17 +781,36 @@ private static Object getOrElseProperty(CamelContext context, Object target, Str
 
         // use configurer if possible
         Object answer = null;
-        GeneratedPropertyConfigurer configurer = context.adapt(ExtendedCamelContext.class).getConfigurerResolver().resolvePropertyConfigurer(target.getClass().getSimpleName(), context);
+        Class<?> type = null;
+
+        GeneratedPropertyConfigurer configurer = PropertyConfigurerHelper.resolvePropertyConfigurer(context, target);
         if (configurer instanceof PropertyConfigurerGetter) {
-            answer = ((PropertyConfigurerGetter) configurer).getOptionValue(target, key, ignoreCase);
+            type = (Class<?>)((PropertyConfigurerGetter)configurer).getAllOptions(target).get(key);

Review comment:
       Maybe this can be done later as you override type in case answer != null and other bits. So wonder if this can be moved down, and to get the type only if you have not computed it

##########
File path: core/camel-support/src/main/java/org/apache/camel/support/PropertyBindingSupport.java
##########
@@ -804,6 +826,38 @@ private static Object getOrElseProperty(CamelContext context, Object target, Str
                     answer = list.get(list.size() - 1);
                 }
             }
+        } else if (type != null && type.isArray() && lookupKey != null) {
+            int idx = Integer.parseInt(lookupKey);
+            int size = answer != null ? Array.getLength(answer) : 0;
+            if (idx >= size) {
+                answer = answer != null ? Arrays.copyOf((Object[]) answer, idx + 1) : Array.newInstance(Object.class, idx + 1);
+            }
+
+            Object result = Array.get(answer, idx);
+            if (result == null) {
+                result = context.getInjector().newInstance(type.getComponentType());
+                Array.set(answer, idx, result);
+            }
+
+            if (idx >= size) {
+                // replace array
+                if (configurer != null) {
+                    configurer.configure(context, target, key, answer, true);
+                } else {
+                    // fallback to reflection
+                    boolean hit;
+                    try {
+                        hit = IntrospectionSupport.setProperty(context.getTypeConverter(), target, key, answer);
+                    } catch (Exception e) {
+                        throw new IllegalArgumentException("Cannot set property: " + key + " as a Map because target bean has no setter method for the Map");
+                    }
+                    if (!hit) {
+                        throw new IllegalArgumentException("Cannot set property: " + key + " as a Map because target bean has no setter method for the Map");

Review comment:
       Looks a bit copy/paste mistake, as this is Array and not Map

##########
File path: core/camel-support/src/main/java/org/apache/camel/support/PropertyBindingSupport.java
##########
@@ -804,6 +826,38 @@ private static Object getOrElseProperty(CamelContext context, Object target, Str
                     answer = list.get(list.size() - 1);
                 }
             }
+        } else if (type != null && type.isArray() && lookupKey != null) {
+            int idx = Integer.parseInt(lookupKey);
+            int size = answer != null ? Array.getLength(answer) : 0;
+            if (idx >= size) {
+                answer = answer != null ? Arrays.copyOf((Object[]) answer, idx + 1) : Array.newInstance(Object.class, idx + 1);
+            }
+
+            Object result = Array.get(answer, idx);
+            if (result == null) {
+                result = context.getInjector().newInstance(type.getComponentType());
+                Array.set(answer, idx, result);
+            }
+
+            if (idx >= size) {
+                // replace array
+                if (configurer != null) {
+                    configurer.configure(context, target, key, answer, true);
+                } else {
+                    // fallback to reflection
+                    boolean hit;
+                    try {
+                        hit = IntrospectionSupport.setProperty(context.getTypeConverter(), target, key, answer);
+                    } catch (Exception e) {
+                        throw new IllegalArgumentException("Cannot set property: " + key + " as a Map because target bean has no setter method for the Map");

Review comment:
       Looks a bit copy/paste mistake, as this is Array and not Map

##########
File path: core/camel-support/src/main/java/org/apache/camel/support/IntrospectionSupport.java
##########
@@ -646,11 +649,43 @@ public static boolean setProperty(CamelContext context, TypeConverter typeConver
                     value = CamelContextHelper.lookup(context, s);
                 }
                 if (isNotEmpty(lookupKey)) {
-                    int idx = Integer.valueOf(lookupKey);
-                    list.add(idx, value);
+                    int idx = Integer.parseInt(lookupKey);
+                    if (idx < list.size()) {
+                        list.set(idx, value);
+                    } else if (idx == list.size()) {
+                        list.add(value);
+                    } else {
+                        if (list instanceof ArrayList) {
+                            ((ArrayList) list).ensureCapacity(idx + 1);
+                        }
+                        while (list.size() < idx) {
+                            list.add(null);
+                        }
+                        list.add(idx, value);
+                    }
                 } else {
                     list.add(value);
                 }
+                return true;
+            } else if (obj.getClass().isArray() && lookupKey != null) {
+                if (context != null && refName != null && value == null) {
+                    String s = StringHelper.replaceAll(refName, "#", "");
+                    value = CamelContextHelper.lookup(context, s);
+                }
+                int idx = Integer.parseInt(lookupKey);
+                int size = Array.getLength(obj);
+                if (idx >= size) {
+                    obj = Arrays.copyOf((Object[])obj, idx + 1);
+
+                    // replace array
+                    boolean hit = IntrospectionSupport.setProperty(context, target, key, obj);
+                    if (!hit) {
+                        throw new IllegalArgumentException("Cannot set property: " + name + " as a Map because target bean has no setter method for the Map");

Review comment:
       Wrong exception message Map -> Array

##########
File path: core/camel-support/src/main/java/org/apache/camel/support/IntrospectionSupport.java
##########
@@ -646,11 +649,43 @@ public static boolean setProperty(CamelContext context, TypeConverter typeConver
                     value = CamelContextHelper.lookup(context, s);
                 }
                 if (isNotEmpty(lookupKey)) {
-                    int idx = Integer.valueOf(lookupKey);
-                    list.add(idx, value);
+                    int idx = Integer.parseInt(lookupKey);
+                    if (idx < list.size()) {
+                        list.set(idx, value);
+                    } else if (idx == list.size()) {
+                        list.add(value);
+                    } else {
+                        if (list instanceof ArrayList) {

Review comment:
       Maybe add a code comment why you do this, eg to expand the array when there is gaps in the index




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [camel] lburgazzoli commented on pull request #4086: Inprove PropertyBindingSupport

Posted by GitBox <gi...@apache.org>.
lburgazzoli commented on pull request #4086:
URL: https://github.com/apache/camel/pull/4086#issuecomment-672789920


   @davsclaus should be ready for review. 
   
   wonder if we need a specific doc about properties binding


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [camel] davsclaus commented on pull request #4086: Inprove PropertyBindingSupport

Posted by GitBox <gi...@apache.org>.
davsclaus commented on pull request #4086:
URL: https://github.com/apache/camel/pull/4086#issuecomment-672794174


   Created the JIRA: https://issues.apache.org/jira/browse/CAMEL-15398


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org