"Unbalanced read/write with collections" hint fails for fields initialized in constructor
See original GitHub issueApache 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:
public final class Test {
private final List<String> 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:
public final class Test {
private final List<String> 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
Issue Analytics
- State:
- Created a year ago
- Comments:6 (4 by maintainers)
I’ve quickly tested this in RC2 and it works fine. Thank you!
change made it into 15 rc2 https://lists.apache.org/thread/qw4sd9kqy3c1lftwhl61ftpkcqcd6m0f