Core Java

Lombok Utility and Scrapbook an eclipse feature

Today we are going to see couple of utility features which could ease coding and help you save some time. They have handy features and I would recommend to have a look and try it. Let’s start a utility called Lombok which is an open source and helps to make code clean and reduce the boiler plate code. It’s very easy and handy to use.

1. Lombok Utility

This utility is annotation based and helps to get rid of  boiler code. For e.g. Getter/Setters is a part of java development and some time you have big POJO’s and you want to see attributes only in a class and don’t want to include Constructors and Getter/Setters code. Lombok will help to do that.

1.1Employee Class

Let’s have a simple example to see how it works. We got here Employee class with standard getter/setters:

Employee.java

public class Employee {

        // Define Employee Attributes

        private int empNo;

        private String empName;

        // Define Getter/Setter here

        public int getEmpNo() {

            return empNo;

        }

        public void setEmpNo(int empNo) {

            this.empNo = empNo;

        }

        public String getEmpName() {

            return empName;

        }

        public void setEmpName(String empName) {

            this.empName = empName;

        }


}

1.2 Getter/Setter with Lombok

It’s a very simple and small class so we don’t mind if we have getter/setters standard code. But what about if we have long class of more than 20 attributes, and have some other methods and we want a clean code with just attributes and relevant methods without getter/setters? Let’s see the code, and how we could use Lombok utility to avoid boiler code:

Employee.java

@Getter
@Setter
public class Employee {

	// Define Employee Attributes
	
	private int empNo;
		
	private String empName;
	
		
	
}

@Getter/@Setter will take care of default getter and setter code and in code we will use call of getter/setter method normally.

1.3 Getter/Setter at Field level

But what if we want to hide getter/setter for some of the fields? We can use it at attributes/field level which depends on the requirements.

Employee.java

public class Employee {

       // Define Employee Attributes

       @Setter private int empNo;

       @Getter @Setter private String empName;

}

1.4 Common annotations with Lombok

We can set access level and  use @Data to remove boiler code for @toString, @EqualsAndHasCode, @Getter/@Setter and RequiredArgsConstructor for constructor etc.

1.5 How to install Lombok

We need to download Lombok jar from https://projectlombok.org and include it in our project build path OR if you are using maven then copy latest Plugin from maven repository.

If you’re using Spring, then we could choose Lombok option from Spring starter project because that will automatically include maven dependency in pom file.

sprintstarter11
Fig 1: SpringLombokSelection

1.6 Summary

It’s a brief introduction about Lombok utility, you can get clean and optimize code which also saves time and give performance. Please visit https://projectlombok.org/features and read more about features and implementation.

2. Scrapebook an Eclipse feature

Eclipse is a popular IDE for java development. Today we will see eclipse Scrapebook feature which is handy and easy to use. Purpose of this feature is to setup a practice area to test pieces of code interactively to get instant result and it is helpful when developing a complex logic in steps and want to test those steps or features.

2.1 Scrapebook setup

To set Scrapebook in project just right click project -> Other -> Java -> Java Run/Debug and select Scrapebook.

scrapebooksetup
Fig 2: ScrapebookSetup

Select Next and give it any name. Lets say MyBoard and then click Finish.

scrapebookproject
Fig 3 : ScrapebookProjectView

2.2 Execute command in Scrapebook

Just click MyBoard to open in editor and write a simple command to test.

scrapebookexample0
Fig 4: ScrapebookExecuteCommand

Select the line of code and press Execute Statement button, that will execute selected statement.

2.3 Scrapebook functionality

There are many things we can do with scrapeboard, let’s define a variable and print it.

scrapeexample1
Fig 5: ScrapebookFunctionality

But if we change value from 1 to 1.1 and execute, it will give an error.

scrapebookexample2
Fig 6: ScrapebookErrorDisplay

Let’s increment employeeNumber, select lines and execute it.

scrapebookexampl3
Fig 7: ScrapebookEmployeeNumberAddition

2.4 Import package in Scrapebook

Lets take an example of date where we will see how to import package in Scrapebook.

scrapebookexample4
Fig 8: ScrapebookImport

To resolve this error lets import java.time package into Scrapebook, right click in the editor and select ‘set import’

scrapebookexample5
Fig 9: ScrapebookImportStep1

scrapebookexample6
Fig 10: ScrapebookImportStep2

scrapebookexample7
Fig 11: ScrapebookImportStep3

Press Ok and execute code in Scrapebook, it will work just fine .

scrapebookexample8
Fig 12: ScrapebookImportRun

2.5 Date arthmetics and object call in Scrapebook

For instance if we want to do simple date arithmetic’s to see what will be the salary date after two weeks then we can use scrapebook to figure out easily.

scrapebookexample9
Fig 13: DateComputation

We can create objects in Scrapebook and test the class. Just define a class and then instantiate in scrapebook but we need to import package of class to create the object. Here is sample class.

PrintNumberWords.java

public class PrintNumberWords {

	public void printNumber(){
		
		for (int i = 0; i<=100; i++){
			
			// if divide by 4 and 5 then print HelloWorld
			if ((divideBy4(i)) && (divideBy5(i))){
				System.out.println("HelloWorld");
				
			}  
			
			else {
				
				// if divide by 4 the print Hello
				if (divideBy4(i)){
					System.out.println("Hello");
					
				}
				
				else {
					
					// if divide by 5 then print World
					if (divideBy5(i)){
						System.out.println("World");
						
					}
					
					else {
						
						System.out.println(i);
					}
				}
			}
		}
	}

	// divide by 5 method
	private boolean divideBy5(int i) {
		
				return (i % 5 == 0);
	}

	// divide by 4 method
	private boolean divideBy4(int i) {
		
				return (i % 4 == 0);
	}

In Scrapebook, we can test the functionality by instantiating the object and call method

scrapebookexample10
Fig 14: ScrapebookObjectCall

3. Summary

It’s a good feature for beginners and experienced developers, it can save time and predetermine the result of different part of code which helps to write more precise and efficient code.

That’s all, go through Scarepbook and make some practise to familiarize yourself.

Have fun.

Adam Aijaz

Adam got initial professional qualification from RMIT University Melbourne Australia, where he got Master in IT and Business, he got further Professional Certification in IT (CP) from Australian Computer Society (ACS) and Diploma in IT and Finance from Australis College, Brisbane. He got more than 15 years of IT experience in Software development on different technologies, he works mostly on Java, spring, JavaScript, Front end like Angular, JSF, Vaadin etc. His interest in Integration of software’s, SOA and process re-engineering. He runs his own IT consultancy and provided all sort of IT solutions to SME (Small to Medium Enterprises).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button