Project Lombok – first look

https://upload.wikimedia.org/wikipedia/commons/b/b3/Large_Cayenne.jpg

Project Lombok allows reducing the unnecessary code. By annotations generates getters, setters, constructors, toString, hash or others. Spice up your Java!

How to add to project?

  1. apt org.projectlombok:lombok:1.16.12
    compile “org.projectlombok:lombok:1.16.12”
    add to section dependencies in gradle
  2. Install plugin “Lombok Plugin”

– @NonNull

Lombok:

Vanilla Java:

– @Getter/@Setter

Lombok:

Vanilla Java:

Note!

  • – generated names in this way: foo -> getFoo()
  • – default access is  public
  • @Getter/@Setter can be used relative to the entire class, then they concern all non-static data
  • – if you want turn off generating setter or getter you can get it throught  AccessLevel.NONE

– @ToString

Lombok:

Vanilla Java:

Note!

  • default class name with fields
  • exlude field through exclude e.g exclude="id"
  • add name fileds through includeFieldNames=true  
  • add method toString from super through callSuper=true

Others:

– @CleanUp

– @EqualAndHashCode

– @NoArgsConstructor, @RequiredArgsConstructor, @AllArgsConstructor

– @Data

– @Value

– @Builder

– @SneakyThrows

– @Synchronized

– @Getter(lazy=true)

– @Log


Links:


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