|
28 | 28 |
|
29 | 29 | import java.lang.annotation.Annotation;
|
30 | 30 | import java.lang.reflect.Constructor;
|
| 31 | +import java.lang.reflect.Field; |
31 | 32 | import java.lang.reflect.Method;
|
32 | 33 | import java.lang.reflect.Type;
|
33 | 34 | import java.util.ArrayDeque;
|
@@ -108,6 +109,12 @@ public abstract class AbstractRandomDataProviderStrategy implements RandomDataPr
|
108 | 109 | private final Map<Class<? extends Annotation>, AttributeStrategy<?>> attributeStrategies
|
109 | 110 | = new ConcurrentHashMap<Class<? extends Annotation>, AttributeStrategy<?>>();
|
110 | 111 |
|
| 112 | + /** |
| 113 | + * Mapping between attributes and attribute strategies |
| 114 | + */ |
| 115 | + private final Map<Class<?>, Map<String,AttributeStrategy<?>>> attributeClassStrategies |
| 116 | + = new ConcurrentHashMap<Class<?>, Map<String,AttributeStrategy<?>>>(); |
| 117 | + |
111 | 118 | /** The constructor comparator */
|
112 | 119 | private AbstractConstructorComparator constructorHeavyComparator =
|
113 | 120 | ConstructorHeavyFirstComparator.INSTANCE;
|
@@ -546,6 +553,59 @@ public AttributeStrategy<?> getStrategyForAnnotation(
|
546 | 553 |
|
547 | 554 | }
|
548 | 555 |
|
| 556 | + /** |
| 557 | + * {@inheritDoc} |
| 558 | + */ |
| 559 | + @Override |
| 560 | + public RandomDataProviderStrategy addOrReplaceAttributeStrategy( |
| 561 | + final Class<?> type, final String attributeName, |
| 562 | + final AttributeStrategy<?> attributeStrategy) { |
| 563 | + |
| 564 | + Map<String,AttributeStrategy<?>> classStrategies = attributeClassStrategies.get(type); |
| 565 | + if (null == classStrategies) { |
| 566 | + classStrategies = new ConcurrentHashMap<String,AttributeStrategy<?>>(); |
| 567 | + attributeClassStrategies.put(type, classStrategies); |
| 568 | + } |
| 569 | + classStrategies.put(attributeName, attributeStrategy); |
| 570 | + |
| 571 | + return this; |
| 572 | + } |
| 573 | + |
| 574 | + /** |
| 575 | + * {@inheritDoc} |
| 576 | + */ |
| 577 | + @Override |
| 578 | + public RandomDataProviderStrategy removeAttributeStrategy( |
| 579 | + final Class<?> type, String attributeName) { |
| 580 | + |
| 581 | + Map<String,AttributeStrategy<?>> classStrategies = attributeClassStrategies.get(type); |
| 582 | + if (null != classStrategies) { |
| 583 | + classStrategies.remove(attributeName); |
| 584 | + } |
| 585 | + |
| 586 | + return this; |
| 587 | + } |
| 588 | + |
| 589 | + /** |
| 590 | + * {@inheritDoc} |
| 591 | + */ |
| 592 | + @Override |
| 593 | + public AttributeStrategy<?> getStrategyForAttribute( |
| 594 | + final ClassAttribute attribute) { |
| 595 | + |
| 596 | + AttributeStrategy<?> attributeStrategy = null; |
| 597 | + Field field = attribute.getAttribute(); |
| 598 | + if (null != field) { |
| 599 | + Class<?> type = field.getDeclaringClass(); |
| 600 | + Map<String,AttributeStrategy<?>> classStrategies = attributeClassStrategies.get(type); |
| 601 | + if (null != classStrategies) { |
| 602 | + attributeStrategy = classStrategies.get(attribute.getName()); |
| 603 | + } |
| 604 | + } |
| 605 | + return attributeStrategy; |
| 606 | + |
| 607 | + } |
| 608 | + |
549 | 609 | /**
|
550 | 610 | * {@inheritDoc}
|
551 | 611 | */
|
|
0 commit comments