-
Notifications
You must be signed in to change notification settings - Fork 163
feat: add @ElementOf annotation #1028
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| /* | ||
| * Copyright 2024 Code Intelligence GmbH | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.code_intelligence.jazzer.mutation.annotation; | ||
|
|
||
| import static java.lang.annotation.ElementType.TYPE_USE; | ||
| import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
|
||
| import com.code_intelligence.jazzer.mutation.utils.AppliesTo; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.Target; | ||
|
|
||
| /** | ||
| * Restricts generated values to a fixed set. | ||
| * | ||
| * <p>Populate exactly one of the type-specific arrays with at least two values. Only the array that | ||
| * matches the annotated parameter type is used; all others are ignored. For {@link String} and | ||
| * wrapper types, the mutator may still emit {@code null}; add {@link NotNull} (the only supported | ||
| * annotation in combination with {@code @ElementOf}) to prevent that. | ||
| * | ||
| * <p>Example usage: | ||
| * | ||
| * <pre>{@code | ||
| * @FuzzTest | ||
| * void fuzz( | ||
| * @ElementOf(integers = {1, 2, 3}) int option, | ||
| * @ElementOf(strings = {"one", "two"}) String label) { | ||
| * // option is always 1, 2, or 3; label is always "one" or "two". | ||
simonresch marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| * } | ||
| * }</pre> | ||
| */ | ||
| @Target(TYPE_USE) | ||
| @Retention(RUNTIME) | ||
| @AppliesTo({ | ||
| byte.class, | ||
| Byte.class, | ||
| short.class, | ||
| Short.class, | ||
| int.class, | ||
| Integer.class, | ||
| long.class, | ||
| Long.class, | ||
| char.class, | ||
| Character.class, | ||
| float.class, | ||
| Float.class, | ||
| double.class, | ||
| Double.class, | ||
| String.class | ||
| }) | ||
| public @interface ElementOf { | ||
| byte[] bytes() default {}; | ||
|
|
||
| short[] shorts() default {}; | ||
|
|
||
| int[] integers() default {}; | ||
|
|
||
| long[] longs() default {}; | ||
|
|
||
| char[] chars() default {}; | ||
|
|
||
| float[] floats() default {}; | ||
|
|
||
| double[] doubles() default {}; | ||
|
|
||
| String[] strings() default {}; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| /* | ||
| * Copyright 2025 Code Intelligence GmbH | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.code_intelligence.jazzer.mutation.mutator.lang; | ||
|
|
||
| import static com.code_intelligence.jazzer.mutation.combinator.MutatorCombinators.mutateIndices; | ||
| import static com.code_intelligence.jazzer.mutation.combinator.MutatorCombinators.mutateThenMap; | ||
| import static com.code_intelligence.jazzer.mutation.support.Preconditions.require; | ||
| import static java.lang.String.format; | ||
| import static java.util.Arrays.stream; | ||
| import static java.util.stream.Collectors.toList; | ||
|
|
||
| import com.code_intelligence.jazzer.mutation.annotation.ElementOf; | ||
| import com.code_intelligence.jazzer.mutation.api.ExtendedMutatorFactory; | ||
| import com.code_intelligence.jazzer.mutation.api.MutatorFactory; | ||
| import com.code_intelligence.jazzer.mutation.api.SerializingMutator; | ||
| import java.lang.reflect.AnnotatedType; | ||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
| import java.util.stream.Collectors; | ||
| import java.util.stream.IntStream; | ||
|
|
||
| final class ElementOfMutatorFactory implements MutatorFactory { | ||
| @Override | ||
| public Optional<SerializingMutator<?>> tryCreate( | ||
| AnnotatedType type, ExtendedMutatorFactory factory) { | ||
| ElementOf elementOf = type.getAnnotation(ElementOf.class); | ||
| if (elementOf == null) { | ||
| return Optional.empty(); | ||
| } | ||
| if (!(type.getType() instanceof Class<?>)) { | ||
| return Optional.empty(); | ||
| } | ||
| Class<?> rawType = (Class<?>) type.getType(); | ||
|
|
||
| if (rawType == byte.class || rawType == Byte.class) { | ||
| return Optional.of( | ||
| elementOfMutator(boxBytes(elementOf.bytes()), "bytes", rawType.getSimpleName())); | ||
| } else if (rawType == short.class || rawType == Short.class) { | ||
| return Optional.of( | ||
| elementOfMutator(boxShorts(elementOf.shorts()), "shorts", rawType.getSimpleName())); | ||
| } else if (rawType == int.class || rawType == Integer.class) { | ||
| return Optional.of( | ||
| elementOfMutator(boxInts(elementOf.integers()), "integers", rawType.getSimpleName())); | ||
| } else if (rawType == long.class || rawType == Long.class) { | ||
| return Optional.of( | ||
| elementOfMutator(boxLongs(elementOf.longs()), "longs", rawType.getSimpleName())); | ||
| } else if (rawType == char.class || rawType == Character.class) { | ||
| return Optional.of( | ||
| elementOfMutator(boxChars(elementOf.chars()), "chars", rawType.getSimpleName())); | ||
| } else if (rawType == float.class || rawType == Float.class) { | ||
| return Optional.of( | ||
| elementOfMutator(boxFloats(elementOf.floats()), "floats", rawType.getSimpleName())); | ||
| } else if (rawType == double.class || rawType == Double.class) { | ||
| return Optional.of( | ||
| elementOfMutator(boxDoubles(elementOf.doubles()), "doubles", rawType.getSimpleName())); | ||
| } else if (rawType == String.class) { | ||
| return Optional.of( | ||
| elementOfMutator(Arrays.asList(elementOf.strings()), "strings", rawType.getSimpleName())); | ||
| } | ||
| return Optional.empty(); | ||
| } | ||
|
|
||
| private static <T> SerializingMutator<T> elementOfMutator( | ||
| List<T> values, String fieldName, String targetTypeName) { | ||
| require( | ||
| values.size() > 1, | ||
| format( | ||
| "@ElementOf %s array must contain at least two values for %s", | ||
| fieldName, targetTypeName)); | ||
| Map<T, Integer> inverse = | ||
| IntStream.range(0, values.size()).boxed().collect(Collectors.toMap(values::get, i -> i)); | ||
| return mutateThenMap( | ||
| mutateIndices(values.size()), | ||
| values::get, | ||
| v -> inverse.getOrDefault(v, 0), | ||
| isInCycle -> format("@ElementOf<%s>[%d]", targetTypeName, values.size())); | ||
| } | ||
|
|
||
| private static List<Byte> boxBytes(byte[] values) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it make sense to only save distinct values?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO tweaking the probabilities is the most intuitive option if a user wants to add duplicate values. Or do you see a problem with duplicate values? |
||
| List<Byte> result = new ArrayList<>(values.length); | ||
| for (byte value : values) { | ||
| result.add(value); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| private static List<Short> boxShorts(short[] values) { | ||
| List<Short> result = new ArrayList<>(values.length); | ||
| for (short value : values) { | ||
| result.add(value); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| private static List<Integer> boxInts(int[] values) { | ||
| return stream(values).boxed().collect(toList()); | ||
| } | ||
|
|
||
| private static List<Long> boxLongs(long[] values) { | ||
| return Arrays.stream(values).boxed().collect(toList()); | ||
| } | ||
|
|
||
| private static List<Character> boxChars(char[] values) { | ||
| List<Character> result = new ArrayList<>(values.length); | ||
| for (char value : values) { | ||
| result.add(value); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| private static List<Float> boxFloats(float[] values) { | ||
| List<Float> result = new ArrayList<>(values.length); | ||
| for (float value : values) { | ||
| result.add(value); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| private static List<Double> boxDoubles(double[] values) { | ||
| return Arrays.stream(values).boxed().collect(toList()); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.