Background
Hundreds of class files need to be renamed with a prefix. For example, rename these:
- com.domain.project.Mary
- com.domain.project.Jones
- com.domain.project.package.Locket
- com.domain.project.package.Washington
to these:
- com.domain.project.PrefixMary
- com.domain.project.PrefixJones
- com.domain.project.package.PrefixLocket
- com.domain.project.package.PrefixWashington
Such a rename could use a regular expression on the corresponding .java filename, such as:
(.+)\.(.+) -> Prefix$1\.$2
Problem
Most solutions describe renaming files. Renaming files won't work because it leaves the class references unchanged, resulting in a broken build.
Question
How would you rename Java source files en mass (in bulk) so that all references are also updated, without performing hundreds of actions manually (i.e., one per file)?
Ideas
- refactobot is inscrutable, but looks promising.
- Spoon's Refactoring API is too buggy and introduced broken code.
- JavaParser doesn't appear to have a concept of related references. Renaming the class resulted in only the class name being changed, but not its constructor, much less other references. This would require a visitor pattern, but even so the output from JavaParser loses formatting and may introduce other issues.
- CodART could help refactor the class names.
- Rename the files using a regular expression and the
findcommand, then use an IDE to fix all the problems. I couldn't see a way for IntelliJ IDEA to correct errors in bulk, which means fixing hundreds of issues one at a time. - Use IntelliJ IDEA's "replace structurally" functionality. This doesn't perform refactoring, resulting in a broken build. Also, there's no easy way to distinguish between files that have already been renamed and files that haven't: you have to rename by selecting classes in batches.
- Use IntelliJ's RenameProcessor API to perform renaming. There doesn't appear to be a fine-grained separation of packages that can be pulled from a central repository.
- Use an IDEA plug-in, such as RenameFilesRefactorBatch. The plug-in has been updated to support regular expressions, making it the most promising candidate.
IDEA
The main stumbling block with using IDEA is that although it can detect the problems as "Project Errors" when renaming the files, it offers no way to resolve the all the errors at once:
The screenshot shows Glue and Num having been renamed to KtGlue and KtNum, respectively. There's no way to select multiple items, and the context menu does not have an option to automatically fix the problems.
