Project Lombok!

Post on 24-Dec-2014

883 views 10 download

Tags:

description

The introduction of Project Lombok, a combination compile-time and development-time code generator, has made Java POJO verbosity history. Add Lombok to your daily coding practices, and you won't go back.

Transcript of Project Lombok!

Project Lombok

Spices up your java!

Mehdi Hassan

Team Lead

Etilize Pvt. Ltd.(GfK)

Problems of the Industry

Boiler Plate Code

Syntax Verbosity

Non Standardization

Not following the standard code conventions!!

Human Error/Dependency

Repeatable things (defining Getters, Setters etc.)

Project Lombok!

Demo Video

Taken from http://projectlombok.org/

Gives an idea what Project Lombok is and how it can be installed.

Simple Demo of few Features.

Project Lombok!

Features

@Getter, @Setter, @ToString, @EqualsAndHashCode, @Data

@NoArgsConstructor, @RequiredArgsConstructor and @AllArgsConstructor

@Cleanup, @Synchronized

@Log

@Delegate

val

Project Lombok!

@Getter and @Setter

Project Lombok!

Automatic Generation of getters and setters.

Method will be public unless you explicitly specify an AccessLevel

@Getter and/or @Setter annotation on class

Disable getter/setter generation for any field by using the special AccessLevel.NONE

@NotNull for nullity check

Demo

@ToString

Project Lombok!

Generates a default implementation of the toString() method

Parameters

Demo

@EqualsAndHashCode

Generates hashCode and equals implementations from the fields (non-static, non-transient)

Parameters

Demo

Project Lombok!

Constructor level Annotations

@NoArgsConstructor, @RequiredArgsConstructor, @AllArgsConstructor

Generates constructors that take no arguments, one argument per final / non-null field, or one argument for every field.

Specialized constructor can be made. Compile-time error in case of any conflicts.

Demo

Project Lombok!

@Data

All together now: A shortcut for @ToString, @EqualsAndHashCode, @Getter on all fields, @Setter on all non-final fields, and @RequiredArgsConstructor

Included annotations with changed parameters can be defined along with @Data

Project Lombok!

@Cleanup

Automatic resource management: Call your close() methods safely with no hassle.

Example: @Cleanup InputStream in = new FileInputStream("some/file");

If the type of object you'd like to cleanup does not have a close() method, but some other no-argument method like dispose() so write it as

@Cleanup("dispose") TestClass t = new TestClass();

Project Lombok!

Logger Annotations!

Generate a logger field with name “log”

Four options

@CommonsLog

@Log

@Log4j

@Slf4j

Project Lombok!

And Lot More!!!

Some other supported features like @Delegate, @Synchronized, val etc.

More to come soon!

Open Source

Details can be found at http://projectlombok.org/

Project Lombok!