Search code examples
javaintellij-ideatostringintellij-inspections

How can I get IntelliJ to flag any implement of toString()?


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


Solution

  • You can create your own inspections.

    Go to Settings -> Editor -> Inspections, and click on the "+" button:

    enter image description here

    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 toString in it, and replacing it with the same class but with that method removed, and adding a @ToString annotation.

    Find:

    class $ClassName$ {
        public String toString() {
            $Statements$;
        }
        $OtherMembers$
    }
    

    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 matches final toString methods, among other things.)

    Replace with:

    @lombok.ToString
    class $ClassName$ {
        $OtherMembers$
    }
    

    enter image description here

    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:

    public String $toString$() {
        $Statements$;
    }
    

    There is a text modifier on $toString$ making sure that its text is always toString. Doing it this way allows you to set the target of the match to $toString$, highlighting only the toString part. Again, there is a count modifier on $Statements$. Make sure that the "Language" option is set to "Java - Class Member".

    enter image description here

    Finally, here is the XML file exported from the above inspections. You can import this in the inspection settings page.

    <component name="InspectionProjectProfileManager">
      <profile version="1.0">
        <option name="myName" value="Some Name" />
        <inspection_tool class="SSBasedInspection" enabled="true" level="WARNING" enabled_by_default="true">
          <searchConfiguration name="No toStrings" uuid="7d07a3df-3fe8-3f60-9714-6cd79f56ae5e" text="public String $toString$() {&#10;    $Statements$;&#10;}" recursive="true" caseInsensitive="true" type="JAVA" pattern_context="member">
            <constraint name="__context__" within="" contains="" />
            <constraint name="Statements" minCount="0" maxCount="2147483647" within="" contains="" />
            <constraint name="toString" regexp="toString" target="true" within="" contains="" />
          </searchConfiguration>
          <replaceConfiguration name="No toStrings wtih replacement" uuid="6ad09c43-eac2-3f8f-b2c3-8061201ea2ad" text="class $ClassName$ {&#10;    public String toString() {&#10;        $Statements$;&#10;    }&#10;    $OtherMembers$&#10;}" recursive="false" caseInsensitive="true" type="JAVA" pattern_context="default" reformatAccordingToStyle="true" shortenFQN="true" replacement="@ToString&#10;class $ClassName$ {&#10;    $OtherMembers$&#10;}">
            <constraint name="__context__" within="" contains="" />
            <constraint name="Statements" minCount="0" maxCount="2147483647" within="" contains="" />
            <constraint name="ClassName" within="" contains="" />
            <constraint name="OtherMembers" within="" contains="" />
          </replaceConfiguration>
        </inspection_tool>
      </profile>
    </component>