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

MapProperty.removeListener(changlistener) removes all the listeners not just one

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: P4
    • Resolution: Fixed
    • Affects Version/s: 8u60
    • Fix Version/s: 9
    • Component/s: javafx
    • Labels:
    • Subcomponent:
    • CPU:
      x86_64
    • OS:
      windows_7

      Description

      FULL PRODUCT VERSION :
      java version "1.8.0_60"
      Java(TM) SE Runtime Environment (build 1.8.0_60-b27)
      Java HotSpot(TM) Client VM (build 25.60-b23, mixed mode, sharing)

      (I hav also tried this on update 40 - same bug. update 20 - works fine)


      ADDITIONAL OS VERSION INFORMATION :
      I have tried this on
      Microsoft Windows [Version 6.1.7601]
      and
      Linux 3.19.0-28-generic #30-Ubuntu SMP Mon Aug 31 15:52:51 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux


      A DESCRIPTION OF THE PROBLEM :
      If you remove one ChangeListener from MapProperty instance, all the change listeners added prior removal will be actually removed. According to javadoc (and java 1.8.0 update 20 version) it should only remove one listener - the one passed in the parameter.

      ADDITIONAL REGRESSION INFORMATION:
      There is no bug in this version of java:

      java version "1.8.0_20"
      Java(TM) SE Runtime Environment (build 1.8.0_20-b26)
      Java HotSpot(TM) 64-Bit Server VM (build 25.20-b23, mixed mode)

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1) Create empty java project
      2) Create new MapProperty instance
      3) Add two ChangeListeners, that print some output when being executed (lets say: executing listener 1 and executing listener 2
      4) remove only one of the two listeners
      5) setValue with new observable map instance for the MapProperty instance



      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Only one listener being removed
      ---------------
      Output from my test case:

      executing listener 1
      executing listener 2
      ---- removing listener now -----
      executing listener 2
      ACTUAL -
      Both listeners are removed
      -----------
      Output from my test case:

      executing listener 1
      executing listener 2
      ---- removing listener now -----

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.util.HashMap;
      import javafx.beans.property.MapProperty;
      import javafx.beans.property.SimpleMapProperty;
      import javafx.beans.value.ChangeListener;
      import javafx.beans.value.ObservableValue;
      import javafx.collections.FXCollections;
      import javafx.collections.ObservableMap;

      public class MapTest {

          public static void main(String[] args) {
              MapProperty<String, Object> mapProp = new SimpleMapProperty<>(FXCollections.observableMap(
                      new HashMap<String, Object>()));

              ChangeListener<ObservableMap<String, Object>> listener1
                      = (ObservableValue<? extends ObservableMap<String, Object>> obs, ObservableMap<String, Object> oldV, ObservableMap<String, Object> newV) -> {
                          System.out.println("executing listener 1");
                      };

              ChangeListener<ObservableMap<String, Object>> listener2
                      = (ObservableValue<? extends ObservableMap<String, Object>> obs, ObservableMap<String, Object> oldV, ObservableMap<String, Object> newV) -> {
                          System.out.println("executing listener 2");
                      };

              mapProp.addListener(listener1);
              mapProp.addListener(listener2);

              mapProp.setValue(FXCollections.observableMap(
                      new HashMap<String, Object>()));
              System.out.println("---- removing listener now -----");

              mapProp.removeListener(listener1);

              mapProp.setValue(FXCollections.observableMap(
                      new HashMap<String, Object>()));

          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      I have not found one yet but I assume I could make my own list of listeners and manage adding and removing listeners by my own mechanism.

        Activity

        Hide
        vadim Vadim Pakhnushev added a comment -
        Chien,
        Could you please review this fix:
        http://cr.openjdk.java.net/~vadim/8136465/webrev.00/

        The immediate problem was double decrementing in 3 places in the MapExpressionHelper and in one place in the SetExpressionHelper.
        I verified that this fixes the problem and tests pass.
        In addition the logic which returns Single... in case of one remaining listener was added.
        Show
        vadim Vadim Pakhnushev added a comment - Chien, Could you please review this fix: http://cr.openjdk.java.net/~vadim/8136465/webrev.00/ The immediate problem was double decrementing in 3 places in the MapExpressionHelper and in one place in the SetExpressionHelper. I verified that this fixes the problem and tests pass. In addition the logic which returns Single... in case of one remaining listener was added.
        Hide
        ckyang Chien Yang added a comment -
        Looks good to me. Just a note, even though I know in this case it wouldn't go negative, I generally prefer not to have array index computation of this form: invalidationListeners[1-index]
        Show
        ckyang Chien Yang added a comment - Looks good to me. Just a note, even though I know in this case it wouldn't go negative, I generally prefer not to have array index computation of this form: invalidationListeners[1-index]
        Hide
        vadim Vadim Pakhnushev added a comment -
        Changeset: 98e22e3d6bdd
        Author: vadim
        Date: 2015-09-23 12:55 +0300
        URL: http://hg.openjdk.java.net/openjfx/9-dev/rt/rev/98e22e3d6bdd
        Show
        vadim Vadim Pakhnushev added a comment - Changeset: 98e22e3d6bdd Author: vadim Date: 2015-09-23 12:55 +0300 URL: http://hg.openjdk.java.net/openjfx/9-dev/rt/rev/98e22e3d6bdd

          People

          • Assignee:
            vadim Vadim Pakhnushev
            Reporter:
            webbuggrp Webbug Group
          • Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: