2727import static com .code_intelligence .jazzer .mutation .mutator .collection .ChunkMutations .mutateRandomChunk ;
2828import static com .code_intelligence .jazzer .mutation .support .Preconditions .require ;
2929import static com .code_intelligence .jazzer .mutation .support .PropertyConstraintSupport .propagatePropertyConstraints ;
30+ import static com .code_intelligence .jazzer .mutation .support .TypeSupport .canBeAssignedTo ;
3031import static com .code_intelligence .jazzer .mutation .support .TypeSupport .parameterTypeIfParameterized ;
3132import static java .lang .Math .min ;
3233import static java .lang .String .format ;
3334
35+ import com .code_intelligence .jazzer .mutation .annotation .DictionaryObject ;
36+ import com .code_intelligence .jazzer .mutation .annotation .DictionaryProvider ;
3437import com .code_intelligence .jazzer .mutation .annotation .WithSize ;
3538import com .code_intelligence .jazzer .mutation .api .Debuggable ;
3639import com .code_intelligence .jazzer .mutation .api .ExtendedMutatorFactory ;
4447import java .io .IOException ;
4548import java .lang .reflect .AnnotatedType ;
4649import java .util .ArrayList ;
50+ import java .util .Arrays ;
4751import java .util .List ;
4852import java .util .Optional ;
4953import java .util .function .Predicate ;
5054import java .util .stream .Collectors ;
55+ import java .util .stream .Stream ;
5156
5257final class ListMutatorFactory implements MutatorFactory {
5358 @ Override
@@ -61,7 +66,7 @@ public Optional<SerializingMutator<?>> tryCreate(
6166 Optional <WithSize > withSize = Optional .ofNullable (type .getAnnotation (WithSize .class ));
6267 int minSize = withSize .map (WithSize ::min ).orElse (ListMutator .DEFAULT_MIN_SIZE );
6368 int maxSize = withSize .map (WithSize ::max ).orElse (ListMutator .DEFAULT_MAX_SIZE );
64- return new ListMutator <>(elementMutator , minSize , maxSize );
69+ return new ListMutator <>(type , elementMutator , minSize , maxSize );
6570 });
6671 }
6772
@@ -72,8 +77,10 @@ private static final class ListMutator<T> extends SerializingInPlaceMutator<List
7277 private final SerializingMutator <T > elementMutator ;
7378 private final int minSize ;
7479 private final int maxSize ;
80+ public final List <List <T >> dictionaryValues ;
7581
76- ListMutator (SerializingMutator <T > elementMutator , int minSize , int maxSize ) {
82+ ListMutator (
83+ AnnotatedType type , SerializingMutator <T > elementMutator , int minSize , int maxSize ) {
7784 this .elementMutator = elementMutator ;
7885 this .minSize = minSize ;
7986 this .maxSize = maxSize ;
@@ -84,6 +91,29 @@ private static final class ListMutator<T> extends SerializingInPlaceMutator<List
8491 format (
8592 "WithSize#min=%d needs to be smaller or equal than WithSize#max=%d" ,
8693 minSize , maxSize ));
94+
95+ DictionaryObject [] dictObj = type .getAnnotationsByType (DictionaryObject .class );
96+ System .out .println (
97+ "DICT OBJECTS for List : " + type + " ------ " + Arrays .toString (dictObj ));
98+
99+ dictionaryValues =
100+ (List <List <T >>)
101+ Arrays .stream (dictObj )
102+ .flatMap (
103+ o -> {
104+ Class <? extends DictionaryProvider > providerClass = o .value ();
105+ try {
106+ DictionaryProvider provider =
107+ providerClass .getDeclaredConstructor ().newInstance ();
108+ return provider .value ();
109+ } catch (ReflectiveOperationException e ) {
110+ return Stream .empty ();
111+ }
112+ })
113+ .filter (v -> canBeAssignedTo (v , type ))
114+ .toList ();
115+
116+ System .out .println (" ------ " + this .dictionaryValues );
87117 }
88118
89119 @ Override
@@ -120,6 +150,17 @@ public void initInPlace(List<T> list, PseudoRandom prng) {
120150
121151 @ Override
122152 public void mutateInPlace (List <T > list , PseudoRandom prng ) {
153+
154+ // With a small probability, replace the entire list with a dictionary value
155+ if (!dictionaryValues .isEmpty () && prng .indexIn (20 ) == 0 ) {
156+ list .clear ();
157+ // copy the list from the dictionary to avoid mutating the dictionary itself
158+ List <T > ref = dictionaryValues .get (prng .indexIn (dictionaryValues .size ()));
159+ list .addAll (ref );
160+ System .out .println ("LIST MUTATE IN PLACE - DICTIONARY VALUE: " + ref );
161+ return ;
162+ }
163+
123164 switch (pickRandomMutationAction (list , minSize , maxSize , prng )) {
124165 case DELETE_CHUNK :
125166 deleteRandomChunk (list , minSize , prng , elementMutator .hasFixedSize ());
0 commit comments