Skip to content

Custom annotations

pawel_labaj edited this page Aug 2, 2023 · 2 revisions

AutoRecord provides two ways to customize the generation of records:

Creating a Custom Annotation

If you need to apply the same customization options to multiple records, you can create a custom annotation that will be used instead of the @AutoRecord annotation. This can help reduce duplication and make it easier to maintain consistency across your codebase.

To create a custom annotation, you need to:

  • annotate it with @AutoRecord.Template annotation
  • place it in pl.com.labaj.autorecord package
  • set its retention policy to SOURCE
  • set its target to TYPE

You can set @AutoRecord.Options and @RecordBuilder.Options applied to all generation triggered by the custom annotation.

Here is the example of custom annotation:

package pl.com.labaj.autorecord;

import (...)

@Retention(SOURCE)
@Target(TYPE)
@AutoRecord.Template(
        recordOptions = @AutoRecord.Options(withBuilder = true),
        builderOptions = @RecordBuilder.Options(useUnmodifiableCollections = true),
        extensions = @AutoRecord.Extension(extensionClass = LoggingExtension.class)
)
public @interface CustomAutoRecord {}

Then, you can use this custom annotation instead of @AutoRecord on your record interfaces:

@CustomAutoRecord
interface Person {
    String name();
}