Skip to content

Commit f30e420

Browse files
authored
[build] Target net9.0 (#1279)
Context: 356485e Context: #1258 Context: dotnet/android#9793 We want to add a call to [Debugger.BreakForUserUnhandledException][0] -- which was added in .NET 9 -- into `Java.Interop.JniRuntime` as part of Issues #1258. In order to do this we need to update `dotnet/java-interop` to build with .NET 9 instead of .NET 8. This also contains fixes for new trimming warnings that were surfaced when targeting .NET 9. Requires companion changes in `dotnet/android`: dotnet/android#9793 Co-authored-by: Jonathan Peppers <[email protected]> [0]: https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.debugger.breakforuserunhandledexception
1 parent 2a7183a commit f30e420

File tree

11 files changed

+47
-35
lines changed

11 files changed

+47
-35
lines changed

Directory.Build.props

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<ProduceReferenceAssemblyInOutDir>true</ProduceReferenceAssemblyInOutDir>
1010
<!-- Disables the transitive restore of packages like Microsoft.AspNetCore.App.Ref, Microsoft.WindowsDesktop.App.Ref -->
1111
<DisableTransitiveFrameworkReferenceDownloads>true</DisableTransitiveFrameworkReferenceDownloads>
12-
<DotNetTargetFrameworkVersion>8.0</DotNetTargetFrameworkVersion>
12+
<DotNetTargetFrameworkVersion>9.0</DotNetTargetFrameworkVersion>
1313
<DotNetTargetFramework>net$(DotNetTargetFrameworkVersion)</DotNetTargetFramework>
1414
</PropertyGroup>
1515

build-tools/automation/azure-pipelines.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ pr:
1919
variables:
2020
RunningOnCI: true
2121
Build.Configuration: Release
22-
DotNetCoreVersion: 8.0.303
23-
DotNetTargetFramework: net8.0
22+
DotNetCoreVersion: 9.0.200
23+
DotNetTargetFramework: net9.0
2424
NetCoreTargetFrameworkPathSuffix: -$(DotNetTargetFramework)
2525
HostedPoolName: Azure Pipelines
2626
HostedWinImage: windows-2022

src/Java.Interop.Export/Java.Interop/MarshalMemberBuilder.cs

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Diagnostics;
4+
using System.Diagnostics.CodeAnalysis;
45
using System.Linq;
56
using System.Linq.Expressions;
67
using System.Reflection;
@@ -11,8 +12,12 @@
1112

1213
namespace Java.Interop {
1314

15+
[RequiresDynamicCode (ExpressionRequiresUnreferencedCode)]
16+
[RequiresUnreferencedCode (ExpressionRequiresUnreferencedCode)]
1417
public class MarshalMemberBuilder : JniRuntime.JniMarshalMemberBuilder
1518
{
19+
internal const string ExpressionRequiresUnreferencedCode = "System.Linq.Expression usage may trim away required code.";
20+
1621
public MarshalMemberBuilder ()
1722
{
1823
}

src/Java.Interop/Java.Interop/JniRuntime.JniMarshalMemberBuilder.cs

+5
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,13 @@ public JniMarshalMemberBuilder MarshalMemberBuilder {
2929

3030
internal bool UseMarshalMemberBuilder => marshalMemberBuilder != null;
3131

32+
const string NotUsedInAndroid = "This code path is not used in Android projects.";
33+
34+
// FIXME: https://github.com/dotnet/java-interop/issues/1192
3235
[DynamicDependency (DynamicallyAccessedMemberTypes.PublicParameterlessConstructor, "Java.Interop.MarshalMemberBuilder", "Java.Interop.Export")]
36+
[UnconditionalSuppressMessage ("Trimming", "IL2026", Justification = NotUsedInAndroid)]
3337
[UnconditionalSuppressMessage ("Trimming", "IL2035", Justification = "Java.Interop.Export.dll is not always present.")]
38+
[UnconditionalSuppressMessage ("Trimming", "IL3050", Justification = NotUsedInAndroid)]
3439
partial void SetMarshalMemberBuilder (CreationOptions options)
3540
{
3641
if (!options.UseMarshalMemberBuilder) {

src/Java.Interop/Java.Interop/JniRuntime.JniTypeManager.cs

+6-9
Original file line numberDiff line numberDiff line change
@@ -266,18 +266,16 @@ protected virtual IEnumerable<string> GetSimpleReferences (Type type)
266266
static readonly Type[] EmptyTypeArray = Array.Empty<Type> ();
267267
const string NotUsedInAndroid = "This code path is not used in Android projects.";
268268

269+
// FIXME: https://github.com/dotnet/java-interop/issues/1192
270+
[UnconditionalSuppressMessage ("Trimming", "IL3050", Justification = NotUsedInAndroid)]
269271
static Type MakeArrayType (Type type) =>
270-
// FIXME: https://github.com/dotnet/java-interop/issues/1192
271-
#pragma warning disable IL3050
272272
type.MakeArrayType ();
273-
#pragma warning restore IL3050
274273

274+
// FIXME: https://github.com/dotnet/java-interop/issues/1192
275275
[UnconditionalSuppressMessage ("Trimming", "IL2055", Justification = NotUsedInAndroid)]
276+
[UnconditionalSuppressMessage ("Trimming", "IL3050", Justification = NotUsedInAndroid)]
276277
static Type MakeGenericType (Type type, Type arrayType) =>
277-
// FIXME: https://github.com/dotnet/java-interop/issues/1192
278-
#pragma warning disable IL3050
279278
type.MakeGenericType (arrayType);
280-
#pragma warning restore IL3050
281279

282280
[UnconditionalSuppressMessage ("Trimming", "IL2073", Justification = "Types returned here should be preserved via other means.")]
283281
[return: DynamicallyAccessedMembers (MethodsConstructors)]
@@ -400,16 +398,15 @@ IEnumerable<Type> CreateGetTypesForSimpleReferenceEnumerator (string jniSimpleRe
400398
// https://github.com/xamarin/xamarin-android/blob/5472eec991cc075e4b0c09cd98a2331fb93aa0f3/src/Microsoft.Android.Sdk.ILLink/MarkJavaObjects.cs#L176-L186
401399
const string makeGenericTypeMessage = "Generic 'Invoker' types are preserved by the MarkJavaObjects trimmer step.";
402400

401+
// FIXME: https://github.com/dotnet/java-interop/issues/1192
403402
[UnconditionalSuppressMessage ("Trimming", "IL2055", Justification = makeGenericTypeMessage)]
403+
[UnconditionalSuppressMessage ("Trimming", "IL3050", Justification = makeGenericTypeMessage)]
404404
[return: DynamicallyAccessedMembers (Constructors)]
405405
static Type MakeGenericType (
406406
[DynamicallyAccessedMembers (Constructors)]
407407
Type type,
408408
Type [] arguments) =>
409-
// FIXME: https://github.com/dotnet/java-interop/issues/1192
410-
#pragma warning disable IL3050
411409
type.MakeGenericType (arguments);
412-
#pragma warning restore IL3050
413410

414411
var signature = type.GetCustomAttribute<JniTypeSignatureAttribute> ();
415412
if (signature == null || signature.InvokerType == null) {

src/Java.Interop/Java.Interop/JniRuntime.JniValueManager.cs

+2-3
Original file line numberDiff line numberDiff line change
@@ -675,12 +675,11 @@ static JniValueMarshaler GetObjectArrayMarshaler (Type elementType)
675675
{
676676
const string makeGenericMethodMessage = "This code path is not used in Android projects.";
677677

678+
// FIXME: https://github.com/dotnet/java-interop/issues/1192
678679
[UnconditionalSuppressMessage ("Trimming", "IL2060", Justification = makeGenericMethodMessage)]
680+
[UnconditionalSuppressMessage ("Trimming", "IL3050", Justification = makeGenericMethodMessage)]
679681
static MethodInfo MakeGenericMethod (MethodInfo method, Type type) =>
680-
// FIXME: https://github.com/dotnet/java-interop/issues/1192
681-
#pragma warning disable IL3050
682682
method.MakeGenericMethod (type);
683-
#pragma warning restore IL3050
684683

685684
Func<JniValueMarshaler> indirect = GetObjectArrayMarshalerHelper<object>;
686685
var reifiedMethodInfo = MakeGenericMethod (

src/Java.Interop/Java.Interop/JniRuntime.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -446,8 +446,7 @@ public virtual void OnUserUnhandledException (ref JniTransition transition, Exce
446446
{
447447
transition.SetPendingException (e);
448448

449-
// TODO: Enable when we move to 'net9.0'
450-
//Debugger.BreakForUserUnhandledException (e);
449+
Debugger.BreakForUserUnhandledException (e);
451450
}
452451

453452
public virtual void RaisePendingException (Exception pendingException)

src/Java.Interop/Java.Interop/JniValueMarshaler.cs

+1
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ Expression CreateSelf (JniValueMarshalerContext context, ParameterExpression sou
177177
return self;
178178
}
179179

180+
[UnconditionalSuppressMessage ("Trimming", "IL2073", Justification = ExpressionRequiresUnreferencedCode)]
180181
[return: DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]
181182
Type _GetType () => GetType ();
182183

src/Java.Runtime.Environment/Java.Interop/JreTypeManager.cs

+15-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Collections.Generic;
55
using System.Collections.ObjectModel;
66
using System.Diagnostics;
7+
using System.Diagnostics.CodeAnalysis;
78
using System.IO;
89
using System.Linq;
910
using System.Reflection;
@@ -17,7 +18,13 @@ namespace Java.Interop {
1718

1819
public class JreTypeManager : JniRuntime.JniTypeManager {
1920

20-
public override void RegisterNativeMembers (JniType nativeClass, Type type, ReadOnlySpan<char> methods)
21+
const string NotUsedInAndroid = "This code path is not used in Android projects.";
22+
23+
public override void RegisterNativeMembers (
24+
JniType nativeClass,
25+
[DynamicallyAccessedMembers (MethodsAndPrivateNested)]
26+
Type type,
27+
ReadOnlySpan<char> methods)
2128
{
2229
if (base.TryRegisterNativeMembers (nativeClass, type, methods)) {
2330
return;
@@ -63,6 +70,10 @@ public override void RegisterNativeMembers (JniType nativeClass, Type type, Read
6370
nativeClass.RegisterNativeMethods (registrations.ToArray ());
6471
}
6572

73+
// FIXME: https://github.com/dotnet/java-interop/issues/1192
74+
[UnconditionalSuppressMessage ("Trimming", "IL2062", Justification = NotUsedInAndroid)]
75+
[UnconditionalSuppressMessage ("Trimming", "IL2070", Justification = NotUsedInAndroid)]
76+
[UnconditionalSuppressMessage ("Trimming", "IL2078", Justification = NotUsedInAndroid)]
6677
static void AddInterfaceMethods (JniMethodMap toRegister, Type type)
6778
{
6879
foreach (var iface in type.GetInterfaces ()) {
@@ -86,7 +97,9 @@ static void AddJniMethod (JniMethodMap toRegister, MethodInfo declaringMethod, M
8697
toRegister [("n_" + signature.MemberName, signature.MemberSignature)] = targetMethod ?? declaringMethod;
8798
}
8899

89-
static void AddClassMethods (JniMethodMap toRegister, Type type)
100+
// FIXME: https://github.com/dotnet/java-interop/issues/1192
101+
[UnconditionalSuppressMessage ("Trimming", "IL2070", Justification = NotUsedInAndroid)]
102+
static void AddClassMethods (JniMethodMap toRegister, [DynamicallyAccessedMembers (Methods)] Type type)
90103
{
91104
const BindingFlags Flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
92105
foreach (var method in type.GetMethods (Flags)) {

src/Java.Runtime.Environment/Java.Interop/ManagedValueManager.cs

+9-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Collections.ObjectModel;
44
using System.Diagnostics;
5+
using System.Diagnostics.CodeAnalysis;
56
using System.IO;
67
using System.Linq;
78
using System.Reflection;
@@ -231,12 +232,17 @@ void ActivateViaReflection (JniObjectReference reference, ConstructorInfo cinfo,
231232
{
232233
var declType = cinfo.DeclaringType ?? throw new NotSupportedException ("Do not know the type to create!");
233234

234-
#pragma warning disable IL2072
235-
var self = (IJavaPeerable) System.Runtime.CompilerServices.RuntimeHelpers.GetUninitializedObject (declType);
236-
#pragma warning restore IL2072
235+
var self = GetUninitializedObject (declType);
237236
self.SetPeerReference (reference);
238237

239238
cinfo.Invoke (self, argumentValues);
239+
240+
// FIXME: https://github.com/dotnet/java-interop/issues/1192
241+
const string getUninitializedObject = "This code path is not used in Android projects.";
242+
[UnconditionalSuppressMessage ("Trimming", "IL2067", Justification = getUninitializedObject)]
243+
[UnconditionalSuppressMessage ("Trimming", "IL2072", Justification = getUninitializedObject)]
244+
static IJavaPeerable GetUninitializedObject (Type type) =>
245+
(IJavaPeerable) System.Runtime.CompilerServices.RuntimeHelpers.GetUninitializedObject (type);
240246
}
241247

242248
public override List<JniSurfacedPeerInfo> GetSurfacedPeers ()

tests/generator-Tests/SupportFiles/DebuggerDisableUserUnhandledExceptionsAttribute.cs

-13
This file was deleted.

0 commit comments

Comments
 (0)