在 Eclipse 中重构“包结构”以将包的子包重定位到其父包

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5970175/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 16:17:37  来源:igfitidea点击:

Refactor "package structure" in Eclipse to relocate a package's child package to its parent package

javaeclipserefactoringpackagespackage-structuring

提问by Chantz

In Eclipse's "Package Explorer", let's say I have a list of packages like this:

在 Eclipse 的“包资源管理器”中,假设我有一个这样的包列表:

  • com.animal.dog
  • com.animal.cat
  • com.animal.frog
  • com.animal.dog
  • com.animal.cat
  • com.animal.frog

If I want to refactor this package structure so that the final package structure looks like

如果我想重构这个包结构,使最终的包结构看起来像

  • com.dog
  • com.cat
  • com.frog
  • 青蛙

So essentially removing the parent package & attaching the child packages to its grandparent. I have many child packages in "com.animal" package & I would like to avoid if possible doing this manually & then also changing the package imports everywhere.

因此,基本上删除父包并将子包附加到其祖父包。我在“com.animal”包中有很多子包,如果可能的话,我想避免手动执行此操作,然后还要更改各处的包导入。

回答by Bozho

If you switch your package representation to hierarchical(from arrow on the top-right), you can right-click the com.animalpackage and rename it to com.

如果您将包表示切换为hierarchical(从右上角的箭头),您可以右键单击com.animal包并将其重命名为com.

回答by Grzegorz Oledzki

Probably you don't have any classes directly under com.animal. What you can do is either follow Bozho's suggestion (and switch to hierarchical view) or create a temporary class directly under com.animal. This way Eclipse would present a separate entry for com.animal.

可能你没有任何直接在com.animal. 您可以做的是遵循 Bozho 的建议(并切换到分层视图)或直接在com.animal. 这样 Eclipse 将为com.animal.

Having selected com.animalyou can simply do the rename refactoring. Make sure you select the 'Rename subpackages' checkbox. enter image description here

选择后,com.animal您可以简单地进行重命名重构。确保选中“重命名子包”复选框。 在此处输入图片说明