Suppress "variable is never assigned" warning in IntelliJ IDEA

JavaIntellij IdeaSuppress Warnings

Java Problem Overview


We use reflection extensively to set class field values in our code. The fields are accessed in code but they are never assigned except via reflection. So IDEA displays "is never assigned" warning. If I ask IDEA to suppress the inspection, it inserts

@SuppressWarnings({"UnusedDeclaration"})

but this also disables the check of whether the field is used or not, which we do not want.

Is it anyhow possible to disable only "not assigned" check and leave "not used" check for specific fields only?

IDEA version is 10.5

Java Solutions


Solution 1 - Java

You could use an annotation to mark it as an injected field. (similar to how it would treat @EJB). The IntelliJ inspections (at least with version 10.5) allow you to configure your own annotations to mark fields as being injected.

Select Analyze, Inspect Code from the menu and then go to the unused declaration inspection and you can configure an annotation.

Solution 2 - Java

  1. run analysis: Analyze > Inpect Code...
  2. Right click on Unused Declaration (below the Declaration redundancy tree node)
  3. Click on the "Configure Annotations..."
  4. Add com.google.google.inject.Inject and javax.inject.Inject

Solution 3 - Java

To configure annotations in Android Studio 1.1.0 after inspecting your code right click Unused declaration -> Edit Settings -> Configure annotations.

Android Studio 1.1.0

Android Studio 1.1.0

Solution 4 - Java

Settings -> Editor -> Inspections -> Declaration Redundancy -> Unused declaration -> Entry points -> Annotations

A new popup opens, split into 2 different areas: "Mark as entry point if annotated by" and "Mark field as implicitly written if annotated by". Only in the latter you click the "+" icon (Add Annotation Class) and select Autowired annotation (or whatever annotation you're using for dependency injection).

This will disable the warning "private field is never assigned" but will not disable the warning "private field is never used" in case you never use the field in the class code, which is the desirable behaviour.

enter image description here

This worked on IntelliJ IDEA Community Edition 2018.3.3 and 2020.2

Solution 5 - Java

If you popup the actions hint window (alt + Enter on Mac), it should suggest you to suppress warning for Inject annotation

Solution 6 - Java

Nope, it seems that IDEA's inspection is not that fine grained. Even with annotation based dependency injected fields the same warning can be suppressed for fields annotated with @Inject. Automatically the warning "not used" is suppressed.

I've just tried running FindBugs-IDEA against the class and no warnings or errors were raised.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionililitView Question on Stackoverflow
Solution 1 - JavaJeff FosterView Answer on Stackoverflow
Solution 2 - JavaJeremy ChoneView Answer on Stackoverflow
Solution 3 - Javatagy22View Answer on Stackoverflow
Solution 4 - JavaBruno 82View Answer on Stackoverflow
Solution 5 - JavaIliiaz AkhmedovView Answer on Stackoverflow
Solution 6 - JavaBoris PavlovićView Answer on Stackoverflow