1

I'm using Eclipse and Java, but the answer doesn't need to be specific to those. I'm sure platform specific answers will get plenty of upvotes if they're far quicker than non-platform specific ones.

Right now I'm using

    grep "classname" find . -regex .*\.java > uses_classname.txt

to find code that might be affected by a change to the class. If there are only a few lines returned by this, I can look at it manually as it is. Otherwise, I can grep again to find the specific methods I'm modifying. Is there a better way to do this?

Anonymous
  • 3,334
  • 3
  • 35
  • 50
  • Assuming that by 'refactoring` you mean 'renaming': In Eclipse, Right-click the method name, References -> Workspace? – The Nail Jan 03 '12 at 22:09
  • What about... performing the refactoring? If you are using version control and you don't have any uncommitted changes, all modified files are the ones touched by refactoring. – Tomasz Nurkiewicz Jan 03 '12 at 22:12
  • Tomasz, that assumes you've checked out the whole code base into your workspace, rather than a portion of the code base that compiles independently. (Which may itself not be the best practice.) – Anonymous Jan 03 '12 at 22:23
  • @TheNail, I don't mean renaming, I mean possibly eliminating a method or changing parameters or something along those lines. Obviously another possible refactoring would be to leave it as is, and add new methods so the old one can gradually be used in less code. – Anonymous Jan 03 '12 at 23:04
  • Ok, then this Eclipse feature should also be useful. It simply shows you where the method is currently being used. BTW, the `grep`-method suffers from the same limitation as a refactoring in Eclipse if you haven't checked out a portion of the code. – The Nail Jan 03 '12 at 23:07

2 Answers2

2

[Just being a bit of a librarian here:]

Finding references to a Java method/class:

How can I find all the methods that call a given method in Java?

How can I find references of a class in Eclipse?

References to Java annotations:

How to Check References of Annotated Methods

Finding references to 'derived' methods in Eclipse:

Eclipse Specific: Is it possible to find references of use of derived methods in a class?

How to do it with VIM:

Is it possible to find usage of java classes or methods in VIM?

And with Emacs:

How can I find the references of a class, method, variable in Emacs with Etags?

Finding (the number of) references to a method in C#:

How do you programmatically identify the number of references to a method with C#

Nice to read: how to find methods that are not used in C#/.NET:

Find unused code

Is there a tool for finding unreferenced functions (dead, obsolete code) in a C# app?

On refactoring of PHP code:

Tools for PHP code refactoring

Finding references in Smalltalk:

Find references to string/symbol/method

Finding references in (Apple) Xcode seems to be a popular question:

Find method references in Xcode

Xcode: view references for a variable?

Finding all references of a variable or a method in Xcode4

Community
  • 1
  • 1
The Nail
  • 8,355
  • 2
  • 35
  • 48
2

A good solution is to use JUnit and practice test-driven development. After refactoring, re-running your test cases will tell you whether any existing functionality has been damaged. Moreover, since you are actually testing the code rather than grep'ing over method names, you can get full backtrace, making debugging much easier. A good introduction to JUnit can be found here.

user2398029
  • 6,699
  • 8
  • 48
  • 80
  • I like the idea of using unit tests to verify that things are still working after the refactor, but that does not answer the question: How long is it going to take? which generally is answered by knowing which classes are going to be refactored and how many times they are being used in your software. – Alexis Wilke Apr 18 '18 at 00:44