Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8195614

FilteredList throws ArrayIndexOutOfBoundsException if created with 1 element

    XMLWordPrintable

Details

    • generic
    • generic

    Description

      FULL PRODUCT VERSION :
      java version "1.8.0_152"
      Java(TM) SE Runtime Environment (build 1.8.0_152-b16)
      Java HotSpot(TM) 64-Bit Server VM (build 25.152-b16, mixed mode)


      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [version 10.0.16299.192]

      A DESCRIPTION OF THE PROBLEM :
      FilteredList throw ArrayIndexOutOfBoundsException when

      - filtered list is created with a source list containing only 1 element

      - another element is added and then is moved at begin of source list (i.e. removed then added at begin of list) and when remove and add methods are called between "beginChange" and "endChange"

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the attached test case


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      /*
       * To change this license header, choose License Headers in Project Properties.
       * To change this template file, choose Tools | Templates
       * and open the template in the editor.
       */
      package bugFilteredList;

      import java.util.ArrayList;
      import java.util.List;
      import javafx.collections.ModifiableObservableListBase;
      import javafx.collections.transformation.FilteredList;

      /**
       *
       * @author daniel
       */
      public class bugFilteredList {

          public static class MyList<Model> extends ModifiableObservableListBase<Model> {

              private final List<Model> children = new ArrayList<>();

              @Override
              public Model get(int index) {
                  return children.get(index);
              }

              @Override
              public int size() {
                  return children.size();
              }

              @Override
              protected void doAdd(int index, Model element) {

                  children.add(index, element);
              }

              @Override
              protected Model doSet(int index, Model element) {
                  if (children.get(index) == element) {
                      return element;
                  } else {
                      Model old = children.set(index, element);
                      return old;
                  }
              }

              @Override
              protected Model doRemove(int index) {
                  Model child = children.remove(index);
                  return child;
              }

              public void moveToBegin(Model o) {
                  beginChange();
                  remove(o);
                  add(0, o);
                  endChange();
              }
          }

          public static void main(String[] args) {
              for (int j = 100; j > 0; j--) {
                  MyList<String> observableList = new MyList<>();
                  System.out.println("observable list with " + j + " elements");
                  for (int i = 0; i < j; i++) {
                      final String a = "a" + i;
                      observableList.add(a);
                  }

                  FilteredList<String> filteredList = new FilteredList<>(observableList);
                  final String b = "b";
                  observableList.add(b);
      // System.out.println("elements = " + filteredList.stream().collect(Collectors.joining(",")));
                  observableList.moveToBegin(b);
      // System.out.println("elements = " + filteredList.stream().collect(Collectors.joining(",")));
              }
          }
      }

      ---------- END SOURCE ----------

      Attachments

        Issue Links

          Activity

            People

              kcr Kevin Rushforth
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

                Created:
                Updated: