|
| 1 | +/* |
| 2 | + * Copyright 2025 Code Intelligence GmbH |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.code_intelligence.jazzer.mutation.mutator.lang; |
| 18 | + |
| 19 | +import static com.code_intelligence.jazzer.mutation.combinator.MutatorCombinators.mutateIndices; |
| 20 | +import static com.code_intelligence.jazzer.mutation.combinator.MutatorCombinators.mutateThenMap; |
| 21 | +import static com.code_intelligence.jazzer.mutation.support.Preconditions.require; |
| 22 | +import static java.lang.String.format; |
| 23 | +import static java.util.Arrays.stream; |
| 24 | +import static java.util.stream.Collectors.toList; |
| 25 | + |
| 26 | +import com.code_intelligence.jazzer.mutation.annotation.ElementOf; |
| 27 | +import com.code_intelligence.jazzer.mutation.api.ExtendedMutatorFactory; |
| 28 | +import com.code_intelligence.jazzer.mutation.api.MutatorFactory; |
| 29 | +import com.code_intelligence.jazzer.mutation.api.SerializingMutator; |
| 30 | +import java.lang.reflect.AnnotatedType; |
| 31 | +import java.util.ArrayList; |
| 32 | +import java.util.Arrays; |
| 33 | +import java.util.List; |
| 34 | +import java.util.Optional; |
| 35 | + |
| 36 | +final class ElementOfMutatorFactory implements MutatorFactory { |
| 37 | + @Override |
| 38 | + public Optional<SerializingMutator<?>> tryCreate( |
| 39 | + AnnotatedType type, ExtendedMutatorFactory factory) { |
| 40 | + ElementOf elementOf = type.getAnnotation(ElementOf.class); |
| 41 | + if (elementOf == null) { |
| 42 | + return Optional.empty(); |
| 43 | + } |
| 44 | + if (!(type.getType() instanceof Class<?>)) { |
| 45 | + return Optional.empty(); |
| 46 | + } |
| 47 | + Class<?> rawType = (Class<?>) type.getType(); |
| 48 | + |
| 49 | + if (rawType == byte.class || rawType == Byte.class) { |
| 50 | + return Optional.of( |
| 51 | + elementOfMutator(boxBytes(elementOf.bytes()), "bytes", rawType.getSimpleName())); |
| 52 | + } else if (rawType == short.class || rawType == Short.class) { |
| 53 | + return Optional.of( |
| 54 | + elementOfMutator(boxShorts(elementOf.shorts()), "shorts", rawType.getSimpleName())); |
| 55 | + } else if (rawType == int.class || rawType == Integer.class) { |
| 56 | + return Optional.of( |
| 57 | + elementOfMutator(boxInts(elementOf.integers()), "integers", rawType.getSimpleName())); |
| 58 | + } else if (rawType == long.class || rawType == Long.class) { |
| 59 | + return Optional.of( |
| 60 | + elementOfMutator(boxLongs(elementOf.longs()), "longs", rawType.getSimpleName())); |
| 61 | + } else if (rawType == char.class || rawType == Character.class) { |
| 62 | + return Optional.of( |
| 63 | + elementOfMutator(boxChars(elementOf.chars()), "chars", rawType.getSimpleName())); |
| 64 | + } else if (rawType == float.class || rawType == Float.class) { |
| 65 | + return Optional.of( |
| 66 | + elementOfMutator(boxFloats(elementOf.floats()), "floats", rawType.getSimpleName())); |
| 67 | + } else if (rawType == double.class || rawType == Double.class) { |
| 68 | + return Optional.of( |
| 69 | + elementOfMutator(boxDoubles(elementOf.doubles()), "doubles", rawType.getSimpleName())); |
| 70 | + } else if (rawType == String.class) { |
| 71 | + return Optional.of( |
| 72 | + elementOfMutator(Arrays.asList(elementOf.strings()), "strings", rawType.getSimpleName())); |
| 73 | + } |
| 74 | + return Optional.empty(); |
| 75 | + } |
| 76 | + |
| 77 | + private static <T> SerializingMutator<T> elementOfMutator( |
| 78 | + List<T> values, String fieldName, String targetTypeName) { |
| 79 | + require( |
| 80 | + !values.isEmpty(), |
| 81 | + format( |
| 82 | + "@ElementOf %s array must contain at least one value for %s", |
| 83 | + fieldName, targetTypeName)); |
| 84 | + |
| 85 | + return mutateThenMap( |
| 86 | + mutateIndices(values.size()), |
| 87 | + values::get, |
| 88 | + value -> { |
| 89 | + int index = values.indexOf(value); |
| 90 | + require( |
| 91 | + index >= 0, |
| 92 | + "@ElementOf produced value not contained in the declared value set for " |
| 93 | + + targetTypeName); |
| 94 | + return index; |
| 95 | + }, |
| 96 | + isInCycle -> |
| 97 | + format("@ElementOf(%s, size=%d) -> %s", fieldName, values.size(), targetTypeName)); |
| 98 | + } |
| 99 | + |
| 100 | + private static List<Byte> boxBytes(byte[] values) { |
| 101 | + List<Byte> result = new ArrayList<>(values.length); |
| 102 | + for (byte value : values) { |
| 103 | + result.add(value); |
| 104 | + } |
| 105 | + return result; |
| 106 | + } |
| 107 | + |
| 108 | + private static List<Short> boxShorts(short[] values) { |
| 109 | + List<Short> result = new ArrayList<>(values.length); |
| 110 | + for (short value : values) { |
| 111 | + result.add(value); |
| 112 | + } |
| 113 | + return result; |
| 114 | + } |
| 115 | + |
| 116 | + private static List<Integer> boxInts(int[] values) { |
| 117 | + return stream(values).boxed().collect(toList()); |
| 118 | + } |
| 119 | + |
| 120 | + private static List<Long> boxLongs(long[] values) { |
| 121 | + return Arrays.stream(values).boxed().collect(toList()); |
| 122 | + } |
| 123 | + |
| 124 | + private static List<Character> boxChars(char[] values) { |
| 125 | + List<Character> result = new ArrayList<>(values.length); |
| 126 | + for (char value : values) { |
| 127 | + result.add(value); |
| 128 | + } |
| 129 | + return result; |
| 130 | + } |
| 131 | + |
| 132 | + private static List<Float> boxFloats(float[] values) { |
| 133 | + List<Float> result = new ArrayList<>(values.length); |
| 134 | + for (float value : values) { |
| 135 | + result.add(value); |
| 136 | + } |
| 137 | + return result; |
| 138 | + } |
| 139 | + |
| 140 | + private static List<Double> boxDoubles(double[] values) { |
| 141 | + return Arrays.stream(values).boxed().collect(toList()); |
| 142 | + } |
| 143 | +} |
0 commit comments