1 | |
package org.jbehave.core.steps; |
2 | |
|
3 | |
import java.lang.reflect.Method; |
4 | |
import java.lang.reflect.ParameterizedType; |
5 | |
import java.lang.reflect.Type; |
6 | |
import java.math.BigDecimal; |
7 | |
import java.math.BigInteger; |
8 | |
import java.text.DateFormat; |
9 | |
import java.text.DecimalFormat; |
10 | |
import java.text.DecimalFormatSymbols; |
11 | |
import java.text.NumberFormat; |
12 | |
import java.text.ParseException; |
13 | |
import java.text.SimpleDateFormat; |
14 | |
import java.util.ArrayList; |
15 | |
import java.util.Date; |
16 | |
import java.util.List; |
17 | |
import java.util.Locale; |
18 | |
import java.util.concurrent.CopyOnWriteArrayList; |
19 | |
import java.util.concurrent.atomic.AtomicInteger; |
20 | |
import java.util.concurrent.atomic.AtomicLong; |
21 | |
|
22 | |
import org.apache.commons.lang.BooleanUtils; |
23 | |
import org.jbehave.core.annotations.AsParameters; |
24 | |
import org.jbehave.core.configuration.MostUsefulConfiguration; |
25 | |
import org.jbehave.core.model.ExamplesTable; |
26 | |
import org.jbehave.core.model.ExamplesTableFactory; |
27 | |
|
28 | |
import static java.util.Arrays.asList; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
public class ParameterConverters { |
53 | |
|
54 | 1 | public static final StepMonitor DEFAULT_STEP_MONITOR = new SilentStepMonitor(); |
55 | 1 | public static final Locale DEFAULT_NUMBER_FORMAT_LOCAL = Locale.ENGLISH; |
56 | |
public static final String DEFAULT_LIST_SEPARATOR = ","; |
57 | |
public static final boolean DEFAULT_THREAD_SAFETY = true; |
58 | |
|
59 | |
private static final String NEWLINES_PATTERN = "(\n)|(\r\n)"; |
60 | 1 | private static final String SYSTEM_NEWLINE = System.getProperty("line.separator"); |
61 | |
private static final String DEFAULT_TRUE_VALUE = "true"; |
62 | |
private static final String DEFAULT_FALSE_VALUE = "false"; |
63 | |
|
64 | |
private final StepMonitor monitor; |
65 | |
private final List<ParameterConverter> converters; |
66 | |
private final boolean threadSafe; |
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
public ParameterConverters() { |
74 | 981 | this(DEFAULT_STEP_MONITOR); |
75 | 981 | } |
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
public ParameterConverters(StepMonitor monitor) { |
83 | 981 | this(monitor, DEFAULT_NUMBER_FORMAT_LOCAL, DEFAULT_LIST_SEPARATOR, DEFAULT_THREAD_SAFETY); |
84 | 981 | } |
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
public ParameterConverters(boolean threadSafe) { |
93 | 1 | this(DEFAULT_STEP_MONITOR, DEFAULT_NUMBER_FORMAT_LOCAL, DEFAULT_LIST_SEPARATOR, threadSafe); |
94 | 1 | } |
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
public ParameterConverters(StepMonitor monitor, Locale locale, String listSeparator, boolean threadSafe) { |
109 | 982 | this(monitor, new ArrayList<ParameterConverter>(), threadSafe); |
110 | 982 | this.addConverters(defaultConverters(locale, listSeparator)); |
111 | 982 | } |
112 | |
|
113 | 1015 | private ParameterConverters(StepMonitor monitor, List<ParameterConverter> converters, boolean threadSafe) { |
114 | 1015 | this.monitor = monitor; |
115 | 1015 | this.threadSafe = threadSafe; |
116 | 1015 | this.converters = (threadSafe ? new CopyOnWriteArrayList<ParameterConverter>(converters) |
117 | |
: new ArrayList<ParameterConverter>(converters)); |
118 | 1015 | } |
119 | |
|
120 | |
protected ParameterConverter[] defaultConverters(Locale locale, String listSeparator) { |
121 | 983 | String escapedListSeparator = escapeRegexPunctuation(listSeparator); |
122 | 983 | ExamplesTableFactory tableFactory = new ExamplesTableFactory(this); |
123 | 983 | ParameterConverter[] defaultConverters = { new BooleanConverter(), |
124 | |
new NumberConverter(NumberFormat.getInstance(locale)), |
125 | |
new NumberListConverter(NumberFormat.getInstance(locale), escapedListSeparator), |
126 | |
new StringListConverter(escapedListSeparator), new DateConverter(), |
127 | |
new EnumConverter(), new EnumListConverter(), |
128 | |
new ExamplesTableConverter(tableFactory), new ExamplesTableParametersConverter(tableFactory) }; |
129 | 983 | return defaultConverters; |
130 | |
} |
131 | |
|
132 | |
|
133 | |
private String escapeRegexPunctuation(String matchThis) { |
134 | 983 | return matchThis.replaceAll("([\\[\\]\\{\\}\\?\\^\\.\\*\\(\\)\\+\\\\])", "\\\\$1"); |
135 | |
} |
136 | |
|
137 | |
public ParameterConverters addConverters(ParameterConverter... converters) { |
138 | 984 | return addConverters(asList(converters)); |
139 | |
} |
140 | |
|
141 | |
public ParameterConverters addConverters(List<ParameterConverter> converters) { |
142 | 1020 | this.converters.addAll(0, converters); |
143 | 1020 | return this; |
144 | |
} |
145 | |
|
146 | |
public Object convert(String value, Type type) { |
147 | |
|
148 | |
|
149 | 121 | for (ParameterConverter converter : converters) { |
150 | 828 | if (converter.accept(type)) { |
151 | 46 | Object converted = converter.convertValue(value, type); |
152 | 46 | monitor.convertedValueOfType(value, type, converted, converter.getClass()); |
153 | 46 | return converted; |
154 | |
} |
155 | 782 | } |
156 | |
|
157 | 75 | if (type == String.class) { |
158 | 73 | return replaceNewlinesWithSystemNewlines(value); |
159 | |
} |
160 | |
|
161 | 2 | throw new ParameterConvertionFailed("No parameter converter for " + type); |
162 | |
} |
163 | |
|
164 | |
private Object replaceNewlinesWithSystemNewlines(String value) { |
165 | 73 | return value.replaceAll(NEWLINES_PATTERN, SYSTEM_NEWLINE); |
166 | |
} |
167 | |
|
168 | |
public ParameterConverters newInstanceAdding(ParameterConverter converter) { |
169 | 33 | List<ParameterConverter> convertersForNewInstance = new ArrayList<ParameterConverter>(converters); |
170 | 33 | convertersForNewInstance.add(converter); |
171 | 33 | return new ParameterConverters(monitor, convertersForNewInstance, threadSafe); |
172 | |
} |
173 | |
|
174 | |
public static interface ParameterConverter { |
175 | |
|
176 | |
boolean accept(Type type); |
177 | |
|
178 | |
Object convertValue(String value, Type type); |
179 | |
|
180 | |
} |
181 | |
|
182 | |
@SuppressWarnings("serial") |
183 | |
public static class ParameterConvertionFailed extends RuntimeException { |
184 | |
|
185 | |
public ParameterConvertionFailed(String message) { |
186 | 2 | super(message); |
187 | 2 | } |
188 | |
|
189 | |
public ParameterConvertionFailed(String message, Throwable cause) { |
190 | 5 | super(message, cause); |
191 | 5 | } |
192 | |
} |
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
|
217 | |
public static class NumberConverter implements ParameterConverter { |
218 | 1 | private static List<Class<?>> primitiveTypes = asList(new Class<?>[] { byte.class, short.class, int.class, |
219 | |
float.class, long.class, double.class }); |
220 | |
|
221 | |
private final NumberFormat numberFormat; |
222 | 1978 | private ThreadLocal<NumberFormat> threadLocalNumberFormat = new ThreadLocal<NumberFormat>(); |
223 | |
|
224 | |
public NumberConverter() { |
225 | 4 | this(NumberFormat.getInstance(DEFAULT_NUMBER_FORMAT_LOCAL)); |
226 | 4 | } |
227 | |
|
228 | 1978 | public NumberConverter(NumberFormat numberFormat) { |
229 | 1978 | synchronized (this) { |
230 | 1978 | this.numberFormat = numberFormat; |
231 | 1978 | this.threadLocalNumberFormat.set((NumberFormat) this.numberFormat.clone()); |
232 | 1978 | } |
233 | 1978 | } |
234 | |
|
235 | |
public boolean accept(Type type) { |
236 | 236 | if (type instanceof Class<?>) { |
237 | 231 | return Number.class.isAssignableFrom((Class<?>) type) || primitiveTypes.contains(type); |
238 | |
} |
239 | 5 | return false; |
240 | |
} |
241 | |
|
242 | |
public Object convertValue(String value, Type type) { |
243 | |
try { |
244 | 235 | Number n = numberFormat().parse(value); |
245 | 226 | if (type == Byte.class || type == byte.class) { |
246 | 14 | return n.byteValue(); |
247 | 215 | } else if (type == Short.class || type == short.class) { |
248 | 14 | return n.shortValue(); |
249 | 202 | } else if (type == Integer.class || type == int.class) { |
250 | 43 | return n.intValue(); |
251 | 157 | } else if (type == Float.class || type == float.class) { |
252 | 32 | return n.floatValue(); |
253 | 129 | } else if (type == Long.class || type == long.class) { |
254 | 26 | return n.longValue(); |
255 | 103 | } else if (type == Double.class || type == double.class) { |
256 | 36 | return n.doubleValue(); |
257 | 67 | } else if (type == BigInteger.class) { |
258 | 7 | return BigInteger.valueOf(n.longValue()); |
259 | 59 | } else if (type == BigDecimal.class) { |
260 | 30 | return new BigDecimal(canonicalize(value)); |
261 | 31 | } else if (type == AtomicInteger.class) { |
262 | 8 | return new AtomicInteger(Integer.parseInt(value)); |
263 | 23 | } else if (type == AtomicLong.class) { |
264 | 7 | return new AtomicLong(Long.parseLong(value)); |
265 | |
} else { |
266 | 16 | return n; |
267 | |
} |
268 | 0 | } catch (NumberFormatException e) { |
269 | 0 | throw new ParameterConvertionFailed(value, e); |
270 | 2 | } catch (ParseException e) { |
271 | 2 | throw new ParameterConvertionFailed(value, e); |
272 | |
} |
273 | |
} |
274 | |
|
275 | |
|
276 | |
|
277 | |
|
278 | |
|
279 | |
|
280 | |
private NumberFormat numberFormat() { |
281 | 293 | if (threadLocalNumberFormat.get() == null) { |
282 | 4 | synchronized (this) { |
283 | 4 | threadLocalNumberFormat.set((NumberFormat) numberFormat.clone()); |
284 | 4 | } |
285 | |
} |
286 | 294 | return threadLocalNumberFormat.get(); |
287 | |
} |
288 | |
|
289 | |
|
290 | |
|
291 | |
|
292 | |
|
293 | |
|
294 | |
|
295 | |
|
296 | |
|
297 | |
|
298 | |
private String canonicalize(String value) { |
299 | 29 | char decimalPointSeparator = '.'; |
300 | 29 | char minusSign = '-'; |
301 | 29 | String rxNotDigits = "[^0-9]"; |
302 | 29 | StringBuilder builder = new StringBuilder(value.length()); |
303 | |
|
304 | |
|
305 | 30 | if (numberFormat() instanceof DecimalFormat) { |
306 | 29 | DecimalFormatSymbols decimalFormatSymbols = ((DecimalFormat) numberFormat()).getDecimalFormatSymbols(); |
307 | 29 | minusSign = decimalFormatSymbols.getMinusSign(); |
308 | 29 | decimalPointSeparator = decimalFormatSymbols.getDecimalSeparator(); |
309 | |
} |
310 | |
|
311 | 29 | value = value.trim(); |
312 | 29 | int decimalPointPosition = value.lastIndexOf(decimalPointSeparator); |
313 | 30 | boolean isNegative = value.charAt(0) == minusSign; |
314 | |
|
315 | 29 | if (isNegative) { |
316 | 0 | builder.append('-'); |
317 | |
} |
318 | |
|
319 | 30 | if (decimalPointPosition != -1) { |
320 | 22 | String sf = value.substring(0, decimalPointPosition).replaceAll(rxNotDigits, ""); |
321 | 22 | String dp = value.substring(decimalPointPosition + 1).replaceAll(rxNotDigits, ""); |
322 | |
|
323 | 23 | builder.append(sf); |
324 | 23 | builder.append('.'); |
325 | 23 | builder.append(dp); |
326 | |
|
327 | 23 | } else { |
328 | 7 | builder.append(value.replaceAll(rxNotDigits, "")); |
329 | |
} |
330 | 30 | return builder.toString(); |
331 | |
} |
332 | |
} |
333 | |
|
334 | |
|
335 | |
|
336 | |
|
337 | |
|
338 | |
|
339 | |
|
340 | |
|
341 | |
public static class NumberListConverter implements ParameterConverter { |
342 | |
|
343 | |
private final NumberConverter numberConverter; |
344 | |
private final String valueSeparator; |
345 | |
|
346 | |
public NumberListConverter() { |
347 | 2 | this(NumberFormat.getInstance(DEFAULT_NUMBER_FORMAT_LOCAL), DEFAULT_LIST_SEPARATOR); |
348 | 2 | } |
349 | |
|
350 | |
|
351 | |
|
352 | |
|
353 | |
|
354 | 987 | public NumberListConverter(NumberFormat numberFormat, String valueSeparator) { |
355 | 987 | this.numberConverter = new NumberConverter(numberFormat); |
356 | 987 | this.valueSeparator = valueSeparator; |
357 | 987 | } |
358 | |
|
359 | |
public boolean accept(Type type) { |
360 | 88 | if (type instanceof ParameterizedType) { |
361 | 7 | Type rawType = rawType(type); |
362 | 7 | Type argumentType = argumentType(type); |
363 | 7 | return List.class.isAssignableFrom((Class<?>) rawType) |
364 | |
&& Number.class.isAssignableFrom((Class<?>) argumentType); |
365 | |
} |
366 | 81 | return false; |
367 | |
} |
368 | |
|
369 | |
private Type rawType(Type type) { |
370 | 7 | return ((ParameterizedType) type).getRawType(); |
371 | |
} |
372 | |
|
373 | |
private Type argumentType(Type type) { |
374 | 18 | return ((ParameterizedType) type).getActualTypeArguments()[0]; |
375 | |
} |
376 | |
|
377 | |
@SuppressWarnings("unchecked") |
378 | |
public Object convertValue(String value, Type type) { |
379 | 11 | Class<? extends Number> argumentType = (Class<? extends Number>) argumentType(type); |
380 | 11 | List<String> values = trim(asList(value.split(valueSeparator))); |
381 | 11 | List<Number> numbers = new ArrayList<Number>(); |
382 | 11 | for (String numberValue : values) { |
383 | 40 | numbers.add((Number) numberConverter.convertValue(numberValue, argumentType)); |
384 | 39 | } |
385 | 10 | return numbers; |
386 | |
} |
387 | |
|
388 | |
} |
389 | |
|
390 | |
|
391 | |
|
392 | |
|
393 | |
|
394 | |
|
395 | |
public static class StringListConverter implements ParameterConverter { |
396 | |
|
397 | |
private String valueSeparator; |
398 | |
|
399 | |
public StringListConverter() { |
400 | 1 | this(DEFAULT_LIST_SEPARATOR); |
401 | 1 | } |
402 | |
|
403 | |
|
404 | |
|
405 | |
|
406 | |
|
407 | 984 | public StringListConverter(String valueSeparator) { |
408 | 984 | this.valueSeparator = valueSeparator; |
409 | 984 | } |
410 | |
|
411 | |
public boolean accept(Type type) { |
412 | 85 | if (type instanceof ParameterizedType) { |
413 | 4 | ParameterizedType parameterizedType = (ParameterizedType) type; |
414 | 4 | Type rawType = parameterizedType.getRawType(); |
415 | 4 | Type argumentType = parameterizedType.getActualTypeArguments()[0]; |
416 | 4 | return List.class.isAssignableFrom((Class<?>) rawType) |
417 | |
&& String.class.isAssignableFrom((Class<?>) argumentType); |
418 | |
} |
419 | 81 | return false; |
420 | |
} |
421 | |
|
422 | |
public Object convertValue(String value, Type type) { |
423 | 3 | if (value.trim().length() == 0) |
424 | 1 | return asList(); |
425 | 2 | return trim(asList(value.split(valueSeparator))); |
426 | |
} |
427 | |
|
428 | |
} |
429 | |
|
430 | |
public static List<String> trim(List<String> values) { |
431 | 15 | List<String> trimmed = new ArrayList<String>(); |
432 | 15 | for (String value : values) { |
433 | 51 | trimmed.add(value.trim()); |
434 | 51 | } |
435 | 15 | return trimmed; |
436 | |
} |
437 | |
|
438 | |
|
439 | |
|
440 | |
|
441 | |
|
442 | |
public static class DateConverter implements ParameterConverter { |
443 | |
|
444 | 1 | public static final DateFormat DEFAULT_FORMAT = new SimpleDateFormat("dd/MM/yyyy"); |
445 | |
|
446 | |
private final DateFormat dateFormat; |
447 | |
|
448 | |
public DateConverter() { |
449 | 985 | this(DEFAULT_FORMAT); |
450 | 985 | } |
451 | |
|
452 | 986 | public DateConverter(DateFormat dateFormat) { |
453 | 986 | this.dateFormat = dateFormat; |
454 | 986 | } |
455 | |
|
456 | |
public boolean accept(Type type) { |
457 | 87 | if (type instanceof Class<?>) { |
458 | 85 | return Date.class.isAssignableFrom((Class<?>) type); |
459 | |
} |
460 | 2 | return false; |
461 | |
} |
462 | |
|
463 | |
public Object convertValue(String value, Type type) { |
464 | |
try { |
465 | 3 | return dateFormat.parse(value); |
466 | 1 | } catch (ParseException e) { |
467 | 1 | throw new ParameterConvertionFailed("Failed to convert value " |
468 | |
+ value |
469 | |
+ " with date format " |
470 | |
+ (dateFormat instanceof SimpleDateFormat ? ((SimpleDateFormat) dateFormat).toPattern() |
471 | |
: dateFormat), e); |
472 | |
} |
473 | |
} |
474 | |
|
475 | |
} |
476 | |
|
477 | |
public static class BooleanConverter implements ParameterConverter { |
478 | |
private String trueValue; |
479 | |
private String falseValue; |
480 | |
|
481 | |
public BooleanConverter() { |
482 | 984 | this(DEFAULT_TRUE_VALUE, DEFAULT_FALSE_VALUE); |
483 | 984 | } |
484 | |
|
485 | 986 | public BooleanConverter(String trueValue, String falseValue) { |
486 | 986 | this.trueValue = trueValue; |
487 | 986 | this.falseValue = falseValue; |
488 | 986 | } |
489 | |
|
490 | |
public boolean accept(Type type) { |
491 | 126 | if (type instanceof Class<?>) { |
492 | 119 | return Boolean.class.isAssignableFrom((Class<?>) type) || Boolean.TYPE.isAssignableFrom((Class<?>) type); |
493 | |
} |
494 | 7 | return false; |
495 | |
} |
496 | |
|
497 | |
public Object convertValue(String value, Type type) { |
498 | |
try { |
499 | 17 | return BooleanUtils.toBoolean(value, trueValue, falseValue); |
500 | 2 | } catch (IllegalArgumentException e) { |
501 | 2 | return false; |
502 | |
} |
503 | |
} |
504 | |
} |
505 | |
|
506 | |
public static class BooleanListConverter implements ParameterConverter { |
507 | |
private final BooleanConverter booleanConverter; |
508 | |
private String valueSeparator; |
509 | |
|
510 | |
public BooleanListConverter() { |
511 | 1 | this(DEFAULT_LIST_SEPARATOR, DEFAULT_TRUE_VALUE, DEFAULT_FALSE_VALUE); |
512 | 1 | } |
513 | |
|
514 | |
public BooleanListConverter(String valueSeparator) { |
515 | 0 | this(valueSeparator, DEFAULT_TRUE_VALUE, DEFAULT_FALSE_VALUE); |
516 | 0 | } |
517 | |
|
518 | 1 | public BooleanListConverter(String valueSeparator, String trueValue, String falseValue) { |
519 | 1 | this.valueSeparator = valueSeparator; |
520 | 1 | booleanConverter = new BooleanConverter(trueValue, falseValue); |
521 | 1 | } |
522 | |
|
523 | |
public boolean accept(Type type) { |
524 | 1 | if (type instanceof ParameterizedType) { |
525 | 1 | Type rawType = rawType(type); |
526 | 1 | Type argumentType = argumentType(type); |
527 | 1 | return List.class.isAssignableFrom((Class<?>) rawType) |
528 | |
&& Boolean.class.isAssignableFrom((Class<?>) argumentType); |
529 | |
} |
530 | 0 | return false; |
531 | |
} |
532 | |
|
533 | |
public Object convertValue(String value, Type type) { |
534 | 1 | List<String> values = trim(asList(value.split(valueSeparator))); |
535 | 1 | List<Boolean> booleans = new ArrayList<Boolean>(); |
536 | |
|
537 | 1 | for (String booleanValue : values) { |
538 | 3 | booleans.add((Boolean) booleanConverter.convertValue(booleanValue, type)); |
539 | 3 | } |
540 | 1 | return booleans; |
541 | |
} |
542 | |
|
543 | |
private Type rawType(Type type) { |
544 | 1 | return ((ParameterizedType) type).getRawType(); |
545 | |
} |
546 | |
|
547 | |
private Type argumentType(Type type) { |
548 | 1 | return ((ParameterizedType) type).getActualTypeArguments()[0]; |
549 | |
} |
550 | |
} |
551 | |
|
552 | |
|
553 | |
|
554 | |
|
555 | 1969 | public static class EnumConverter implements ParameterConverter { |
556 | |
|
557 | |
public boolean accept(Type type) { |
558 | 85 | if (type instanceof Class<?>) { |
559 | 84 | return ((Class<?>) type).isEnum(); |
560 | |
} |
561 | 1 | return false; |
562 | |
} |
563 | |
|
564 | |
public Object convertValue(String value, Type type) { |
565 | 5 | String typeClass = ((Class<?>) type).getName(); |
566 | 5 | Class<?> enumClass = (Class<?>) type; |
567 | 5 | Method valueOfMethod = null; |
568 | |
try { |
569 | 5 | valueOfMethod = enumClass.getMethod("valueOf", new Class[] { String.class }); |
570 | 5 | valueOfMethod.setAccessible(true); |
571 | 5 | return valueOfMethod.invoke(enumClass, new Object[] { value }); |
572 | 1 | } catch (Exception e) { |
573 | 1 | throw new ParameterConvertionFailed("Failed to convert " + value + " for Enum " + typeClass, e); |
574 | |
} |
575 | |
} |
576 | |
} |
577 | |
|
578 | |
|
579 | |
|
580 | |
|
581 | |
|
582 | |
public static class EnumListConverter implements ParameterConverter { |
583 | |
private final EnumConverter enumConverter; |
584 | |
private String valueSeparator; |
585 | |
|
586 | |
public EnumListConverter() { |
587 | 984 | this(DEFAULT_LIST_SEPARATOR); |
588 | 984 | } |
589 | |
|
590 | 984 | public EnumListConverter(String valueSeparator) { |
591 | 984 | this.enumConverter = new EnumConverter(); |
592 | 984 | this.valueSeparator = valueSeparator; |
593 | 984 | } |
594 | |
|
595 | |
public boolean accept(Type type) { |
596 | 82 | if (type instanceof ParameterizedType) { |
597 | 1 | Type rawType = rawType(type); |
598 | 1 | Type argumentType = argumentType(type); |
599 | 1 | return List.class.isAssignableFrom((Class<?>) rawType) && enumConverter.accept(argumentType); |
600 | |
} |
601 | 81 | return false; |
602 | |
} |
603 | |
|
604 | |
public Object convertValue(String value, Type type) { |
605 | 1 | Type argumentType = argumentType(type); |
606 | 1 | List<String> values = trim(asList(value.split(valueSeparator))); |
607 | 1 | List<Enum<?>> enums = new ArrayList<Enum<?>>(); |
608 | 1 | for (String string : values) { |
609 | 3 | enums.add((Enum<?>) enumConverter.convertValue(string, argumentType)); |
610 | 3 | } |
611 | 1 | return enums; |
612 | |
} |
613 | |
|
614 | |
private Type rawType(Type type) { |
615 | 1 | return ((ParameterizedType) type).getRawType(); |
616 | |
} |
617 | |
|
618 | |
private Type argumentType(Type type) { |
619 | 2 | return ((ParameterizedType) type).getActualTypeArguments()[0]; |
620 | |
} |
621 | |
} |
622 | |
|
623 | |
|
624 | |
|
625 | |
|
626 | |
|
627 | |
public static class ExamplesTableConverter implements ParameterConverter { |
628 | |
|
629 | |
private final ExamplesTableFactory factory; |
630 | |
|
631 | |
public ExamplesTableConverter() { |
632 | 1 | this(new ExamplesTableFactory()); |
633 | 1 | } |
634 | |
|
635 | 984 | public ExamplesTableConverter(ExamplesTableFactory factory) { |
636 | 984 | this.factory = factory; |
637 | 984 | } |
638 | |
|
639 | |
public boolean accept(Type type) { |
640 | 84 | if (type instanceof Class<?>) { |
641 | 83 | return ExamplesTable.class.isAssignableFrom((Class<?>) type); |
642 | |
} |
643 | 1 | return false; |
644 | |
} |
645 | |
|
646 | |
public Object convertValue(String value, Type type) { |
647 | 1 | return factory.createExamplesTable(value); |
648 | |
} |
649 | |
|
650 | |
} |
651 | |
|
652 | |
|
653 | |
|
654 | |
|
655 | |
|
656 | |
public static class ExamplesTableParametersConverter implements ParameterConverter { |
657 | |
|
658 | |
private final ExamplesTableFactory factory; |
659 | |
|
660 | |
public ExamplesTableParametersConverter() { |
661 | 2 | this(new ExamplesTableFactory()); |
662 | 2 | } |
663 | |
|
664 | 985 | public ExamplesTableParametersConverter(ExamplesTableFactory factory) { |
665 | 985 | this.factory = factory; |
666 | 985 | } |
667 | |
|
668 | |
public boolean accept(Type type) { |
669 | 87 | if (type instanceof ParameterizedType) { |
670 | 1 | Class<?> rawClass = rawClass(type); |
671 | 1 | Class<?> argumentClass = argumentClass(type); |
672 | 1 | if (rawClass.isAnnotationPresent(AsParameters.class) |
673 | |
|| argumentClass.isAnnotationPresent(AsParameters.class)) { |
674 | 1 | return true; |
675 | |
} |
676 | 0 | } else if (type instanceof Class) { |
677 | 84 | return ((Class<?>) type).isAnnotationPresent(AsParameters.class); |
678 | |
} |
679 | 2 | return false; |
680 | |
} |
681 | |
|
682 | |
private Class<?> rawClass(Type type) { |
683 | 1 | return (Class<?>) ((ParameterizedType) type).getRawType(); |
684 | |
} |
685 | |
|
686 | |
private Class<?> argumentClass(Type type) { |
687 | 3 | if (type instanceof ParameterizedType) { |
688 | 2 | return (Class<?>) ((ParameterizedType) type).getActualTypeArguments()[0]; |
689 | |
} else { |
690 | 1 | return (Class<?>) type; |
691 | |
} |
692 | |
} |
693 | |
|
694 | |
public Object convertValue(String value, Type type) { |
695 | 2 | List<?> rows = factory.createExamplesTable(value).getRowsAs(argumentClass(type)); |
696 | 2 | if (type instanceof ParameterizedType) { |
697 | 1 | return rows; |
698 | |
} |
699 | 1 | return rows.iterator().next(); |
700 | |
} |
701 | |
|
702 | |
} |
703 | |
|
704 | |
|
705 | |
|
706 | |
|
707 | |
public static class MethodReturningConverter implements ParameterConverter { |
708 | |
private Method method; |
709 | |
private Class<?> stepsType; |
710 | |
private InjectableStepsFactory stepsFactory; |
711 | |
|
712 | 4 | public MethodReturningConverter(Method method, Object instance) { |
713 | 4 | this.method = method; |
714 | 4 | this.stepsType = instance.getClass(); |
715 | 4 | this.stepsFactory = new InstanceStepsFactory(new MostUsefulConfiguration(), instance); |
716 | 4 | } |
717 | |
|
718 | 1 | public MethodReturningConverter(Method method, Class<?> stepsType, InjectableStepsFactory stepsFactory) { |
719 | 1 | this.method = method; |
720 | 1 | this.stepsType = stepsType; |
721 | 1 | this.stepsFactory = stepsFactory; |
722 | 1 | } |
723 | |
|
724 | |
public boolean accept(Type type) { |
725 | 16 | if (type instanceof Class<?>) { |
726 | 15 | return method.getReturnType().isAssignableFrom((Class<?>) type); |
727 | |
} |
728 | 1 | return false; |
729 | |
} |
730 | |
|
731 | |
public Object convertValue(String value, Type type) { |
732 | |
try { |
733 | 5 | Object instance = instance(); |
734 | 5 | return method.invoke(instance, value); |
735 | 1 | } catch (Exception e) { |
736 | 1 | throw new ParameterConvertionFailed("Failed to invoke method " + method + " with value " + value |
737 | |
+ " in " + type, e); |
738 | |
} |
739 | |
} |
740 | |
|
741 | |
private Object instance() { |
742 | 5 | return stepsFactory.createInstanceOfType(stepsType); |
743 | |
} |
744 | |
|
745 | |
} |
746 | |
} |