This is the mail archive of the java-patches@sources.redhat.com mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Patch: Serialization for java.beans.{PropertyChangeSupport,VetoableChangeSupport}


Folks,
Here are the changes for 2 more special case serialization classes,
java.beans.{PropertyChangeSupport,VetoableChangeSupport}.  In doing these,
I found an assert failure in the reflection code having to do with
Constructors; I've included that patch here too, since the serialization
code being checked in is dependent on it.  Enjoy!
--warrenl


2000-11-16  Warren Levy  <warrenl@cygnus.com>

        * java/beans/PropertyChangeSupport.java (propertyListeners): Made
        transient.
        (listeners): Made transient.
        (source): Renamed from 'bean'.
        (children): New field for serialization.
        (propertyChangeSupportSerializedDataVersion): Ditto.
        (serialVersionUID): Ditto.
        (writeObject): New serialization method.
        (readObject): New serialization method.
        * java/beans/VetoableChangeSupport.java (propertyListeners): Made
        transient.
        (listeners): Made transient.
        (source): Renamed from 'bean'.
        (children): New field for serialization.
        (vetoableChangeSupportSerializedDataVersion): Ditto.
        (serialVersionUID): Ditto.
        (writeObject): New serialization method.
        (readObject): New serialization method.
        * java/lang/reflect/natMethod.cc (_Jv_CallAnyMethodA): Fixed assert 
        to allow constructor to have a return type (i.e. the class that the
        constructor constructs).


Index: java/beans/PropertyChangeSupport.java
===================================================================
RCS file: /cvs/java/libgcj/libjava/java/beans/PropertyChangeSupport.java,v
retrieving revision 1.1
diff -u -p -r1.1 PropertyChangeSupport.java
--- PropertyChangeSupport.java	2000/05/19 17:55:30	1.1
+++ PropertyChangeSupport.java	2000/11/16 21:02:19
@@ -1,5 +1,5 @@
 /* java.beans.PropertyChangeSupport
-   Copyright (C) 1998, 1999 Free Software Foundation, Inc.
+   Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
 
 This file is part of GNU Classpath.
 
@@ -28,6 +28,11 @@ executable file might be covered by the 
 package java.beans;
 import java.util.Hashtable;
 import java.util.Vector;
+import java.util.Enumeration;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.IOException;
+import java.io.Serializable;
 
 /**
  ** PropertyChangeSupport makes it easy to fire property
@@ -39,16 +44,47 @@ import java.util.Vector;
  **/
 
 public class PropertyChangeSupport implements java.io.Serializable {
-	Hashtable propertyListeners = new Hashtable();
-	Vector listeners = new Vector();
-	Object bean;
+	transient Hashtable propertyListeners = new Hashtable();
+	transient Vector listeners = new Vector();
+	Hashtable children;
+	Object source;
+	int propertyChangeSupportSerializedDataVersion = 2;
+	private static final long serialVersionUID = 6401253773779951803L;
+
+	/**
+	 * Saves the state of the object to the stream. */
+	private void writeObject(ObjectOutputStream stream) throws IOException {
+		children = propertyListeners.isEmpty() ? null : propertyListeners;
+		stream.defaultWriteObject();
+		for (Enumeration e = listeners.elements(); e.hasMoreElements(); ) {
+			PropertyChangeListener l = (PropertyChangeListener)e.nextElement();
+			if (l instanceof Serializable)
+			  stream.writeObject(l);
+		}
+		stream.writeObject(null);
+	}
+
+	/**
+	 * Reads the object back from stream (deserialization).
+	 */
+	private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
+		stream.defaultReadObject();
+		propertyListeners = (children == null) ? new Hashtable() : children;
+		PropertyChangeListener l;
+		while ((l = (PropertyChangeListener)stream.readObject()) != null) {
+			addPropertyChangeListener(l);
+		}
+		// FIXME: XXX: There is no spec for JDK 1.1 serialization
+		// so it is unclear what to do if the value of
+		// propertyChangeSupportSerializedDataVersion is 1.
+	}
 
 	/** Create PropertyChangeSupport to work with a specific
 	 ** source bean.
-	 ** @param bean the source bean to use.
+	 ** @param source the source bean to use.
 	 **/
