Extract field: use top level interface/class as reference instead of actual implementation?


In Java, consider this simple architecture:


* An interface called Foo

* An implementation of this interface called DefaultFoo

* A mock implementation (just for the sake of it) called MockFoo



If I create a new DefaultFoo, then I would use the reference Foo, like this:

Foo foo = new DefaultFoo();


Now consider I want to lazy initialize this member variable. Instead of declaring Foo foo as a member variable, I want to create a local instance of DefaultFoo, and then use Extract field which would move this up to a member variable, omitting that extra declaration step. The problem is the reference would look like this:

DefaultFoo foo;

I have to manually remove Default. Is there any way to customize the Extract field to use the top level interface/class as reference instead of the actual implementation? The flow would look like this:

* new DefaultFoo(); (local variable)

Extract field

Foo foo; (as a member variable)

* foo = new DefaultFoo(); (lazy initializing)


There is dedicated Refactor | Use Interface Where Possible... action available when you are in DefaultFoo class, but it is not possible to invoke it at once when you are extracting a field. Feel free to file a request at https://youtrack.jetbrains.com/issues/IDEA


Thank you for the information. I will file a request!


I see this refactor available as documented for Java. However in a mixed Java / Kotlin project this doesn't seem to detect Kotlin uses. Is there another way to do this that includes Kotlin support?