Finding places where a field is used/assigned?

I'm working on an Action whose end goal is to add the "final" modfiier to all class fields which are private and unmodified.
My initial idea was to grab all of the "private" fields from the class, look up references for those fields, check to see if any are modifications, and mark the field as "final" if not.
This is what I have so far: https://github.com/champgm/IntelliJCodeGenerationPlugin/blob/10-Final-Fields/src/com/champgm/intellij/plugin/fields/MakeFieldsFinal.java#L27

But there are no references contained in these fields. Did I misinterpret what references are? Am I going about this the right way, or are there some util classes that will make this much easier?

8 comments
Comment actions Permalink

First of all, the feature that you're trying to build already exists in IntelliJ, in the form of the "field can be final" inspection.

The field is a declaration, not a reference, so it doesn't have references in itself, but it has references pointing to it. To find those references, use the ReferencesSearch.search() method.

0
Comment actions Permalink

Thank you Dmitry; that makes sense. I will investigate the ReferenceSearch utility next.

As for the Intention, I realize the field-can-be-final intention exists, but I could not find a way to apply this intention's reccomended action on a class-wide scale. I turned it on in my project, and I can see the highlights in my file(s). But, what I really want is a button/menu option that will run through the file and apply the change to everything it can. Is there a way to do that without a plugin? If not, is there a way for my Action to access the data that Intention collects (so I don't have to collect it myself with ReferenceSearch) and apply those changes?

0
Comment actions Permalink

Analyze | Code Cleanup is the feature that allows you to batch apply inspection quickfixes on a given scope (class, package etc.)

0
Comment actions Permalink

Hmm, maybe I am doing something wrong.

In my code, I forced a situation where a field was highlighted by the can-be-final Inspection.
I click "Analyze" then "Code Cleanup"
Select "Current File"
Click the ellipsis under "Inspection Profile"
A "Code Cleanup Inspections" dialoge appears
I click "Add" and name my new profile, "AUTOMATED"
In the search box I type "final"
But I can only see "Unnecessary 'final' on local variable or parameter" and "Redundant throws declaration"

How can I add the other 'final'-related inspections that are available in the Preference menu's "Inspections" section?

0
Comment actions Permalink

Actually I wasn't completely aware of how "Code cleanup" works. It supports running only a subset of inspections which are considered to be appropriate for batch application of quickfixes, and the set is hard-coded in IntelliJ IDEA. I've added the "Field may be final" inspection to the set.

0
Comment actions Permalink

Ok, thank you for that. Would you be open to adding "Local variable or parameter can be 'final'" as well?
In the meantime, I will continue working on my plugin's Action for fun/education. Thank you for your help.

0
Comment actions Permalink

We already have "Unnecessary 'final' on a local variable or parameter" in the set, and we can't really have both an inspection and its opposite. (And, in my personal opinion, marking fields as final makes code significantly easier to reason about, whereas marking local variables and parameters as final adds no value but clutters the code a lot.)

0
Comment actions Permalink

Okay, what you say makes sense, and I think I agree.
I still need to pursue automating these ideas/inspections/actions for other reasons, though. I will post back here if I run into any more trouble, thanks again.

0

Please sign in to leave a comment.