Search in sources :

Example 1 with DataType.fromType

use of org.mule.runtime.api.metadata.DataType.fromType in project mule by mulesoft.

the class ReflectiveExpressionFunctionExecutor method getWrapper.

private Function<Object, Object> getWrapper(Parameter parameter) {
    Type[] generics = parameter.getType().getGenericInterfaces();
    Class<?> type;
    if (generics.length == 0) {
        type = Object.class;
    } else {
        type = model.getAllParameterModels().stream().filter(p -> p.getModelProperty(ImplementingParameterModelProperty.class).map(mp -> mp.getParameter().getName().equals(parameter.getName())).orElse(false)).map(p -> getType(p.getType()).orElse(Object.class)).findFirst().orElseThrow(() -> new IllegalStateException(format("Missing parameter with name [%s]", parameter.getName())));
    }
    DataType expected = DataType.fromType(type);
    return value -> {
        if (value == null) {
            return null;
        }
        if (value instanceof TypedValue) {
            if (((TypedValue) value).getDataType().equals(DataType.TYPED_VALUE)) {
                // DW will wrap anything of type TypedValue in a new TypedValue with DataType.TYPED_VALUE
                value = ((TypedValue) value).getValue();
            }
            TypedValue typedValue = (TypedValue) value;
            // We have to check for transformations of the value because weave won't be able to validate types
            return type.isInstance(typedValue.getValue()) ? value : new TypedValue<>(transformationService.transform(typedValue.getValue(), typedValue.getDataType(), expected), typedValue.getDataType());
        } else {
            return new TypedValue<>(value, fromObject(value));
        }
    };
}
Also used : FunctionModel(org.mule.runtime.api.meta.model.function.FunctionModel) BindingContext(org.mule.runtime.api.el.BindingContext) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) ImplementingParameterModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.ImplementingParameterModelProperty) DataType.fromObject(org.mule.runtime.api.metadata.DataType.fromObject) LifecycleUtils.initialiseIfNeeded(org.mule.runtime.core.api.lifecycle.LifecycleUtils.initialiseIfNeeded) Function(java.util.function.Function) ReflectionUtils.invokeMethod(org.springframework.util.ReflectionUtils.invokeMethod) ClassUtils(org.mule.runtime.core.api.util.ClassUtils) Inject(javax.inject.Inject) MuleContext(org.mule.runtime.core.api.MuleContext) MuleException(org.mule.runtime.api.exception.MuleException) Parameter(java.lang.reflect.Parameter) FunctionParameter(org.mule.runtime.api.metadata.FunctionParameter) LifecycleUtils.stopIfNeeded(org.mule.runtime.core.api.lifecycle.LifecycleUtils.stopIfNeeded) ClassUtils.withContextClassLoader(org.mule.runtime.core.api.util.ClassUtils.withContextClassLoader) Method(java.lang.reflect.Method) TransformationService(org.mule.runtime.api.transformation.TransformationService) ExecutionContext(org.mule.runtime.extension.api.runtime.operation.ExecutionContext) Logger(org.slf4j.Logger) DataType(org.mule.runtime.api.metadata.DataType) Optional.ofNullable(java.util.Optional.ofNullable) LifecycleUtils.startIfNeeded(org.mule.runtime.core.api.lifecycle.LifecycleUtils.startIfNeeded) String.format(java.lang.String.format) LifecycleUtils.disposeIfNeeded(org.mule.runtime.core.api.lifecycle.LifecycleUtils.disposeIfNeeded) TypedValue(org.mule.runtime.api.metadata.TypedValue) ExtensionMetadataTypeUtils.getType(org.mule.runtime.extension.api.util.ExtensionMetadataTypeUtils.getType) Lifecycle(org.mule.runtime.api.lifecycle.Lifecycle) List(java.util.List) Type(java.lang.reflect.Type) DataType.fromType(org.mule.runtime.api.metadata.DataType.fromType) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) Optional(java.util.Optional) DataType(org.mule.runtime.api.metadata.DataType) ExtensionMetadataTypeUtils.getType(org.mule.runtime.extension.api.util.ExtensionMetadataTypeUtils.getType) Type(java.lang.reflect.Type) DataType.fromType(org.mule.runtime.api.metadata.DataType.fromType) DataType(org.mule.runtime.api.metadata.DataType) ImplementingParameterModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.ImplementingParameterModelProperty) TypedValue(org.mule.runtime.api.metadata.TypedValue)

Aggregations

String.format (java.lang.String.format)1 Method (java.lang.reflect.Method)1 Parameter (java.lang.reflect.Parameter)1 Type (java.lang.reflect.Type)1 List (java.util.List)1 Optional (java.util.Optional)1 Optional.ofNullable (java.util.Optional.ofNullable)1 Function (java.util.function.Function)1 Inject (javax.inject.Inject)1 BindingContext (org.mule.runtime.api.el.BindingContext)1 MuleException (org.mule.runtime.api.exception.MuleException)1 InitialisationException (org.mule.runtime.api.lifecycle.InitialisationException)1 Lifecycle (org.mule.runtime.api.lifecycle.Lifecycle)1 FunctionModel (org.mule.runtime.api.meta.model.function.FunctionModel)1 DataType (org.mule.runtime.api.metadata.DataType)1 DataType.fromObject (org.mule.runtime.api.metadata.DataType.fromObject)1 DataType.fromType (org.mule.runtime.api.metadata.DataType.fromType)1 FunctionParameter (org.mule.runtime.api.metadata.FunctionParameter)1 TypedValue (org.mule.runtime.api.metadata.TypedValue)1 TransformationService (org.mule.runtime.api.transformation.TransformationService)1