Findbugs correctly figures out that a field annotated @Inject doesn't need to be explicitly initialized.
It also knows that a field annotated @Nonnull doesn't need to be tested against null before dereference.
But if a field is both @Inject and @Nonnull finbugs issues warnings about uninitialized fields.
Here is a class. Running finbugs 3.0.1 against it with default settings issues incorrect warnings
import java.util.List;
import javax.annotation.Nonnull;
import javax.inject.Inject;
public class InjectedFields {
// @Inject
// @Nonnull
List<Integer> list1; // NP warning, uninit warning
@Inject
// @Nonnull
List<Integer> list2; // injected so assumed not null
// @Inject
@Nonnull
List<Integer> list3; // uninit warning
@Inject
@Nonnull
List<Integer> list4; // should be ok, but FB issues uninitialized warning!
@Inject
@edu.umd.cs.findbugs.annotations.NonNull
List<Integer> list5; // should be ok and is!
public boolean isEmpty1() {
return list1.isEmpty();
}
public boolean isEmpty2() {
return list2.isEmpty();
}
public boolean isEmpty3() {
return list3.isEmpty();
}
public boolean isEmpty4() {
return list4.isEmpty();
}
public boolean isEmpty5() {
return list5.isEmpty();
}
}
- There is a correct unwritten field warinig for list1 which is unannotated
- There is no warning against list2 which is @Inject. This is correct, I believe.
- There is a non-null field not initialized warning against list3 which is @Nonnull, also correct
- There is a non-null field not initialized warning against list4 which is incorrect. list4 is @Inject and @Nonnull, so there should be no arning issued.
- Strangely, when I replace @Nonnull with the Findbugs @NonNull on list 5, there is no warning anymore.