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