RFR: 8079136: Accessing a nested sublist leads to StackOverflowError

Ivan Gerasimov ivan.gerasimov at oracle.com
Tue May 5 18:02:31 UTC 2015


Hi Paul

On 05.05.2015 19:56, Paul Sandoz wrote:
> Hi Ivan,
>
> ArrayList
> --
>
> You can simplify SubList with:
>
> private final class SubList extends AbstractList<E> implements RandomAccess {
>      private final SubList parent;
>      private final int offset;
>      int size;
>
>      // Top level sub-list
>      SubList(int offset, int fromIndex, int toIndex) {
>          this.parent = null;
>          this.offset = offset + fromIndex;
>          this.size = toIndex - fromIndex;
>          this.modCount = ArrayList.this.modCount;
>      }
>
>      // Sub sub-lst
>      SubList(SubList parent,
>              int offset, int fromIndex, int toIndex) {
>          this.parent = parent;
>          this.offset = offset + fromIndex;
>          this.size = toIndex - fromIndex;
>          this.modCount = ArrayList.this.modCount;
>      }
>
> ArrayList.subList becomes:
>
> public List<E> subList(int fromIndex, int toIndex) {
>      subListRangeCheck(fromIndex, toIndex, size);
>      return new SubList(0, fromIndex, toIndex);
> }
>
> And SubList.subList:
>
> public List<E> subList(int fromIndex, int toIndex) {
>      subListRangeCheck(fromIndex, toIndex, size);
>      return new SubList(this, offset, fromIndex, toIndex);
> }
>
> And SubList. updateSizeAndModCount:
>
> private void updateSizeAndModCount(int sizeChange) {
>      int modCount = ArrayList.this.modCount;
>      for (SubList slist = this; slist != null; slist = slist.parent) {
>          slist.size += sizeChange;
>          slist.modCount = modCount;
>      }
> }
>
Thanks for suggestion!
I should have realized this myself, that there's no need to set parent 
to ArrayList.this.
It was a left-over from the previous design, when parent was used in 
different ways.

> AbstractList
> --
>
> Similar changes can be made as above to ArrayList.SubList etc.
>
> The construction of sub-lists does indeed require a second take. A comment is worthwhile. IMO such scoping is not necessary for ArrayList, i have actually found it rare to require such scoping so using it when not necessary is rather jarring.
>
Okay, I'll reorganize it to make SubList classes stand-alone, not inner 
classes.
Let's see, if it makes the things nicer.

> NestedSubList
> --
>
> My preference is you use a testng data provider so you don't have to roll your own failure checking and reporting.
> Are there any other tests for testing the integrity of sublists?

I found only a few tests that test some parts of the functionality:
test/java/util/List/LockStep.java
test/java/util/Collection/MOAT.java


I'll post an update on the code and test soon.

Sincerely yours,
Ivan




More information about the core-libs-dev mailing list