From 52e5386fc78599a26bf6740d2e4a155febbdf8ee Mon Sep 17 00:00:00 2001 From: gyk <147011991+gyk4j@users.noreply.github.com> Date: Fri, 16 Feb 2024 16:24:50 +0800 Subject: [PATCH] Fix type generics, length property and misssing streams (#172) --- JShim/JShim.csproj | 4 ++ JShim/Java/IO/ObjectInputStream.cs | 27 +++++++++ JShim/Java/IO/ObjectOutputStream.cs | 26 +++++++++ JShim/Java/Util/EventListener.cs | 13 +++++ JShim/Javax/Swing/DefaultBoundedRangeModel.cs | 2 +- JShim/Javax/Swing/Event/ChangeListener.cs | 3 +- JShim/Javax/Swing/Event/EventListenerList.cs | 55 ++++++++++--------- 7 files changed, 103 insertions(+), 27 deletions(-) create mode 100644 JShim/Java/IO/ObjectInputStream.cs create mode 100644 JShim/Java/IO/ObjectOutputStream.cs create mode 100644 JShim/Java/Util/EventListener.cs diff --git a/JShim/JShim.csproj b/JShim/JShim.csproj index 9e6ff7f..56b9146 100644 --- a/JShim/JShim.csproj +++ b/JShim/JShim.csproj @@ -53,6 +53,8 @@ + + @@ -60,6 +62,7 @@ + @@ -90,6 +93,7 @@ + diff --git a/JShim/Java/IO/ObjectInputStream.cs b/JShim/Java/IO/ObjectInputStream.cs new file mode 100644 index 0000000..ecc4258 --- /dev/null +++ b/JShim/Java/IO/ObjectInputStream.cs @@ -0,0 +1,27 @@ + +using System; +using System.IO; +using System.Runtime.Serialization.Formatters.Binary; + +namespace Java.IO +{ + /// + /// Description of ObjectInputStream. + /// + public class ObjectInputStream + { + public void DefaultReadObject() + { + + } + + public object ReadObject() + { + Stream s = new MemoryStream(); + BinaryFormatter formatter = new BinaryFormatter(); + object o = formatter.Deserialize(s); + s.Close(); + return o; + } + } +} diff --git a/JShim/Java/IO/ObjectOutputStream.cs b/JShim/Java/IO/ObjectOutputStream.cs new file mode 100644 index 0000000..4cb2997 --- /dev/null +++ b/JShim/Java/IO/ObjectOutputStream.cs @@ -0,0 +1,26 @@ + +using System; +using System.IO; +using System.Runtime.Serialization.Formatters.Binary; + +namespace Java.IO +{ + /// + /// Description of ObjectOutputStream. + /// + public class ObjectOutputStream + { + public void DefaultWriteObject() + { + + } + + public void WriteObject(object o) + { + Stream s = new MemoryStream(); + BinaryFormatter formatter = new BinaryFormatter(); + formatter.Serialize(s, o); + s.Close(); + } + } +} diff --git a/JShim/Java/Util/EventListener.cs b/JShim/Java/Util/EventListener.cs new file mode 100644 index 0000000..ad2f1ef --- /dev/null +++ b/JShim/Java/Util/EventListener.cs @@ -0,0 +1,13 @@ + +using System; + +namespace Java.Util +{ + /// + /// A tagging interface that all event listener interfaces must extend. + /// + public interface EventListener + { + + } +} diff --git a/JShim/Javax/Swing/DefaultBoundedRangeModel.cs b/JShim/Javax/Swing/DefaultBoundedRangeModel.cs index e049722..186e9c6 100644 --- a/JShim/Javax/Swing/DefaultBoundedRangeModel.cs +++ b/JShim/Javax/Swing/DefaultBoundedRangeModel.cs @@ -328,7 +328,7 @@ public void SetValueIsAdjusting(bool b) /// public ChangeListener[] GetChangeListeners() { - return listenerList.GetListeners(typeof(ChangeListener)); + return listenerList.GetListeners(typeof(ChangeListener)); } /// diff --git a/JShim/Javax/Swing/Event/ChangeListener.cs b/JShim/Javax/Swing/Event/ChangeListener.cs index edf3fcb..16ecd38 100644 --- a/JShim/Javax/Swing/Event/ChangeListener.cs +++ b/JShim/Javax/Swing/Event/ChangeListener.cs @@ -1,12 +1,13 @@  using System; +using Java.Util; namespace Javax.Swing.Event { /// /// Defines an object which listens for ChangeEvents. /// - public interface ChangeListener + public interface ChangeListener : EventListener { /// /// Invoked when the target of the listener has changed its state. diff --git a/JShim/Javax/Swing/Event/EventListenerList.cs b/JShim/Javax/Swing/Event/EventListenerList.cs index c7e77d5..a0b58f9 100644 --- a/JShim/Javax/Swing/Event/EventListenerList.cs +++ b/JShim/Javax/Swing/Event/EventListenerList.cs @@ -1,5 +1,10 @@  using System; +using System.IO; +using System.Runtime.Serialization; + +using Java.IO; +using Java.Util; namespace Javax.Swing.Event { @@ -67,7 +72,7 @@ namespace Javax.Swing.Event /// the same version of Swing. As of 1.4, support for long term storage /// of all JavaBeans™ /// has been added to the Java.Beans namespace. - /// Please see {@link java.beans.XMLEncoder}. + /// Please see . /// public class EventListenerList { @@ -105,13 +110,13 @@ public object[] GetListenerList() /// /// /// all of the listeners of the specified type. - public T[] GetListeners(Class t) where T : EventListener + public T[] GetListeners(Type t) where T : EventListener { object[] lList = listenerList; int n = GetListenerCount(lList, t); T[] result = (T[])Array.CreateInstance(t, n); int j = 0; - for (int i = lList.length-2; i>=0; i-=2) + for (int i = lList.Length-2; i>=0; i-=2) { if (lList[i] == t) { @@ -127,7 +132,7 @@ public T[] GetListeners(Class t) where T : EventListener /// public int GetListenerCount() { - return listenerList.length/2; + return listenerList.Length/2; } /** @@ -163,7 +168,7 @@ private int GetListenerCount(object[] list, Type t) /// /// the type of the listener to be added /// the listener to be added - public void Add(Type t, T l) where T : EventListener + public void Add(Type t, T l) where T : EventListener { if (l==null) { @@ -172,7 +177,7 @@ public void Add(Type t, T l) where T : EventListener // something wrong return; } - if (!(l is t)) + if (l.GetType() != t) { throw new ArgumentException("Listener " + l + " is not of type " + t); @@ -186,7 +191,7 @@ public void Add(Type t, T l) where T : EventListener else { // Otherwise copy the array and add the new listener - int i = listenerList.length; + int i = listenerList.Length; object[] tmp = new object[i+2]; Array.Copy(listenerList, 0, tmp, 0, i); @@ -202,7 +207,7 @@ public void Add(Type t, T l) where T : EventListener /// /// the type of the listener to be removed /// the listener to be removed - public void Remove(Type t, T l) where T : EventListener + public void Remove(Type t, T l) where T : EventListener { if (l ==null) { @@ -211,7 +216,7 @@ public void Remove(Type t, T l) where T : EventListener // something wrong return; } - if (!(l is t)) + if (l.GetType() != t) { throw new ArgumentException("Listener " + l + " is not of type " + t); @@ -230,17 +235,17 @@ public void Remove(Type t, T l) where T : EventListener // If so, remove it if (index != -1) { - object[] tmp = new object[listenerList.length-2]; + object[] tmp = new object[listenerList.Length-2]; // Copy the list up to index Array.Copy(listenerList, 0, tmp, 0, index); // Copy from two past the index, up to // the end of tmp (which is two elements // shorter than the old list) - if (index < tmp.length) + if (index < tmp.Length) Array.Copy(listenerList, index+2, tmp, index, - tmp.length - index); + tmp.Length - index); // set the listener array to the new array or null - listenerList = (tmp.length == 0) ? NULL_ARRAY : tmp; + listenerList = (tmp.Length == 0) ? NULL_ARRAY : tmp; } } @@ -251,13 +256,13 @@ private void WriteObject(ObjectOutputStream s) s.DefaultWriteObject(); // Save the non-null event listeners: - for (int i = 0; i < lList.length; i+=2) + for (int i = 0; i < lList.Length; i+=2) { - Class t = (Class)lList[i]; + Type t = (Type)lList[i]; EventListener l = (EventListener)lList[i+1]; - if ((l!=null) && (l instanceof Serializable)) + if ((l!=null) && (l is ISerializable)) { - s.WriteObject(t.GetName()); + s.WriteObject(t.FullName); s.WriteObject(l); } } @@ -273,11 +278,11 @@ private void ReadObject(ObjectInputStream s) while (null != (listenerTypeOrNull = s.ReadObject())) { - ClassLoader cl = Thread.CurrentThread().GetContextClassLoader(); - EventListener l = (EventListener)s.ReadObject(); - string name = (string) listenerTypeOrNull; - ReflectUtil.CheckPackageAccess(name); - Add((Class)Class.forName(name, true, cl), l); +// ClassLoader cl = Thread.CurrentThread().GetContextClassLoader(); +// EventListener l = (EventListener)s.ReadObject(); +// string name = (string) listenerTypeOrNull; +// ReflectUtil.CheckPackageAccess(name); +// Add((Type)Type.forName(name, true, cl), l); } } @@ -285,14 +290,14 @@ private void ReadObject(ObjectInputStream s) /// Returns a string representation of the EventListenerList. /// /// - public string ToString() + public override string ToString() { object[] lList = listenerList; string s = "EventListenerList: "; - s += lList.length/2 + " listeners: "; + s += lList.Length/2 + " listeners: "; for (int i = 0 ; i <= lList.Length-2 ; i+=2) { - s += " type " + ((Class)lList[i]).GetName(); + s += " type " + ((Type)lList[i]).FullName; s += " listener " + lList[i+1]; } return s;