|
15 | 15 | */ |
16 | 16 | package com.ocpsoft.rewrite.bind; |
17 | 17 |
|
| 18 | +import java.lang.reflect.Array; |
18 | 19 | import java.util.Collection; |
19 | 20 |
|
20 | 21 | import com.ocpsoft.common.services.ServiceLoader; |
@@ -64,13 +65,44 @@ public BindingBuilder validatedBy(final Validator<?> validator) |
64 | 65 | @SuppressWarnings({ "unchecked", "rawtypes" }) |
65 | 66 | public boolean validates(final Rewrite event, final EvaluationContext context, final Object value) |
66 | 67 | { |
67 | | - // We must assume the value was properly converted. |
| 68 | + if (value != null && value.getClass().isArray()) |
| 69 | + { |
| 70 | + Object[] values = (Object[]) value; |
| 71 | + for (int i = 0; i < values.length; i++) { |
| 72 | + if (!((Validator) validator).validate(event, context, values[i])) |
| 73 | + { |
| 74 | + return false; |
| 75 | + } |
| 76 | + } |
| 77 | + return true; |
| 78 | + } |
68 | 79 | return ((Validator) validator).validate(event, context, value); |
69 | 80 | } |
70 | 81 |
|
71 | 82 | @Override |
72 | 83 | public Object convert(final Rewrite event, final EvaluationContext context, final Object value) |
73 | 84 | { |
| 85 | + if (value != null && value.getClass().isArray()) |
| 86 | + { |
| 87 | + Object[] values = (Object[]) value; |
| 88 | + Object[] convertedValues = new Object[values.length]; |
| 89 | + for (int i = 0; i < convertedValues.length; i++) { |
| 90 | + convertedValues[i] = converter.convert(event, context, values[i]); |
| 91 | + } |
| 92 | + |
| 93 | + Class<?> type = Object.class; |
| 94 | + for (Object object : convertedValues) { |
| 95 | + if (object != null) |
| 96 | + { |
| 97 | + type = object.getClass(); |
| 98 | + break; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + Object[] result = (Object[]) Array.newInstance(type, convertedValues.length); |
| 103 | + System.arraycopy(convertedValues, 0, result, 0, result.length); |
| 104 | + return result; |
| 105 | + } |
74 | 106 | return converter.convert(event, context, value); |
75 | 107 | } |
76 | 108 |
|
|
0 commit comments