All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.smallrye.graphql.schema.creator.WrapperCreator Maven / Gradle / Ivy

package io.smallrye.graphql.schema.creator;

import java.util.Optional;

import org.jboss.jandex.Type;

import io.smallrye.graphql.schema.Annotations;
import io.smallrye.graphql.schema.Classes;
import io.smallrye.graphql.schema.helper.NonNullHelper;
import io.smallrye.graphql.schema.model.Wrapper;
import io.smallrye.graphql.schema.model.WrapperType;

/**
 * Helper with detecting if this field is in a wrapper
 *
 * If it is we create an WrapperInfo model that contains the relevant information
 *
 * @author Phillip Kruger ([email protected])
 */
public class WrapperCreator {

    private WrapperCreator() {
    }

    public static Optional createWrapper(Type type) {
        return createWrapper(null, type);
    }

    /**
     * Create a Wrapper for a Field (that has properties and methods)
     *
     * @param fieldType the java field type
     * @param methodType the java method type
     * @return optional array
     */
    public static Optional createWrapper(Type fieldType, Type methodType) {
        if (Classes.isWrapper(methodType)) {
            Wrapper wrapper = new Wrapper(getWrapperType(methodType), methodType.name().toString());
            // NotNull
            if (markParameterizedTypeNonNull(fieldType, methodType)) {
                wrapper.setWrappedTypeNotNull(true);
            }
            // Wrapper of wrapper
            Optional wrapperOfWrapper = getWrapperOfWrapper(methodType);
            if (wrapperOfWrapper.isPresent()) {
                wrapper.setWrapper(wrapperOfWrapper.get());
            }

            return Optional.of(wrapper);
        }
        return Optional.empty();
    }

    private static WrapperType getWrapperType(Type type) {
        if (Classes.isOptional(type)) {
            return WrapperType.OPTIONAL;
        } else if (Classes.isArray(type)) {
            return WrapperType.ARRAY;
        } else if (Classes.isCollection(type)) {
            return WrapperType.COLLECTION;
        } else if (Classes.isMap(type)) {
            return WrapperType.MAP;
        } else if (Classes.isParameterized(type)) {
            return WrapperType.UNKNOWN;
        }
        return null;
    }

    private static Optional getWrapperOfWrapper(Type type) {
        if (Classes.isArray(type)) {
            Type typeInArray = type.asArrayType().component();
            return createWrapper(typeInArray);
        } else if (Classes.isParameterized(type)) {
            Type typeInCollection = type.asParameterizedType().arguments().get(0);
            return createWrapper(typeInCollection);
        }
        return Optional.empty();
    }

    private static boolean markParameterizedTypeNonNull(Type fieldType, Type methodType) {
        if (fieldType == null)
            fieldType = methodType;
        if (Classes.isWrapper(fieldType)) {
            Type typeInCollection = getTypeInCollection(fieldType);
            Type methodTypeInCollection = getTypeInCollection(methodType);
            Annotations annotationsInParameterizedType = Annotations.getAnnotationsForArray(typeInCollection,
                    methodTypeInCollection);

            return NonNullHelper.markAsNonNull(typeInCollection, annotationsInParameterizedType, false);
        }
        return false;
    }

    private static Type getTypeInCollection(Type type) {
        if (Classes.isArray(type)) {
            return type.asArrayType().componentType();
        } else if (Classes.isParameterized(type)) {
            return type.asParameterizedType().arguments().get(0);
        }
        return type;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy