Skip to content

Commit f119aa8

Browse files
committed
feat: support generic classes in ConstructorBasedBeanMutator
1 parent d13bf65 commit f119aa8

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

src/main/java/com/code_intelligence/jazzer/mutation/mutator/aggregate/CachedConstructorMutatorFactory.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import static com.code_intelligence.jazzer.mutation.mutator.aggregate.AggregatesHelper.asInstantiationFunction;
2020
import static com.code_intelligence.jazzer.mutation.mutator.aggregate.BeanSupport.findConstructorsByParameterCount;
21+
import static com.code_intelligence.jazzer.mutation.mutator.aggregate.BeanSupport.resolveAnnotatedParameters;
2122
import static com.code_intelligence.jazzer.mutation.support.StreamSupport.findFirstPresent;
2223
import static com.code_intelligence.jazzer.mutation.support.TypeSupport.asSubclassOrEmpty;
2324

@@ -63,7 +64,7 @@ private static Optional<SerializingMutator<?>> buildMutator(
6364
return AggregatesHelper.createMutator(
6465
factory,
6566
constructor.getDeclaringClass(),
66-
constructor.getAnnotatedParameterTypes(),
67+
resolveAnnotatedParameters(constructor, initialType),
6768
fromParametersToObject,
6869
fromObjectToParameters,
6970
initialType,

src/test/java/com/code_intelligence/jazzer/mutation/mutator/StressTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,27 @@ public String toString() {
492492
}
493493
}
494494

495+
public static class GenericOnlyConstructorBean<T> {
496+
private final T t;
497+
498+
GenericOnlyConstructorBean(T t) {
499+
this.t = t;
500+
}
501+
502+
@Override
503+
public boolean equals(Object o) {
504+
if (this == o) return true;
505+
if (o == null || getClass() != o.getClass()) return false;
506+
GenericOnlyConstructorBean<T> that = (GenericOnlyConstructorBean<T>) o;
507+
return Objects.equals(t, that.t);
508+
}
509+
510+
@Override
511+
public int hashCode() {
512+
return Objects.hash(t);
513+
}
514+
}
515+
495516
public static class SuperBuilderTarget {
496517
private final String foo;
497518

@@ -971,6 +992,12 @@ void singleParam(int parameter) {}
971992
false,
972993
manyDistinctElements(),
973994
manyDistinctElements()),
995+
arguments(
996+
new TypeHolder<@NotNull GenericOnlyConstructorBean<String>>() {}.annotatedType(),
997+
"[Nullable<String>] -> GenericOnlyConstructorBean",
998+
false,
999+
manyDistinctElements(),
1000+
manyDistinctElements()),
9741001
arguments(
9751002
new TypeHolder<@NotNull List<OnlyConstructorBean>>() {}.annotatedType(),
9761003
"List<Nullable<[Nullable<String>, Nullable<List<Nullable<Integer>>>, Boolean] ->"

src/test/java/com/code_intelligence/jazzer/mutation/mutator/aggregate/CachedConstructorMutatorTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,4 +192,34 @@ void testEmptyArgsConstructor() throws IOException {
192192
EmptyArgs read = mutator.readExclusive(new ByteArrayInputStream(new byte[] {}));
193193
mutator.writeExclusive(read, new ByteArrayOutputStream());
194194
}
195+
196+
static class GenericClass<T> {
197+
private final T t;
198+
199+
GenericClass(T t) {
200+
this.t = t;
201+
}
202+
203+
@Override
204+
public boolean equals(Object o) {
205+
if (this == o) return true;
206+
if (o == null || getClass() != o.getClass()) return false;
207+
GenericClass<T> that = (GenericClass<T>) o;
208+
return Objects.equals(t, that.t);
209+
}
210+
211+
@Override
212+
public int hashCode() {
213+
return Objects.hash(t);
214+
}
215+
}
216+
217+
@Test
218+
void testGenericClass() {
219+
SerializingMutator<GenericClass<String>> mutator =
220+
(SerializingMutator<GenericClass<String>>)
221+
Mutators.newFactory()
222+
.createOrThrow(new TypeHolder<GenericClass<String>>() {}.annotatedType());
223+
assertThat(mutator.toString()).startsWith("Nullable<[Nullable<String>] -> GenericClass>");
224+
}
195225
}

0 commit comments

Comments
 (0)