Skip to content

Commit 8155111

Browse files
committed
fix: allow creation of parameterized and generic array types
In addition to primitive arrays, these types are now also possible: - List<Integer> [] - List<Integer> [][][]
1 parent da56919 commit 8155111

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

src/main/java/com/code_intelligence/jazzer/mutation/mutator/collection/ArrayMutatorFactory.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static com.code_intelligence.jazzer.mutation.mutator.collection.ChunkMutations.MutationAction.pickRandomMutationAction;
2020
import static com.code_intelligence.jazzer.mutation.support.Preconditions.require;
2121
import static com.code_intelligence.jazzer.mutation.support.PropertyConstraintSupport.propagatePropertyConstraints;
22+
import static com.code_intelligence.jazzer.mutation.support.TypeSupport.extractRawClass;
2223
import static java.lang.Math.min;
2324
import static java.lang.String.format;
2425

@@ -35,6 +36,7 @@
3536
import java.lang.reflect.AnnotatedArrayType;
3637
import java.lang.reflect.AnnotatedType;
3738
import java.lang.reflect.Array;
39+
import java.lang.reflect.Type;
3840
import java.util.Arrays;
3941
import java.util.Optional;
4042
import java.util.function.Predicate;
@@ -53,12 +55,16 @@ public Optional<SerializingMutator<?>> tryCreate(
5355

5456
AnnotatedType elementType = ((AnnotatedArrayType) type).getAnnotatedGenericComponentType();
5557
AnnotatedType propagatedElementType = propagatePropertyConstraints(type, elementType);
56-
Class<?> propagatedElementClazz = (Class<?>) propagatedElementType.getType();
57-
return Optional.of(propagatedElementType)
58-
.flatMap(factory::tryCreate)
59-
.map(
60-
elementMutator ->
61-
new ArrayMutator<>(elementMutator, propagatedElementClazz, minLength, maxLength));
58+
Type rawType = propagatedElementType.getType();
59+
return extractRawClass(rawType)
60+
.flatMap(
61+
propagatedElementClass ->
62+
Optional.of(propagatedElementType)
63+
.flatMap(factory::tryCreate)
64+
.map(
65+
elementMutator ->
66+
new ArrayMutator<>(
67+
elementMutator, propagatedElementClass, minLength, maxLength)));
6268
}
6369

6470
enum CrossOverAction {

src/main/java/com/code_intelligence/jazzer/mutation/support/TypeSupport.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,12 @@
3737
import java.lang.reflect.AnnotatedTypeVariable;
3838
import java.lang.reflect.AnnotatedWildcardType;
3939
import java.lang.reflect.Array;
40+
import java.lang.reflect.GenericArrayType;
4041
import java.lang.reflect.InvocationTargetException;
4142
import java.lang.reflect.ParameterizedType;
4243
import java.lang.reflect.Type;
44+
import java.lang.reflect.TypeVariable;
45+
import java.lang.reflect.WildcardType;
4346
import java.util.ArrayDeque;
4447
import java.util.Arrays;
4548
import java.util.Collections;
@@ -687,4 +690,21 @@ public static boolean annotatedTypeEquals(AnnotatedType left, AnnotatedType righ
687690
return left.getType().equals(right.getType())
688691
&& Arrays.equals(left.getAnnotations(), right.getAnnotations());
689692
}
693+
694+
public static Optional<Class<?>> extractRawClass(Type type) {
695+
if (type instanceof Class<?>) {
696+
return Optional.of((Class<?>) type);
697+
} else if (type instanceof ParameterizedType) {
698+
return Optional.of((Class<?>) ((ParameterizedType) type).getRawType());
699+
} else if (type instanceof GenericArrayType) {
700+
Type componentType = ((GenericArrayType) type).getGenericComponentType();
701+
Optional<Class<?>> componentClass = extractRawClass(componentType);
702+
return componentClass.map(aClass -> Array.newInstance(aClass, 0).getClass());
703+
} else if (type instanceof TypeVariable<?> || type instanceof WildcardType) {
704+
// Default fallback — assume Object array
705+
return Optional.of(Object.class);
706+
} else {
707+
return Optional.empty();
708+
}
709+
}
690710
}

0 commit comments

Comments
 (0)