Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Suppress "variable is never assigned" warning in IntelliJ IDEA

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

like image 305
ililit Avatar asked Jul 20 '11 12:07

ililit


2 Answers

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.

like image 148
Jeff Foster Avatar answered Sep 24 '22 18:09

Jeff Foster


  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
like image 31
Jeremy Chone Avatar answered Sep 23 '22 18:09

Jeremy Chone