Вопрос

I have a large body of Java code that uses a class XXX. Over time, the meaning of the class has changed, and the name is no longer appropriate. To make the code more readable, I would like to change the name of the class to YYY.

One option is to edit all the files in a single pass and change XXX to YYY, but the actual name of the class is not unique -- there are other classes with the same name, different package -- so I need to look at code. I would like to be able to make some changes, test that it is correct, and change some more, test some more, etc. During this transition time, I want to be able to use YYY and XXX interchangeably. Eventually I would eliminate the XXX.

What I need is an "alias" concept, where I could make YYY and alias of XXX, but Java does not have this.

I could make YYY a subclass of XXX with all the same constructors and methods. That will work fine where a variable is declared XXX, and I assign a value of type YYY. It will also work where a method parameter is XXX, and I pass a YYY. But it will not work the other way around when the parameter is declared YYY and I have an XXX value to send.

I could inject YYY into the inheritance, so that XXX extends YYY, but I have the same problem as above: in code that has mixed use of XXX declarations and YYY values there is trouble.

The transition time will take a couple of months. During this time, I don't care about performance or efficiency, I just need it to compile and run correctly enough to test. Any suggestions for a best practice to allow for incremental change of a class name in a body of Java code?

UPDATE

The answer below is that while there is no way to do this in Java, there also should be no reason to either. I followed up by installing and refactoring the code, and wrote up the experience in this blog post: Don’t Suffer Poor Names

Это было полезно?

Решение

Use an IDE such as Eclipse and use its refactoring tools to rename the class. This will automatically change it everywhere it's used, update imports, etc.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top