Search in sources :

Example 6 with ImplementingTypeModelProperty

use of org.mule.runtime.module.extension.internal.loader.java.property.ImplementingTypeModelProperty in project mule by mulesoft.

the class DisplayDeclarationEnricher method enrichTypes.

private void enrichTypes(BaseDeclaration declaration) {
    final Optional<ImplementingTypeModelProperty> modelProperty = declaration.getModelProperty(ImplementingTypeModelProperty.class);
    if (modelProperty.isPresent()) {
        final Class<?> annotatedType = modelProperty.get().getType();
        final Summary summaryAnnotation = getAnnotation(annotatedType, Summary.class);
        final DisplayName displayNameAnnotation = getAnnotation(annotatedType, DisplayName.class);
        final Example exampleAnnotation = getAnnotation(annotatedType, Example.class);
        final Path pathAnnotation = getAnnotation(annotatedType, Path.class);
        final ClassValue classAnnotation = getAnnotation(annotatedType, ClassValue.class);
        createDisplayModelProperty(declaration, summaryAnnotation, displayNameAnnotation, exampleAnnotation, pathAnnotation, classAnnotation);
    }
}
Also used : Path(org.mule.runtime.extension.api.annotation.param.display.Path) ClassValue(org.mule.runtime.extension.api.annotation.param.display.ClassValue) Example(org.mule.runtime.extension.api.annotation.param.display.Example) DisplayName(org.mule.runtime.extension.api.annotation.param.display.DisplayName) Summary(org.mule.runtime.extension.api.annotation.param.display.Summary) ImplementingTypeModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.ImplementingTypeModelProperty)

Example 7 with ImplementingTypeModelProperty

use of org.mule.runtime.module.extension.internal.loader.java.property.ImplementingTypeModelProperty in project mule by mulesoft.

the class InjectedFieldsModelValidator method validate.

