How do I fix imports after refactoring?

I did a massive search/replace of one method with another. That went perfectly. However, then all the classes that used the new method were missing an import statement for the class (its a static method call). Is there any way to bulk update the packages so that they import the new class?

5 comments

Andy Kriger wrote:

I did a massive search/replace of one method with another. That went perfectly. However, then all the classes that used the new method were missing an import statement for the class (its a static method call). Is there any way to bulk update the packages so that they import the new class?


If you can find a way to use the automatic refactorings to accomplish
the 'search/replace', then you can avoid such problems. If fixing the
imports manually (Alt-Enter on each file) is too much work, then you
might want to undo your changes (you can possibly use Local History or
your version control system to do this), and try it again using
automatic refactorings.

Some refactorings you should know about that would help: Rename (works
on methods of course), Make Method Static, Move (for methods, only works
if it is static; next version is supposed to support it for instance
methods I think), Convert to Instance Method, Extract Method, Inline
(good for replacing a call to a method with the code it contains, such
as a call to some other method), Replace Constructor with Factory Method.

Some combination of these refactorings can usually accomplish what you
need. If these methods don't work, you can use Structural Search and
Replace. Use the class' fully-qualified name (e.g.
myproj.tools.MyClass), and the SSR will replace it with the short name
and an import. Generally, using FQ names is a good idea with IDEA's
tools because it usually does FQ name shortening, e.g. live templates, etc.

Or you could just use simple search and replace and use the FQ name, and
maybe run a code analysis to detect unneccessary FQ names.

--
Rob Harwood
Software Developer
JetBrains Inc.
http://www.jetbrains.com
"Develop with pleasure!"

0

Rob Harwood (JetBrains) wrote:

Some refactorings you should know about that would help: Rename (works
on methods of course), Make Method Static, Move (for methods, only works
if it is static; next version is supposed to support it for instance
methods I think), Convert to Instance Method, Extract Method, Inline
(good for replacing a call to a method with the code it contains, such
as a call to some other method), Replace Constructor with Factory Method.


I knew I would forget one. Another very important refactoring is Change
Method Signature. It's like an uber-Refactoring for methods.

--
Rob Harwood
Software Developer
JetBrains Inc.
http://www.jetbrains.com
"Develop with pleasure!"

0

Andy Kriger wrote:

I did a massive search/replace of one method with another. That went perfectly. However, then all the classes that used the new method were missing an import statement for the class (its a static method call). Is there any way to bulk update the packages so that they import the new class?


If you had a bunch of calls to A.foo(), that you wanted to replace with a call to B.bar(), the simplest way is to replace the
contents of A.foo() with a call to B.bar() and then "refactor"->"inline" A.foo().

/Kreiger



Attachment(s):
signature.asc
0

What started this problem was replacing a method call in one class with a method call in another class that had a different signature. I used the structural replace because that looked like the only way to copy over parameters but use them in a different way.

With the tips here, I ended up Find/Replacing calls to the new class with the FQN and then using Code Analyze to strip those FQNs back to a single import statement. In the future I'll do these replacements using the FQN.

thx for the advice

0

It's probably too late now. You should've used Structural Search & Replace,
I think it would've handled this for you. You might still be able to do this,
somehow, but I don't know how you would.

-Keith

I did a massive search/replace of one method with another. That went
perfectly. However, then all the classes that used the new method were
missing an import statement for the class (its a static method call).
Is there any way to bulk update the packages so that they import the
new class?



0

Please sign in to leave a comment.