Java Lombok

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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s