@Override
public void validate(ExtensionModel extensionModel, ProblemsReporter problemsReporter) {
    final Set<Class<?>> validatedTypes = new HashSet<>();
    // TODO - MULE-14401 - Make InjectedFieldsModelValidator work in AST Mode
    Boolean isASTMode = !extensionModel.getModelProperty(ExtensionTypeDescriptorModelProperty.class).map(mp -> mp.getType().getDeclaringClass().isPresent()).orElse(false);
    if (!isASTMode) {
        extensionModel.getModelProperty(ClassLoaderModelProperty.class).ifPresent(classLoaderModelProperty -> {
            new ExtensionWalker() {

                @Override
                protected void onSource(HasSourceModels owner, SourceModel model) {
                    validateFields(model, model.getModelProperty(ImplementingTypeModelProperty.class), DefaultEncoding.class);
                }

                @Override
                protected void onConfiguration(ConfigurationModel model) {
                    validateFields(model, model.getModelProperty(ImplementingTypeModelProperty.class), DefaultEncoding.class);
                    validateFields(model, model.getModelProperty(ImplementingTypeModelProperty.class), RefName.class);
                }

                @Override
                protected void onOperation(HasOperationModels owner, OperationModel model) {
                    validateArguments(model, model.getModelProperty(ExtensionOperationDescriptorModelProperty.class), DefaultEncoding.class);
                }

                @Override
                protected void onConnectionProvider(HasConnectionProviderModels owner, ConnectionProviderModel model) {
                    validateFields(model, model.getModelProperty(ImplementingTypeModelProperty.class), DefaultEncoding.class);
                    validateFields(model, model.getModelProperty(ImplementingTypeModelProperty.class), RefName.class);
                }

                @Override
                protected void onParameter(ParameterizedModel owner, ParameterGroupModel groupModel, ParameterModel model) {
                    if (model.getType().getMetadataFormat().equals(JAVA)) {
                        model.getType().accept(new MetadataTypeVisitor() {

                            @Override
                            public void visitObject(ObjectType objectType) {
                                if (!objectType.getAnnotation(InfrastructureTypeAnnotation.class).isPresent()) {
                                    try {
                                        Class<?> type = getType(objectType, classLoaderModelProperty.getClassLoader());
                                        if (validatedTypes.add(type)) {
                                            validateType(model, type, DefaultEncoding.class);
                                        }
                                    } catch (Exception e) {
                                        problemsReporter.addWarning(new Problem(model, "Could not validate Class: " + e.getMessage()));
                                    }
                                }
                            }
                        });
                    }
                }

                private void validateArguments(NamedObject model, Optional<ExtensionOperationDescriptorModelProperty> modelProperty, Class<? extends Annotation> annotationClass) {
                    modelProperty.ifPresent(operationDescriptorModelProperty -> {
                        MethodElement operation = operationDescriptorModelProperty.getOperationMethod();
                        int size = operation.getParametersAnnotatedWith(annotationClass).size();
                        if (size == 0) {
                            return;
                        } else if (size > 1) {
                            problemsReporter.addError(new Problem(model, format("Operation method '%s' has %d arguments annotated with @%s. Only one argument may carry that annotation", operation.getName(), size, annotationClass.getSimpleName())));
                        }
                        ExtensionParameter argument = operation.getParametersAnnotatedWith(annotationClass).get(0);
                        if (!argument.getType().isSameType(String.class)) {
                            problemsReporter.addError(new Problem(model, format("Operation method '%s' declares an argument '%s' which is annotated with @%s and is of type '%s'. Only " + "arguments of type String are allowed to carry such annotation", operation.getName(), argument.getName(), annotationClass.getSimpleName(), argument.getType().getName())));
                        }
                    });
                }

                private void validateFields(NamedObject model, Optional<ImplementingTypeModelProperty> modelProperty, Class<? extends Annotation> annotationClass) {
                    modelProperty.ifPresent(implementingTypeModelProperty -> {
                        validateType(model, implementingTypeModelProperty.getType(), annotationClass);
                    });
                }

                private void validateType(NamedObject model, Class<?> type, Class<? extends Annotation> annotationClass) {
                    List<Field> fields = getAnnotatedFields(type, annotationClass);
                    if (fields.isEmpty()) {
                        return;
                    } else if (fields.size() > 1) {
                        problemsReporter.addError(new Problem(model, format("Class '%s' has %d fields annotated with @%s. Only one field may carry that annotation", type.getName(), fields.size(), annotationClass.getSimpleName())));
                    }
                    Field field = fields.get(0);
                    if (!String.class.equals(field.getType())) {
                        problemsReporter.addError(new Problem(model, format("Class '%s' declares the field '%s' which is annotated with @%s and is of type '%s'. Only " + "fields of type String are allowed to carry such annotation", type.getName(), field.getName(), annotationClass.getSimpleName(), field.getType().getName())));
                    }
                }
            }.walk(extensionModel);
        });
    }
}
Also used : ParameterModel(org.mule.runtime.api.meta.model.parameter.ParameterModel) IntrospectionUtils.getAnnotatedFields(org.mule.runtime.module.extension.internal.util.IntrospectionUtils.getAnnotatedFields) OperationModel(org.mule.runtime.api.meta.model.operation.OperationModel) ConnectionProviderModel(org.mule.runtime.api.meta.model.connection.ConnectionProviderModel) NamedObject(org.mule.runtime.api.meta.NamedObject) ExtensionTypeDescriptorModelProperty(org.mule.runtime.module.extension.internal.loader.java.type.property.ExtensionTypeDescriptorModelProperty) JAVA(org.mule.metadata.api.model.MetadataFormat.JAVA) HashSet(java.util.HashSet) InfrastructureTypeAnnotation(org.mule.runtime.extension.api.declaration.type.annotation.InfrastructureTypeAnnotation) SourceModel(org.mule.runtime.api.meta.model.source.SourceModel) MethodElement(org.mule.runtime.module.extension.api.loader.java.type.MethodElement) ParameterGroupModel(org.mule.runtime.api.meta.model.parameter.ParameterGroupModel) Problem(org.mule.runtime.extension.api.loader.Problem) HasConnectionProviderModels(org.mule.runtime.api.meta.model.connection.HasConnectionProviderModels) ExtensionModelValidator(org.mule.runtime.extension.api.loader.ExtensionModelValidator) ObjectType(org.mule.metadata.api.model.ObjectType) ParameterizedModel(org.mule.runtime.api.meta.model.parameter.ParameterizedModel) ProblemsReporter(org.mule.runtime.extension.api.loader.ProblemsReporter) Set(java.util.Set) ConfigurationModel(org.mule.runtime.api.meta.model.config.ConfigurationModel) ClassLoaderModelProperty(org.mule.runtime.extension.api.property.ClassLoaderModelProperty) Field(java.lang.reflect.Field) String.format(java.lang.String.format) MetadataTypeVisitor(org.mule.metadata.api.visitor.MetadataTypeVisitor) ExtensionModel(org.mule.runtime.api.meta.model.ExtensionModel) DefaultEncoding(org.mule.runtime.extension.api.annotation.param.DefaultEncoding) List(java.util.List) HasOperationModels(org.mule.runtime.api.meta.model.operation.HasOperationModels) ExtensionWalker(org.mule.runtime.api.meta.model.util.ExtensionWalker) RefName(org.mule.runtime.extension.api.annotation.param.RefName) Annotation(java.lang.annotation.Annotation) Optional(java.util.Optional) ExtensionParameter(org.mule.runtime.module.extension.api.loader.java.type.ExtensionParameter) ImplementingTypeModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.ImplementingTypeModelProperty) JavaTypeUtils.getType(org.mule.metadata.java.api.utils.JavaTypeUtils.getType) HasSourceModels(org.mule.runtime.api.meta.model.source.HasSourceModels) ExtensionOperationDescriptorModelProperty(org.mule.runtime.module.extension.internal.loader.java.type.property.ExtensionOperationDescriptorModelProperty) ExtensionOperationDescriptorModelProperty(org.mule.runtime.module.extension.internal.loader.java.type.property.ExtensionOperationDescriptorModelProperty) ExtensionParameter(org.mule.runtime.module.extension.api.loader.java.type.ExtensionParameter) RefName(org.mule.runtime.extension.api.annotation.param.RefName) ExtensionWalker(org.mule.runtime.api.meta.model.util.ExtensionWalker) MethodElement(org.mule.runtime.module.extension.api.loader.java.type.MethodElement) MetadataTypeVisitor(org.mule.metadata.api.visitor.MetadataTypeVisitor) ObjectType(org.mule.metadata.api.model.ObjectType) Field(java.lang.reflect.Field) List(java.util.List) ConnectionProviderModel(org.mule.runtime.api.meta.model.connection.ConnectionProviderModel) HashSet(java.util.HashSet) ClassLoaderModelProperty(org.mule.runtime.extension.api.property.ClassLoaderModelProperty) HasOperationModels(org.mule.runtime.api.meta.model.operation.HasOperationModels) ConfigurationModel(org.mule.runtime.api.meta.model.config.ConfigurationModel) ExtensionTypeDescriptorModelProperty(org.mule.runtime.module.extension.internal.loader.java.type.property.ExtensionTypeDescriptorModelProperty) NamedObject(org.mule.runtime.api.meta.NamedObject) HasConnectionProviderModels(org.mule.runtime.api.meta.model.connection.HasConnectionProviderModels) SourceModel(org.mule.runtime.api.meta.model.source.SourceModel) ParameterModel(org.mule.runtime.api.meta.model.parameter.ParameterModel) ParameterizedModel(org.mule.runtime.api.meta.model.parameter.ParameterizedModel) ParameterGroupModel(org.mule.runtime.api.meta.model.parameter.ParameterGroupModel) Problem(org.mule.runtime.extension.api.loader.Problem) HasSourceModels(org.mule.runtime.api.meta.model.source.HasSourceModels) DefaultEncoding(org.mule.runtime.extension.api.annotation.param.DefaultEncoding) OperationModel(org.mule.runtime.api.meta.model.operation.OperationModel) ImplementingTypeModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.ImplementingTypeModelProperty)

