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:
us...@infra.apache.org


Reply via email to