From 57c502c0ab8ca51f5779042df33e9addd56611b1 Mon Sep 17 00:00:00 2001
From: masesdevelopers <94312179+masesdevelopers@users.noreply.github.com>
Date: Tue, 2 Jul 2024 23:21:08 +0200
Subject: [PATCH] Update KNetSerialization to anticipate preparation of some
fields
---
.../Serialization/KNetSerialization.cs | 135 +++++++++---------
tests/net/Common/SharedKNetCore.cs | 6 +-
tests/net/General/KNetTestSerDes/Program.cs | 3 +-
3 files changed, 75 insertions(+), 69 deletions(-)
diff --git a/src/net/KNet/Specific/Serialization/KNetSerialization.cs b/src/net/KNet/Specific/Serialization/KNetSerialization.cs
index e11597f09a..279208d971 100644
--- a/src/net/KNet/Specific/Serialization/KNetSerialization.cs
+++ b/src/net/KNet/Specific/Serialization/KNetSerialization.cs
@@ -16,13 +16,8 @@
* Refer to LICENSE for more information.
*/
-using Java.Lang;
-using Java.Nio;
using MASES.JCOBridge.C2JBridge;
using MASES.JCOBridge.C2JBridge.JVMInterop;
-using Org.Apache.Kafka.Common.Errors;
-using Org.Apache.Kafka.Common.Serialization;
-using Org.Apache.Kafka.Common.Utils;
using System;
using System.Linq;
using System.Reflection;
@@ -35,6 +30,44 @@ namespace MASES.KNet.Serialization
///
public static class KNetSerialization
{
+ #region Private fields
+ static readonly Org.Apache.Kafka.Common.Serialization.BooleanSerializer _BooleanSerializer = new();
+ static readonly Org.Apache.Kafka.Common.Serialization.ByteBufferSerializer _ByteBufferSerializer = new();
+ static readonly Org.Apache.Kafka.Common.Serialization.BytesSerializer _BytesSerializer = new();
+ static readonly Org.Apache.Kafka.Common.Serialization.DoubleSerializer _DoubleSerializer = new();
+ static readonly Org.Apache.Kafka.Common.Serialization.FloatSerializer _FloatSerializer = new();
+ static readonly Org.Apache.Kafka.Common.Serialization.IntegerSerializer _IntSerializer = new();
+ static readonly Org.Apache.Kafka.Common.Serialization.LongSerializer _LongSerializer = new();
+ static readonly Org.Apache.Kafka.Common.Serialization.ShortSerializer _ShortSerializer = new();
+
+ static readonly Org.Apache.Kafka.Common.Serialization.BooleanDeserializer _BooleanDeserializer = new();
+ static readonly Org.Apache.Kafka.Common.Serialization.ByteBufferDeserializer _ByteBufferDeserializer = new();
+ static readonly Org.Apache.Kafka.Common.Serialization.BytesDeserializer _BytesDeserializer = new();
+ static readonly Org.Apache.Kafka.Common.Serialization.DoubleDeserializer _DoubleDeserializer = new();
+ static readonly Org.Apache.Kafka.Common.Serialization.FloatDeserializer _FloatDeserializer = new();
+ static readonly Org.Apache.Kafka.Common.Serialization.IntegerDeserializer _IntDeserializer = new();
+ static readonly Org.Apache.Kafka.Common.Serialization.LongDeserializer _LongDeserializer = new();
+ static readonly Org.Apache.Kafka.Common.Serialization.ShortDeserializer _ShortDeserializer = new();
+
+ static bool CheckRevert(byte[] dotnet, byte[] java)
+ {
+ bool main = !dotnet.SequenceEqual(java);
+ if (main)
+ {
+ Array.Reverse(dotnet); // revert and check consistency
+ if (!dotnet.SequenceEqual(java)) throw new InvalidOperationException($"The sequence {BitConverter.ToString(dotnet)} is not equal to {BitConverter.ToString(java)}");
+ }
+ return main;
+ }
+ static readonly bool ShallRevertByteOrderShort = CheckRevert(BitConverter.GetBytes((short)1), _ShortSerializer.Serialize("", Java.Lang.Short.ValueOf(1)));
+ static readonly bool ShallRevertByteOrderInt = CheckRevert(BitConverter.GetBytes(1), _IntSerializer.Serialize("", Java.Lang.Integer.ValueOf(1)));
+ static readonly bool ShallRevertByteOrderLong = CheckRevert(BitConverter.GetBytes((long)1), _LongSerializer.Serialize("", Java.Lang.Long.ValueOf(1)));
+ static readonly bool ShallRevertByteOrderFloat = CheckRevert(BitConverter.GetBytes(1.1F), _FloatSerializer.Serialize("", Java.Lang.Float.ValueOf(1.1F)));
+ static readonly bool ShallRevertByteOrderDouble = CheckRevert(BitConverter.GetBytes(1.1), _DoubleSerializer.Serialize("", Java.Lang.Double.ValueOf(1.1)));
+
+ #endregion
+
+ #region Public properties
///
/// Identity the type of the key used
///
@@ -42,7 +75,7 @@ public static class KNetSerialization
///
/// Identity the type of the key used using a
///
- public static readonly Java.Lang.String KeyTypeIdentifierJVM = new Java.Lang.String(KeyTypeIdentifier);
+ public static readonly Java.Lang.String KeyTypeIdentifierJVM = new(KeyTypeIdentifier);
///
/// Identity the serializer for the key
///
@@ -50,7 +83,7 @@ public static class KNetSerialization
///
/// Identity the serializer for the key using a
///
- public static readonly Java.Lang.String KeySerializerIdentifierJVM = new Java.Lang.String(KeySerializerIdentifier);
+ public static readonly Java.Lang.String KeySerializerIdentifierJVM = new(KeySerializerIdentifier);
///
/// Identity the type of the value used
///
@@ -58,7 +91,7 @@ public static class KNetSerialization
///
/// Identity the type of the value used using a
///
- public static readonly Java.Lang.String ValueTypeIdentifierJVM = new Java.Lang.String(ValueTypeIdentifier);
+ public static readonly Java.Lang.String ValueTypeIdentifierJVM = new(ValueTypeIdentifier);
///
/// Identity the serializer for the value
///
@@ -66,16 +99,16 @@ public static class KNetSerialization
///
/// Identity the serializer for the value using a
///
- public static readonly Java.Lang.String ValueSerializerIdentifierJVM = new Java.Lang.String(ValueSerializerIdentifier);
+ public static readonly Java.Lang.String ValueSerializerIdentifierJVM = new(ValueSerializerIdentifier);
+
+ #endregion
+
///
/// Returns the typename with the assembly qualification to help reload better the types
///
/// The to be converted
/// A string with along with
- public static string ToAssemblyQualified(this Type type)
- {
- return $"{type.FullName}, {type.Assembly.GetName().Name}";
- }
+ public static string ToAssemblyQualified(this Type type) => $"{type.FullName}, {type.Assembly.GetName().Name}";
///
/// Serializer types
@@ -140,10 +173,8 @@ public enum SerializationType
///
/// The type to check
/// if managed
- public static bool IsInternalManaged()
- {
- return IsInternalManaged(typeof(TData));
- }
+ public static bool IsInternalManaged() => IsInternalManaged(typeof(TData));
+
///
/// Check if a serializer is available for
///
@@ -151,7 +182,7 @@ public static bool IsInternalManaged()
/// if managed
public static bool IsInternalManaged(Type type)
{
- if (type == typeof(bool) || type == typeof(byte[]) || type == typeof(ByteBuffer) || type == typeof(Bytes)
+ if (type == typeof(bool) || type == typeof(byte[]) || type == typeof(Java.Nio.ByteBuffer) || type == typeof(Org.Apache.Kafka.Common.Utils.Bytes)
|| type == typeof(double) || type == typeof(float) || type == typeof(int) || type == typeof(long) || type == typeof(short) || type == typeof(string)
|| type == typeof(Guid) || type == typeof(void))
{
@@ -165,10 +196,8 @@ public static bool IsInternalManaged(Type type)
///
/// The type to check
/// if managed
- public static bool IsJVMInternalManaged()
- {
- return IsJVMInternalManaged(typeof(TData));
- }
+ public static bool IsJVMInternalManaged() => IsJVMInternalManaged(typeof(TData));
+
///
/// Check if a JVM serializer is available for
///
@@ -176,8 +205,8 @@ public static bool IsJVMInternalManaged()
/// if managed
public static bool IsJVMInternalManaged(Type type)
{
- if (type == typeof(Java.Lang.Boolean) || type == typeof(byte[]) || type == typeof(ByteBuffer) || type == typeof(Bytes)
- || type == typeof(Java.Lang.Double) || type == typeof(Java.Lang.Float) || type == typeof(Java.Lang.Integer) || type == typeof(Java.Lang.Long)
+ if (type == typeof(Java.Lang.Boolean) || type == typeof(byte[]) || type == typeof(Java.Nio.ByteBuffer) || type == typeof(Org.Apache.Kafka.Common.Utils.Bytes)
+ || type == typeof(Java.Lang.Double) || type == typeof(Java.Lang.Float) || type == typeof(Java.Lang.Integer) || type == typeof(Java.Lang.Long)
|| type == typeof(Java.Lang.Short) || type == typeof(Java.Lang.String)
|| type == typeof(Java.Util.UUID) || type == typeof(Java.Lang.Void))
{
@@ -191,10 +220,8 @@ public static bool IsJVMInternalManaged(Type type)
///
/// The type to check
///
- public static SerializationType InternalSerDesType()
- {
- return InternalSerDesType(typeof(TData));
- }
+ public static SerializationType InternalSerDesType() => InternalSerDesType(typeof(TData));
+
///
/// Returns the serializer for
///
@@ -204,8 +231,8 @@ public static SerializationType InternalSerDesType(Type type)
{
if (type == typeof(bool)) return SerializationType.Boolean;
else if (type == typeof(byte[])) return SerializationType.ByteArray;
- else if (type == typeof(ByteBuffer)) return SerializationType.ByteBuffer;
- else if (type == typeof(Bytes)) return SerializationType.Bytes;
+ else if (type == typeof(Java.Nio.ByteBuffer)) return SerializationType.ByteBuffer;
+ else if (type == typeof(Org.Apache.Kafka.Common.Utils.Bytes)) return SerializationType.Bytes;
else if (type == typeof(double)) return SerializationType.Double;
else if (type == typeof(float)) return SerializationType.Float;
else if (type == typeof(int)) return SerializationType.Integer;
@@ -223,10 +250,8 @@ public static SerializationType InternalSerDesType(Type type)
///
/// The type to check
///
- public static SerializationType InternalJVMSerDesType()
- {
- return InternalJVMSerDesType(typeof(TData));
- }
+ public static SerializationType InternalJVMSerDesType()=> InternalJVMSerDesType(typeof(TData));
+
///
/// Returns the JVM serializer for
///
@@ -236,8 +261,8 @@ public static SerializationType InternalJVMSerDesType(Type type)
{
if (type == typeof(Java.Lang.Boolean)) return SerializationType.Boolean;
else if (type == typeof(byte[])) return SerializationType.ByteArray;
- else if (type == typeof(ByteBuffer)) return SerializationType.ByteBuffer;
- else if (type == typeof(Bytes)) return SerializationType.Bytes;
+ else if (type == typeof(Java.Nio.ByteBuffer)) return SerializationType.ByteBuffer;
+ else if (type == typeof(Org.Apache.Kafka.Common.Utils.Bytes)) return SerializationType.Bytes;
else if (type == typeof(Java.Lang.Double)) return SerializationType.Double;
else if (type == typeof(Java.Lang.Float)) return SerializationType.Float;
else if (type == typeof(Java.Lang.Integer)) return SerializationType.Integer;
@@ -250,7 +275,6 @@ public static SerializationType InternalJVMSerDesType(Type type)
return SerializationType.External;
}
- static readonly Org.Apache.Kafka.Common.Serialization.BooleanSerializer _BooleanSerializer = new Org.Apache.Kafka.Common.Serialization.BooleanSerializer();
///
/// Serialize a
///
@@ -268,26 +292,23 @@ public static byte[] SerializeByteArray(bool fallbackToKafka, string topic, byte
return data;
}
- static readonly Org.Apache.Kafka.Common.Serialization.ByteBufferSerializer _ByteBufferSerializer = new Org.Apache.Kafka.Common.Serialization.ByteBufferSerializer();
///
/// Serialize a
///
- public static byte[] SerializeByteBuffer(bool fallbackToKafka, string topic, ByteBuffer data)
+ public static byte[] SerializeByteBuffer(bool fallbackToKafka, string topic, Java.Nio.ByteBuffer data)
{
if (fallbackToKafka) return _ByteBufferSerializer.Serialize(topic, data);
return data.ToArray(true);
}
- static readonly Org.Apache.Kafka.Common.Serialization.BytesSerializer _BytesSerializer = new Org.Apache.Kafka.Common.Serialization.BytesSerializer();
///
/// Serialize a
///
- public static byte[] SerializeBytes(bool fallbackToKafka, string topic, Bytes data)
+ public static byte[] SerializeBytes(bool fallbackToKafka, string topic, Org.Apache.Kafka.Common.Utils.Bytes data)
{
return _BytesSerializer.Serialize(topic, data);
}
- static readonly Org.Apache.Kafka.Common.Serialization.DoubleSerializer _DoubleSerializer = new Org.Apache.Kafka.Common.Serialization.DoubleSerializer();
///
/// Serialize a
///
@@ -299,7 +320,6 @@ public static byte[] SerializeDouble(bool fallbackToKafka, string topic, double
return array;
}
- static readonly Org.Apache.Kafka.Common.Serialization.FloatSerializer _FloatSerializer = new Org.Apache.Kafka.Common.Serialization. FloatSerializer();
///
/// Serialize a
///
@@ -311,7 +331,6 @@ public static byte[] SerializeFloat(bool fallbackToKafka, string topic, float da
return array;
}
- static readonly Org.Apache.Kafka.Common.Serialization.IntegerSerializer _IntSerializer = new Org.Apache.Kafka.Common.Serialization.IntegerSerializer();
///
/// Serialize a
///
@@ -326,7 +345,6 @@ public static byte[] SerializeInt(bool fallbackToKafka, string topic, int data)
//return new byte[] { (byte)(data >>> 24), (byte)(data >>> 16), (byte)(data >>> 8), ((byte)data) };
}
- static readonly Org.Apache.Kafka.Common.Serialization.LongSerializer _LongSerializer = new Org.Apache.Kafka.Common.Serialization.LongSerializer();
///
/// Serialize a
///
@@ -341,7 +359,6 @@ public static byte[] SerializeLong(bool fallbackToKafka, string topic, long data
//return new byte[] { (byte)((int)(data >>> 56)), (byte)((int)(data >>> 48)), (byte)((int)(data >>> 40)), (byte)((int)(data >>> 32)), (byte)((int)(data >>> 24)), (byte)((int)(data >>> 16)), (byte)((int)(data >>> 8)), ((byte)data) };
}
- static readonly Org.Apache.Kafka.Common.Serialization.ShortSerializer _ShortSerializer = new Org.Apache.Kafka.Common.Serialization.ShortSerializer();
///
/// Serialize a
///
@@ -377,7 +394,6 @@ public static byte[] SerializeVoid(bool fallbackToKafka, string topic, Java.Lang
return null;
}
- static readonly Org.Apache.Kafka.Common.Serialization.BooleanDeserializer _BooleanDeserializer = new Org.Apache.Kafka.Common.Serialization.BooleanDeserializer();
///
/// Deserialize a
///
@@ -404,27 +420,24 @@ public static byte[] DeserializeByteArray(bool fallbackToKafka, string topic, by
return data;
}
- static readonly Org.Apache.Kafka.Common.Serialization.ByteBufferDeserializer _ByteBufferDeserializer = new Org.Apache.Kafka.Common.Serialization.ByteBufferDeserializer();
///
/// Deserialize a
///
- public static ByteBuffer DeserializeByteBuffer(bool fallbackToKafka, string topic, byte[] data)
+ public static Java.Nio.ByteBuffer DeserializeByteBuffer(bool fallbackToKafka, string topic, byte[] data)
{
if (data == null || data.Length == 0) return default;
- return JVMBridgeBase.WrapsDirect(_ByteBufferDeserializer.Deserialize(topic, data) as IJavaObject);
+ return JVMBridgeBase.WrapsDirect(_ByteBufferDeserializer.Deserialize(topic, data) as IJavaObject);
}
- static readonly Org.Apache.Kafka.Common.Serialization.BytesDeserializer _BytesDeserializer = new Org.Apache.Kafka.Common.Serialization.BytesDeserializer();
///
/// Deserialize a
///
- public static Bytes DeserializeBytes(bool fallbackToKafka, string topic, byte[] data)
+ public static Org.Apache.Kafka.Common.Utils.Bytes DeserializeBytes(bool fallbackToKafka, string topic, byte[] data)
{
if (data == null || data.Length == 0) return default;
- return JVMBridgeBase.WrapsDirect(_BytesDeserializer.Deserialize(topic, data) as IJavaObject);
+ return JVMBridgeBase.WrapsDirect(_BytesDeserializer.Deserialize(topic, data) as IJavaObject);
}
- static readonly Org.Apache.Kafka.Common.Serialization.DoubleDeserializer _DoubleDeserializer = new Org.Apache.Kafka.Common.Serialization.DoubleDeserializer();
///
/// Deserialize a
///
@@ -444,7 +457,6 @@ public static double DeserializeDouble(bool fallbackToKafka, string topic, byte[
return BitConverter.ToDouble(data, 0);
}
- static readonly Org.Apache.Kafka.Common.Serialization.FloatDeserializer _FloatDeserializer = new Org.Apache.Kafka.Common.Serialization.FloatDeserializer();
///
/// Deserialize a
///
@@ -464,7 +476,6 @@ public static float DeserializeFloat(bool fallbackToKafka, string topic, byte[]
return BitConverter.ToSingle(data, 0);
}
- static readonly Org.Apache.Kafka.Common.Serialization.IntegerDeserializer _IntDeserializer = new Org.Apache.Kafka.Common.Serialization.IntegerDeserializer();
///
/// Deserialize a
///
@@ -509,7 +520,6 @@ public static int DeserializeInt(bool fallbackToKafka, string topic, byte[] data
//}
}
- static readonly Org.Apache.Kafka.Common.Serialization.LongDeserializer _LongDeserializer = new Org.Apache.Kafka.Common.Serialization.LongDeserializer();
///
/// Deserialize a
///
@@ -554,7 +564,6 @@ public static long DeserializeLong(bool fallbackToKafka, string topic, byte[] da
//}
}
- static readonly Org.Apache.Kafka.Common.Serialization.ShortDeserializer _ShortDeserializer = new Org.Apache.Kafka.Common.Serialization.ShortDeserializer();
///
/// Deserialize a
///
@@ -621,19 +630,13 @@ public static Java.Lang.Void DeserializeVoid(bool fallbackToKafka, string topic,
{
if (data != null || data.Length != 0)
{
- JVMBridgeException.ThrowNew("Data should be null for a VoidDeserializer.");
- throw new IllegalArgumentException();
+ JVMBridgeException.ThrowNew("Data should be null for a VoidDeserializer.");
+ throw new Java.Lang.IllegalArgumentException();
}
else
{
return null;
}
}
-
- static readonly bool ShallRevertByteOrderShort = !SerializeShort(false, "", 1).SequenceEqual(SerializeShort(true, "", 1));
- static readonly bool ShallRevertByteOrderInt = !SerializeInt(false, "", 1).SequenceEqual(SerializeInt(true, "", 1));
- static readonly bool ShallRevertByteOrderLong = !SerializeLong(false, "", 1).SequenceEqual(SerializeLong(true, "", 1));
- static readonly bool ShallRevertByteOrderFloat = !SerializeFloat(false, "", 1.1F).SequenceEqual(SerializeFloat(true, "", 1.1F));
- static readonly bool ShallRevertByteOrderDouble = !SerializeDouble(false, "", 1.1).SequenceEqual(SerializeDouble(true, "", 1.1));
}
}
diff --git a/tests/net/Common/SharedKNetCore.cs b/tests/net/Common/SharedKNetCore.cs
index d711025fac..d868bfe8ce 100644
--- a/tests/net/Common/SharedKNetCore.cs
+++ b/tests/net/Common/SharedKNetCore.cs
@@ -84,7 +84,11 @@ public static void Create()
public static int ManageException(System.Exception e)
{
int retCode = 0;
- if (e is ClassNotFoundException cnfe)
+ if (e is TypeInitializationException tie)
+ {
+ return ManageException(tie.InnerException);
+ }
+ else if (e is ClassNotFoundException cnfe)
{
Console.WriteLine($"Failed with {cnfe}, current ClassPath is {SharedKNetCore.GlobalInstance.ClassPath}");
retCode = 1;
diff --git a/tests/net/General/KNetTestSerDes/Program.cs b/tests/net/General/KNetTestSerDes/Program.cs
index 0ab4a1181f..ba3c940b46 100644
--- a/tests/net/General/KNetTestSerDes/Program.cs
+++ b/tests/net/General/KNetTestSerDes/Program.cs
@@ -35,8 +35,7 @@ static void Main(string[] args)
}
catch (Exception e)
{
- Console.WriteLine(e.ToString());
- Environment.ExitCode = 1;
+ Environment.ExitCode = SharedKNetCore.ManageException(e);
}
}