Example 8 with ImplementingTypeModelProperty

use of org.mule.runtime.module.extension.internal.loader.java.property.ImplementingTypeModelProperty in project mule by mulesoft.

the class RefNameDeclarationEnricherTestCase method mockImplementingProperty.

private void mockImplementingProperty(BaseDeclaration declaration, Class<?> type) {
    ImplementingTypeModelProperty property = type != null ? new ImplementingTypeModelProperty(type) : null;
    when(declaration.getModelProperty(ImplementingTypeModelProperty.class)).thenReturn(Optional.ofNullable(property));
}
Also used : ImplementingTypeModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.ImplementingTypeModelProperty)

Example 9 with ImplementingTypeModelProperty

use of org.mule.runtime.module.extension.internal.loader.java.property.ImplementingTypeModelProperty in project mule by mulesoft.

the class JavaDeclarationDelegateTestCase method assertHeisenbergSource.

private void assertHeisenbergSource(SourceDeclaration source, String sourceName, Class<? extends Source> type) {
    assertThat(source.getName(), is(sourceName));
    List<ParameterDeclaration> parameters = source.getAllParameters();
    assertThat(parameters, hasSize(31));
    assertParameter(parameters, SOURCE_PARAMETER, "", INT_TYPE, true, NOT_SUPPORTED, null);
    assertParameter(parameters, SOURCE_CALLBACK_PARAMETER, "", toMetadataType(Long.class), false, SUPPORTED, "#[payload]");
    assertParameter(parameters, SOURCE_REPEATED_CALLBACK_PARAMETER, "", STRING_TYPE, false, SUPPORTED, null);
    assertParameter(parameters, "methylamine", "", toMetadataType(Methylamine.class), false, SUPPORTED, null);
    ImplementingTypeModelProperty typeModelProperty = source.getModelProperty(ImplementingTypeModelProperty.class).get();
    assertThat(typeModelProperty.getType(), equalTo(type));
}
Also used : Methylamine(org.mule.test.heisenberg.extension.model.Methylamine) ParameterDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.ParameterDeclaration) ImplementingTypeModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.ImplementingTypeModelProperty)

