Search in sources :

Example 6 with Type

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

the class ConnectionProviderModelValidator method validateConfigLevelConnectionTypes.

private void validateConfigLevelConnectionTypes(Multimap<ConfigurationModel, ConnectionProviderModel> configLevelConnectionProviders, ProblemsReporter problemsReporter) {
    configLevelConnectionProviders.asMap().forEach((configModel, providerModels) -> {
        for (ConnectionProviderModel providerModel : providerModels) {
            Type connectionType = MuleExtensionUtils.getConnectionType(providerModel);
            configModel.getOperationModels().forEach(operationModel -> validateConnectionTypes(providerModel, operationModel, connectionType, problemsReporter));
        }
    });
}
Also used : Type(org.mule.runtime.module.extension.api.loader.java.type.Type) ConnectionProviderModel(org.mule.runtime.api.meta.model.connection.ConnectionProviderModel)

Example 7 with Type

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

the class ConnectionProviderModelValidator method validateGlobalConnectionTypes.

private void validateGlobalConnectionTypes(ExtensionModel extensionModel, Set<ConnectionProviderModel> globalConnectionProviders, ProblemsReporter problemsReporter) {
    if (globalConnectionProviders.isEmpty()) {
        return;
    }
    for (ConnectionProviderModel connectionProviderModel : globalConnectionProviders) {
        final Type connectionType = MuleExtensionUtils.getConnectionType(connectionProviderModel);
        new IdempotentExtensionWalker() {

            @Override
            protected void onOperation(OperationModel operationModel) {
                validateConnectionTypes(connectionProviderModel, operationModel, connectionType, problemsReporter);
            }

            @Override
            protected void onSource(SourceModel sourceModel) {
                validateConnectionTypes(connectionProviderModel, sourceModel, connectionType, problemsReporter);
            }
        }.walk(extensionModel);
    }
}
Also used : SourceModel(org.mule.runtime.api.meta.model.source.SourceModel) Type(org.mule.runtime.module.extension.api.loader.java.type.Type) IdempotentExtensionWalker(org.mule.runtime.api.meta.model.util.IdempotentExtensionWalker) ConnectionProviderModel(org.mule.runtime.api.meta.model.connection.ConnectionProviderModel) OperationModel(org.mule.runtime.api.meta.model.operation.OperationModel)

Example 8 with Type

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

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

the class IntrospectionUtils method toDataType.

/**
 * Transforms a {@link MetadataType} and generates the correspondent {@link DataType}
 *
 * @param metadataType to introspect a create a {@link DataType} from it.
 * @return a {@link DataType} based on the given {@link MetadataType}
 */
public static DataType toDataType(MetadataType metadataType) {
    Class<?> type = getType(metadataType).orElse(null);
    if (type == null) {
        return DataType.fromType(Object.class);
    }
    Reference<DataType> dataType = new Reference<>();
    metadataType.accept(new MetadataTypeVisitor() {

        @Override
        protected void defaultVisit(MetadataType metadataType) {
            dataType.set(DataType.fromType(type));
        }

        @Override
        public void visitArrayType(ArrayType arrayType) {
            Class itemClass = getType(arrayType.getType()).get();
            if (Collection.class.isAssignableFrom(type)) {
                dataType.set(DataType.builder().collectionType((Class<? extends Collection>) type).itemType(itemClass).build());
            } else if (Iterator.class.isAssignableFrom(type)) {
                dataType.set(DataType.builder().streamType((Class<? extends Iterator>) type).itemType(itemClass).build());
            } else {
                defaultVisit(arrayType);
            }
        }

        @Override
        public void visitObject(ObjectType objectType) {
            if (Map.class.isAssignableFrom(type)) {
                dataType.set(DataType.builder().mapType((Class<? extends Map>) type).keyType(String.class).valueType(objectType.getOpenRestriction().map(restriction -> {
                    if (restriction.getAnnotation(TypedValueTypeAnnotation.class).isPresent()) {
                        return TypedValue.class;
                    }
                    return getType(restriction).get();
                }).orElse(Object.class)).build());
            } else {
                defaultVisit(objectType);
            }
        }
    });
    return dataType.get();
}
Also used : FieldElement(org.mule.runtime.module.extension.api.loader.java.type.FieldElement) READ_ONLY(org.mule.runtime.module.extension.api.loader.java.type.PropertyElement.Accessibility.READ_ONLY) ParameterDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.ParameterDeclaration) Message(org.mule.runtime.api.message.Message) Optional.of(java.util.Optional.of) RequireNameField(org.mule.runtime.module.extension.internal.loader.java.property.RequireNameField) ImplementingParameterModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.ImplementingParameterModelProperty) TypeElement(javax.lang.model.element.TypeElement) ClassUtils(org.mule.runtime.core.api.util.ClassUtils) DeclaringMemberModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.DeclaringMemberModelProperty) ArrayUtils.isEmpty(org.apache.commons.lang3.ArrayUtils.isEmpty) Type(org.mule.runtime.module.extension.api.loader.java.type.Type) ArrayType(org.mule.metadata.api.model.ArrayType) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) Objects.isNull(java.util.Objects.isNull) LiteralTypeAnnotation(org.mule.runtime.extension.api.declaration.type.annotation.LiteralTypeAnnotation) ClassTypeLoader(org.mule.metadata.api.ClassTypeLoader) ModelProperty(org.mule.runtime.api.meta.model.ModelProperty) Startable(org.mule.runtime.api.lifecycle.Startable) Collectors.toImmutableList(org.mule.runtime.api.util.collection.Collectors.toImmutableList) ObjectType(org.mule.metadata.api.model.ObjectType) ConnectionProvider(org.mule.runtime.api.connection.ConnectionProvider) Member(java.lang.reflect.Member) Set(java.util.Set) ParameterResolverTypeAnnotation(org.mule.runtime.extension.api.declaration.type.annotation.ParameterResolverTypeAnnotation) Element(javax.lang.model.element.Element) JavaTypeUtils.getId(org.mule.metadata.java.api.utils.JavaTypeUtils.getId) MetadataTypeVisitor(org.mule.metadata.api.visitor.MetadataTypeVisitor) IntrospectionException(java.beans.IntrospectionException) BaseTypeBuilder.create(org.mule.metadata.api.builder.BaseTypeBuilder.create) IllegalConfigurationModelDefinitionException(org.mule.runtime.extension.api.exception.IllegalConfigurationModelDefinitionException) ObjectFieldType(org.mule.metadata.api.model.ObjectFieldType) StringType(org.mule.metadata.api.model.StringType) ParameterResolver(org.mule.runtime.extension.api.runtime.parameter.ParameterResolver) Stream(java.util.stream.Stream) PropertyDescriptor(java.beans.PropertyDescriptor) MetadataType(org.mule.metadata.api.model.MetadataType) InjectedFieldModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.InjectedFieldModelProperty) Expression(org.mule.runtime.extension.api.annotation.Expression) AnnotatedElement(java.lang.reflect.AnnotatedElement) Optional.empty(java.util.Optional.empty) MetadataTypeUtils.isObjectType(org.mule.metadata.api.utils.MetadataTypeUtils.isObjectType) AnyType(org.mule.metadata.api.model.AnyType) ConnectionProviderModel(org.mule.runtime.api.meta.model.connection.ConnectionProviderModel) VariableElement(javax.lang.model.element.VariableElement) Preconditions.checkArgument(org.mule.runtime.api.util.Preconditions.checkArgument) Modifier.isStatic(java.lang.reflect.Modifier.isStatic) Supplier(java.util.function.Supplier) BaseTypeBuilder(org.mule.metadata.api.builder.BaseTypeBuilder) ArrayList(java.util.ArrayList) Introspector(java.beans.Introspector) DeclaredType(javax.lang.model.type.DeclaredType) ParameterGroup(org.mule.runtime.extension.api.annotation.param.ParameterGroup) LinkedHashSet(java.util.LinkedHashSet) Disposable(org.mule.runtime.api.lifecycle.Disposable) ParameterizedModel(org.mule.runtime.api.meta.model.parameter.ParameterizedModel) DataType(org.mule.runtime.api.metadata.DataType) Field(java.lang.reflect.Field) ExtensionModel(org.mule.runtime.api.meta.model.ExtensionModel) TypedValue(org.mule.runtime.api.metadata.TypedValue) ExtensionMetadataTypeUtils.getType(org.mule.runtime.extension.api.util.ExtensionMetadataTypeUtils.getType) Modifier.isPublic(java.lang.reflect.Modifier.isPublic) ParameterizedType(java.lang.reflect.ParameterizedType) Stoppable(org.mule.runtime.api.lifecycle.Stoppable) ExtensionWalker(org.mule.runtime.api.meta.model.util.ExtensionWalker) Reference(org.mule.runtime.api.util.Reference) RefName(org.mule.runtime.extension.api.annotation.param.RefName) CollectionUtils.isEmpty(org.apache.commons.collections.CollectionUtils.isEmpty) ReflectionUtils(org.reflections.ReflectionUtils) ReflectionUtils.getAllFields(org.reflections.ReflectionUtils.getAllFields) SubTypesModel(org.mule.runtime.api.meta.model.SubTypesModel) HasOutputModel(org.mule.runtime.api.meta.model.HasOutputModel) TargetModelProperty(org.mule.runtime.extension.internal.property.TargetModelProperty) NamedObject(org.mule.runtime.api.meta.NamedObject) Source(org.mule.runtime.extension.api.runtime.source.Source) VoidType(org.mule.metadata.api.model.VoidType) MetadataTypeUtils.isEnum(org.mule.metadata.api.utils.MetadataTypeUtils.isEnum) DefaultEncodingModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.DefaultEncodingModelProperty) ParameterGroupModel(org.mule.runtime.api.meta.model.parameter.ParameterGroupModel) SUPPORTED(org.mule.runtime.api.meta.ExpressionSupport.SUPPORTED) ResolvableType(org.springframework.core.ResolvableType) Pair(org.mule.runtime.api.util.Pair) Method(java.lang.reflect.Method) Collectors.toSet(java.util.stream.Collectors.toSet) Collections.reverse(java.util.Collections.reverse) Collections.emptyList(java.util.Collections.emptyList) Collection(java.util.Collection) ClassInformationAnnotation(org.mule.metadata.java.api.annotation.ClassInformationAnnotation) Types(javax.lang.model.util.Types) String.format(java.lang.String.format) List(java.util.List) Modifier(java.lang.reflect.Modifier) Annotation(java.lang.annotation.Annotation) Optional(java.util.Optional) TypeVariable(javax.lang.model.type.TypeVariable) EnrichableModel(org.mule.runtime.api.meta.model.EnrichableModel) Arrays.stream(java.util.Arrays.stream) IllegalModelDefinitionException(org.mule.runtime.extension.api.exception.IllegalModelDefinitionException) MessageMetadataTypeBuilder(org.mule.metadata.message.api.MessageMetadataTypeBuilder) ParameterModel(org.mule.runtime.api.meta.model.parameter.ParameterModel) Alias(org.mule.runtime.extension.api.annotation.Alias) TypeGeneric(org.mule.runtime.module.extension.api.loader.java.type.TypeGeneric) ComponentModel(org.mule.runtime.api.meta.model.ComponentModel) JAVA(org.mule.metadata.api.model.MetadataFormat.JAVA) MetadataKeyId(org.mule.runtime.extension.api.annotation.metadata.MetadataKeyId) Ignore(org.mule.runtime.extension.api.annotation.Ignore) HashSet(java.util.HashSet) Collectors.toCollection(java.util.stream.Collectors.toCollection) MethodElement(org.mule.runtime.module.extension.api.loader.java.type.MethodElement) ImmutableList(com.google.common.collect.ImmutableList) Literal(org.mule.runtime.extension.api.runtime.parameter.Literal) ParameterGroupModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.ParameterGroupModelProperty) LinkedList(java.util.LinkedList) Parameter(org.mule.runtime.extension.api.annotation.param.Parameter) TypedValueTypeAnnotation(org.mule.runtime.extension.api.declaration.type.annotation.TypedValueTypeAnnotation) AccessibleObject(java.lang.reflect.AccessibleObject) Iterator(java.util.Iterator) ElementKind(javax.lang.model.element.ElementKind) PUBLIC(javax.lang.model.element.Modifier.PUBLIC) ExpressionSupport(org.mule.runtime.api.meta.ExpressionSupport) ExecutableElement(javax.lang.model.element.ExecutableElement) READ_WRITE(org.mule.runtime.module.extension.api.loader.java.type.PropertyElement.Accessibility.READ_WRITE) Initialisable(org.mule.runtime.api.lifecycle.Initialisable) Result(org.mule.runtime.extension.api.runtime.operation.Result) DefaultEncoding(org.mule.runtime.extension.api.annotation.param.DefaultEncoding) Collectors.toList(java.util.stream.Collectors.toList) TypeMirror(javax.lang.model.type.TypeMirror) BaseDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.BaseDeclaration) METHOD(javax.lang.model.element.ElementKind.METHOD) PagingProvider(org.mule.runtime.extension.api.runtime.streaming.PagingProvider) ProcessingEnvironment(javax.annotation.processing.ProcessingEnvironment) ComponentLocation(org.mule.runtime.api.component.location.ComponentLocation) MuleExtensionAnnotationParser(org.mule.runtime.module.extension.internal.loader.java.MuleExtensionAnnotationParser) ImplementingTypeModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.ImplementingTypeModelProperty) Reference(org.mule.runtime.api.util.Reference) MetadataType(org.mule.metadata.api.model.MetadataType) MetadataTypeVisitor(org.mule.metadata.api.visitor.MetadataTypeVisitor) ArrayType(org.mule.metadata.api.model.ArrayType) ObjectType(org.mule.metadata.api.model.ObjectType) MetadataTypeUtils.isObjectType(org.mule.metadata.api.utils.MetadataTypeUtils.isObjectType) Iterator(java.util.Iterator) DataType(org.mule.runtime.api.metadata.DataType) Collection(java.util.Collection) Collectors.toCollection(java.util.stream.Collectors.toCollection) Map(java.util.Map)

Example 10 with Type

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

Type (org.mule.runtime.module.extension.api.loader.java.type.Type)26 MetadataType (org.mule.metadata.api.model.MetadataType)20 ObjectType (org.mule.metadata.api.model.ObjectType)16 ArrayType (org.mule.metadata.api.model.ArrayType)15 AnyType (org.mule.metadata.api.model.AnyType)13 StringType (org.mule.metadata.api.model.StringType)11 VoidType (org.mule.metadata.api.model.VoidType)11 DataType (org.mule.runtime.api.metadata.DataType)11 ResolvableType (org.springframework.core.ResolvableType)11 ObjectFieldType (org.mule.metadata.api.model.ObjectFieldType)9 TypeGeneric (org.mule.runtime.module.extension.api.loader.java.type.TypeGeneric)9 ExtensionTypeDescriptorModelProperty (org.mule.runtime.module.extension.internal.loader.java.type.property.ExtensionTypeDescriptorModelProperty)9 ParameterizedType (java.lang.reflect.ParameterizedType)8 Optional (java.util.Optional)8 DeclaredType (javax.lang.model.type.DeclaredType)8 MetadataTypeUtils.isObjectType (org.mule.metadata.api.utils.MetadataTypeUtils.isObjectType)8 IllegalModelDefinitionException (org.mule.runtime.extension.api.exception.IllegalModelDefinitionException)8 String.format (java.lang.String.format)7 Field (java.lang.reflect.Field)6 Map (java.util.Map)6