15
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.

informatik01
  • 16,038
  • 10
  • 74
  • 104
IttayD
  • 28,271
  • 28
  • 124
  • 178

3 Answers3

17

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!

Pejman
  • 2,442
  • 4
  • 34
  • 62
IttayD
  • 28,271
  • 28
  • 124
  • 178
  • 2
    For people of the future - Structural Search only works in Ultimate edition of IntelliJ. – Vic Jul 05 '12 at 08:33
  • 1
    This also works in Community Edition as of the 14 EAP (build IC-138.2210.3. I don't know if it'll be taken out in the Ultimate edition for 14. – yshavit Oct 15 '14 at 19:08
8

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

MalsR
  • 1,197
  • 1
  • 12
  • 23
  • 1
    Find Usages returns only creation of instances of that class, not of subclasses of this class (or trait) – IttayD Apr 01 '12 at 06:23
  • @IttayD under find usages instantiated subclasses of the 'abstract' class should be listed under 'local variable declaration' for e.g. I did give this a try and it listed all initialised instaces of subclasses. Maybe you can paste some e.g. code you have which does not lists under find usages. thanks – MalsR Apr 01 '12 at 12:36
0

You can create empty default constructor of Base and press Ctrl+Alt+H (Hierarchy Callers). Then you'll see all creations of A and B in as a tree.

Vic
  • 21,473
  • 11
  • 76
  • 97