AndiCover
AndiCover

Reputation: 1734

How to rename all package names to lowercase

I have a large project with many packages that are CamelCase. I want to rename all of them to lowercase e.g:

main.MyPackages.Utils to main.mypackages.utils


What is the best/fastest way to do so?

  1. The first approach I was thinking about was using the IDE (Eclipse in my case), but this is a lot of work and does not always work because in some cases it thinks the package already exists.
  2. The second approach would be to write a program myself which handles this. The program should rename all folders to lowercase and replace all import statements in java files with lowercase packages. Sounds easier than it actually is, because renaming/moving non empty directories requires some extra work and on Windows it is not possible to create a new directory with lowercase name using Java.

Is there a better way than the two mentioned approaches? If not I will go with approach two. Maybe using a UNIX OS would be better.

Upvotes: 3

Views: 1614

Answers (3)

AndiCover
AndiCover

Reputation: 1734

Renaming all packages using Eclipse:

I tried to rename the packages using Eclipse. Soon enough I faced the problem with this approach. First merge of master branch in my local branch restored all old packages again. The idea was to rename the packages on a weekend when no one works on the project. But it still would have taken 5+ hours per branch and I had to change at least the master and the last release branch to prevent merge conflicts. I realized that this does not work in my case.

Renaming all packages using a custom Java program:

I wrote a Java program which performs following steps:

  1. In the first iteration through all directories it copied all folders to a new directory with only lowercase names (The first subpackage was completely renamed to prevent problems on Windows hosts).
  2. Then the program started a second iteration through the new directory and renamed all import packages in Java files. For most imports this is quiet simple but I needed to use reflection to correctly rename static imports and imports of Enums in Java classes e.g import my.package.MyClass.MyEnum.
  3. The next step of the program was to rename all other occurrences of the old package names found in Eclipse e.g. TestNG XML files, ...
  4. The last step was to delete the old folders.

The runtime of the program is about 2 minutes per branch. The updated project compiles and I did not face any issues.


I did not add any code because the program is only working for this project. Maybe it still helps someone.

Upvotes: 1

ZhekaKozlov
ZhekaKozlov

Reputation: 39536

This can be performed with two steps using an intermediate package name:

  • Right-click on the package main.MyPackages.Utils and select Refactor -> Rename...
  • Enter main.mypackages2.utils2 (check Rename subpackages if you have subpackages) and click OK

Now repeat the same and rename main.mypackages2.utils2 to main.mypackages.utils.

Upvotes: 0

sandeep thawait
sandeep thawait

Reputation: 1

In my case i use IntelliJ Idea IDE and refactoring mentioned is lot easier. You just have to right click and change the package name as you mentioned, this will update package name every where this was being used. Let me know if it works. See the screenshot for reference

Upvotes: 0

Related Questions