netbeans icon indicating copy to clipboard operation
netbeans copied to clipboard

"Unbalanced read/write with collections" hint fails for fields initialized in constructor

Open negora opened this issue 3 years ago • 6 comments

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:

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

negora avatar Jul 19 '22 10:07 negora

i can probably fix this for final fields. Non-final would be harder to fix.

mbien avatar Jul 21 '22 20:07 mbien

i can probably fix this for final fields. Non-final would be harder to fix.

Good point. In my opinion, fixing it for final fields would be enough, because it would cover most common situations.

For non-final fields, I think it might be even impossible to achieve in some situations. Because the detection would depend on the order in which the re-assignations occurred. And if the latter were initiated by non-private methods of the current class, then the order would be undefined from the point of view of the class, Right?

Thank you a lot for fixing it.

negora avatar Jul 21 '22 21:07 negora

i can probably fix this for final fields. Non-final would be harder to fix.

Good point. In my opinion, fixing it for final fields would be enough, because it would cover most common situations.

For non-final fields, I think it might be even impossible to achieve in some situations. Because the detection would depend on the order in which the re-assignations occurred.

exactly. The way it works is that it records read/write operations in the first pass, the second pass looks at the declaration and prints the hint if its "unbalanced".

an assignment "collection = foo" is marked as read+write and the hint is not shown. However, if we know that the field is final we can simply ignore assignments since we know that its the initial declaration and can't be anything else. The hint doesn't really know in which order things happen as you correctly said.

mbien avatar Jul 22 '22 08:07 mbien

change made it into 15 rc2 https://lists.apache.org/thread/qw4sd9kqy3c1lftwhl61ftpkcqcd6m0f

mbien avatar Jul 27 '22 18:07 mbien

I've quickly tested this in RC2 and it works fine. Thank you!

negora avatar Jul 28 '22 06:07 negora

going to reopen this since we had to revert the hotfix due to it causing more issues.

mbien avatar Oct 11 '22 17:10 mbien