Search in sources :

Example 1 with IllegalConfigurationModelDefinitionException

use of org.mule.runtime.extension.api.exception.IllegalConfigurationModelDefinitionException in project mule by mulesoft.

the class RefNameDeclarationEnricher method doEnrich.

private void doEnrich(BaseDeclaration<?> declaration) {
    declaration.getModelProperty(ImplementingTypeModelProperty.class).ifPresent(typeProperty -> {
        Collection<Field> fields = getAllFields(typeProperty.getType(), withAnnotation(RefName.class));
        if (isEmpty(fields)) {
            return;
        }
        if (fields.size() > 1) {
            throw new IllegalConfigurationModelDefinitionException(String.format("Only one field is allowed to be annotated with @%s, but class '%s' has %d fields " + "with such annotation. Offending fields are: [%s]", RefName.class.getSimpleName(), typeProperty.getType().getName(), fields.size(), Joiner.on(", ").join(fields.stream().map(Field::getName).collect(toList()))));
        }
        final Field configNameField = fields.iterator().next();
        if (!String.class.equals(configNameField.getType())) {
            throw new IllegalConfigurationModelDefinitionException(String.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", typeProperty.getType().getName(), configNameField.getName(), RefName.class.getSimpleName(), configNameField.getType().getName()));
        }
        declaration.addModelProperty(new RequireNameField(configNameField));
    });
}
Also used : RequireNameField(org.mule.runtime.module.extension.internal.loader.java.property.RequireNameField) Field(java.lang.reflect.Field) RequireNameField(org.mule.runtime.module.extension.internal.loader.java.property.RequireNameField) IllegalConfigurationModelDefinitionException(org.mule.runtime.extension.api.exception.IllegalConfigurationModelDefinitionException) RefName(org.mule.runtime.extension.api.annotation.param.RefName) ImplementingTypeModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.ImplementingTypeModelProperty)

Example 2 with IllegalConfigurationModelDefinitionException

use of org.mule.runtime.extension.api.exception.IllegalConfigurationModelDefinitionException in project mule by mulesoft.

the class DefaultEncodingDeclarationEnricher method doEnrich.

private void doEnrich(BaseDeclaration declaration) {
    declaration.getModelProperty(ImplementingTypeModelProperty.class).ifPresent(p -> {
        ImplementingTypeModelProperty typeProperty = (ImplementingTypeModelProperty) p;
        Collection<Field> fields = getAllFields(typeProperty.getType(), withAnnotation(DefaultEncoding.class));
        if (isEmpty(fields)) {
            return;
        }
        // TODO: MULE-9220 - Add a syntax validator which checks that the annotated parameter is a String
        if (fields.size() > 1) {
            throw new IllegalConfigurationModelDefinitionException(String.format("Only one field is allowed to be annotated with @%s, but class '%s' has %d fields " + "with such annotation. Offending fields are: [%s]", DefaultEncoding.class.getSimpleName(), typeProperty.getType().getName(), fields.size(), Joiner.on(", ").join(fields.stream().map(Field::getName).collect(toList()))));
        }
        final Field defaultEncodingField = fields.iterator().next();
        if (!String.class.equals(defaultEncodingField.getType())) {
            throw new IllegalConfigurationModelDefinitionException(String.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", typeProperty.getType().getName(), defaultEncodingField.getName(), DefaultEncoding.class.getSimpleName(), defaultEncodingField.getType().getName()));
        }
        declaration.addModelProperty(new DefaultEncodingModelProperty(defaultEncodingField));
    });
}
Also used : Field(java.lang.reflect.Field) IllegalConfigurationModelDefinitionException(org.mule.runtime.extension.api.exception.IllegalConfigurationModelDefinitionException) DefaultEncodingModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.DefaultEncodingModelProperty) DefaultEncoding(org.mule.runtime.extension.api.annotation.param.DefaultEncoding) ImplementingTypeModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.ImplementingTypeModelProperty)

Example 3 with IllegalConfigurationModelDefinitionException

use of org.mule.runtime.extension.api.exception.IllegalConfigurationModelDefinitionException in project mule by mulesoft.

the class IntrospectionUtils method injectFieldFromModelProperty.

private static void injectFieldFromModelProperty(Object target, String value, Optional<? extends InjectedFieldModelProperty> modelProperty, Class<? extends Annotation> annotationClass) {
    if (value == null) {
        return;
    }
    modelProperty.ifPresent(property -> {
        final Field field = property.getField();
        if (!field.getDeclaringClass().isInstance(target)) {
            throw new IllegalConfigurationModelDefinitionException(format("field '%s' is annotated with @%s but not defined on an instance of type '%s'", field.toString(), annotationClass.getSimpleName(), target.getClass().getName()));
        }
        new FieldSetter<>(field).set(target, value);
    });
}
Also used : RequireNameField(org.mule.runtime.module.extension.internal.loader.java.property.RequireNameField) Field(java.lang.reflect.Field) IllegalConfigurationModelDefinitionException(org.mule.runtime.extension.api.exception.IllegalConfigurationModelDefinitionException)

Example 4 with IllegalConfigurationModelDefinitionException

use of org.mule.runtime.extension.api.exception.IllegalConfigurationModelDefinitionException in project mule by mulesoft.

the class ConfigModelLoaderDelegate method checkConfigurationIsNotAnOperation.

private void checkConfigurationIsNotAnOperation(ExtensionElement extensionElement, ComponentElement configurationType) {
    List<OperationContainerElement> allOperations = new ArrayList<>();
    allOperations.addAll(extensionElement.getOperationContainers());
    allOperations.addAll(configurationType.getOperationContainers());
    for (OperationContainerElement operationClass : allOperations) {
        if (configurationType.isAssignableFrom(operationClass) || configurationType.isAssignableTo(operationClass)) {
            throw new IllegalConfigurationModelDefinitionException(format("Configuration class '%s' cannot be the same class (nor a derivative) of any operation class '%s", configurationType.getName(), operationClass.getName()));
        }
    }
}
Also used : IllegalConfigurationModelDefinitionException(org.mule.runtime.extension.api.exception.IllegalConfigurationModelDefinitionException) ArrayList(java.util.ArrayList) OperationContainerElement(org.mule.runtime.module.extension.api.loader.java.type.OperationContainerElement)

Aggregations

IllegalConfigurationModelDefinitionException (org.mule.runtime.extension.api.exception.IllegalConfigurationModelDefinitionException)4 Field (java.lang.reflect.Field)3 ImplementingTypeModelProperty (org.mule.runtime.module.extension.internal.loader.java.property.ImplementingTypeModelProperty)2 RequireNameField (org.mule.runtime.module.extension.internal.loader.java.property.RequireNameField)2 ArrayList (java.util.ArrayList)1 DefaultEncoding (org.mule.runtime.extension.api.annotation.param.DefaultEncoding)1 RefName (org.mule.runtime.extension.api.annotation.param.RefName)1 OperationContainerElement (org.mule.runtime.module.extension.api.loader.java.type.OperationContainerElement)1 DefaultEncodingModelProperty (org.mule.runtime.module.extension.internal.loader.java.property.DefaultEncodingModelProperty)1