Let’s take a look at the Java project Lombok. It is used to get a lot of the required boilerplate code by using simple annotations. Bellow you can see a few of the most used annotations and what they mean.
- @Getter: creates a getter (method and class level)
- @Setter: creates a setter (method and class level)
The access level can be changed by the AccessLevel enum. for example : @Getter(AccessLevel.NONE) which makes sure this attribuut gets no getter.
- @GeneratedValue: for auto increment values (method level)
- @Id: it’s the unique identifier in the class (method level)
- @ToString: creates toString. You can exclude fields with (exclude=”attribute”) (class level)
- @EqualsAndHashCode: creates equels and hashcode methodes (class level)
- @RequiredArgsConstructor: creates a constructor for all attributes and throws nullpointerexception for attributes annotated with @NonNull. (class level)
- @Data: a combination of all the annotations above (class level)
- @NonNull: creates a check what the attributes are not null for all methods annotated with this annotation (method level)
I love this project, because all the clutter in the classes made was one of the reasons I much preferred C# over Java. Now I know of this project I will use it whenever possible.