Skip to content

Translating Object Properties

Geoffrey Wiseman edited this page May 21, 2014 · 2 revisions

Object properties can be translated with explicit configuration or implicitly.

Implicit Translation

When a class is translated, Moo will search that class for properties using the default Access Mode (see Access Modes). Any properties found this way will be translated from the source, if they can be found.

You can configure the default access mode in the Moo Configuration class, or you can change the access mode for a class using the @AccessMode annotation. Alternately, you can configure properties of the object for explicit translation.

Explicit Translation

In addition to any implicitly-located properties, you might choose to explicitly configure the properties on a class. There are lots of reasons to do this and lots of ways to do this.

The most common would be to include the @Property annotation on a property, but the @Ignore or @Optionality attributes would also qualify.

Ignoring Properties

You can mark properties as being ignored for translation with the @Ignore annotation if they're properties that might be picked up implicitly and you don't want them to be picked up (see Ignoring Properties).

Optional Properties

If you don't want to ignore the property, you want it to be translated when it's available but get no complaints when it is not, then you might want to look at the @Optionality annotation or the optionality attribute on the Property annotation (see Optional and Required Properties).

Explicit Property

You might mark a field or method with the Property annotation if you just want to pick up a property that might not otherwise be found:

@Access(AccessMode.METHOD)
public MyClass {
  @Property
  private OtherClass other;
}

Or you might need one of the many attributes on the @Property annotation.

Source Attribute

In some cases, the source object for the property might not be as simple as the field or property with the same name as the destination. You might have a different name on the source, or you might need to translate only a single property of the source, or you might have a more complicated translation that requires the assistance of outside objects. The source attribute can help you with all of these cases.

(see Source Expressions)

Translate Attribute

Not every property needs a translation. Some can simply be copied. The translate attribute allows you to identify if the source object requires a translation before it can be used in the destination.

Update Attribute

Some properties should be totally replaced with a new translation, others can be updated from the source object or from the translation. The update attribute allows you to control whether the object can be updated (update=true) or should be replaced (update=false).

Optionality Attribute

As above, sometimes an attribute might be optional. You can use the optionality attribute or the optionality annotation to let Moo know.

(see Optional and Required Properties)

Factory Attribute

Moo will create the destination object for you when a translation is required, but sometimes the destination object that Moo would create isn't the one you would have picked. In that case, you might need a TranslationTargetFactory to help out.

(see Translation Target Factories)

Clone this wiki locally