Re-assignment of Variables

Hi there,

so what I am currently working on is a plugin for java classes that makes all eligible variables final.

I think I have covered about 90% of the potential cases when a variable (either local, member or parameter) can be made final.

Now while working on the last tedious ~10% of coverage and after having learned about a small fraction of the API, I wanted to ask whether there is an easier way to determine data assignments of variables - rather than to count those myself (with various other conditions in mind, of course).

Especially when it comes to branching (if-else and switch statements), it can get tricky to determine what counts as a further data assignment or just an alternative.

So my question is really a 2-part question:

1) Considering the whole potential final modifier for variables: Is there a simple way to determine those variables?

2) Considering the previously mentioned branching statements: Is there a simple way to determine data assignments of variables with the final modifier in mind?

I'm talking about API mechanisms or the such which might make things easier. For one thing, it would be annoying to find out that there is a much simpler solution after having put a lot of effort into development. Furthermore, using suitable inherent mechanisms to achieve a goal might turn out to featuer better performance and in general a higher level of thoroughness.

10 comments
Comment actions Permalink

Does the existing inspection"Local variable or parameter can be final" not suit your need?

0
Comment actions Permalink

It would, if I knew how to utilize this information in my plugin's code to perform an action.

Can you help me out with this one?

0
Comment actions Permalink

Why does the existing quickfix to add "final" to found items not suit your need? what "action" do you want to perform in your plugin?

 

0
Comment actions Permalink

Where can I find this existing quickfix? Is it a button somewhere in the IDE?

I want to write a plugin that provides a button that makes all eligible variables final on click. One could even go so far that when activated, the plugin might manage finals of all variables on the fly.

Would something like this be possible with your mentioned existing quickfix?

0
Comment actions Permalink

You can run the inspection on any scope and use "Apply Fix to all" to do this. Use "Analyze->Run Inspection By Name" to perform batch inspection.

0
Comment actions Permalink

 Are you talking about code or the IDE user interface?

I know there is already a validation running that checks for the variables and whether they can be final. My question is now, can I access this information in my plugin?

0
Comment actions Permalink

Using IDE to fix existing code. When writing Java code, you can use File | Settings | Editor | Code Style | Java "Code Generation" options, section "Final Modifier".

0
Comment actions Permalink

The only two options this menu offers are

Make generated local variables final

Make generated parameters final

Checking those two options does nothing for me - meaning my variables that are eligible for final will not be made final when these options are checked. I want an option/action/whatever that makes all variables (member variables, local variables and parameters) final, depending on their usage. Currently I have a plugin that provides this functionality. For this purpose, I determined the eligibility of the variables for being made final myself (although I know that the IDE already does this check somewhere, but I don't know how or whether I can access this info in my plugin).

Currently I am implementing the last missing cases to achieve full coverage of my final plugin. At that point, I wanted to ask the community if the API could make life easier for me, e.g. by pointing me towards mechanisms of the API that might be suitable for such a task - for all I know there could be a predefined search somewhere that already handles what I am doing - identifying variables that are qualified to be final.

0
Comment actions Permalink

Those two options are in effect upon code generation only.

You can examine com.intellij.codeInspection.localCanBeFinal.LocalCanBeFinal on how to determine "final-bility".

0
Comment actions Permalink

Yes, I figured so.

Thanks, I will look into that!

0

Please sign in to leave a comment.