Skip to main content

Feature Anchor Options

In some cases the behaviour of the feature anchors needs to be modified to generate the correct artifact content. Both @anchor and @hook statements in the templates allow you to define options in the start anchor.

@anchor(options...)

optiondescriptionexample
prefixAdds the prefix at the start of the content, but only if at least 1 feature injects content.@anchor:interfaces(prefix: 'implements ')
suffixAdds the suffix at the end of the content, but only if at least 1 feature injects content.@anchor:items(suffix: ', ')
separatorAdds the separator between each feature. For inline anchors, if the injected content has multiple lines, the separator is placed between the lines as well.@anchor:items(separator: ', ')
commentLineFormatProvides a format for the start and end anchor comments. Useful if the default comment format is not correct for the generated file.@anchor:vars(commentLineFormat: '# %s')
noCommentsRemoves the start and end anchor comments. This reduces traceability, but some file formats do not support comments.@anchor:vars(noComments: true)
sortDefine a java class that implements Comparator<FeatureSegmentComposite>, which will be used to sort the injected features.@anchor:vars(sort: org.example.SortAlphabetically)
inline feature anchors

Inline feature anchors are a special variant of the feature anchor. They combine the feature injections into a single line, usually using a separator.

base() ::= <<
class <className> @anchor:interfaces(prefix: 'implements ', separator: ', ')@{
}
>>

Results in:

class MyClass /*@anchor:interfaces:start*/implements Serializable, MyInterface/*@anchor:interfaces:end*/ {
}

@hook(options....)

For feature templates, options need to be defined in the start hook.

base() ::= <<
@hook:variables(first: true):start
private String myVariable; // Always placed first.
@hook:variables:end
>>
optiondescriptionexample
firstPlace the content of this feature segment at the start of the anchor.@hook:vars(first: true):start
lastPlace the content of this feature segment at the end of the anchor.@hook:vars(last: true):start
groupOnly works in a @hook:methods anchor. Features defining the same group are placed together and preceded by a comment with the group name.@hook:methods(group: 'Getters'):start