Print
Strong Typing Mode

Strong typing mode puts MVEL into a mode that requires that all types within a script are resolvable. This means that the compiler must be informed of the types of any and all injected variables. It also means that it's possible for the compiler to report on the return type at compile-time.

Turning on Strong Typing

You can turn on strong typing with the setStrongTyping() method in the ParserContext. For example:

The above code will throw an exception.

MVEL is unable to safely qualify the type of the identifier 'str' in this case, and therefore cannot determine that toUpperCase() even is valid call.

We can fix it like so:

This will now succeed. In fact, we can also determine the return type of the expression now by calling getEgressType() against the CompiledExpression object.

If we changed the expression to: str.length(). The egress type would return java.lang.Integer.

Generics

MVEL is also generics aware. You can specify type parameters for generic types by using the overloaded addInput() method:

In this case, the expression compiles safely because MVEL is able to determine the return type of foo.get('bar') due to type parameter being specified on input.

Powered by Atlassian Confluence