Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static com.code_intelligence.jazzer.mutation.mutator.collection.ChunkMutations.MutationAction.pickRandomMutationAction;
import static com.code_intelligence.jazzer.mutation.support.Preconditions.require;
import static com.code_intelligence.jazzer.mutation.support.PropertyConstraintSupport.propagatePropertyConstraints;
import static com.code_intelligence.jazzer.mutation.support.TypeSupport.extractRawClass;
import static java.lang.Math.min;
import static java.lang.String.format;

Expand All @@ -35,6 +36,7 @@
import java.lang.reflect.AnnotatedArrayType;
import java.lang.reflect.AnnotatedType;
import java.lang.reflect.Array;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.Optional;
import java.util.function.Predicate;
Expand All @@ -53,12 +55,16 @@ public Optional<SerializingMutator<?>> tryCreate(

AnnotatedType elementType = ((AnnotatedArrayType) type).getAnnotatedGenericComponentType();
AnnotatedType propagatedElementType = propagatePropertyConstraints(type, elementType);
Class<?> propagatedElementClazz = (Class<?>) propagatedElementType.getType();
return Optional.of(propagatedElementType)
.flatMap(factory::tryCreate)
.map(
elementMutator ->
new ArrayMutator<>(elementMutator, propagatedElementClazz, minLength, maxLength));
Type rawType = propagatedElementType.getType();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this change relevant to the DictionaryProvider? If not, it could make sense to split it from this PR.

return extractRawClass(rawType)
.flatMap(
propagatedElementClass ->
Optional.of(propagatedElementType)
.flatMap(factory::tryCreate)
.map(
elementMutator ->
new ArrayMutator<>(
elementMutator, propagatedElementClass, minLength, maxLength)));
}

enum CrossOverAction {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,12 @@
import java.lang.reflect.AnnotatedTypeVariable;
import java.lang.reflect.AnnotatedWildcardType;
import java.lang.reflect.Array;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.lang.reflect.WildcardType;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -687,4 +690,21 @@ public static boolean annotatedTypeEquals(AnnotatedType left, AnnotatedType righ
return left.getType().equals(right.getType())
&& Arrays.equals(left.getAnnotations(), right.getAnnotations());
}

public static Optional<Class<?>> extractRawClass(Type type) {
if (type instanceof Class<?>) {
return Optional.of((Class<?>) type);
} else if (type instanceof ParameterizedType) {
return Optional.of((Class<?>) ((ParameterizedType) type).getRawType());
} else if (type instanceof GenericArrayType) {
Type componentType = ((GenericArrayType) type).getGenericComponentType();
Optional<Class<?>> componentClass = extractRawClass(componentType);
return componentClass.map(aClass -> Array.newInstance(aClass, 0).getClass());
} else if (type instanceof TypeVariable<?> || type instanceof WildcardType) {
// Default fallback — assume Object array
return Optional.of(Object.class);
} else {
return Optional.empty();
}
}
}