Search in sources :

Example 1 with DefaultEncodingModelProperty

use of org.mule.runtime.module.extension.internal.loader.java.property.DefaultEncodingModelProperty 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)

Aggregations

Field (java.lang.reflect.Field)1 DefaultEncoding (org.mule.runtime.extension.api.annotation.param.DefaultEncoding)1 IllegalConfigurationModelDefinitionException (org.mule.runtime.extension.api.exception.IllegalConfigurationModelDefinitionException)1 DefaultEncodingModelProperty (org.mule.runtime.module.extension.internal.loader.java.property.DefaultEncodingModelProperty)1 ImplementingTypeModelProperty (org.mule.runtime.module.extension.internal.loader.java.property.ImplementingTypeModelProperty)1