This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 206855 - "Unbalanced read/write with arrays" hint misfiring
Summary: "Unbalanced read/write with arrays" hint misfiring
Status: VERIFIED FIXED
Alias: None
Product: java
Classification: Unclassified
Component: Hints (show other bugs)
Version: 7.1
Hardware: All All
: P3 normal (vote)
Assignee: Jan Lahoda
URL:
Keywords:
: 208547 (view as bug list)
Depends on:
Blocks:
 
Reported: 2012-01-03 05:29 UTC by supuhstar
Modified: 2012-02-17 13:06 UTC (History)
3 users (show)

See Also:
Issue Type: DEFECT
Exception Reporter:


Attachments
A Java class (fully implements MCRecipe, not included), wherein the reported bug triggers (1.21 KB, application/octet-stream)
2012-01-03 05:29 UTC, supuhstar
Details
Code snippet that improperly causes "Collection only added to, never read" hint. (522 bytes, text/x-java)
2012-01-07 19:59 UTC, sparkee
Details

Note You need to log in before you can comment on or make changes to this bug.
Description supuhstar 2012-01-03 05:29:03 UTC
Created attachment 114564 [details]
A Java class (fully implements MCRecipe, not included), wherein the reported bug triggers

In the attached file BHRecipeAdapter, NetBeans claims that the "grid" array is only read and never written to, even though the constructor and one method write to it and one method reads it.
Comment 1 Jiri Prox 2012-01-03 08:05:08 UTC
repriducible
Comment 2 sparkee 2012-01-05 17:09:31 UTC
I too, have encountered this.  I have a class with a private set that shows an unbalanced collection, even though there is one method in the class (actually an instance method of the object) that adds to the set, and another that removes from the set.
Comment 3 Jan Lahoda 2012-01-06 11:06:30 UTC
Fix for the array case:
http://hg.netbeans.org/jet-main/rev/327ce9229aad

sparkee, if you would have a reproducible test case of the Set case that you could attach here, that would be awesome. Otherwise, what is the declared type of the variable on which the bug manifests? E.g. java.util.Set, java.util.HashSet, java.util.NavigableSet, etc.  Thank you very much.
Comment 4 Quality Engineering 2012-01-07 16:22:08 UTC
Integrated into 'main-golden'
Changeset: http://hg.netbeans.org/main-golden/rev/327ce9229aad
User: Jan Lahoda <jlahoda@netbeans.org>
Log: #206855: ignoring arrays of arrays in the unbalanced hint.
Comment 5 sparkee 2012-01-07 19:59:39 UTC
Created attachment 114709 [details]
Code snippet that improperly causes "Collection only added to, never read" hint.

As requested,a code snippet that improperly hints.
Comment 6 sparkee 2012-01-07 20:01:06 UTC
My attachment is hacked out of a piece of code for my project at work if it looks weird.
Comment 7 Quality Engineering 2012-01-08 15:43:58 UTC
Integrated into 'main-golden'
Changeset: http://hg.netbeans.org/main-golden/rev/43558d0c447e
User: Jan Lahoda <jlahoda@netbeans.org>
Log: #206855: if the result of the "add" method is checked, then it should be used as both write and read to/from the collection.
Comment 8 sparkee 2012-01-08 16:30:44 UTC
(In reply to comment #7)
> Integrated into 'main-golden'
> Changeset: http://hg.netbeans.org/main-golden/rev/43558d0c447e
> User: Jan Lahoda <jlahoda@netbeans.org>
> Log: #206855: if the result of the "add" method is checked, then it should be
> used as both write and read to/from the collection.

I would suggest (presuming the code is not already doing this) that you consider checking the results of the following with the same rationale: 
  remove(E e);
  addAll(Collection<E> c);
  removalAll(Collection<E> c);
  retainAll(Collection<E> c);
  
These are also technically "reads" of a collection.
Comment 9 Jan Lahoda 2012-01-09 09:19:26 UTC
(In reply to comment #8)
> (In reply to comment #7)
> > Integrated into 'main-golden'
> > Changeset: http://hg.netbeans.org/main-golden/rev/43558d0c447e
> > User: Jan Lahoda <jlahoda@netbeans.org>
> > Log: #206855: if the result of the "add" method is checked, then it should be
> > used as both write and read to/from the collection.
> 
> I would suggest (presuming the code is not already doing this) that you
> consider checking the results of the following with the same rationale: 
>   remove(E e);
>   addAll(Collection<E> c);
>   removalAll(Collection<E> c);
>   retainAll(Collection<E> c);
> 
> These are also technically "reads" of a collection.

The patch ensures that if the result of any add, addAll and set methods are checked, they are considered to both read and write the collection.

The handling of remove&retain methods is a bit different (and somewhat counter-intuitive): the remove&retain methods are not considered to be writing the collection at all, they are handled in the same way as methods that read from the collection. The reason is that if nothing is added to a collection (using add/addAll), there is nothing to remove anyway, and the remove&retain methods have no effect.

Thanks for the report and testcase.
Comment 10 Jiri Prox 2012-02-06 11:07:27 UTC
verified
Comment 12 Quality Engineering 2012-02-08 02:33:43 UTC
Integrated into 'releases', will be available in build *201202072200* or newer. Wait for official and publicly available build.
Changeset: http://hg.netbeans.org/releases/rev/0bb9e3a2f8dd
User: Jan Lahoda <jlahoda@netbeans.org>
Log: #206855: ignoring arrays of arrays in the unbalanced hint.
(transplanted from 327ce9229aad56a678da8857f00c38051618b048)
Comment 13 Jiri Prox 2012-02-13 15:33:19 UTC
verified in patch1
Comment 14 Alexander Simon 2012-02-17 13:06:30 UTC
*** Bug 208547 has been marked as a duplicate of this bug. ***