Example 10 with ImplementingTypeModelProperty

use of org.mule.runtime.module.extension.internal.loader.java.property.ImplementingTypeModelProperty in project mule by mulesoft.

the class SoapModelLoaderDelegate method getConfigDeclarer.

private ConfigurationDeclarer getConfigDeclarer(ExtensionDeclarer declarer, SoapExtensionTypeWrapper<?> extension, Set<ErrorModel> soapErrors) {
    // TODO - MULE-14311 - Make loader work in compile time
    Class<?> clazz = extension.getDeclaringClass().get();
    TypeAwareConfigurationFactory configurationFactory = new TypeAwareConfigurationFactory(clazz, clazz.getClassLoader());
    ConfigurationDeclarer configDeclarer = declarer.withConfig(DEFAULT_CONFIG_NAME).describedAs(DEFAULT_CONFIG_DESCRIPTION).withModelProperty(new ConfigurationFactoryModelProperty(configurationFactory)).withModelProperty(new ImplementingTypeModelProperty(clazz));
    operationDeclarer.declare(configDeclarer, typeLoader, soapErrors);
    return configDeclarer;
}
Also used : ConfigurationDeclarer(org.mule.runtime.api.meta.model.declaration.fluent.ConfigurationDeclarer) TypeAwareConfigurationFactory(org.mule.runtime.module.extension.internal.loader.java.TypeAwareConfigurationFactory) ConfigurationFactoryModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.ConfigurationFactoryModelProperty) ImplementingTypeModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.ImplementingTypeModelProperty)

Aggregations

ImplementingTypeModelProperty (org.mule.runtime.module.extension.internal.loader.java.property.ImplementingTypeModelProperty)15 ExtensionTypeDescriptorModelProperty (org.mule.runtime.module.extension.internal.loader.java.type.property.ExtensionTypeDescriptorModelProperty)5 ParameterDeclarationContext (org.mule.runtime.module.extension.internal.loader.utils.ParameterDeclarationContext)4 Field (java.lang.reflect.Field)2 ConfigurationDeclarer (org.mule.runtime.api.meta.model.declaration.fluent.ConfigurationDeclarer)2 ConnectionProviderDeclarer (org.mule.runtime.api.meta.model.declaration.fluent.ConnectionProviderDeclarer)2 ExtensionDeclarer (org.mule.runtime.api.meta.model.declaration.fluent.ExtensionDeclarer)2 ParameterDeclaration (org.mule.runtime.api.meta.model.declaration.fluent.ParameterDeclaration)2 DefaultEncoding (org.mule.runtime.extension.api.annotation.param.DefaultEncoding)2 ExtensionParameter (org.mule.runtime.module.extension.api.loader.java.type.ExtensionParameter)2 Type (org.mule.runtime.module.extension.api.loader.java.type.Type)2 String.format (java.lang.String.format)1 Annotation (java.lang.annotation.Annotation)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Optional (java.util.Optional)1 Set (java.util.Set)1 JAVA (org.mule.metadata.api.model.MetadataFormat.JAVA)1 MetadataType (org.mule.metadata.api.model.MetadataType)1 ObjectType (org.mule.metadata.api.model.ObjectType)1