We use Lombok, and my architect wants us to always use the Lombok @ToString instead of custom implementation.
I want IntelliJ to flag any toString() implementation as a warning.
How do I do that?
I use IntelliJ Ultimate 2023.3
We use Lombok, and my architect wants us to always use the Lombok @ToString instead of custom implementation.
I want IntelliJ to flag any toString() implementation as a warning.
How do I do that?
I use IntelliJ Ultimate 2023.3
Copyright © 2021 Jogjafile Inc.
You can create your own inspections.
Go to Settings -> Editor -> Inspections, and click on the "+" button:
Choose either "Add Structural Search Inspection" or "Add Structural Replace Inspection" depending on whether you want a "quick fix" to the problem.
Here's an example of finding a class with a
toStringin it, and replacing it with the same class but with that method removed, and adding a@ToStringannotation.Find:
Note that there is a count modifier on
$Statements$allowing 0 or more instances(Since this is powered by Structural Search,
public String toString()also matchesfinaltoStringmethods, among other things.)Replace with:
Note that this will highlight the whole class, because the whole class needs to be replaced.
Alternatively, you can just find instances of
toString. You just need to find:There is a text modifier on
$toString$making sure that its text is alwaystoString. Doing it this way allows you to set the target of the match to$toString$, highlighting only thetoStringpart. Again, there is a count modifier on$Statements$. Make sure that the "Language" option is set to "Java - Class Member".Finally, here is the XML file exported from the above inspections. You can import this in the inspection settings page.