Search code examples
mulemule-studiomvel

Mule MEL usage difference


I have been using different forms of Mule's Expression language. I couldn't figure out the difference between

#[flowVars.myVariable]  

and

#[flowVars['myVariable']]

They both give the result when there is a variable. But why do they behave differently when the variable is not present?

Like if the variable being called is not available, then the first expression would result in a exception. Whereas the second expression just gives out a warning or prints out as is, if in a logger message.

Why is this difference?

Also when going through the documentation for Mule 3.6 I found that the second expression is not longer shown in the documentation.

Is the expression #[flowVars['myVariable']] being deprecated?


Solution

  • The difference comes from the way MVEL deals with these two different ways of accessing map entries.

    • #[flowVars['myVariable']] is equivalent to flowVars.get('myVariable'), which does not fail if the flowVars map does not contain the 'myVariable' entry,
    • #[flowVars.myVariable] treats the flowVars map as a virtual object, leading to an exception if the 'myVariable' entry is missing because in this case it doesn't resolve to a map get but instead to directly using an object member (either a field or a method), which must exist before being accessed.

    I don't think #[flowVars['myVariable']] could be deprecated since it's a core feature provided by MVEL.

    Reference: http://mvel.codehaus.org/MVEL+2.0+Property+Navigation#MVEL2.0PropertyNavigation-MapAccess