Skip to content

Instantly share code, notes, and snippets.

@62mkv
Last active February 21, 2022 16:27
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 62mkv/d1cdbac42e225d157226b3f8337c84af to your computer and use it in GitHub Desktop.
Save 62mkv/d1cdbac42e225d157226b3f8337c84af to your computer and use it in GitHub Desktop.
IntelliJ IDEA Receipts

Examples of Structural Search and Replace (IntelliJ IDEA 2017)

Use case 1 (Search)

Find all *DTO classes, that have a (at least one) public field that is neither @ApiModelProperty, nor a @JsonIgnore

  1. Open Edit->Find->Search structurally

  2. Paste this into Search template field:

    class $Class$ {
        @$FieldAnnotation$ public $type$ $field$;
    }
  3. Click Edit Variables:

    1. for variable Class: provide a regex for a class name: .+DTO
    2. for variable FieldAnnotation:
      • Text/regexp: ApiModelProperty|JsonIgnore
      • Minimum count/Maximum count: 0/0
  4. Choose Scope (see here to define a scope)

  5. Click Find

  6. If you're happy with results, click Save Template... and provide a template name

Use case 2 (Search)

Find all *DTO classes that do not implement toString()

  1. Open Edit->Find->Search structurally

  2. Paste this into Search template field:

    class $className$ { 
        public String $methodName$ ();
    }
  3. Click Edit Variables:

    1. for variable Class: provide a regex for a class name: .+DTO
    2. for variable methodName:
      • Text/regexp: toString
      • Minimum count/Maximum count: 0/0
  4. Choose Scope (see here to define a scope)

  5. Click Find

  6. If you're happy with results, click Save Template... and provide a template name

Use case 3 (Replace)

Refactor string literals in some context into using enum constants instead

  1. Open Edit->Find->Replace structurally

  2. Paste this into Search template field:

    map.put("$field$","$a$");
  3. Paste this into Replace template field:

    map.put(FieldNames.$ENUMFIELD$, "$a$");
  4. Click Edit variables:

    1. for variable ENUMFIELD, provide Script text as follows:

      import static com.google.common.base.CaseFormat.LOWER_CAMEL as LOWER_CAMEL
      import static com.google.common.base.CaseFormat.UPPER_UNDERSCORE as UPPER_UNDERSCORE
      
      LOWER_CAMEL.to(UPPER_UNDERSCORE, field.getInnerText())
    2. Click OK

  5. Click Find

  6. Check validity of the results and replace when necessary

Use case 4 (Replace)

Remove public access specifier on methods annotated with Test

  1. Open Edit->Find->Replace structurally
  2. Paste this into Search field:
        @$MethodAnnotation$
        public void $MethodName$(@mockit.Injectable $ParameterType$ $Parameter$) {
            $statement$;
        }
  3. Choose file type: Java - Class Member
  4. Add filter for MethodAnnotation variable: Text=org.junit.jupiter.api.Test
  5. Add Min=0 for statement and Parameter variables
  6. Paste this into Replace field:
        @$MethodAnnotation$
        void $MethodName$(@Injectable $ParameterType$ $Parameter$) {
            $statement$;
        }
    (mind the indentation, otherwise it will be screwed!)
  7. Select Complete match value for Search target
  8. Find

NB: for some reason, I could not make it "parameter annotation" agnostic... that's why @Injectable annotation is hardcoded in the template

How to define a scope

  1. On a Structural Search window, click on ellipsis ... button next to Scope combobox
  2. Click + button (Add Scope) or press Ins
  3. In order to include only specific folder (with subfolders) in a search, put a construction into Pattern field:
    • file[modulename]:src/main/java/com/my/package//*

How to use saved templates

  1. Open Edit->Find->Search structurally
  2. Click Existing templates
  3. Select necessary template and click OK
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment