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 | 978 | this(DEFAULT_STEP_MONITOR); |
75 | 978 | } |
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
public ParameterConverters(StepMonitor monitor) { |
83 | 978 | this(monitor, DEFAULT_NUMBER_FORMAT_LOCAL, DEFAULT_LIST_SEPARATOR, DEFAULT_THREAD_SAFETY); |
84 | 978 | } |
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 | 979 | this(monitor, new ArrayList<ParameterConverter>(), threadSafe); |
110 | 979 | this.addConverters(defaultConverters(locale, listSeparator)); |
111 | 979 | } |
112 | |
|
113 | 1012 | private ParameterConverters(StepMonitor monitor, List<ParameterConverter> converters, boolean threadSafe) { |
114 | 1012 | this.monitor = monitor; |
115 | 1012 | this.threadSafe = threadSafe; |
116 | 1012 | this.converters = (threadSafe ? new CopyOnWriteArrayList<ParameterConverter>(converters) |
117 | |
: new ArrayList<ParameterConverter>(converters)); |
118 | 1012 | } |
119 | |
|
120 | |
protected ParameterConverter[] defaultConverters(Locale locale, String listSeparator) { |
121 | 980 | String escapedListSeparator = escapeRegexPunctuation(listSeparator); |
122 | 980 | ExamplesTableFactory tableFactory = new ExamplesTableFactory(this); |
123 | 980 | 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 | 980 | return defaultConverters; |
130 | |
} |
131 | |
|
132 | |
|
133 | |
private String escapeRegexPunctuation(String matchThis) { |
134 | 980 | return matchThis.replaceAll("([\\[\\]\\{\\}\\?\\^\\.\\*\\(\\)\\+\\\\])", "\\\\$1"); |
135 | |
} |
136 | |
|
137 | |
public ParameterConverters addConverters(ParameterConverter... converters) { |
138 | 981 | return addConverters(asList(converters)); |
139 | |
} |
140 | |
|
141 | |
public ParameterConverters addConverters(List<ParameterConverter> converters) { |
142 | 1017 | this.converters.addAll(0, converters); |
143 | 1017 | 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 | 1972 | private ThreadLocal<NumberFormat> threadLocalNumberFormat = new ThreadLocal<NumberFormat>(); |
223 | |
|
224 | |
public NumberConverter() { |
225 | 4 | this(NumberFormat.getInstance(DEFAULT_NUMBER_FORMAT_LOCAL)); |
226 | 4 | } |
227 | |
|
228 | 1972 | public NumberConverter(NumberFormat numberFormat) { |
229 | 1972 | synchronized (this) { |
230 | 1972 | this.numberFormat = numberFormat; |
231 | 1972 | this.threadLocalNumberFormat.set((NumberFormat) this.numberFormat.clone()); |
232 | 1972 | } |
233 | 1972 | } |
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 | 234 | Number n = numberFormat().parse(value); |
245 | 233 | if (type == Byte.class || type == byte.class) { |
246 | 14 | return n.byteValue(); |
247 | 219 | } else if (type == Short.class || type == short.class) { |
248 | 14 | return n.shortValue(); |
249 | 205 | } else if (type == Integer.class || type == int.class) { |
250 | 43 | return n.intValue(); |
251 | 162 | } else if (type == Float.class || type == float.class) { |
252 | 32 | return n.floatValue(); |
253 | 130 | } else if (type == Long.class || type == long.class) { |
254 | 26 | return n.longValue(); |
255 | 104 | } else if (type == Double.class || type == double.class) { |
256 | 36 | return n.doubleValue(); |
257 | 68 | } else if (type == BigInteger.class) { |
258 | 7 | return BigInteger.valueOf(n.longValue()); |
259 | 61 | } 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 | 294 | if (threadLocalNumberFormat.get() == null) { |
282 | 4 | synchronized (this) { |
283 | 4 | threadLocalNumberFormat.set((NumberFormat) numberFormat.clone()); |
284 | 4 | } |
285 | |
} |
286 | 295 | return threadLocalNumberFormat.get(); |
287 | |
} |
288 | |
|
289 | |
|
290 | |
|
291 | |
|
292 | |
|
293 | |
|
294 | |
|
295 | |
|
296 | |
|
297 | |
|
298 | |
private String canonicalize(String value) { |
299 | 30 | char decimalPointSeparator = '.'; |
300 | 30 | char minusSign = '-'; |
301 | 30 | String rxNotDigits = "[^0-9]"; |
302 | 30 | StringBuilder builder = new StringBuilder(value.length()); |
303 | |
|
304 | |
|
305 | 30 | if (numberFormat() instanceof DecimalFormat) { |
306 | 30 | DecimalFormatSymbols decimalFormatSymbols = ((DecimalFormat) numberFormat()).getDecimalFormatSymbols(); |
307 | 30 | minusSign = decimalFormatSymbols.getMinusSign(); |
308 | 30 | decimalPointSeparator = decimalFormatSymbols.getDecimalSeparator(); |
309 | |
} |
310 | |
|
311 | 30 | value = value.trim(); |
312 | 30 | int decimalPointPosition = value.lastIndexOf(decimalPointSeparator); |
313 | 30 | boolean isNegative = value.charAt(0) == minusSign; |
314 | |
|
315 | 30 | if (isNegative) { |
316 | 0 | builder.append('-'); |
317 | |
} |
318 | |
|
319 | 30 | if (decimalPointPosition != -1) { |
320 | 23 | String sf = value.substring(0, decimalPointPosition).replaceAll(rxNotDigits, ""); |
321 | 23 | 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 | 984 | public NumberListConverter(NumberFormat numberFormat, String valueSeparator) { |
355 | 984 | this.numberConverter = new NumberConverter(numberFormat); |
356 | 984 | this.valueSeparator = valueSeparator; |
357 | 984 | } |
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 | 981 | public StringListConverter(String valueSeparator) { |
408 | 981 | this.valueSeparator = valueSeparator; |
409 | 981 | } |
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 | 982 | this(DEFAULT_FORMAT); |
450 | 982 | } |
451 | |
|
452 | 983 | public DateConverter(DateFormat dateFormat) { |
453 | 983 | this.dateFormat = dateFormat; |
454 | 983 | } |
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 | 981 | this(DEFAULT_TRUE_VALUE, DEFAULT_FALSE_VALUE); |
483 | 981 | } |
484 | |
|
485 | 982 | public BooleanConverter(String trueValue, String falseValue) { |
486 | 982 | this.trueValue = trueValue; |
487 | 982 | this.falseValue = falseValue; |
488 | 982 | } |
489 | |
|
490 | |
public boolean accept(Type type) { |
491 | 121 | if (type instanceof Class<?>) { |
492 | 115 | return Boolean.class.isAssignableFrom((Class<?>) type); |
493 | |
} |
494 | 6 | return false; |
495 | |
} |
496 | |
|
497 | |
public Object convertValue(String value, Type type) { |
498 | 12 | return BooleanUtils.toBoolean(value, trueValue, falseValue); |
499 | |
} |
500 | |
} |
501 | |
|
502 | |
public static class BooleanListConverter implements ParameterConverter { |
503 | |
private final BooleanConverter booleanConverter; |
504 | |
private String valueSeparator; |
505 | |
|
506 | |
public BooleanListConverter() { |
507 | 1 | this(DEFAULT_LIST_SEPARATOR, DEFAULT_TRUE_VALUE, DEFAULT_FALSE_VALUE); |
508 | 1 | } |
509 | |
|
510 | |
public BooleanListConverter(String valueSeparator) { |
511 | 0 | this(valueSeparator, DEFAULT_TRUE_VALUE, DEFAULT_FALSE_VALUE); |
512 | 0 | } |
513 | |
|
514 | 1 | public BooleanListConverter(String valueSeparator, String trueValue, String falseValue) { |
515 | 1 | this.valueSeparator = valueSeparator; |
516 | 1 | booleanConverter = new BooleanConverter(trueValue, falseValue); |
517 | 1 | } |
518 | |
|
519 | |
public boolean accept(Type type) { |
520 | 1 | if (type instanceof ParameterizedType) { |
521 | 1 | Type rawType = rawType(type); |
522 | 1 | Type argumentType = argumentType(type); |
523 | 1 | return List.class.isAssignableFrom((Class<?>) rawType) |
524 | |
&& Boolean.class.isAssignableFrom((Class<?>) argumentType); |
525 | |
} |
526 | 0 | return false; |
527 | |
} |
528 | |
|
529 | |
public Object convertValue(String value, Type type) { |
530 | 1 | List<String> values = trim(asList(value.split(valueSeparator))); |
531 | 1 | List<Boolean> booleans = new ArrayList<Boolean>(); |
532 | |
|
533 | 1 | for (String booleanValue : values) { |
534 | 3 | booleans.add((Boolean) booleanConverter.convertValue(booleanValue, type)); |
535 | 3 | } |
536 | 1 | return booleans; |
537 | |
} |
538 | |
|
539 | |
private Type rawType(Type type) { |
540 | 1 | return ((ParameterizedType) type).getRawType(); |
541 | |
} |
542 | |
|
543 | |
private Type argumentType(Type type) { |
544 | 1 | return ((ParameterizedType) type).getActualTypeArguments()[0]; |
545 | |
} |
546 | |
} |
547 | |
|
548 | |
|
549 | |
|
550 | |
|
551 | 1963 | public static class EnumConverter implements ParameterConverter { |
552 | |
|
553 | |
public boolean accept(Type type) { |
554 | 85 | if (type instanceof Class<?>) { |
555 | 84 | return ((Class<?>) type).isEnum(); |
556 | |
} |
557 | 1 | return false; |
558 | |
} |
559 | |
|
560 | |
public Object convertValue(String value, Type type) { |
561 | 5 | String typeClass = ((Class<?>) type).getName(); |
562 | 5 | Class<?> enumClass = (Class<?>) type; |
563 | 5 | Method valueOfMethod = null; |
564 | |
try { |
565 | 5 | valueOfMethod = enumClass.getMethod("valueOf", new Class[] { String.class }); |
566 | 5 | valueOfMethod.setAccessible(true); |
567 | 5 | return valueOfMethod.invoke(enumClass, new Object[] { value }); |
568 | 1 | } catch (Exception e) { |
569 | 1 | throw new ParameterConvertionFailed("Failed to convert " + value + " for Enum " + typeClass, e); |
570 | |
} |
571 | |
} |
572 | |
} |
573 | |
|
574 | |
|
575 | |
|
576 | |
|
577 | |
|
578 | |
public static class EnumListConverter implements ParameterConverter { |
579 | |
private final EnumConverter enumConverter; |
580 | |
private String valueSeparator; |
581 | |
|
582 | |
public EnumListConverter() { |
583 | 981 | this(DEFAULT_LIST_SEPARATOR); |
584 | 981 | } |
585 | |
|
586 | 981 | public EnumListConverter(String valueSeparator) { |
587 | 981 | this.enumConverter = new EnumConverter(); |
588 | 981 | this.valueSeparator = valueSeparator; |
589 | 981 | } |
590 | |
|
591 | |
public boolean accept(Type type) { |
592 | 82 | if (type instanceof ParameterizedType) { |
593 | 1 | Type rawType = rawType(type); |
594 | 1 | Type argumentType = argumentType(type); |
595 | 1 | return List.class.isAssignableFrom((Class<?>) rawType) && enumConverter.accept(argumentType); |
596 | |
} |
597 | 81 | return false; |
598 | |
} |
599 | |
|
600 | |
public Object convertValue(String value, Type type) { |
601 | 1 | Type argumentType = argumentType(type); |
602 | 1 | List<String> values = trim(asList(value.split(valueSeparator))); |
603 | 1 | List<Enum<?>> enums = new ArrayList<Enum<?>>(); |
604 | 1 | for (String string : values) { |
605 | 3 | enums.add((Enum<?>) enumConverter.convertValue(string, argumentType)); |
606 | 3 | } |
607 | 1 | return enums; |
608 | |
} |
609 | |
|
610 | |
private Type rawType(Type type) { |
611 | 1 | return ((ParameterizedType) type).getRawType(); |
612 | |
} |
613 | |
|
614 | |
private Type argumentType(Type type) { |
615 | 2 | return ((ParameterizedType) type).getActualTypeArguments()[0]; |
616 | |
} |
617 | |
} |
618 | |
|
619 | |
|
620 | |
|
621 | |
|
622 | |
|
623 | |
public static class ExamplesTableConverter implements ParameterConverter { |
624 | |
|
625 | |
private final ExamplesTableFactory factory; |
626 | |
|
627 | |
public ExamplesTableConverter() { |
628 | 1 | this(new ExamplesTableFactory()); |
629 | 1 | } |
630 | |
|
631 | 981 | public ExamplesTableConverter(ExamplesTableFactory factory) { |
632 | 981 | this.factory = factory; |
633 | 981 | } |
634 | |
|
635 | |
public boolean accept(Type type) { |
636 | 84 | if (type instanceof Class<?>) { |
637 | 83 | return ExamplesTable.class.isAssignableFrom((Class<?>) type); |
638 | |
} |
639 | 1 | return false; |
640 | |
} |
641 | |
|
642 | |
public Object convertValue(String value, Type type) { |
643 | 1 | return factory.createExamplesTable(value); |
644 | |
} |
645 | |
|
646 | |
} |
647 | |
|
648 | |
|
649 | |
|
650 | |
|
651 | |
|
652 | |
public static class ExamplesTableParametersConverter implements ParameterConverter { |
653 | |
|
654 | |
private final ExamplesTableFactory factory; |
655 | |
|
656 | |
public ExamplesTableParametersConverter() { |
657 | 2 | this(new ExamplesTableFactory()); |
658 | 2 | } |
659 | |
|
660 | 982 | public ExamplesTableParametersConverter(ExamplesTableFactory factory) { |
661 | 982 | this.factory = factory; |
662 | 982 | } |
663 | |
|
664 | |
public boolean accept(Type type) { |
665 | 87 | if (type instanceof ParameterizedType) { |
666 | 1 | Class<?> rawClass = rawClass(type); |
667 | 1 | Class<?> argumentClass = argumentClass(type); |
668 | 1 | if (rawClass.isAnnotationPresent(AsParameters.class) |
669 | |
|| argumentClass.isAnnotationPresent(AsParameters.class)) { |
670 | 1 | return true; |
671 | |
} |
672 | 0 | } else if (type instanceof Class) { |
673 | 84 | return ((Class<?>) type).isAnnotationPresent(AsParameters.class); |
674 | |
} |
675 | 2 | return false; |
676 | |
} |
677 | |
|
678 | |
private Class<?> rawClass(Type type) { |
679 | 1 | return (Class<?>) ((ParameterizedType) type).getRawType(); |
680 | |
} |
681 | |
|
682 | |
private Class<?> argumentClass(Type type) { |
683 | 3 | if (type instanceof ParameterizedType) { |
684 | 2 | return (Class<?>) ((ParameterizedType) type).getActualTypeArguments()[0]; |
685 | |
} else { |
686 | 1 | return (Class<?>) type; |
687 | |
} |
688 | |
} |
689 | |
|
690 | |
public Object convertValue(String value, Type type) { |
691 | 2 | List<?> rows = factory.createExamplesTable(value).getRowsAs(argumentClass(type)); |
692 | 2 | if (type instanceof ParameterizedType) { |
693 | 1 | return rows; |
694 | |
} |
695 | 1 | return rows.iterator().next(); |
696 | |
} |
697 | |
|
698 | |
} |
699 | |
|
700 | |
|
701 | |
|
702 | |
|
703 | |
public static class MethodReturningConverter implements ParameterConverter { |
704 | |
private Method method; |
705 | |
private Class<?> stepsType; |
706 | |
private InjectableStepsFactory stepsFactory; |
707 | |
|
708 | 4 | public MethodReturningConverter(Method method, Object instance) { |
709 | 4 | this.method = method; |
710 | 4 | this.stepsType = instance.getClass(); |
711 | 4 | this.stepsFactory = new InstanceStepsFactory(new MostUsefulConfiguration(), instance); |
712 | 4 | } |
713 | |
|
714 | 1 | public MethodReturningConverter(Method method, Class<?> stepsType, InjectableStepsFactory stepsFactory) { |
715 | 1 | this.method = method; |
716 | 1 | this.stepsType = stepsType; |
717 | 1 | this.stepsFactory = stepsFactory; |
718 | 1 | } |
719 | |
|
720 | |
public boolean accept(Type type) { |
721 | 16 | if (type instanceof Class<?>) { |
722 | 15 | return method.getReturnType().isAssignableFrom((Class<?>) type); |
723 | |
} |
724 | 1 | return false; |
725 | |
} |
726 | |
|
727 | |
public Object convertValue(String value, Type type) { |
728 | |
try { |
729 | 5 | Object instance = instance(); |
730 | 5 | return method.invoke(instance, value); |
731 | 1 | } catch (Exception e) { |
732 | 1 | throw new ParameterConvertionFailed("Failed to invoke method " + method + " with value " + value |
733 | |
+ " in " + type, e); |
734 | |
} |
735 | |
} |
736 | |
|
737 | |
private Object instance() { |
738 | 5 | return stepsFactory.createInstanceOfType(stepsType); |
739 | |
} |
740 | |
|
741 | |
} |
742 | |
} |