Skip to content

Commit f43d460

Browse files
committed
Replace usage of FormatterServices.GetUninitializedObject on NET Core with RuntimeHelpers. Should remove dependency on System.Runtime.Serialization.Formatters?
1 parent c6568da commit f43d460

File tree

6 files changed

+39
-12
lines changed

6 files changed

+39
-12
lines changed

src/IKVM.MSBuild.Tasks/IkvmJsonParser.cs

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Collections;
66
using System.Collections.Generic;
77
using System.Reflection;
8+
using System.Runtime.CompilerServices;
89
using System.Runtime.Serialization;
910
using System.Text;
1011

@@ -321,7 +322,11 @@ static Dictionary<string, T> CreateMemberNameDictionary<T>(T[] members) where T
321322

322323
static object ParseObject(Type type, string json)
323324
{
325+
#if NETFRAMEWORK
324326
object instance = FormatterServices.GetUninitializedObject(type);
327+
#else
328+
object instance = RuntimeHelpers.GetUninitializedObject(type);
329+
#endif
325330

326331
//The list is split into key/value pairs only, this means the split must be divisible by 2 to be valid JSON
327332
List<string> elems = Split(json);

src/IKVM.Runtime/JNI/JNIEnv.cs

+7-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ Jeroen Frijters
2525
using System.Collections.Generic;
2626
using System.Diagnostics;
2727
using System.Reflection;
28+
using System.Runtime.CompilerServices;
2829
using System.Runtime.InteropServices;
30+
using System.Runtime.Serialization;
2931

3032
using IKVM.ByteCode.Text;
3133

@@ -739,7 +741,11 @@ static jobject AllocObjectImpl(JNIEnv* pEnv, RuntimeJavaType wrapper)
739741
return IntPtr.Zero;
740742
}
741743
wrapper.Finish();
742-
return pEnv->MakeLocalRef(System.Runtime.Serialization.FormatterServices.GetUninitializedObject(wrapper.TypeAsBaseType));
744+
#if NETFRAMEWORK
745+
return pEnv->MakeLocalRef(FormatterServices.GetUninitializedObject(wrapper.TypeAsBaseType));
746+
#else
747+
return pEnv->MakeLocalRef(RuntimeHelpers.GetUninitializedObject(wrapper.TypeAsBaseType));
748+
#endif
743749
}
744750
catch (RetargetableJavaException e)
745751
{

src/IKVM.Runtime/Java/Externs/sun/misc/Unsafe.cs

+4
Original file line numberDiff line numberDiff line change
@@ -1704,7 +1704,11 @@ public static object allocateInstance(object self, global::java.lang.Class cls)
17041704
throw x.ToJava();
17051705
}
17061706

1707+
#if NETFRAMEWORK
17071708
return FormatterServices.GetUninitializedObject(wrapper.TypeAsBaseType);
1709+
#else
1710+
return RuntimeHelpers.GetUninitializedObject(wrapper.TypeAsBaseType);
1711+
#endif
17081712
}
17091713

17101714
/// <summary>

src/IKVM.Runtime/Java/Externs/sun/reflect/ReflectionFactory.cs

+15-5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ Jeroen Frijters
2525
using System.Reflection;
2626
#if !NO_REF_EMIT
2727
using System.Reflection.Emit;
28+
using System.Runtime.CompilerServices;
29+
2830
#endif
2931
using System.Runtime.Serialization;
3032
using System.Security;
@@ -282,7 +284,11 @@ internal SerializationConstructorAccessorImpl(global::java.lang.reflect.Construc
282284
[SecuritySafeCritical]
283285
public object newInstance(object[] args)
284286
{
287+
#if NETFRAMEWORK
285288
var obj = FormatterServices.GetUninitializedObject(type);
289+
#else
290+
var obj = RuntimeHelpers.GetUninitializedObject(type);
291+
#endif
286292
if (mw != null)
287293
mw.Invoke(obj, ConvertArgs(mw.DeclaringType.GetClassLoader(), mw.GetParameters(), args));
288294

@@ -966,13 +972,17 @@ public object newInstance(object[] args)
966972

967973
}
968974

969-
private sealed class FastSerializationConstructorAccessorImpl : global::sun.reflect.ConstructorAccessor
975+
sealed class FastSerializationConstructorAccessorImpl : global::sun.reflect.ConstructorAccessor
970976
{
971977

972-
private static readonly MethodInfo GetTypeFromHandleMethod = typeof(Type).GetMethod("GetTypeFromHandle", new Type[] { typeof(RuntimeTypeHandle) });
973-
private static readonly MethodInfo GetUninitializedObjectMethod = typeof(FormatterServices).GetMethod("GetUninitializedObject", new Type[] { typeof(Type) });
974-
private delegate object InvokeCtor();
975-
private InvokeCtor invoker;
978+
static readonly MethodInfo GetTypeFromHandleMethod = typeof(Type).GetMethod(nameof(Type.GetTypeFromHandle), new[] { typeof(RuntimeTypeHandle) });
979+
#if NETFRAMEWORK
980+
static readonly MethodInfo GetUninitializedObjectMethod = typeof(FormatterServices).GetMethod(nameof(FormatterServices.GetUninitializedObject), new[] { typeof(Type) });
981+
#else
982+
static readonly MethodInfo GetUninitializedObjectMethod = typeof(RuntimeHelpers).GetMethod(nameof(RuntimeHelpers.GetUninitializedObject), new[] { typeof(Type) });
983+
#endif
984+
delegate object InvokeCtor();
985+
InvokeCtor invoker;
976986

977987
internal FastSerializationConstructorAccessorImpl(global::java.lang.reflect.Constructor constructorToCall, global::java.lang.Class classToInstantiate)
978988
{

src/IKVM.Runtime/RuntimeAssemblyClassLoader.cs

+7
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ Jeroen Frijters
3131
using IKVM.Attributes;
3232
using IKVM.Runtime.Syntax;
3333

34+
using System.Runtime.CompilerServices;
35+
36+
3437
#if IMPORTER || EXPORTER
3538
using IKVM.Reflection;
3639

@@ -1253,7 +1256,11 @@ void InitializeJavaClassLoader(JavaClassLoaderConstructionInProgress jclcip, Typ
12531256
[System.Security.SecuritySafeCritical]
12541257
static object GetUninitializedObject(Type type)
12551258
{
1259+
#if NETFRAMEWORK
12561260
return FormatterServices.GetUninitializedObject(type);
1261+
#else
1262+
return RuntimeHelpers.GetUninitializedObject(type);
1263+
#endif
12571264
}
12581265

12591266
static void LoadCustomClassLoaderRedirects(RuntimeContext context)

src/IKVM.Tools.Exporter/IkvmExporterContext.NetCore.cs

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
#if NETCOREAPP
22

33
using System;
4-
using System.IO;
54
using System.Reflection;
65
using System.Runtime.Loader;
7-
using System.Runtime.Serialization.Formatters.Binary;
6+
using System.Text.Json;
87
using System.Threading;
98
using System.Threading.Tasks;
109

11-
using Microsoft.Extensions.DependencyModel.Resolution;
12-
13-
using System.Text.Json;
14-
1510
namespace IKVM.Tools.Exporter
1611
{
1712

0 commit comments

Comments
 (0)