This was an interesting question ... since I believe that you will have to write a custom NetBeans plugin to do what you want (functionality is not available out of the box), and I was looking for an excuse to study the development of NetBeans plugins.
However, after spending a couple of hours reading textbooks and slipping through javadocs ... it becomes clear that this subject is quite a big bite to chew on and probably much more active than you want.
I think the BEST clause forgets about deleting unused imports at save time and instead performs this step at build time . NetBeans offers excellent integration with Ant and / or Maven (for build purposes it is basically just a GUI wrapper around these tools), and there are several Ant tasks that can do what you want. Cm:
http://ant.apache.org/external.html
(find the tasks "CleanImports" and "Importscrubber")
If your NetBeans project is based on Maven, you can always connect one of these Ant tasks there using the AntRun plugin for Maven .
If you’re not used to accessing Ant or Maven directly in NetBeans, simply go to the Files tab and look at the project’s root directory. If his project is Maven, the build script will be called pom.xml . Otherwise, your project will usually be Ant, and the build script will be called build.xml . The documentation for these points above should clearly explain how to move on from there.
I noticed that these two Ant tasks were not updated after a while, so if you run into problems, you can check out the very popular and updated PMD system , which has its own documentation for integrating with NetBeans . However, the problem is PMD primarily for reporting ... I don’t know if it can be used to actually take action and modify the source files.
source share