RFR(s): 8154801 deprecate java.util.Observable/Observer
Stuart Marks
stuart.marks at oracle.com
Fri Apr 22 02:17:12 UTC 2016
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.
*/
+ at 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.
*/
+ at Deprecated(since="9")
public interface Observer {
/**
* This method is called whenever the observed object is changed. An
More information about the core-libs-dev
mailing list