|
| 1 | +package graphql.kickstart.tools |
| 2 | + |
| 3 | +import com.fasterxml.classmate.ResolvedType |
| 4 | +import graphql.kickstart.tools.util.JavaType |
| 5 | +import graphql.kickstart.tools.util.ParameterizedTypeImpl |
| 6 | +import graphql.kickstart.tools.util.Primitives |
| 7 | +import org.apache.commons.lang3.reflect.TypeUtils |
| 8 | +import java.lang.reflect.ParameterizedType |
| 9 | +import java.lang.reflect.TypeVariable |
| 10 | +import java.lang.reflect.WildcardType |
| 11 | + |
| 12 | +/** |
| 13 | + * @author Andrew Potter |
| 14 | + */ |
| 15 | +internal class GenericTypeResolver(private val containingType: JavaType, private val options: SchemaParserOptions) { |
| 16 | + |
| 17 | + fun getRawClass() = getRawClass(containingType) |
| 18 | + |
| 19 | + fun getRawClass(type: JavaType): Class<*> = TypeUtils.getRawType(type, containingType) |
| 20 | + |
| 21 | + fun isAssignableFrom(type: JavaType) = TypeUtils.isAssignable(type, containingType) |
| 22 | + |
| 23 | + /** |
| 24 | + * Turns a type as declared in the source (a method's return or parameter type, or a field's type) into the type |
| 25 | + * the GraphQL type is matched against: |
| 26 | + * - type variables are resolved relative to the containing type, |
| 27 | + * e.g. `T` of `AbstractItem<T>.getId()` becomes `Long` for `class Item : AbstractItem<Long>()` |
| 28 | + * - generic wrappers are replaced by their wrapped type, e.g. `CompletableFuture<Foo>` becomes `Foo` |
| 29 | + * - wildcards are replaced by their upper bound, e.g. `? extends Foo` becomes `Foo` |
| 30 | + * - primitives are replaced by their boxed class, e.g. `int` becomes `Integer` |
| 31 | + */ |
| 32 | + fun resolve(javaType: JavaType): JavaType { |
| 33 | + return when (val type = resolveTypeVariables(javaType)) { |
| 34 | + is ParameterizedType -> { |
| 35 | + val rawType = type.rawType |
| 36 | + val wrapper = options.genericWrappers.find { it.type == rawType } |
| 37 | + ?: return type |
| 38 | + |
| 39 | + val typeArguments = type.actualTypeArguments |
| 40 | + if (typeArguments.size <= wrapper.index) { |
| 41 | + throw IndexOutOfBoundsException("Generic type '${TypeUtils.toString(type)}' does not have a type argument at index ${wrapper.index}!") |
| 42 | + } |
| 43 | + |
| 44 | + val unwrapsTo = wrapper.schemaWrapper.invoke(typeArguments[wrapper.index]) |
| 45 | + resolve(unwrapsTo) |
| 46 | + } |
| 47 | + is TypeVariable<*> -> error("Could not resolve type variable '${TypeUtils.toLongString(type)}' relative to ${TypeUtils.toString(containingType)}") |
| 48 | + is WildcardType -> type.upperBounds.firstOrNull() |
| 49 | + ?: error("Unable to unwrap type, wildcard has no upper bound: $type") |
| 50 | + is Class<*> -> if (type.isPrimitive) Primitives.wrap(type) else type |
| 51 | + else -> error("Unable to unwrap type: $type") |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + private fun resolveTypeVariables(type: JavaType, resolving: Set<TypeVariable<*>> = emptySet()): JavaType { |
| 56 | + return when (type) { |
| 57 | + is ParameterizedType -> { |
| 58 | + val actualTypeArguments = type.actualTypeArguments.map { resolveTypeVariables(it, resolving) }.toTypedArray() |
| 59 | + ParameterizedTypeImpl(type.rawType as Class<*>, actualTypeArguments, type.ownerType?.let { resolveTypeVariables(it, resolving) }) |
| 60 | + } |
| 61 | + is WildcardType -> TypeUtils.wildcardType() |
| 62 | + .withUpperBounds(*type.upperBounds.map { resolveTypeVariables(it, resolving) }.toTypedArray()) |
| 63 | + .withLowerBounds(*type.lowerBounds.map { resolveTypeVariables(it, resolving) }.toTypedArray()) |
| 64 | + .build() |
| 65 | + is ResolvedType -> { |
| 66 | + if (type.typeParameters.isEmpty()) { |
| 67 | + type.erasedType |
| 68 | + } else { |
| 69 | + val actualTypeArguments = type.typeParameters.map { resolveTypeVariables(it) }.toTypedArray() |
| 70 | + ParameterizedTypeImpl(type.erasedType, actualTypeArguments, null) |
| 71 | + } |
| 72 | + } |
| 73 | + is TypeVariable<*> -> { |
| 74 | + val genericDeclaration = type.genericDeclaration |
| 75 | + when { |
| 76 | + // only a variable leaked from a raw type can be bound to a type containing itself (e.g. T -> List<T>), |
| 77 | + // erase it like the raw type does instead of expanding it forever |
| 78 | + type in resolving -> TypeUtils.getRawType(type.bounds.first(), null) ?: Any::class.java |
| 79 | + // the containing type binds the variables of all its supertypes |
| 80 | + genericDeclaration is Class<*> -> generateSequence(containingType) { (it as? ParameterizedType)?.ownerType } |
| 81 | + // an inner class can also use the variables of its outer class, those are bound by its owner type (e.g. Connection<Owner>.Entry) |
| 82 | + .firstNotNullOfOrNull { TypeUtils.getTypeArguments(it, genericDeclaration)?.get(type) } |
| 83 | + // keep the full type argument (e.g. List<Foo>) rather than its raw class so nested generics aren't lost |
| 84 | + ?.let { resolveTypeVariables(it, resolving + type) } |
| 85 | + ?: type |
| 86 | + else -> type |
| 87 | + } |
| 88 | + } |
| 89 | + else -> type |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments