Petro Semeniuk
Petro Semeniuk

Reputation: 7038

How I can find all unused import programmatically?

In my case there are two reason for doing that:

I'm curious if there is any way to find unused imports programmatically (lets say from unit test) and fail locally if there are any.

Maybe failing a build because of unused import sounds harsh, but if it saves time for team overall it makes sens to do so (would love to hear opinion on that as well).

UPDATE:

I followed yegor256 suggestion and incorporated Checkstyle task with initially small subset of Sun Code Conventions (unused imports is one of them) and made it break a build if violations found.

After one week of trial we've got zero unused imports in our codebase and surprisingly zero complaints about this rule (by the way, Checkstyle is really fast: analyzing ~100KLoc taking less than one second).

As for using IDE for such analysis: yes, it good choice, but having this kind of checks run as part of automated build is better.

Upvotes: 9

Views: 12650

Answers (6)

shaILU
shaILU

Reputation: 2116

I see lot of comments in same way that use this IDE or that IDE. But all my friends try to understand the difference. Doing something programmatically is different and using IDE is different.

If I want a process to be programmatic then suggestion of IDE is not useful. It might be possible some one is asking this question because he is building complete process and this step is part of it. How opening IDE would help him on different machines and OS where CI is working?

I too building one tool on similar lines. I achieved it up to some level but it programmatically open IDE and close it automatically and fixes source code too. But opening same in Linux might be a question for me.

Understanding some one's view before answering is really very important.

Upvotes: 0

Christian
Christian

Reputation: 14077

If you are using eclipse IDE or IntelliJ IDEA, you can configure them to

1a. organize imports / remove unused imports on save or before commit (see cleanup preferences)

1b. switch the "unused imports" warning to an error (see error settings)

2a. configure a jre which does not include com.* stuff

2b. configure the warning of proprietary api usage from the jre to be an error

You might still want to check that on the build server, though. In this case the more complicated stuff like configuring CheckStyle would still be necessary.

Upvotes: 5

yegor256
yegor256

Reputation: 105053

What you're trying to do is called static code analysis. Checkstyle can help you. If you're using Maven, this plugin will do the automation for you: http://maven.apache.org/plugins/maven-checkstyle-plugin/

You can also take a look at qulice.com (I'm one of its developers), which integrates a few static analysis tools and pre-configures them (incl. Checkstyle, PMD, FindBugs).

Upvotes: 11

Peter Lawrey
Peter Lawrey

Reputation: 533492

I'm curious if there is any way to find unused imports programmatically (lets say from unit test) and fail build locally if there are any.

I use IntelliJ to organise imports, this removes all the unused imports. You can do this with one hot key from the top of you code base to correct all the imports. (It also has over 700 other types of static checks and fixes)

Maybe failing a build because of unused import sounds harsh, but if it saves time for team overall it makes sens to do so (would love to hear opinion on that as well).

I have IntelliJ check in code which formatted and with imports organised so the issue never arises in the first place. ;)

Upvotes: 1

meriton
meriton

Reputation: 70564

Aren't these unrelated questions? If you import classes only present in the local JDK, these imports are used (just unsatisfied). For either problem, I recommend solving it in the IDE so the problem will be detected when code is written, rather than prior to checkin (the earlier the detection, the easier the fix ...).

In eclipse, you could prevent unsatisfied imports with access rules, and automatically fix imports whenever a source file is saved by enabling the appropriate save action. If you check these settings into version control, you can easily share them with the team.

Upvotes: 0

Kumar Vivek Mitra
Kumar Vivek Mitra

Reputation: 33534

In Computer Science the name given to such a process of analyzing the code without executing is known as static code analysis.

Try using an IDE, I am using Eclipse, which marks all the Unused imports and Unused Variables or methods in with a Yellow color underline....

Upvotes: 0

Related Questions