-
Notifications
You must be signed in to change notification settings - Fork 163
feat: add mutator support for generic classes #1008
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
b1a87f3
726e25f
c126abe
4674efb
3b402e9
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 |
|---|---|---|
|
|
@@ -16,6 +16,7 @@ | |
|
|
||
| package com.code_intelligence.jazzer.mutation.mutator.aggregate; | ||
|
|
||
| import static com.code_intelligence.jazzer.mutation.support.ParameterizedTypeSupport.resolveTypeArguments; | ||
| import static com.code_intelligence.jazzer.mutation.support.StreamSupport.getOrEmpty; | ||
| import static com.code_intelligence.jazzer.mutation.support.StreamSupport.toArrayOrEmpty; | ||
| import static java.util.Arrays.stream; | ||
|
|
@@ -26,9 +27,13 @@ | |
| import static java.util.stream.Collectors.toMap; | ||
|
|
||
| import java.beans.Introspector; | ||
| import java.lang.reflect.AnnotatedType; | ||
| import java.lang.reflect.Constructor; | ||
| import java.lang.reflect.Executable; | ||
| import java.lang.reflect.GenericArrayType; | ||
| import java.lang.reflect.Method; | ||
| import java.lang.reflect.Modifier; | ||
| import java.lang.reflect.ParameterizedType; | ||
| import java.lang.reflect.Type; | ||
| import java.util.Arrays; | ||
| import java.util.Comparator; | ||
|
|
@@ -49,6 +54,42 @@ static Optional<Class<?>> optionalClassForName(String targetClassName) { | |
| } | ||
| } | ||
|
|
||
| private static Class<?> rawType(Type classType) { | ||
| if (classType instanceof Class<?>) { | ||
| return (Class<?>) classType; | ||
| } else if (classType instanceof ParameterizedType) { | ||
| return rawType(((ParameterizedType) classType).getRawType()); | ||
| } else if (classType instanceof GenericArrayType) { | ||
| return rawType(((GenericArrayType) classType).getGenericComponentType()); | ||
| } else { | ||
| // Bail out on wildcard types or type variables. | ||
| throw new UnsupportedOperationException("Unsupported type: " + classType); | ||
| } | ||
| } | ||
|
|
||
| // Returns the annotated parameter types of a method or constructor resolving all generic type | ||
| // arguments. | ||
| public static AnnotatedType[] resolveAnnotatedParameterTypes( | ||
| Executable e, AnnotatedType classType) { | ||
| Class<?> clazz = rawType(classType.getType()); | ||
| return stream(e.getAnnotatedParameterTypes()) | ||
| .map(t -> resolveTypeArguments(clazz, classType, t)) | ||
| .toArray(AnnotatedType[]::new); | ||
| } | ||
|
|
||
| // Returns the parameter types of a method or constructor resolving all generic type arguments. | ||
| public static Type[] resolveParameterTypes(Executable e, AnnotatedType classType) { | ||
| return stream(resolveAnnotatedParameterTypes(e, classType)) | ||
| .map(AnnotatedType::getType) | ||
| .toArray(Type[]::new); | ||
| } | ||
|
|
||
| static Type resolveReturnType(Method method, AnnotatedType classType) { | ||
| return resolveTypeArguments( | ||
| rawType(classType.getType()), classType, method.getAnnotatedReturnType()) | ||
| .getType(); | ||
| } | ||
|
|
||
| static boolean isConcreteClass(Class<?> clazz) { | ||
| return !Modifier.isAbstract(clazz.getModifiers()); | ||
| } | ||
|
|
@@ -88,9 +129,11 @@ static Optional<Method[]> findGettersByPropertyNames( | |
| propertyNames.map(gettersByPropertyName::get).map(Optional::ofNullable), Method[]::new); | ||
| } | ||
|
|
||
| static Optional<Method[]> findGettersByPropertyTypes(Class<?> clazz, Stream<Class<?>> types) { | ||
| Map<Class<?>, List<Method>> gettersByType = | ||
| findMethods(clazz, BeanSupport::isGetter).collect(groupingBy(Method::getReturnType)); | ||
| static Optional<Method[]> findGettersByPropertyTypes( | ||
| Class<?> clazz, AnnotatedType classType, Stream<Type> types) { | ||
| Map<Type, List<Method>> gettersByType = | ||
| findMethods(clazz, BeanSupport::isGetter) | ||
| .collect(groupingBy(m -> resolveReturnType(m, classType))); | ||
|
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. If
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. I believe the additions in |
||
| return toArrayOrEmpty( | ||
| types.map( | ||
| type -> { | ||
|
|
@@ -122,10 +165,10 @@ private static Optional<String> trimPrefix(String name, String prefix) { | |
| } | ||
| } | ||
|
|
||
| static boolean matchingReturnTypes(Method[] methods, Type[] types) { | ||
| static boolean matchingReturnTypes(Method[] methods, AnnotatedType classType, Type[] types) { | ||
| for (int i = 0; i < methods.length; i++) { | ||
| // TODO: Support Optional<T> getters, which often have a corresponding T setter. | ||
| if (!methods[i].getAnnotatedReturnType().getType().equals(types[i])) { | ||
| if (!resolveReturnType(methods[i], classType).equals(types[i])) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.