How to find unused methods?

Hi all,

Just wondering: is there an inspection to find unused (private and public) methods?

Thanks, Erik

7 comments
Comment actions Permalink


Erik Pragt wrote:

Just wondering: is there an inspection to find unused (private and
public) methods?


Yes, it is called "Unused declaration".

Bas

0
Comment actions Permalink

Hi, Erik,

Yes, there are a variety of "unused" inspections. For methods it's "Unused declaration" in the Declaration Redundancy section.

But before you asked, I didn't know for sure. In the pertinent configuration dialog, where you can define collections of inspections, there is a search / filter field. There are two such filters, in fact: one for the settings sections themselves and one within the IDE Profiles and Project Profiles, which filters the individual inspections. Enter a keyword ("unused", e.g.) and you'll see all the inspections with that word in their name or description.

I often get to this dialog via the Analyze -> Inspect Code... dialog. The ellipsis button ("...") in the lower right will open the Project Profiles dialog where you can configure an existing or create a new set of inspections. Then, when you confirm back to the Specify Inspection Scope dialog you can choose that profile (and the inspection scope) and initiate inspection.

Also, you've probably noticed declarations (and imports) that appear in gray. That's IDEA's way of telling you that you're looking at something that's known not to be referenced. Entities with public accessibility are never shown gray since this highlighting reflects only usages within the file.


Randall Schulz

Nonsense & gibberish reduction by RRS on Jun 10, 2008 1:00 PM

0
Comment actions Permalink

Thanks you both for the responses. Exactly what I was looking for.

Erik

Btw, the inspection doesn't work here, I get an error about not permgenspace. I'll change my settings (currently I have 192MB) and see what happens.

0
Comment actions Permalink

I have -XX:MaxPermSize=120m and haven't had any trouble running inspections. But I usually define a highly pared-down set of inspections to apply lest the results (many of them relating to inspections with which I don't agree...) be excessively voluminous.


Randall Schulz

0
Comment actions Permalink

Right, when I'm running inspections like this, I usually create an IDE profile for just the thing I'm looking for (unused methods, in this case).

One gotcha I found with this is that if you select a scope, the results are not correct - they will list all methods in the scope that are not called from that scope. I'm in charge of our infrastructure section, so I'd like to just run these inspections over that scope, but if there are methods called from other modules they're incorrectly marked as unused. Is there any way around this?

Cheers,
Colin

Edited by: Colin Fleming on Jun 10, 2008 7:14 PM

0
Comment actions Permalink

Hello Colin,

please file a JIRA issue for this.

As a workaround (inconvenient though) I can suggest to setup project profile
mapping: profile with selected unused declaration for your scope and empty
profile as default project profile

Thank you
-


Anna Kozlova
JetBrains Inc.
http://www.intellij.com
"Develop with pleasure!"

Right, when I'm running inspections like this, I usually create an IDE
profile for just the thing I'm looking for (unused methods, in this
case).

One gotcha I found with this is that if you select a scope, the
results are not correct - they will list all methods in the scope that
are not called from that scope. I'm in charge of our infrastructure
section, so I'd like to just run these inspections over that scope,
but if there are methods called from other modules they're incorrectly
marked as unused. Is there any way around this?

Cheers,
Colin
Edited by: Colin Fleming on Jun 10, 2008 7:14 PM



0

Please sign in to leave a comment.