Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IntelliJ IDEA: how to find all instance creation of a class?

abstract class Base {}

class A extends Base
class B extends Base

How do I find all places in the code that create Base? (that is, have either new A() or new B())

UPDATE
To make it clear, the above is just and example. I'm interested in a way of searching for object creation of any class, including 3rd party classes that I don't control.

like image 217
IttayD Avatar asked Mar 28 '12 10:03

IttayD


People also ask

How do I get all the methods of a class in IntelliJ?

By default, IntelliJ IDEA shows all classes, methods, and other elements of the current file. To toggle the elements you want to show, click the corresponding buttons on the Structure tool window toolbar. to show class fields.

How do I find class references in IntelliJ?

Select a symbol for which you want to find usages, right-click the symbol, and select Find Usages from its context menu or press Alt+F7 . Check the results in the Find tool window. ) the results by files, packages, directories, and so on. on the toolbar in the Find tool window or press Ctrl+Alt+Shift+F7 .

How do I search everywhere in IntelliJ?

Use ⇧⇧ (macOS), or Shift+Shift (Windows/Linux), to bring up the Search Everywhere dialog. You can search across Classes, Files, Symbols and Actions. You can also use forward slash to filter the results to a specific area, such as /editor.

How do I show project structure in IntelliJ?

or select File | Project Structure ( Ctrl+Alt+Shift+S ) from the main menu.


2 Answers

Using Structural Search (Edit -> Find -> Search Structurally):

  • Create a template: new $Type$($P$)
  • Edit Type variable: type Base in the text field, check 'Apply constraint within type hierarchy', check 'This variable is target of the search'
  • Edit P variable: type .* in the text field, set Minimum count to 0, check Unlimited under Maximum count.

Voila!

like image 106
IttayD Avatar answered Oct 20 '22 05:10

IttayD


IttayD if I have understood correctly your latest update, what I normally do (IntelliJ 9.0.4) if I have a similar need to yours is Right click on class name and do "Find Usages" and this will list results in the form of usage categories,

  • Variable declaration
  • New Instance creation, to name a few.

As far as I'm aware I do not think there is a specific option/selection choice to fulfill such a usage search check. Thanks

enter image description here

like image 40
MalsR Avatar answered Oct 20 '22 06:10

MalsR