Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace getters with property with IntelliJ Structural Search and Replace

Due to changes in a library I'm using, the getters were replaced with direct access to the property. Eg:

public class MyLibraryClass {
  private String field;

  public String getField() { return field; }
}

has become

public class MyLibraryClass {
  public String field;
}

Consequently I'd want to use IntelliJ's SSR to replace the getter calls with the Bean property. However I've gotten stuck on how to use regexp capture groups for my replacement. If there's another IntelliJ shortcut to do what I want, I'm happy to use it.

The following SSR search parameters find the getters, but I can't seem to be able to use the capture group for $property$ in the replacement. I've tried \1 and \$1 and $1 see (using backreferences in IntelliJ)

I also tried changing the search to $instance$.get$property$() (clearing the constraints for $property$ but the search didn't return any results.

  1. How can I access the capture group ($1)
  2. Is it possible to perform any processing on $1 before substitution (to fix the case)

I noticed the Script text option which accepts Groovy code, however I couldn't find any documentation on this feature.

SSR search templateinstance variable configproperty variable config

like image 850
kierans Avatar asked Jun 06 '14 03:06

kierans


People also ask

How do I use structural search in IntelliJ?

From the main menu, select Edit | Find | Search Structurally. In the Structural Search dialog, under the Java | Class-based node, select All Fields of a Class which will look for all field declarations and click Find. As a result, in the Find tool window, IntelliJ IDEA shows all the fields declared in our Java code.

How do I find and replace in IntelliJ?

Press Ctrl+R or select Edit | Find | Replace from the main menu to open the Replace in File window. located in the replace field. for a multi-line replace. For example, if you want to replace a comma with a comma and a new line, enter a comma in the search field and a comma and the new line in the replace field.

Where is find and replace in Mac IntelliJ?

Press Ctrl+H or choose Edit | Find | Replace from the main menu. The search and replace pane appears on top of the active editor. If necessary, specify the search and replace options. In the search field, start typing the search string.


1 Answers

Put in the replacement template $instance$.$field$. This template introduces new variable field which should contain following script text:

String name = property.methodExpression.referenceName.substring(3)
name[0].toLowerCase() + name.substring(1) 
like image 71
parsons Avatar answered Sep 24 '22 11:09

parsons