SlideShare a Scribd company logo
1 of 15
Lombok
Techognite
Compete coverage on Tech
Agenda
Introduction
Installation
Annotations
Benefits
Q&A
Introduction
Lombok is used to reduce boilerplate code for model/data objects, e.g., it can
generate getters and setters for those object automatically by using Lombok
annotations. The easiest way is to use the @Data annotation.
"Boilerplate" is a term used to describe code that is repeated in many parts of
an application with little alteration
Lombok provides a lot of annotations for remove this Boilerplate code.
Like @Data, @Getter, @Setter etc.
Installation
Download install and integrating Lombok jar to IDE
By integrating into the IDE, Project Lombok is able to inject code that is
immediately available to the developer. For example, simply adding
the @Data annotation to a data class, as below, results in a number of new
methods in the IDE.
Installation
Create a maven project and install the dependency.
A jar file will still need to be included in the classpath of any projects that will
use Project Lombok annotations.
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.16</version>
<scope>provided</scope>
</dependency>
Annotations
@Getter and @Setter
1. generate a getter and setter for a field, respectively.
2. Correctly follow convention for boolean properties.
Annotations
@Getter and @Setter
@Getter @Setter
private boolean employed = true;
private boolean employed = true;
public boolean isEmployed() {
return employed;
}
public void setEmployed(final boolean employed) {
this.employed = employed;
}
Annotations
@NonNull
1. Provide fast-fail null check on the corresponding member.
2. NullPointerException will thrown if Lombok is generating setter and
constructor method for your class.
Annotations
@NonNull
@Getter @Setter @NonNull
private List<Person> members;
@NonNull
private List<Person> members;
public Family(@NonNull final List<Person> members) {
if (members == null) throw new java.lang.NullPointerException("members");
this.members = members;
}
@NonNull
public List<Person> getMembers() { return members;}
public void setMembers(@NonNull final List<Person> members) {
if (members == null) throw new java.lang.NullPointerException("members");
this.members = members;
}
Annotations
@ToString
1. Implementation of the toString method.
2. By default, any non-static fields will be included
3. Use exclude parameter to exclude any field
4. Set callSuper parameter to true to include output of super class.
@ToString(callSuper=true,exclude="someExcludedField")
Annotations
@EqualsAndHashCode
1. Generate both equals and hashCode methods
2. Follow equals and hashCode contract.
3. Set callSuper parameter to true to include output of super class.
4. Use exclude parameter to exclude any field
@EqualsAndHashCode(callSuper=true,exclude={"address","city","state","zip"})
Annotations
@Data
1. Most frequently used annotation.
2. Combines the functionality
of @ToString, @EqualsAndHashCode, @Getter and @Setter
3. Generate a constructor.
4. provides everything needed for a Plain Old Java Object (POJO).
5. If a superclass has no default constructor any subclasses cannot use the
@Data annotation
Annotations
Some more useful annotations.
1. @NoArgsConstructor, @RequiredArgsConstructor and
@AllArgsConstructor
2. @Cleanup @Cleanup InputStream in = new FileInputStream(args[0]);
3. @Builder
Benefits
1. Greatly reduce the number of lines of boilerplate code.
2. Reduced maintenance overhead.
3. Fewer bugs and more readable classes.
4. Fast developemt
Lombok

More Related Content

What's hot

Java Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By StepJava Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By Step
Guo Albert
 

What's hot (20)

Java Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By StepJava Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By Step
 
Spring Data JPA
Spring Data JPASpring Data JPA
Spring Data JPA
 
JDBC - JPA - Spring Data
JDBC - JPA - Spring DataJDBC - JPA - Spring Data
JDBC - JPA - Spring Data
 
Spring Boot Tutorial
Spring Boot TutorialSpring Boot Tutorial
Spring Boot Tutorial
 
Java Annotations
Java AnnotationsJava Annotations
Java Annotations
 
Introduction to spring boot
Introduction to spring bootIntroduction to spring boot
Introduction to spring boot
 
Java annotations
Java annotationsJava annotations
Java annotations
 
Spring boot
Spring bootSpring boot
Spring boot
 
Spring annotation
Spring annotationSpring annotation
Spring annotation
 
Jdbc
Jdbc   Jdbc
Jdbc
 
Spring boot
Spring bootSpring boot
Spring boot
 
Java - Interfaces & Packages
Java - Interfaces & PackagesJava - Interfaces & Packages
Java - Interfaces & Packages
 
JUnit & Mockito, first steps
JUnit & Mockito, first stepsJUnit & Mockito, first steps
JUnit & Mockito, first steps
 
Java IO
Java IOJava IO
Java IO
 
Hibernate ppt
Hibernate pptHibernate ppt
Hibernate ppt
 
Introduction to java 8 stream api
Introduction to java 8 stream apiIntroduction to java 8 stream api
Introduction to java 8 stream api
 
Spring Data JPA
Spring Data JPASpring Data JPA
Spring Data JPA
 
Java Collections
Java  Collections Java  Collections
Java Collections
 
Spring boot Introduction
Spring boot IntroductionSpring boot Introduction
Spring boot Introduction
 
Spring boot - an introduction
Spring boot - an introductionSpring boot - an introduction
Spring boot - an introduction
 

Similar to Lombok

FileWrite.javaFileWrite.java  To change this license header.docx
FileWrite.javaFileWrite.java  To change this license header.docxFileWrite.javaFileWrite.java  To change this license header.docx
FileWrite.javaFileWrite.java  To change this license header.docx
ssuser454af01
 
Ta Javaserverside Eran Toch
Ta Javaserverside Eran TochTa Javaserverside Eran Toch
Ta Javaserverside Eran Toch
Adil Jafri
 
Ejb3 Struts Tutorial En
Ejb3 Struts Tutorial EnEjb3 Struts Tutorial En
Ejb3 Struts Tutorial En
Ankur Dongre
 
Ejb3 Struts Tutorial En
Ejb3 Struts Tutorial EnEjb3 Struts Tutorial En
Ejb3 Struts Tutorial En
Ankur Dongre
 

Similar to Lombok (20)

Final Project Presentation
Final Project PresentationFinal Project Presentation
Final Project Presentation
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 
Annotation Processing in Android
Annotation Processing in AndroidAnnotation Processing in Android
Annotation Processing in Android
 
FileWrite.javaFileWrite.java  To change this license header.docx
FileWrite.javaFileWrite.java  To change this license header.docxFileWrite.javaFileWrite.java  To change this license header.docx
FileWrite.javaFileWrite.java  To change this license header.docx
 
Spring boot
Spring bootSpring boot
Spring boot
 
Bring the fun back to java
Bring the fun back to javaBring the fun back to java
Bring the fun back to java
 
Introducing Struts 2
Introducing Struts 2Introducing Struts 2
Introducing Struts 2
 
create-netflix-clone-02-server_transcript.pdf
create-netflix-clone-02-server_transcript.pdfcreate-netflix-clone-02-server_transcript.pdf
create-netflix-clone-02-server_transcript.pdf
 
Ta Javaserverside Eran Toch
Ta Javaserverside Eran TochTa Javaserverside Eran Toch
Ta Javaserverside Eran Toch
 
Os Johnson
Os JohnsonOs Johnson
Os Johnson
 
Django tutorial
Django tutorialDjango tutorial
Django tutorial
 
ActiveWeb: Chicago Java User Group Presentation
ActiveWeb: Chicago Java User Group PresentationActiveWeb: Chicago Java User Group Presentation
ActiveWeb: Chicago Java User Group Presentation
 
Ejb3 Struts Tutorial En
Ejb3 Struts Tutorial EnEjb3 Struts Tutorial En
Ejb3 Struts Tutorial En
 
Ejb3 Struts Tutorial En
Ejb3 Struts Tutorial EnEjb3 Struts Tutorial En
Ejb3 Struts Tutorial En
 
Jsp and jstl
Jsp and jstlJsp and jstl
Jsp and jstl
 
Bt0083 server side programing 2
Bt0083 server side programing  2Bt0083 server side programing  2
Bt0083 server side programing 2
 
JEE5 New Features
JEE5 New FeaturesJEE5 New Features
JEE5 New Features
 
SCR Annotations for Fun and Profit
SCR Annotations for Fun and ProfitSCR Annotations for Fun and Profit
SCR Annotations for Fun and Profit
 
Spring framework Controllers and Annotations
Spring framework   Controllers and AnnotationsSpring framework   Controllers and Annotations
Spring framework Controllers and Annotations
 
