Bug 44201 - [replace invocations] Add refactoring to replace call sites of a method with an existing method
Summary: [replace invocations] Add refactoring to replace call sites of a method with ...
Status: ASSIGNED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 3.0   Edit
Hardware: All All
: P3 enhancement with 1 vote (vote)
Target Milestone: ---   Edit
Assignee: JDT-UI-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2003-10-05 11:02 EDT by Gary Gregory CLA
Modified: 2011-10-06 07:38 EDT (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Gary Gregory CLA 2003-10-05 11:02:06 EDT
The user story is: I am porting client code of our internal String util library
to the Apache Jakarta Commons Land StringUtil. I want to replace all calls of
OurStringClass.isEmpty(String) with Apache's StringUtils.isEmpty(String); and
others too.

This is could be possible if Refactor/Move... would itself be refactored such
that I could pick an /existing/ method (as opposed to just a class now). Perhaps
the new refactoring could be called Refactor/Replace... where the dialog's class
list would have each class be a tree node which would expand to show methods
(static and instance methods both).

The workaround I use now is to replace the old method with a call to the new
method and then do a Refactor/In line... This only works in the case where the
arguments are the same of course. So either only matching argument methods need
to be enabled in the tree node or the UI gets more complicated since params need
to reordered.

Thank you for considering this request.
Comment 1 Martin Aeschlimann CLA 2003-10-06 05:16:33 EDT
+1 for that! I had exactly the same problem when porting our code to the new
codeformatter.
I would very hepful to have a refactoring that allows to redirect all calls to a
  method to a other method.

Reordering or even be able to give expressions how parameters should be mapped
would be great, but I would keep it simple simple and not offer it. What the
user can do is the following:
- create first a helper method in a utility class, that has the same signature
as the source method. The implementation of this utility class calls the target
method (including the reordering and calculation of the parameters)
- redirect all calls to the source method to the helper method
- inline the helper method.



Comment 2 Dirk Baeumer CLA 2003-10-06 17:39:33 EDT
I don't really see how this refactoring can do a better job than using the 
inline "workaround", except that you can't inline a whole class. Specify an 
expression how arguments are mapped is exactly what you would code in the body 
of the old method.

In general what you are asking for is the find/replace in ASTs where you can 
use some sort of regexp for both the pattern to be found and the replacing AST 
node.

Have you looked at Inject/J. They claim that they have a Eclipse plug-in as 
well.
Comment 3 Gary Gregory CLA 2003-10-08 15:11:33 EDT
How would you use the in-line workaround to replace one static reference with
another? Should this case I just encountered be addressed in a separate ticket?
Comment 4 Gary Gregory CLA 2003-10-08 15:12:33 EDT
Sorry, I should have been more precise, in my case replacing a 'public static
final String' with another in a different class.
Comment 5 Martin Aeschlimann CLA 2006-06-12 05:51:18 EDT
this is planed to be implemented for 3.3: Replace invocations
Comment 6 Joachim Sauer CLA 2011-10-06 05:14:50 EDT
Has there been any progress on this issue? It *seems* to have been planned for 3.2/3.3/3.4 but never landed and I never found anything stating if or why it was dropped.

In my particular case I'd love to be able to effectively do a "Introduce Factory", but the factory method *already exists* and is in a binary-only reference (i.e. replace object instantiation with a specific constructor with an invocation of the existing factory method).

I've explained my problem in some detail on this SO question:
http://stackoverflow.com/questions/7566565/is-there-a-way-to-apply-a-refactoring-script-just-for-its-side-effects

I don't know if this issue even includes my use case, but even if it doesn't work with constructors, I could still see this feature as being very useful.

Especially in combination with refactoring scripts: developers of a library could distribute a set of refactoring scripts that do at least the trivial steps of conversion to the new version.
Comment 7 Markus Keller CLA 2011-10-06 06:35:21 EDT
We once started work on Replace Invocations, but never found the time to finish it.

Manual workaround:
- apply Refactor > Introduce Indirection... on the method to replace
- in the generated indirection method, replace the body with a call to the better implementation
- apply Refactor > Inline... on the indirection method