Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find usages of inherited method only from a specific subclass?

I have a superclass which is inherited by many subclasses. I would like to find all calls to a certain method in this superclass which originate from instances of a specific inheriting class. Is this possible in VS2012 (with Resharper 7.1)?

Code example:

public class Super
{
    public void foo(Arg a)
    {
        ...
    }
}

public class Sub1 : Super
{
    ...
}

public class Sub2 : Super
{
    ...
}

public class SomeClass
{
    public void Run()
    {
        ...
        var sub1 = new Sub1();
        sub1.foo(a);

        var sub2 = new Sub2();
        sub2.foo(b);
    }
}

I would like to find only the call sub2.foo(b) not sub1.foo(a) in the example above.

like image 654
Polymorphix Avatar asked Dec 17 '14 08:12

Polymorphix


People also ask

Do subclasses inherit protected fields?

A subclass does not inherit the private members of its parent class. However, if the superclass has public or protected methods for accessing its private fields, these can also be used by the subclass. A nested class has access to all the private members of its enclosing class—both fields and methods.

Do subclasses inherit variables?

A subclass also inherits variables and methods from its superclass's superclass, and so on up the inheritance tree.

Is subclass and inheritance same?

A subclass is the same as an inherited class. In example A, bar is an inner class.

What does a subclass inherit from its superclass?

lang. Object is the root of a hierarchy of classes. The subclass inherits state and behavior in the form of variables and methods from its superclass. The subclass can just use the items inherited from its superclass as is, or the subclass can modify or override it.


1 Answers

You should be able to use Structural Search and Replace to set up a pattern to find the usages. Go to ReSharper -> Find -> Search with Pattern. Create a pattern such as $exp$.Foo($args$). Then add an "expression" placeholder for exp. You can specify what type this should be, and check the tick box to specify exact type. Here you'd enter the fully qualified type All.Your.Namespaces.Sub2. Then add an "arguments" placeholder for args. Leave everything unchecked - it will match any number of arguments. Clicking find should find all calls to Foo from any expression that matches Sub2.

like image 152
citizenmatt Avatar answered Oct 18 '22 12:10

citizenmatt