57

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

ililit
  • 1,309
  • 1
  • 12
  • 19

6 Answers6

54

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.

Jeff Foster
  • 43,770
  • 11
  • 86
  • 103
  • 2
    Worked nicely, thanks. My fields were already annotated with JAXB's `@XmlElement` and `@XmlAttribute` annotations, so I didn't even have to change my source! – Andrew Swan May 29 '13 at 12:27
  • Jeff, can you explain what you mean "you can configure an annotation". I'm running IntelliJ 13 and I can do the inspect code analysis and find the "unused declarations" (which are annotated as @Requirement). What do I need to do to to inform IntelliJ that @Requirement means the value will be injected at runtime? – William Jan 30 '14 at 06:15
  • 7
    In 13.1: File > Settings. Inspections: Declaration redundancy > Unused symbol. Configure annotations... – BennyMcBenBen Mar 19 '14 at 18:33
  • 3
    @BennyMcBenBen This works fine for the "unused symbol", but not for the "private field is never assigned" inspection. – bvdb Jun 22 '15 at 09:19
  • @bvdb you might want to take a look at my answer, it addresses the exact issue you pointed out – Bruno 82 May 10 '19 at 09:25
23
  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
Jeremy Chone
  • 3,079
  • 1
  • 27
  • 28
  • 1
    I much prefer this method, but be aware that it will trigger a "code re-inspection" that could take a while to complete depending on the size of your code base. – Carl Anderson Dec 28 '15 at 23:37
  • 2
    I had to: 3. Click on "Edit Settings" 4. Click "Entry points" 5. Click "Annotations...". I believe the UI has changed since 2014 :) – Johan Walles Jun 21 '17 at 07:32
8

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

tagy22
  • 1,353
  • 17
  • 26
8

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

lrkwz
  • 6,105
  • 3
  • 36
  • 59
Bruno 82
  • 449
  • 2
  • 6
  • 15
  • Is there a way to do the same for if the class is only ever initialised via a @RequestBody annotation? – Chris A Apr 10 '23 at 13:29
5

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

Iliiaz Akhmedov
  • 867
  • 7
  • 17
3

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.

Boris Pavlović
  • 63,078
  • 28
  • 122
  • 148