Issue
To readers who like this question: I asked this question eight years ago (as of 2022) and there are only three upvotes. Please be aware that there are not many of us who need this feature and it is unlikely this will be supported by IntelliJ (or any modern IDE) for the foreseeable future.
When I use IntelliJ to copy a package which contains subpackages, every internal references to other classes in the same package is converted to an ugly fully-qualified name. Worse yet, this fully-qualified name links back to the old package instead of to the copied class in the new package! Does anyone know a workaround for this problem?
To reproduce this problem, I create a project structure like this:
Where A.java is:
package example;
public class A { }
And B. java is: package example;
public class B {
private A a;
public B(A a) {
this.a = a;
}
}
I then Ctrl-C and Ctrl-V the example folder, and edit the dialog as below:
The copied version of classB now has every instance of A replaced with example.A package example2;
import example.*;
public class B {
private example.A a;
public B(example.A a) {
this.a = a;
}
}
Does anyone have a suggested work-around to avoid this behavior, and simple change the package name, leaving everything else alone? Thanks!
P.S. While version control is usually the best way to track different versions of a program, I want to create an actual copy in this case. I use IntelliJ for teaching programming, and I create multiple versions of small, multi-packaged programs demonstrating how the program improves as I model refactoring techniques.
[Reported on Jet Brains here] 3.
Solution
My latest workaround:
- Set up IntelliJ to show excluded packages
- Exclude the package to be copied (e.g. example)
- Rename the package to be copied to a temporary name (e.g. example_temp). Ignore the warning about names not being updated.
- Copy the package into the src folder. Rename it to the original name (e.g. example) during the copy. Since the original name is no longer excluded, the package will reappear.
- Rename the copy to the new name. (e.g. rename example to example_v2)
- Rename the temporary file back to the original (e.g. rename example_temp to example)
- Remove the exclusion on the original directory (e.g. right-click example_temp->Mark directory as->Cancel Exclusion.)
Answered By - Josiah Yoder
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.