Skip to content

Commit

Permalink
Fix type generics, length property and misssing streams (#172)
Browse files Browse the repository at this point in the history
  • Loading branch information
gyk4j authored Feb 16, 2024
1 parent e523bac commit 52e5386
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 27 deletions.
4 changes: 4 additions & 0 deletions JShim/JShim.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,16 @@
<Compile Include="Javax\Swing\Event\EventListenerList.cs" />
<Compile Include="Javax\Swing\WindowConstants.cs" />
<Compile Include="Java\Beans\PropertyChangeSupport.cs" />
<Compile Include="Java\IO\ObjectInputStream.cs" />
<Compile Include="Java\IO\ObjectOutputStream.cs" />
<Compile Include="Java\Lang\Runnable.cs" />
<Compile Include="Java\Time\Instant.cs" />
<Compile Include="Java\Time\LocalDateTime.cs" />
<Compile Include="Java\Time\ZonedDateTime.cs" />
<Compile Include="Java\Time\ZoneId.cs" />
<Compile Include="Java\Util\Concurrent\Future.cs" />
<Compile Include="Java\Util\Concurrent\RunnableFuture.cs" />
<Compile Include="Java\Util\EventListener.cs" />
<Compile Include="Microsoft\NET\FSUtils.cs" />
<Compile Include="Sun\NIO\FS\WindowsFileStore.cs" />
<Compile Include="Sun\NIO\FS\WindowsFileSystem.cs" />
Expand Down Expand Up @@ -90,6 +93,7 @@
<Folder Include="Javax\Swing" />
<Folder Include="Javax\Swing\Event" />
<Folder Include="Java\Lang" />
<Folder Include="Java\IO" />
<Folder Include="Java\Time" />
<Folder Include="Microsoft" />
<Folder Include="Microsoft\NET" />
Expand Down
27 changes: 27 additions & 0 deletions JShim/Java/IO/ObjectInputStream.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace Java.IO
{
/// <summary>
/// Description of ObjectInputStream.
/// </summary>
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;
}
}
}
26 changes: 26 additions & 0 deletions JShim/Java/IO/ObjectOutputStream.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace Java.IO
{
/// <summary>
/// Description of ObjectOutputStream.
/// </summary>
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();
}
}
}
13 changes: 13 additions & 0 deletions JShim/Java/Util/EventListener.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

using System;

namespace Java.Util
{
/// <summary>
/// A tagging interface that all event listener interfaces must extend.
/// </summary>
public interface EventListener
{

}
}
2 changes: 1 addition & 1 deletion JShim/Javax/Swing/DefaultBoundedRangeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ public void SetValueIsAdjusting(bool b)
/// <see cref="#RemoveChangeListener" />
public ChangeListener[] GetChangeListeners()
{
return listenerList.GetListeners(typeof(ChangeListener));
return listenerList.GetListeners<ChangeListener>(typeof(ChangeListener));
}

/// <summary>
Expand Down
3 changes: 2 additions & 1 deletion JShim/Javax/Swing/Event/ChangeListener.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@

using System;
using Java.Util;

namespace Javax.Swing.Event
{
/// <summary>
/// Defines an object which listens for ChangeEvents.
/// </summary>
public interface ChangeListener
public interface ChangeListener : EventListener
{
/// <summary>
/// Invoked when the target of the listener has changed its state.
Expand Down
55 changes: 30 additions & 25 deletions JShim/Javax/Swing/Event/EventListenerList.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@

using System;
using System.IO;
using System.Runtime.Serialization;

using Java.IO;
using Java.Util;

namespace Javax.Swing.Event
{
Expand Down Expand Up @@ -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&trade;
/// has been added to the <c>Java.Beans</c> namespace.
/// Please see {@link java.beans.XMLEncoder}.
/// Please see <see cref="java.beans.XMLEncoder"></see>.
/// </summary>
public class EventListenerList
{
Expand Down Expand Up @@ -105,13 +110,13 @@ public object[] GetListenerList()
/// </summary>
/// <param name="t"></param>
/// <returns>all of the listeners of the specified type.</returns>
public T[] GetListeners(Class<T> t) where T : EventListener
public T[] GetListeners<T>(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)
{
Expand All @@ -127,7 +132,7 @@ public T[] GetListeners(Class<T> t) where T : EventListener
/// <returns></returns>
public int GetListenerCount()
{
return listenerList.length/2;
return listenerList.Length/2;
}

/**
Expand Down Expand Up @@ -163,7 +168,7 @@ private int GetListenerCount(object[] list, Type t)
/// </summary>
/// <param name="t">the type of the listener to be added</param>
/// <param name="l">the listener to be added</param>
public void Add(Type<T> t, T l) where T : EventListener
public void Add<T>(Type t, T l) where T : EventListener
{
if (l==null)
{
Expand All @@ -172,7 +177,7 @@ public void Add(Type<T> 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);
Expand All @@ -186,7 +191,7 @@ public void Add(Type<T> 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);

Expand All @@ -202,7 +207,7 @@ public void Add(Type<T> t, T l) where T : EventListener
/// </summary>
/// <param name="t">the type of the listener to be removed</param>
/// <param name="l">the listener to be removed</param>
public void Remove(Type<T> t, T l) where T : EventListener
public void Remove<T>(Type t, T l) where T : EventListener
{
if (l ==null)
{
Expand All @@ -211,7 +216,7 @@ public void Remove(Type<T> 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);
Expand All @@ -230,17 +235,17 @@ public void Remove(Type<T> 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;
}
}

Expand All @@ -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);
}
}
Expand All @@ -273,26 +278,26 @@ 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<EventListener>)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<EventListener>)Type.forName(name, true, cl), l);
}
}

/// <summary>
/// Returns a string representation of the EventListenerList.
/// </summary>
/// <returns></returns>
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;
Expand Down

0 comments on commit 52e5386

Please sign in to comment.