Get Hip with JHipster - GIDS 2019
Get Hip with JHipster - GIDS 2019Get Hip with JHipster - GIDS 2019
Get Hip with JHipster - GIDS 2019
 

Recently uploaded

Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Recently uploaded (20)

"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Decarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational PerformanceDecarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational Performance
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
AI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by Anitaraj
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
JavaScript Usage Statistics 2024 - The Ultimate Guide
JavaScript Usage Statistics 2024 - The Ultimate GuideJavaScript Usage Statistics 2024 - The Ultimate Guide
JavaScript Usage Statistics 2024 - The Ultimate Guide
 
Simplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptxSimplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptx
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Less Is More: Utilizing Ballerina to Architect a Cloud Data Platform
Less Is More: Utilizing Ballerina to Architect a Cloud Data PlatformLess Is More: Utilizing Ballerina to Architect a Cloud Data Platform
Less Is More: Utilizing Ballerina to Architect a Cloud Data Platform
 
API Governance and Monetization - The evolution of API governance
API Governance and Monetization -  The evolution of API governanceAPI Governance and Monetization -  The evolution of API governance
API Governance and Monetization - The evolution of API governance
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 

Lombok

  • 3. Introduction Lombok is used to reduce boilerplate code for model/data objects, e.g., it can generate getters and setters for those object automatically by using Lombok annotations. The easiest way is to use the @Data annotation. "Boilerplate" is a term used to describe code that is repeated in many parts of an application with little alteration Lombok provides a lot of annotations for remove this Boilerplate code. Like @Data, @Getter, @Setter etc.
  • 4. Installation Download install and integrating Lombok jar to IDE By integrating into the IDE, Project Lombok is able to inject code that is immediately available to the developer. For example, simply adding the @Data annotation to a data class, as below, results in a number of new methods in the IDE.
  • 5. Installation Create a maven project and install the dependency. A jar file will still need to be included in the classpath of any projects that will use Project Lombok annotations. <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.16</version> <scope>provided</scope> </dependency>
  • 6. Annotations @Getter and @Setter 1. generate a getter and setter for a field, respectively. 2. Correctly follow convention for boolean properties.
  • 7. Annotations @Getter and @Setter @Getter @Setter private boolean employed = true; private boolean employed = true; public boolean isEmployed() { return employed; } public void setEmployed(final boolean employed) { this.employed = employed; }
  • 8. Annotations @NonNull 1. Provide fast-fail null check on the corresponding member. 2. NullPointerException will thrown if Lombok is generating setter and constructor method for your class.
  • 9. Annotations @NonNull @Getter @Setter @NonNull private List<Person> members; @NonNull private List<Person> members; public Family(@NonNull final List<Person> members) { if (members == null) throw new java.lang.NullPointerException("members"); this.members = members; } @NonNull public List<Person> getMembers() { return members;} public void setMembers(@NonNull final List<Person> members) { if (members == null) throw new java.lang.NullPointerException("members"); this.members = members; }
  • 10. Annotations @ToString 1. Implementation of the toString method. 2. By default, any non-static fields will be included 3. Use exclude parameter to exclude any field 4. Set callSuper parameter to true to include output of super class. @ToString(callSuper=true,exclude="someExcludedField")
  • 11. Annotations @EqualsAndHashCode 1. Generate both equals and hashCode methods 2. Follow equals and hashCode contract. 3. Set callSuper parameter to true to include output of super class. 4. Use exclude parameter to exclude any field @EqualsAndHashCode(callSuper=true,exclude={"address","city","state","zip"})
  • 12. Annotations @Data 1. Most frequently used annotation. 2. Combines the functionality of @ToString, @EqualsAndHashCode, @Getter and @Setter 3. Generate a constructor. 4. provides everything needed for a Plain Old Java Object (POJO). 5. If a superclass has no default constructor any subclasses cannot use the @Data annotation
  • 13. Annotations Some more useful annotations. 1. @NoArgsConstructor, @RequiredArgsConstructor and @AllArgsConstructor 2. @Cleanup @Cleanup InputStream in = new FileInputStream(args[0]); 3. @Builder
  • 14. Benefits 1. Greatly reduce the number of lines of boilerplate code. 2. Reduced maintenance overhead. 3. Fewer bugs and more readable classes. 4. Fast developemt