Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IDE refactoring support in a Lombok project

Project Lombok is tempting to reduces boilerplate code in our Java 8 code base. The downside is that it limits tool support (refactoring, static analysis).

For example, in my experiments with IntelliJ, refactoring of fields of a class annotated with @Builder, no longer works. I know of no workaround (you have to manually fix locations, where the old method name of the Builder is used).

Another example is that in Eclipse "find references" on a field does not find the references, but a good workaround is to open the outline and apply "find references" on the generated getter/setter.

My questions:

  • Which refactoring features of the major IDEs (especially Eclipse, IntelliJ) does it break?
  • Are there good workarounds?
like image 406
Philipp Claßen Avatar asked Jul 28 '15 12:07

Philipp Claßen


People also ask

What is Project Lombok used for?

Project Lombok is a java library tool that is used to minimize/remove the boilerplate code and save the precious time of developers during development by just using some annotations. In addition to it, it also increases the readability of the source code and saves space.

What can I use instead of Lombok?

Immutables, Kotlin, Spring, Jackson, and Java 8 are the most popular alternatives and competitors to Lombok.

Is it safe to use Project Lombok?

The short answer is it's perfectly safe to use and I'd highly recommend using it if we're writing Java code. Given that support for JDK 17 has been out for awhile and was released less than a month after the JDK was officially released, the safety of Lombok sticking around is high.


2 Answers

Here's a small workaround to refactor the getter/setter of a variable in a @Data class. This works in eclipse and probably elsewhere as well:

Sample class, where we want to refactor "value" to "value2":

import lombok.Data;
@Data
public class Thing {
    int value;
}

(1) Rename (don't refactor) the variable to something temporary to remove lombak's generated getter/setter for the original name. You'll get compile errors wherever the old getter/setter were referenced, but that is temporary:

@Data
public class Thing {
    int valueXXX; // reference to getValue() are broken for the moment
}

(2) Manually create a dummy getter/setter for the old name. Your compile errors will go away now:

@Data
public class Thing {
    int valueXXX;
    public int getValue() { return 0; }
    public void setValue(int value) {}
}

(3) Use eclipse to refactor your dummy getter/setter. All references in your codebase now use getValue2() and setValue2():

@Data
public class Thing {
    int valueXXX; // 
    public int getValue2() { return 0; }
    public void setValue2(int value) {}
}

(4) Delete the renamed dummy getter/setter and change the variable name from your temporary name to the new one. Now it's all lombakized again:

@Data
public class Thing {
    int value2;
}

Admittedly this is a bit annoying, but it doesn't actually take that long and it sure beats changing hundreds of references by hand.

like image 143
bcb Avatar answered Sep 21 '22 10:09

bcb


One I recently came across:

In IntelliJ (don't know about Eclipse), you can't extract an interface that includes any methods generated by lombok. They don't show up on the relevant dialog.

There is an easy workaround: Let IntelliJ create the methods, extract the interface, revert your class and have it implement the interface again.

like image 30
Nicktar Avatar answered Sep 18 '22 10:09

Nicktar