Extract variable is pulling method annotations
Answered
Hi, I've noticed that after updating IntelliJ, all "extract variables" refactorings were declaring the new variable "final". I was fine with that, until I realized it is also pulling all annotations from the method call which I'm extracting the variable from.
So now I'm getting something like:
@Valid @NotNull final String name = obj.getName();
I find it quite annoying and unnecessary.
How do I stop this?
Please sign in to leave a comment.
While extracting the variable you may use "declare final" option in popup:
Please provide the code sample where the annotations are incorrectly inferred. Thank you
Hi.
I don't have that popup. It takes whatever setting I've used before when I enabled the dialog. But the final thing is fine, I actually like it.
My problem is with the annotations. And I'm not concerned if they're right or wrong, I don't want to pull them at all. Java is verbose enough, I don't need more code in my variable declaration.
But here's an example:
Method I'm calling:
The code after extracting the variable:
I don't need that @Size annotation there.
Maybe the feature has always been there, but I've only noticed it now because I'm using swagger.io's SpringCodeGen, which generates methods that are heavily annotated for validation.
The point is, I don't want to propagate those annotations throughout my code and I don't want to have to clean it up every time I use "extract variable" either.
There's got to be a way to turn it off.
Please clarify the IDEA version that you use. Is the issue reproduced for you with 2019.1.3 (https://www.jetbrains.com/idea/download/)? Thanks
Here's the version I'm using:
IntelliJ IDEA 2019.1.3 (Community Edition)
Build #IC-191.7479.19, built on May 28, 2019
JRE: 1.8.0_202-release-1483-b58 amd64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Windows 10 10.0
Please also share the version of the annotation dependencies that you use. Thank you
The annotations are from javax.validation:validation-api:2.0.1.Final.
Thank you for providing the information, now the issue is reproduced. Unfortunately it's not possible to disable auto-inclusion of annotations while extraction when you use
TYPE_USEannotations which are a part of type definition (i.e @Size).Please follow/vote/comment the issue already created for this case:
https://youtrack.jetbrains.com/issue/IDEA-206570
Thank you