使用Java中的import语句自动删除显式的包声明 [英] Automatically remove explicit package declarations with import statements in Java

查看:173
本文介绍了使用Java中的import语句自动删除显式的包声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由其他人创建的项目,其中包括数千个类文件,并且对于每个对其任何类的引用都明确地输出了包名称。看起来代码是反向工程的。有没有一个很好的Java工具来重构代码,以便明确键入的包前缀从类引用中删除并移入import语句。



提前谢谢。 / p>

编辑:



我认为一个例子将有所帮助。我想让进口在顶部,我不在乎有多少进口。

  javax.swing。 JButton button1 = new javax.swing.JButton(); 

想像一下上面的代码,但绝对无处不在,成千上万个类文件中的成千上万行代码。我想要删除所有的前缀,只是有一个不错的导入javax.swing.JButton;在每个类文件的顶部。

解决方案

我不知道这个用例的一个工具,但是我不得不做几个月前相似的东西。




  • 编写脚本或使用regex进行搜索替换以摆脱显式类型的包前缀。


  • 比eclipse使用组织导入来休息。提示:为避免歧义,请设置不超过所需libs的类路径。
    对于约800个课程,我在2小时内完成了。




    • 或获得值得这样做的人。


    $编辑:您应该知道,在Prefeneces / Java / Editor / Save Actions中,组织导入可以配置为保存操作。


    I have a project created by others that includes thousands of class files and has the package names explicitly typed out for every reference to any of their classes. It looks like the code was reverse engineered. Is there a good tool for Java that refactors the code so that the explicitly typed package prefixes are removed from class references and moved into import statements.

    Thank you in advance.

    EDIT:

    I think an example will help. I want to have the imports at the top, and I don't care how many imports there are.

    javax.swing.JButton button1 = new javax.swing.JButton();
    

    Imagine the code above but absolutely everywhere in thousands upon thousands of lines of code amongst thousands of class files. I would like to be able to remove all of the prefixes and just have a nice import javax.swing.JButton; at the top of each class file.

    解决方案

    I don't know a tool for this use case, but I had to do something similar a few month ago.

    • Write a script or do a search replace with regex to get rid of the explicitly typed package prefixes.

    • Than let eclipse do the rest using "organize imports". Ctrl-1

    Hint: to avoid ambiguities, setup the classpath with no more than the required libs. For sround about 800 classes I was done in 2 hours.

    • Or get someone who deserved it to do this job.

    EDIT: You should know that in Prefeneces/Java/Editor/Save Actions, Organize imports can be configured as save action.

    这篇关于使用Java中的import语句自动删除显式的包声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