From the course: Learning Lombok: Streamlined Java Programming

Abating setter and getter code bloat - Spring Tutorial

From the course: Learning Lombok: Streamlined Java Programming

Abating setter and getter code bloat

- [Instructor] Many times when working with Java, you're confronted with a set of plain old Java objects, famously called POJOs. These are often encountered when working with entity persistence files like this one. And the first thing you may notice and get annoyed with is the creation and maintenance of all the setter and getter methods that are required for an entity class. These encompass the boiler plate code that generally feels out of place in modern programming languages. What if you could select all of the getters and setters and delete them and then replace these with two annotations at the top? One setter and one getter annotation. Now, I'm not talking about the Nashorn JavaScript setter and getter annotations that have been removed as of JDK 15. The idea in theory here would be that these annotations would automatically generate the required setters and getters at compile time, but keep the code base much smaller, simpler to read and easier to manage. Wouldn't that be great? We would have a much smaller and cleaner code base. I'm going to show you how to do this with Project Lombok. So first, open up an IntelliJ, project 0101 begin, Lombok. And then once you have that open scroll down to the bottom find the Build Gradle file and open it. What we'll do is add a dependency right here above the Spring Dev tools we'll type compile only organization, project Lombok colon Lombok. I'm going to copy that. And then right above the test implementation we'll type annotation processor and we'll paste that same dependency again. This will automatically pull in the latest version of Lombok which at this recording of this video is 1.18.24. With the gradle dependencies in place, we will not need to change or mess with this file anymore. We also are not going to be writing any unit test cases, so as to keep things simple. But if you would need to add these dependencies as well we will type right above the test implementation test compile only, paste that dependency in again and then we'll type test annotation processor and we'll paste again, the same dependency. Okay, let's go to the terminal. We're going to go ahead and build this. We'll type gradle W clean build. Next, let's jump over to the employee class and we'll type at the top at setter, make sure it is the Lombok version and at getter. If the imports are not immediately available to you, you may need to close IntelliJ and reopen this project to force the dependency to be recognized by IntelliJ. Next, if you've not done so yet, let's go ahead and select all of the setter and getters. We'll delete them. And then let's go open up the address entity class and we'll do the same there as well at at setter and at getter. Again, we'll select all of the getters and setters. We'll delete them and we'll save. And finally, let's go ahead back to the terminal. We'll rebuild our project again with dot gradle clean build. And as you can see, there are no compile time errors and it acts as if the setters and getters are all there. Now to prove this point, we'll go ahead and open up the employee service class. This will be under the service package with employee service. We'll open that, we'll go to line 83 and we'll see that on line 81 we declare the address object from the address entity class. This address object is using a dot get for grabbing the IDs, the address types and so forth. And we can see that Lombok has generated all of these and IntelliJ is not complaining.

Contents