asdasasd
asdasasd

Reputation: 81

Auto remove getter-setters with lombok?

I have a project. I don't use Lombok. I use manually created getters and setters.

Now, I want to import Lombok. After I import, I will use for all entities.

Do I have to manually add @Data to all entities? And I have to remove all entities manually? Is not there another way to automatically convert those classes?

I could not find any questions like this:

https://www.google.com/search?q=lombok+automatic+remove+getter+site:stackoverflow.com&num=100&safe=off&sa=X&ved=2ahUKEwjXyqKT39bfAhXysYsKHWw4Ah0QrQIoBDAKegQIPxAM&biw=1497&bih=762

Upvotes: 3

Views: 3313

Answers (4)

peer
peer

Reputation: 4719

I am not aware of any IDE that can convert a whole project so I wrote a command line tool based on JavaParser: lomboker. So far it doesn't implement conversion to @Data but all the steps towards it: removing getters, setters, noargsconstructors, toString and EqualsAndHash and adding the respective annotations and their imports. So it should be fairly easy to extend it to cover @Data as well.

Upvotes: 0

naXa stands with Ukraine
naXa stands with Ukraine

Reputation: 37936

In IntelliJ IDEA with Lombok plugin installed you should have this feature:

  1. Navigate to the target class
  2. menu Refactor | Lombok | Default @Data

This way you can refactor your classes one by one. I don't know if there're any existing batch refactoring tools for Lombok.

If you're interested in batch refactoring, please upvote issue #574 in Lombok plugin tracker. Or even contribute code to this project.

refactor menu
(source: github.io)

Upvotes: 9

Carl
Carl

Reputation: 36

The best way in IntelliJ IDEA is using the shortcut Command + Shift + A and choose the refactor you want.

see this screenshot

Upvotes: 1

maaartinus
maaartinus

Reputation: 46432

As an entity has on average maybe ten fields, there's a lot of text to be removed per class. Using standard formatting and a single line between methods, these 10 getters and ten setters mean 10 * 2 * 4 = 80 lines per class. All you have to add a single @Data annotation.

So I'd concentrate on the removal and write a simple regex recognizing trivial getters and setters. Adding the annotation to all modified files afterwards is something I'd gladly do manually even for tens of entities. Forgetting one is no problem as it leads to obvious compile-time errors.


A trivial untested regex for trivial getters:

[ \t]+public [\\w<,> ]+ get[A-Z](\\w+)\\(\\)\\s*\\{\\s*return\\s+[a-z]\\1;\\s*\\}\\s*\n

No such regex can be perfect (mine breaks e.g., when arrays get returned or when you use non-standard formatting or naming; this is easily fixable, but other problems may come). Nonetheless, it can do its job.

My regex allows no comments in the method body, but I wouldn't call a commented getter trivial.

Upvotes: 1

Related Questions