Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IntelliJ IDEA find usages of overriding method without finding sibling methods

I have a parent class P with subclasses A and B.

I would like to find all usages of a method f of A.

So either p.f() or a.f() but not b.f(), because an instance B b cannot call A.f.

I know I can find

  • Calls directly to A.f. This misses:

    P p = new A();
    p.f();
    
  • Calls to P.f and any override. This has too many false positives.

I know there are always going to be false positives using only compile-time information, but there could be fewer.

like image 876
Mark Avatar asked Nov 07 '18 09:11

Mark


People also ask

How can I see override methods in IntelliJ?

On the Code menu, click Override methods Ctrl+O . Alternatively, you can right-click anywhere in the class file, then click Generate Alt+Insert , and select Override methods. Select the methods to override (hold the Shift or Ctrl key to perform a multiple select).

How do I list all methods 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. to have protected class members shown in the tree.

How do I search a method in IntelliJ?

Ctrl+Alt+Shift+N : finds a symbol. In IntelliJ IDEA, a symbol is any code element such as method, field, class, constant, and so on. Ctrl+Shift+A : finds an action by name. You can find any action even if it doesn't have a mapped shortcut or appear in the menu.

How do you select a method to override?

quick command search : ctrl + shift + A Type constructor to override all of them quickly :D.


Video Answer


2 Answers

Now it's possible to search usages in more depth. Just hit Ctrl+Shift+Alt+F7 instead of Alt+F7 and check what you want to search for. In your case, uncheck 'Search for base method usages'.

For more information check out the official site: https://www.jetbrains.com/help/idea/find-usages-method-options.html

like image 136
starkm Avatar answered Sep 17 '22 19:09

starkm


You could use Structural Search (Edit | Find | Search Structurally...) for this. Use a query like:

$x$.f()

with expression type filter A|P. This will find all calls to f() on expressions of a type of either A or P. If you have any more classes in the hierarchy between A and P, you will need to add these to the expression type filter also.

like image 42
Bas Leijdekkers Avatar answered Sep 18 '22 19:09

Bas Leijdekkers