Need help to find all references and delete some associated code

I have just inherited some ugly code that looks basically like this

static GetInstance() { return new 3rdPartyClass(); }

using( 3rdPartyClass c = FactoryClass.GetInstance() )

Problem is that the Init() method should be called in the factory. In fact I need to add some additional calls to the instance before I return it and they need to come after Init() is called.

There are about 500 various flavors of the SomeClient code in the solution. But they all wrap the factory invocation in a using block that contains an Init() invocation.

So I want to find all usages of FactoryClass.GetInstance() and then, I want to remove the c.Init() line that follows it.

This is obviously going to be a use once and throwaway piece of code but given the looks of the code I have inherited, I expect that I will have other uses for this basic pattern of finding all usages of a method and then mangling the code in the vicinity of the invocation so if someone can someone help me out in figuring out the basics it would be a great help.

I have figured out how to find all usages if I start of the with the GetInstance() declaration selected in my code editor: It looks like this:

solution = context.GetData( DataConstants.SOLUTION );
IDeclaredElement element = context.GetData( DataConstants.DECLARED_ELEMENT );

PsiManagerImpl manager = PsiManagerImpl.GetInstance( solution );
IReference[] references = manager.Finder.FindReferences(
element , element.GetAccessibilityDomain() , NullProgressIndicator.INSTANCE );

What i would like to be able to do is get IDeclaredElement just by knowing its fully qualified name ("Namespace.FactoryClass.GetInstance"). Can I do this and how?

More importantly, once I have my IReference[] array, how do I programmatically inspect and modify the code each reference points to?

1 comment
Comment actions Permalink

P.S. Is there any documentation on the Resharper OpenAPI beyond this forum? I am a reasonably competent developer and feel I could manage this effort on my own if I could find a good API reference and an introduction to the concepts behind the API. As it is I feel really adrift in a huge sea of classes and methods the the Object Browser as my only map.


Please sign in to leave a comment.