Magnilex
Magnilex

Reputation: 11978

Find classes that does not override method

Is there any way in IntelliJ IDEA to search for classes that does not override a certain method?

Let's say I have these classes:

public abstract class BaseClass {
  public void methodThatMightBeOverridden() {
  }
}

public class ConcreteClassWithOverriddenMethod extends BaseClass {
  @Override
  public void methodThatMightBeOverridden() {
    super.methodThatMightBeOverridden();
  }
}

public class ConcreteClassWithoutOverriddenMethod extends BaseClass {
}

public class AnotherConcreteClassWithoutOverriddenMethod extends ConcreteClassWithoutOverriddenMethod {
}

Is it possible in IntelliJ to find all extensions of BaseClass that does not override methodThatMightBeOverridden()? Note that I would need to find classes even though they do not directly implement BaseClass. In the example above, that would be ConcreteClassWithoutOverriddenMethod and AnotherConcreteClassWithoutOverriddenMethod.

I know how to use type hierarchy to find classes where the method is overridden, but have not found a way to do it the other way around.

I have tried googling for it without any luck. Also, this is a simplified example. In the real code we have many implementations of the sub classes, of which some does not extend the class.

Upvotes: 6

Views: 440

Answers (2)

Magnilex
Magnilex

Reputation: 11978

I found the support for this. By standing on the method and pressing Ctrl + Shift + H (or Navigate -> Method Hierarchy in the menu), you get a nice view of all overriding methods:

Method Hierarchy

Here I put all four classes in a class called Test as below, hence the "in Test" text after the class names. The picture is pretty self explanatory, but the minus sign means that the class does not override the method.

package test;

public class Test {

  public abstract class BaseClass {
    public void methodThatMightBeOverridden() {
    }
  }

  public class ConcreteClassWithOverriddenMethod extends BaseClass {
    @Override
    public void methodThatMightBeOverridden() {
      super.methodThatMightBeOverridden();
    }
  }

  public class ConcreteClassWithoutOverriddenMethod extends BaseClass {
  }

  public class AnotherConcreteClassWithoutOverriddenMethod extends ConcreteClassWithoutOverriddenMethod {
  }

}

Upvotes: 4

Bas Leijdekkers
Bas Leijdekkers

Reputation: 26522

You could use Structural Search. Use a search template like this:

class $A$ extends $B$ {

   void $m$();
}

Where under Edit Variables variable m has text/regexp methodThatMightBeOverridden and min+max occurrences count of 0. And variable B has text/regexp BaseClass and the Apply constraint within type hierarchy checkbox enabled.

Upvotes: 1

Related Questions