private static AnnotatedType getCommonSuperType(List<AnnotatedType> types, Set<String> seenTypeCombos, AnnotatedType fallback) {
if (types == null || types.isEmpty()) {
throw new IllegalArgumentException("At least one type must be provided");
}
if (types.size() == 1) {
return types.get(0);
}
Annotation[] mergedAnnotations = getMergedAnnotations(types.toArray(new AnnotatedType[types.size()]));
if (types.stream().map(AnnotatedType::getType).allMatch(type -> type.equals(types.get(0).getType()))) {
return GenericTypeReflector.replaceAnnotations(types.get(0), mergedAnnotations);
}
List<Class<?>> classes = types.stream().map(AnnotatedType::getType).map(ClassUtils::getRawType).collect(Collectors.toList());
String typeNames = types.stream().map(type -> type.getType().getTypeName()).sorted().collect(Collectors.joining(","));
if (seenTypeCombos.contains(typeNames)) {
return fallbackOrException(fallback);
}
seenTypeCombos.add(typeNames);
//deal with arrays first as they are special
if (types.stream().allMatch(type -> type instanceof AnnotatedArrayType)) {
List<AnnotatedType> componentTypes = types.stream()
.map(type -> ((AnnotatedArrayType) type).getAnnotatedGenericComponentType())
.collect(Collectors.toList());
AnnotatedType componentType = getCommonSuperType(componentTypes, seenTypeCombos, fallback);
return TypeFactory.arrayOf(componentType, mergedAnnotations);
}
Class<?> commonRawSuperType = getCommonSuperTypes(classes).get(0);
if (classes.stream().noneMatch(ROOT_TYPES::contains) && ROOT_TYPES.contains(commonRawSuperType)) {
return fallbackOrException(fallback);
}
List<AnnotatedType> normalizedTypes = types.stream()
.map(type -> GenericTypeReflector.getExactSuperType(type, commonRawSuperType))
.collect(Collectors.toList());
if (normalizedTypes.stream().anyMatch(type -> GenericTypeReflector.isMissingTypeParameters(type.getType()))) {
throw new TypeMappingException("Automatic type inference failed because some of the types are missing generic type parameter(s).");
}
if (normalizedTypes.stream().allMatch(type -> type.getType() instanceof Class)) {
return annotate(commonRawSuperType, mergedAnnotations);
}
if (normalizedTypes.stream().allMatch(type -> type instanceof AnnotatedParameterizedType)) {
AnnotatedType[] parameters = Arrays.stream(commonRawSuperType.getTypeParameters())
.map(param -> normalizedTypes.stream().map(type -> GenericTypeReflector.getTypeParameter(type, param)).collect(Collectors.toList()))
.map(paramTypes -> getCommonSuperType(paramTypes, seenTypeCombos, fallback))
.toArray(AnnotatedType[]::new);
return TypeFactory.parameterizedAnnotatedClass(commonRawSuperType, mergedAnnotations, parameters);
}
return fallbackOrException(fallback);
}
ClassUtils.java 文件源码
java
阅读 42
收藏 0
点赞 0
评论 0
项目:graphql-spqr
作者:
评论列表
文章目录