RFR(s): 8154801 deprecate java.util.Observable/Observer
Hi all, Here's a small proposal to deprecate the java.util.Observable and Observer. This deprecation will not be for removal. See https://bugs.openjdk.java.net/browse/JDK-8154801 for some background. Essentially, these classes are a very thin implementation of the Observer pattern. These classes provide little beyond maintaining a list of callbacks plus the ability calling them upon request. A 1999 comment from Josh Bloch in a related bug report said that they are no longer under active development and have been superseded by the 1.1 Beans/AWT event model. Their only use in the JDK is in the Hotspot Serviceability Agent, which doesn't even use Observable, and which uses Observer as a callback interface. SA's use of Observer could easily be replaced with lambdas and Consumer<TypeDataBase>. There is some use of these classes "in the wild" but only for conventional callback purposes. Deprecation will generate warnings if that code is recompiled with deprecation warnings enabled. Diffs follow. Thanks, s'marks diff -r 92280897299f -r e16d8d56da15 src/java.base/share/classes/java/util/Observable.java --- a/src/java.base/share/classes/java/util/Observable.java Mon Apr 18 14:10:14 2016 -0700 +++ b/src/java.base/share/classes/java/util/Observable.java Thu Apr 21 15:48:23 2016 -0700 @@ -58,7 +58,19 @@ * @see java.util.Observer * @see java.util.Observer#update(java.util.Observable, java.lang.Object) * @since 1.0 + * + * @deprecated + * This class and the {@link Observer} interface have been deprecated. + * The event model supported by {@code Observer} and {@code Observable} + * is quite limited, the order of notifications delivered by + * {@code Observable} is unspecified, and state changes are not in + * one-for-one correspondence with notifications. + * For a richer event model, consider using the + * {@link java.beans} package. For reliable and ordered + * messaging among threads, consider using one of the concurrent data + * structures in the {@link java.util.concurrent} package. */ +@Deprecated(since="9") public class Observable { private boolean changed = false; private Vector<Observer> obs; diff -r 92280897299f -r e16d8d56da15 src/java.base/share/classes/java/util/Observer.java --- a/src/java.base/share/classes/java/util/Observer.java Mon Apr 18 14:10:14 2016 -0700 +++ b/src/java.base/share/classes/java/util/Observer.java Thu Apr 21 15:48:23 2016 -0700 @@ -31,7 +31,12 @@ * @author Chris Warth * @see java.util.Observable * @since 1.0 + * + * @deprecated + * This interface has been deprecated. See the {@link Observable} + * class for further information. */ +@Deprecated(since="9") public interface Observer { /** * This method is called whenever the observed object is changed. An
Looks good Stuart; thanks, -Joe On 4/21/2016 7:17 PM, Stuart Marks wrote:
Hi all,
Here's a small proposal to deprecate the java.util.Observable and Observer. This deprecation will not be for removal.
See https://bugs.openjdk.java.net/browse/JDK-8154801 for some background.
Essentially, these classes are a very thin implementation of the Observer pattern. These classes provide little beyond maintaining a list of callbacks plus the ability calling them upon request. A 1999 comment from Josh Bloch in a related bug report said that they are no longer under active development and have been superseded by the 1.1 Beans/AWT event model.
Their only use in the JDK is in the Hotspot Serviceability Agent, which doesn't even use Observable, and which uses Observer as a callback interface. SA's use of Observer could easily be replaced with lambdas and Consumer<TypeDataBase>.
There is some use of these classes "in the wild" but only for conventional callback purposes. Deprecation will generate warnings if that code is recompiled with deprecation warnings enabled.
Diffs follow.
Thanks,
s'marks
diff -r 92280897299f -r e16d8d56da15 src/java.base/share/classes/java/util/Observable.java --- a/src/java.base/share/classes/java/util/Observable.java Mon Apr 18 14:10:14 2016 -0700 +++ b/src/java.base/share/classes/java/util/Observable.java Thu Apr 21 15:48:23 2016 -0700 @@ -58,7 +58,19 @@ * @see java.util.Observer * @see java.util.Observer#update(java.util.Observable, java.lang.Object) * @since 1.0 + * + * @deprecated + * This class and the {@link Observer} interface have been deprecated. + * The event model supported by {@code Observer} and {@code Observable} + * is quite limited, the order of notifications delivered by + * {@code Observable} is unspecified, and state changes are not in + * one-for-one correspondence with notifications. + * For a richer event model, consider using the + * {@link java.beans} package. For reliable and ordered + * messaging among threads, consider using one of the concurrent data + * structures in the {@link java.util.concurrent} package. */ +@Deprecated(since="9") public class Observable { private boolean changed = false; private Vector<Observer> obs; diff -r 92280897299f -r e16d8d56da15 src/java.base/share/classes/java/util/Observer.java --- a/src/java.base/share/classes/java/util/Observer.java Mon Apr 18 14:10:14 2016 -0700 +++ b/src/java.base/share/classes/java/util/Observer.java Thu Apr 21 15:48:23 2016 -0700 @@ -31,7 +31,12 @@ * @author Chris Warth * @see java.util.Observable * @since 1.0 + * + * @deprecated + * This interface has been deprecated. See the {@link Observable} + * class for further information. */ +@Deprecated(since="9") public interface Observer { /** * This method is called whenever the observed object is changed. An
On 04/21/2016 10:17 PM, Stuart Marks wrote:
Hi all,
Here's a small proposal to deprecate the java.util.Observable and Observer. This deprecation will not be for removal.
These days, anyone encountering these is probably hitting them by mistake while using RxJava or other reactive-stream frameworks. In which case, users will normally want to instead use the jdk9 java.util.concurrent.Flow APIs that all reactive-streams frameworks should be compatible/interoperable with in their planned upcoming jdk9-compatible versions. I'm not sure how to state this now in any @Deprecated docs or other Observable and Observer javadocs though. -Doug
On 4/24/16 6:13 AM, Doug Lea wrote:
These days, anyone encountering these is probably hitting them by mistake while using RxJava or other reactive-stream frameworks. In which case, users will normally want to instead use the jdk9 java.util.concurrent.Flow APIs that all reactive-streams frameworks should be compatible/interoperable with in their planned upcoming jdk9-compatible versions. I'm not sure how to state this now in any @Deprecated docs or other Observable and Observer javadocs though.
Ah, good point! I can certainly add a note saying that these old Observer/Observable types have nothing to do with reactive streams, and also add a link to the Flow APIs. I'll do this in a follow-on bug report: JDK-8155052. s'marks
participants (3)
-
Doug Lea
-
joe darcy
-
Stuart Marks