Search in sources :

Example 1 with VoidType

use of org.mule.metadata.api.model.VoidType in project mule by mulesoft.

the class IntrospectionUtils method getMethodReturnAttributesType.

/**
 * Returns a {@link MetadataType} representing the {@link Result#getAttributes()} that will be set after executing the given
 * {@code method}.
 * <p>
 * If the {@code method} returns a {@link Result}, then it returns the type of the {@code Attributes} generic. In any other case
 * (including raw uses of {@link Result}) it will return a {@link VoidType}
 * <p>
 * If the {@code method} returns a collection or a {@link PagingProvider} of {@link Result}, then this will return
 * {@link VoidType} since the messages in the main output already contain an attributes for each item.
 *
 * @param method the {@link Method} being introspected
 * @return a {@link MetadataType}
 * @throws IllegalArgumentException is method is {@code null}
 */
public static MetadataType getMethodReturnAttributesType(MethodElement method) {
    Type returnType = method.getReturnType();
    Type attributesType = null;
    if (returnType.isAssignableTo(Result.class)) {
        List<TypeGeneric> generics = returnType.getGenerics();
        if (generics.size() == 2) {
            Type genericType = generics.get(1).getConcreteType();
            if (genericType != null) {
                if (genericType.isAnyType()) {
                    return typeBuilder().anyType().build();
                } else {
                    attributesType = genericType;
                }
            } else {
                return typeBuilder().anyType().build();
            }
        } else {
            return typeBuilder().anyType().build();
        }
    }
    if (isPagingProvider(returnType)) {
        Type second = getPagingProviderTypes(returnType).getSecond();
        if (second.isSameType(Result.class)) {
            attributesType = null;
        }
    }
    if (isCollection(returnType)) {
        List<TypeGeneric> generics = returnType.getGenerics();
        if (generics.size() > 0) {
            Type genericType = generics.get(0).getConcreteType();
            if (genericType.isAssignableTo(Result.class)) {
                attributesType = null;
            }
        }
    }
    return attributesType != null ? attributesType.asMetadataType() : typeBuilder().voidType().build();
}
Also used : Type(org.mule.runtime.module.extension.api.loader.java.type.Type) ArrayType(org.mule.metadata.api.model.ArrayType) ObjectType(org.mule.metadata.api.model.ObjectType) ObjectFieldType(org.mule.metadata.api.model.ObjectFieldType) StringType(org.mule.metadata.api.model.StringType) MetadataType(org.mule.metadata.api.model.MetadataType) MetadataTypeUtils.isObjectType(org.mule.metadata.api.utils.MetadataTypeUtils.isObjectType) AnyType(org.mule.metadata.api.model.AnyType) DeclaredType(javax.lang.model.type.DeclaredType) DataType(org.mule.runtime.api.metadata.DataType) ExtensionMetadataTypeUtils.getType(org.mule.runtime.extension.api.util.ExtensionMetadataTypeUtils.getType) ParameterizedType(java.lang.reflect.ParameterizedType) VoidType(org.mule.metadata.api.model.VoidType) ResolvableType(org.springframework.core.ResolvableType) TypeGeneric(org.mule.runtime.module.extension.api.loader.java.type.TypeGeneric)

Aggregations

ParameterizedType (java.lang.reflect.ParameterizedType)1 DeclaredType (javax.lang.model.type.DeclaredType)1 AnyType (org.mule.metadata.api.model.AnyType)1 ArrayType (org.mule.metadata.api.model.ArrayType)1 MetadataType (org.mule.metadata.api.model.MetadataType)1 ObjectFieldType (org.mule.metadata.api.model.ObjectFieldType)1 ObjectType (org.mule.metadata.api.model.ObjectType)1 StringType (org.mule.metadata.api.model.StringType)1 VoidType (org.mule.metadata.api.model.VoidType)1 MetadataTypeUtils.isObjectType (org.mule.metadata.api.utils.MetadataTypeUtils.isObjectType)1 DataType (org.mule.runtime.api.metadata.DataType)1 ExtensionMetadataTypeUtils.getType (org.mule.runtime.extension.api.util.ExtensionMetadataTypeUtils.getType)1 Type (org.mule.runtime.module.extension.api.loader.java.type.Type)1 TypeGeneric (org.mule.runtime.module.extension.api.loader.java.type.TypeGeneric)1 ResolvableType (org.springframework.core.ResolvableType)1