Union.java 文件源码

java
阅读 61 收藏 0 点赞 0 评论 0

项目:graphql-spqr 作者:
public static AnnotatedType unionize(AnnotatedType[] types) {
    Objects.requireNonNull(types);
    if (types.length < 2) {
        if (types.length == 1 && GenericTypeReflector.isSuperType(Union.class, types[0].getType())) {
            return types[0];
        }
        throw new IllegalArgumentException(SINGLE_TYPE_UNION_ERROR);
    }
    AnnotatedType t1 = types[0];
    if (stream(types).anyMatch(t -> t.isAnnotationPresent(GraphQLUnion.class))) {
        if (stream(types).allMatch(t -> t.isAnnotationPresent(GraphQLUnion.class) &&
                t.getAnnotation(GraphQLUnion.class).name().equals(t1.getAnnotation(GraphQLUnion.class).name()))) {
            return of(types);
        } else {
            throw new IllegalArgumentException("All union members must be explicitly annotated: " + Arrays.toString(types));
        }
    }
    if (stream(types).allMatch(t -> t instanceof AnnotatedParameterizedType)) {
        AnnotatedParameterizedType p1 = (AnnotatedParameterizedType) t1;
        AnnotatedParameterizedType[] pTypes = stream(types)
                .map(t -> (AnnotatedParameterizedType) t)
                .toArray(AnnotatedParameterizedType[]::new);
        AnnotatedType[] params = new AnnotatedType[p1.getAnnotatedActualTypeArguments().length];
        for (int i = 0; i < p1.getAnnotatedActualTypeArguments().length; i++) {
            final int j = i;
            params[i] = unionize(stream(pTypes)
                    .map(p -> p.getAnnotatedActualTypeArguments()[j])
                    .toArray(AnnotatedType[]::new));
        }
        Class<?> rawType = ((Class<?>) ((ParameterizedType) p1.getType()).getRawType());
        return TypeFactory.parameterizedAnnotatedClass(rawType, ClassUtils.getAllAnnotations(stream(types)), params);
    }
    if (stream(types).allMatch(t -> t instanceof AnnotatedArrayType)) {
        AnnotatedType[] components = stream(types)
                .map(type -> ((AnnotatedArrayType) type).getAnnotatedGenericComponentType())
                .toArray(AnnotatedType[]::new);
        return TypeFactory.arrayOf(unionize(components), ClassUtils.getAllAnnotations(stream(types)));
    }
    if (stream(types).allMatch(t -> types[0].getType().equals(t.getType()))) {
        return types[0];
    }
    throw new IllegalArgumentException("Types are incompatible and can not be unionized: ");
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号