-	public PropertyChangeSupport(Object bean) {
-		this.bean = bean;
+	public PropertyChangeSupport(Object source) {
+		this.source = source;
 	}
 
 	/** Adds a PropertyChangeListener to the list of listeners.
@@ -166,7 +202,7 @@ public class PropertyChangeSupport imple
 	 ** @param newVal the new value.
 	 **/
 	public void firePropertyChange(String propertyName, Object oldVal, Object newVal) {
-		firePropertyChange(new PropertyChangeEvent(bean,propertyName,oldVal,newVal));
+		firePropertyChange(new PropertyChangeEvent(source,propertyName,oldVal,newVal));
 	}
 
 	/** Fire a PropertyChangeEvent containing the old and new values of the property to all the listeners.
@@ -176,7 +212,7 @@ public class PropertyChangeSupport imple
 	 ** @param newVal the new value.
 	 **/
 	public void firePropertyChange(String propertyName, boolean oldVal, boolean newVal) {
-		firePropertyChange(new PropertyChangeEvent(bean, propertyName, new Boolean(oldVal), new Boolean(newVal)));
+		firePropertyChange(new PropertyChangeEvent(source, propertyName, new Boolean(oldVal), new Boolean(newVal)));
 	}
 
 	/** Fire a PropertyChangeEvent containing the old and new values of the property to all the listeners.
@@ -186,7 +222,7 @@ public class PropertyChangeSupport imple
 	 ** @param newVal the new value.
 	 **/
 	public void firePropertyChange(String propertyName, int oldVal, int newVal) {
-		firePropertyChange(new PropertyChangeEvent(bean, propertyName, new Integer(oldVal), new Integer(newVal)));
+		firePropertyChange(new PropertyChangeEvent(source, propertyName, new Integer(oldVal), new Integer(newVal)));
 	}
 
 	/** Tell whether the specified property is being listened on or not.
Index: java/beans/VetoableChangeSupport.java
===================================================================
RCS file: /cvs/java/libgcj/libjava/java/beans/VetoableChangeSupport.java,v
retrieving revision 1.1
diff -u -p -r1.1 VetoableChangeSupport.java
--- VetoableChangeSupport.java	2000/05/19 17:55:30	1.1
+++ VetoableChangeSupport.java	2000/11/16 21:02:19
@@ -1,6 +1,6 @@
 /*
  * java.beans.VetoableChangeSupport: part of the Java Class Libraries project.
- * Copyright (C) 1998 Free Software Foundation
+ * Copyright (C) 1998, 2000 Free Software Foundation
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public
@@ -21,6 +21,11 @@
 package java.beans;
 import java.util.Hashtable;
 import java.util.Vector;
+import java.util.Enumeration;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.IOException;
+import java.io.Serializable;
 
 /**
  ** VetoableChangeSupport makes it easy to fire vetoable
@@ -33,16 +38,48 @@ import java.util.Vector;
  **/
 
 public class VetoableChangeSupport implements java.io.Serializable {
-	Hashtable propertyListeners = new Hashtable();
-	Vector listeners = new Vector();
-	Object bean;
+	transient Hashtable propertyListeners = new Hashtable();
+	transient Vector listeners = new Vector();
+	Hashtable children;
+	Object source;
+	int vetoableChangeSupportSerializedDataVersion = 2;
+	private static final long serialVersionUID = -5090210921595982017L;
+
+	/**
+	 * Saves the state of the object to the stream. */
+	private void writeObject(ObjectOutputStream stream) throws IOException {
+		children = propertyListeners.isEmpty() ? null : propertyListeners;
+		stream.defaultWriteObject();
+		for (Enumeration e = listeners.elements(); e.hasMoreElements(); ) {
+			VetoableChangeListener l = (VetoableChangeListener)e.nextElement();
+			if (l instanceof Serializable)
+			  stream.writeObject(l);
+		}
+		stream.writeObject(null);
+	}
+
+	/**
+	 * Reads the object back from stream (deserialization).
+	 */
+	private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
+		stream.defaultReadObject();
+		propertyListeners = (children == null) ? new Hashtable() : children;
+		VetoableChangeListener l;
+		while ((l = (VetoableChangeListener)stream.readObject()) != null) {
+			addVetoableChangeListener(l);
+		}
+		// FIXME: XXX: There is no spec for JDK 1.1 serialization
+		// so it is unclear what to do if the value of
+		// vetoableChangeSupportSerializedDataVersion is 1.
+	}
+
 
 	/** Create VetoableChangeSupport to work with a specific
 	 ** source bean.
-	 ** @param bean the source bean to use.
+	 ** @param source the source bean to use.
 	 **/
-	public VetoableChangeSupport(Object bean) {
-		this.bean = bean;
+	public VetoableChangeSupport(Object source) {
+		this.source = source;
 	}
 
 	/** Adds a VetoableChangeListener to the list of listeners.
@@ -199,7 +236,7 @@ public class VetoableChangeSupport imple
 	 ** @exception PropertyVetoException if the change is vetoed.
 	 **/
 	public void fireVetoableChange(String propertyName, Object oldVal, Object newVal) throws PropertyVetoException {
-		fireVetoableChange(new PropertyChangeEvent(bean,propertyName,oldVal,newVal));
+		fireVetoableChange(new PropertyChangeEvent(source,propertyName,oldVal,newVal));
 	}
 
 	/** Fire a VetoableChangeEvent containing the old and new values of the property to all the listeners.
@@ -213,7 +250,7 @@ public class VetoableChangeSupport imple
 	 ** @exception PropertyVetoException if the change is vetoed.
 	 **/
 	public void fireVetoableChange(String propertyName, boolean oldVal, boolean newVal) throws PropertyVetoException {
-		fireVetoableChange(new PropertyChangeEvent(bean,propertyName,new Boolean(oldVal),new Boolean(newVal)));
+		fireVetoableChange(new PropertyChangeEvent(source,propertyName,new Boolean(oldVal),new Boolean(newVal)));
 	}
 
 	/** Fire a VetoableChangeEvent containing the old and new values of the property to all the listeners.
@@ -227,7 +264,7 @@ public class VetoableChangeSupport imple
 	 ** @exception PropertyVetoException if the change is vetoed.
 	 **/
 	public void fireVetoableChange(String propertyName, int oldVal, int newVal) throws PropertyVetoException {
-		fireVetoableChange(new PropertyChangeEvent(bean,propertyName,new Integer(oldVal),new Integer(newVal)));
+		fireVetoableChange(new PropertyChangeEvent(source,propertyName,new Integer(oldVal),new Integer(newVal)));
 	}
 
 
Index: java/lang/reflect/natMethod.cc
===================================================================
RCS file: /cvs/java/libgcj/libjava/java/lang/reflect/natMethod.cc,v
retrieving revision 1.17
diff -u -p -r1.17 natMethod.cc
--- natMethod.cc	2000/10/06 01:49:31	1.17
+++ natMethod.cc	2000/11/16 21:02:19
@@ -312,7 +312,7 @@ _Jv_CallAnyMethodA (jobject obj,
 		    jvalue *result)
 {
   JvAssert (! is_constructor || ! obj);
-  JvAssert (! is_constructor || ! return_type);
+  JvAssert (! is_constructor || return_type);
 
   // See whether call needs an object as the first argument.  A
   // constructor does need a `this' argument, but it is one we create.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]