"Unbalanced read/write with collections" hint fails for fields initialized in constructor
- Dominant language
- Java
- Stars
- 3.1k
- Forks
- 935
- Avg merge
- 2d 3h
- Merged PRs (30d)
- 17
Description
### Apache NetBeans version
Apache NetBeans 14
### What happened
I've a Maven project of type *jar*. Suppose that I also have a class with a single field of type `List`. Then, I initialize that field in the declaration itself. If I never add any element to that collection, NetBeans properly warns me that *The collection is never added to*.
However, if I initialize the field in the constructor, NetBeans seems to be unable to detect that I still have not added any element to the collection.
### How to reproduce
In this piece of code, NetBeans correctly detects that no element has been added to the `collection` field:
```java
public final class Test {
private final List collection = new ArrayList<> ();
// ^^^ WARNING! The collection is never added to.
public Test () {
}
public boolean isEmpty () {
return collection.isEmpty ();
}
}
```
However, if I initialize the field in the constructor, the hint is never shown:
```java
public final class Test {
private final List collection;
public Test () {
collection = new ArrayList<> ();
}
public boolean isEmpty () {
return collection.isEmpty ();
}
}
```
### Did this work correctly in an earlier version?
No
### Operating System
Debian GNU/Linux 11.4 (Bullseye)
### JDK
OpenJDK Runtime Environment (build 17.0.3+7-Debian-1deb11u1)
### Apache NetBeans packaging
Apache NetBeans binary zip
### Anything else
_No response_
### Are you willing to submit a pull request?
No
### Code of Conduct
Yes
Contributor guide
Research direction
Reproduce the warning in a Java Maven jar project using the two Test examples, comparing field-declaration initialization with constructor initialization. Done means the “The collection is never added to” hint also appears when the List field is initialized in the constructor.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100