Denis Bazhenov
Denis Bazhenov

Reputation: 9945

Finding Annotations in IntelliJ IDEA

I can easily find all mentions of some annotation in my project using SSR (structural search and replace). For example I have following spring based code:

class DashboardController {

  @RequestMapping("/dashboard")
  public void doDashboard() {
    [...]
  }
}

If I search by pattern org.springframework.web.bind.annotation.RequestMapping than I will find my code. But what if I want to find methods annotated with parametrized annotation, so find only method with annotations @RequestMapping for "/dashboard" url?

I can simply search by @RequestMapping("/dashboard") string, but annotation can be written in several ways:

@RequestMapping("/dashboard")
@RequestMapping(value = "/dashboard", method = {RequestMethod.POST})
@RequestMapping(headers = "content-type=application/*", value = "/dashboard")

etc.

Upvotes: 5

Views: 3521

Answers (3)

Bas Leijdekkers
Bas Leijdekkers

Reputation: 26462

Use Structural Search and Replace in IntelliJ IDEA 14. Copy the existing template annotated methods and add "/dashboard" as an argument to the annotation like so:

class $Class$ {
  @$Annotation$("/dashboard")
  $MethodType$ $MethodName$($ParameterType$ $ParameterName$);
}

You might also want to edit the $Annotation$ variable and give it the text/regexp "RequestMapping" This should find all possible formats of the annotation (on methods only).

Upvotes: 3

Colin Hebert
Colin Hebert

Reputation: 93157

Why don't you search this :

@RequestMapping\(((.*?)value\s*=\s*)?"/dashboard"(.*?)\)

Upvotes: 1

Jon Freedman
Jon Freedman

Reputation: 9687

Why not just do a text search for @RequestMapping("/dashboard"), its unlikely to give that many false positives.

Upvotes: 0

Related Questions