Search in sources :

Example 1 with TypeGeneric

use of org.mule.runtime.module.extension.api.loader.java.type.TypeGeneric in project mule by mulesoft.

the class IntrospectionUtils method getReturnType.

private static MetadataType getReturnType(Type returnType) {
    Type type = returnType;
    if (returnType.isAssignableTo(Result.class)) {
        List<TypeGeneric> generics = returnType.getGenerics();
        if (generics.isEmpty()) {
            return typeBuilder().anyType().build();
        }
        Type payloadType = generics.get(0).getConcreteType();
        if (!payloadType.isAnyType()) {
            type = payloadType;
        } else {
            type = null;
        }
    }
    if (isPagingProvider(returnType)) {
        Type itemType = getPagingProviderTypes(returnType).getSecond();
        if (itemType.isSameType(Result.class)) {
            return returnListOfMessagesType(returnType, itemType);
        } else {
            return typeBuilder().arrayType().of(itemType.asMetadataType()).with(returnType.getClassInformation()).build();
        }
    }
    if (returnType.isAssignableTo(ParameterResolver.class) || returnType.isAssignableTo(TypedValue.class) || returnType.isAssignableTo(Literal.class)) {
        type = returnType.getGenerics().get(0).getConcreteType();
    }
    if (isCollection(returnType) && !returnType.getGenerics().isEmpty()) {
        Type itemType = returnType.getGenerics().get(0).getConcreteType();
        if (itemType.isAssignableTo(Result.class)) {
            return returnListOfMessagesType(returnType, itemType);
        }
    }
    return type != null ? type.asMetadataType() : typeBuilder().anyType().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) Literal(org.mule.runtime.extension.api.runtime.parameter.Literal)

Example 2 with TypeGeneric

use of org.mule.runtime.module.extension.api.loader.java.type.TypeGeneric 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)

Example 3 with TypeGeneric

use of org.mule.runtime.module.extension.api.loader.java.type.TypeGeneric in project mule by mulesoft.

the class IntrospectionUtils method returnListOfMessagesType.

private static MetadataType returnListOfMessagesType(Type returnType, Type resultType) {
    if (resultType.getGenerics().isEmpty()) {
        AnyType anyType = typeBuilder().anyType().build();
        return getListOfMessageType(returnType, anyType, anyType);
    } else {
        TypeGeneric genericType = resultType.getGenerics().get(0);
        Type payloadType = genericType.getConcreteType();
        MetadataType outputType;
        if (payloadType.isAnyType()) {
            outputType = typeBuilder().anyType().build();
        } else {
            if (payloadType.isAssignableTo(TypedValue.class)) {
                payloadType = payloadType.getGenerics().get(0).getConcreteType();
            }
            outputType = payloadType.asMetadataType();
        }
        Type attributesType = resultType.getGenerics().get(1).getConcreteType();
        MetadataType attributesOutputType = attributesType.isAnyType() ? typeBuilder().anyType().build() : attributesType.asMetadataType();
        return getListOfMessageType(returnType, outputType, attributesOutputType);
    }
}
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) MetadataType(org.mule.metadata.api.model.MetadataType) AnyType(org.mule.metadata.api.model.AnyType)

Example 4 with TypeGeneric

use of org.mule.runtime.module.extension.api.loader.java.type.TypeGeneric in project mule by mulesoft.

the class OperationModelLoaderDelegate method processPagingTx.

private void processPagingTx(OperationDeclarer operation, MethodElement method) {
    checkArgument(method != null, "Can't introspect a null method");
    Type returnTypeElement = method.getReturnType();
    List<TypeGeneric> generics = returnTypeElement.getGenerics();
    if (!generics.isEmpty()) {
        operation.transactional(generics.get(0).getConcreteType().isAssignableTo(TransactionalConnection.class));
    } else {
        operation.transactional(false);
    }
}
Also used : AnyType(org.mule.metadata.api.model.AnyType) Type(org.mule.runtime.module.extension.api.loader.java.type.Type) MetadataType(org.mule.metadata.api.model.MetadataType) TransactionalConnection(org.mule.runtime.extension.api.connectivity.TransactionalConnection) TypeGeneric(org.mule.runtime.module.extension.api.loader.java.type.TypeGeneric)

Aggregations

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