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

Binding value left null when immediately revalidated in invalidation listener

    XMLWordPrintable

Details

    Backports

      Description

        I just discovered a bug that was introduced as part of the fluent bindings PR. It affects all ObjectBinding's.

        Below program will print "null" for binding.getValue(), which is incorrect (it should be false).

                SimpleObjectProperty<Boolean> condition = new SimpleObjectProperty<>(true);
                ObservableValue<String> binding = condition.map(Object::toString);

                binding.addListener(o -> { binding.getValue(); });

                condition.set(false);

                System.out.println(">>> binding: " + binding.getValue());

        This is caused by the clearing of the cached value in ObjectBinding when a binding is being invalidated. This clearing however did not take into account that the binding might become valid again while it is informing its listeners.

        This is fixed by checking if the binding is still invalid after calling listeners before clearing the value:

            @Override
            public final void invalidate() {
                if (valid) {
                    valid = false;
                    onInvalidating();
                    ExpressionHelper.fireValueChangedEvent(helper);

                    if (!valid) { // if still invalid after calling listeners...
                        value = null; // clear cached value to avoid hard reference to stale data
                    }
                }
            }

        I think this must be fixed in jfx 19 or the fluent bindings PR must be reverted.

        I will open a PR in a moment which solves the issue.

        Attachments

          Issue Links

            Activity

              People

                jhendrikx John Hendrikx
                jhendrikx John Hendrikx
                Votes:
                0 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                  Created:
                  Updated:
                  Resolved: