Skip to content
Geoffrey Wiseman edited this page May 9, 2014 · 1 revision

Thus far, I've only had cause to make a single extension to Moo, moo-mvel. If moo-mvel is present on the classpath, Moo's CompositeSourcePropertyFactory will pick it up using reflection.

moo-mvel

MVEL was a part of Moo's core library for all of the v1.x releases, but was extracted into an extension during Moo's v2.0 development cycle.

Essentially, moo-mvel allows you to use more complicated source expressions using the MVEL expression language, which can:

  • traverse properties
  • manipulate collections
  • invoke static methods on classes
  • use external variables

Not every project needs these features, but it can make complicated translations simpler or even possible.

Adding moo-mvel

You can use moo-mvel by adding it to your classpath. Maven users can simply add the following to their POM file:

<dependency>
    <groupId>com.codiform</groupId>
    <artifactId>moo-mvel</artifactId>
    <version>2.0</version>
</dependency>

Because of Maven's transitive dependencies, you don't need to depend on Moo's core module directly (although I would, since I'd be using elements of it directly).

If you're not using Maven, you can add moo-core and moo-mvel to your classpath in whatever way is appropriate for your project.

Using moo-mvel

If you want to invoke moo-mvel explicitly, simply add mvel: before the source expression. If you use an unprefixed expression, Moo will first try Moo's core ReflectionSourcePropertyFactory, but if that fails to find the source, it will give moo-mvel's MvelSourcePropertyFactory an opportunity to offer a source property for the expression as well.

Here are some examples of source expressions that are possible with moo-mvel:

  • java.util.EnumSet.copyOf(this.letters)
  • enumUtil.toString(this.letters)
  • attributes['createdDate']
  • roles[0]
  • owner.lastName
  • owner.?lastName.length()
  • userDao.getUser(author.id)
  • mvel:score
  • counter.getCount(value)

You can the above and other examples in the tests for moo-mvel.