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

Observable doesn't check HasChanged() method to notify Observers

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P5 P5
    • None
    • 7u25
    • core-libs

      FULL PRODUCT VERSION :


      A DESCRIPTION OF THE PROBLEM :
      java.util.Observable should check hasChanged() method to check if it should notify observers or not (as per documentation). But it doesn't call hasChanged() method in notifyObservers(Object arg0) , instead it just checks the instance field "change" in Observable class. check this code line for details :
      http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/Observable.java#152

      Hence clients can't override behaviour of notifyObservers() method by overriding hasChanged() method.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      class CustomObservable extends Observable{
       @Override
          protected synchronized Boolean hasChanged()
          {
              return Boolean.False;
          }

          @Override
          public void notifyObservers(Object disbursementEvent)
          {
              this.setChanged();
              super.notifyObservers("notification argument");
              
          }
      }

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      observers should not be called as hasChanged() returns false
      ACTUAL -
      Observers get called irrespective of what overridden hasChanged method returns.

      REPRODUCIBILITY :
      This bug can be reproduced always.

            Unassigned Unassigned
            aefimov Aleksej Efimov
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: