From 184ae5c9a0992dc59fb28ceb945f6cc703cbe082 Mon Sep 17 00:00:00 2001 From: Jonathan Pobst Date: Mon, 12 Aug 2024 10:58:28 -1000 Subject: [PATCH 1/2] [generator] Place interface alternatives `[Register]` classes in dummy package. --- .../XAJavaInterop1-NRT/WriteInterface.txt | 4 +- .../ObsoleteInterfaceAlternativeClass.txt | 2 +- .../WriteConstSugarInterfaceFields.txt | 2 +- .../XAJavaInterop1/WriteInterface.txt | 4 +- .../WriteInterfaceFieldAsDimProperty.txt | 4 +- .../WriteStaticInterfaceMethod.txt | 4 +- ...erfaceMethodWithRegisteredAlternatives.txt | 104 ++++++++++++++++++ .../DefaultInterfaceMethodsTests.cs | 16 +++ .../TestInterface/Test.ME.ITestInterface.cs | 4 +- tools/generator/CodeGenerationOptions.cs | 1 + tools/generator/CodeGenerator.cs | 1 + tools/generator/CodeGeneratorOptions.cs | 2 + .../InterfaceMemberAlternativeClass.cs | 9 +- 13 files changed, 143 insertions(+), 14 deletions(-) create mode 100644 tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/XAJavaInterop1/WriteStaticInterfaceMethodWithRegisteredAlternatives.txt diff --git a/tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/XAJavaInterop1-NRT/WriteInterface.txt b/tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/XAJavaInterop1-NRT/WriteInterface.txt index bf2f261cd..b5db9ed4f 100644 --- a/tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/XAJavaInterop1-NRT/WriteInterface.txt +++ b/tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/XAJavaInterop1-NRT/WriteInterface.txt @@ -1,4 +1,4 @@ -[Register ("java/code/IMyInterface", DoNotGenerateAcw=true)] +[Register ("mono/internal/java/code/IMyInterface", DoNotGenerateAcw=true)] public abstract class MyInterface : Java.Lang.Object { internal MyInterface () { @@ -19,7 +19,7 @@ public abstract class MyInterface : Java.Lang.Object { } -[Register ("java/code/IMyInterface", DoNotGenerateAcw=true)] +[Register ("mono/internal/java/code/IMyInterface", DoNotGenerateAcw=true)] [global::System.Obsolete (@"Use the 'MyInterface' type. This type will be removed in a future release.", error: true)] public abstract class MyInterfaceConsts : MyInterface { private MyInterfaceConsts () diff --git a/tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/XAJavaInterop1/ObsoleteInterfaceAlternativeClass.txt b/tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/XAJavaInterop1/ObsoleteInterfaceAlternativeClass.txt index e14a7db52..e33507b67 100644 --- a/tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/XAJavaInterop1/ObsoleteInterfaceAlternativeClass.txt +++ b/tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/XAJavaInterop1/ObsoleteInterfaceAlternativeClass.txt @@ -1,4 +1,4 @@ -[Register ("com/xamarin/android/Parent", DoNotGenerateAcw=true)] +[Register ("mono/internal/com/xamarin/android/Parent", DoNotGenerateAcw=true)] [global::System.Obsolete (@"Use the 'Com.Xamarin.Android.IParent' type. This class will be removed in a future release.")] public abstract class Parent : Java.Lang.Object { internal Parent () diff --git a/tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/XAJavaInterop1/WriteConstSugarInterfaceFields.txt b/tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/XAJavaInterop1/WriteConstSugarInterfaceFields.txt index 25e31bc37..b488f9a2f 100644 --- a/tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/XAJavaInterop1/WriteConstSugarInterfaceFields.txt +++ b/tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/XAJavaInterop1/WriteConstSugarInterfaceFields.txt @@ -1,4 +1,4 @@ -[Register ("java/code/IMyInterface", DoNotGenerateAcw=true)] +[Register ("mono/internal/java/code/IMyInterface", DoNotGenerateAcw=true)] public abstract class MyInterface : Java.Lang.Object { internal MyInterface () { diff --git a/tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/XAJavaInterop1/WriteInterface.txt b/tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/XAJavaInterop1/WriteInterface.txt index 593da8196..9a0540855 100644 --- a/tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/XAJavaInterop1/WriteInterface.txt +++ b/tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/XAJavaInterop1/WriteInterface.txt @@ -1,4 +1,4 @@ -[Register ("java/code/IMyInterface", DoNotGenerateAcw=true)] +[Register ("mono/internal/java/code/IMyInterface", DoNotGenerateAcw=true)] public abstract class MyInterface : Java.Lang.Object { internal MyInterface () { @@ -19,7 +19,7 @@ public abstract class MyInterface : Java.Lang.Object { } -[Register ("java/code/IMyInterface", DoNotGenerateAcw=true)] +[Register ("mono/internal/java/code/IMyInterface", DoNotGenerateAcw=true)] [global::System.Obsolete (@"Use the 'MyInterface' type. This type will be removed in a future release.", error: true)] public abstract class MyInterfaceConsts : MyInterface { private MyInterfaceConsts () diff --git a/tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/XAJavaInterop1/WriteInterfaceFieldAsDimProperty.txt b/tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/XAJavaInterop1/WriteInterfaceFieldAsDimProperty.txt index 399abba9d..f19f65d0f 100644 --- a/tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/XAJavaInterop1/WriteInterfaceFieldAsDimProperty.txt +++ b/tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/XAJavaInterop1/WriteInterfaceFieldAsDimProperty.txt @@ -1,4 +1,4 @@ -[Register ("com/xamarin/android/MyInterface", DoNotGenerateAcw=true)] +[Register ("mono/internal/com/xamarin/android/MyInterface", DoNotGenerateAcw=true)] public abstract class MyInterface : Java.Lang.Object { internal MyInterface () { @@ -32,7 +32,7 @@ public abstract class MyInterface : Java.Lang.Object { } -[Register ("com/xamarin/android/MyInterface", DoNotGenerateAcw=true)] +[Register ("mono/internal/com/xamarin/android/MyInterface", DoNotGenerateAcw=true)] [global::System.Obsolete (@"Use the 'MyInterface' type. This type will be removed in a future release.", error: true)] public abstract class MyInterfaceConsts : MyInterface { private MyInterfaceConsts () diff --git a/tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/XAJavaInterop1/WriteStaticInterfaceMethod.txt b/tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/XAJavaInterop1/WriteStaticInterfaceMethod.txt index c0e0aab82..7a37f828b 100644 --- a/tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/XAJavaInterop1/WriteStaticInterfaceMethod.txt +++ b/tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/XAJavaInterop1/WriteStaticInterfaceMethod.txt @@ -1,4 +1,4 @@ -[Register ("java/code/IMyInterface", DoNotGenerateAcw=true)] +[Register ("mono/internal/java/code/IMyInterface", DoNotGenerateAcw=true)] public abstract class MyInterface : Java.Lang.Object { internal MyInterface () { @@ -19,7 +19,7 @@ public abstract class MyInterface : Java.Lang.Object { } -[Register ("java/code/IMyInterface", DoNotGenerateAcw=true)] +[Register ("mono/internal/java/code/IMyInterface", DoNotGenerateAcw=true)] [global::System.Obsolete (@"Use the 'MyInterface' type. This type will be removed in a future release.", error: true)] public abstract class MyInterfaceConsts : MyInterface { private MyInterfaceConsts () diff --git a/tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/XAJavaInterop1/WriteStaticInterfaceMethodWithRegisteredAlternatives.txt b/tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/XAJavaInterop1/WriteStaticInterfaceMethodWithRegisteredAlternatives.txt new file mode 100644 index 000000000..c0e0aab82 --- /dev/null +++ b/tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/XAJavaInterop1/WriteStaticInterfaceMethodWithRegisteredAlternatives.txt @@ -0,0 +1,104 @@ +[Register ("java/code/IMyInterface", DoNotGenerateAcw=true)] +public abstract class MyInterface : Java.Lang.Object { + internal MyInterface () + { + } + + // Metadata.xml XPath method reference: path="/api/package[@name='java.code']/interface[@name='IMyInterface']/method[@name='DoSomething' and count(parameter)=0]" + [Register ("DoSomething", "()V", "")] + public static unsafe void DoSomething () + { + const string __id = "DoSomething.()V"; + try { + _members.StaticMethods.InvokeVoidMethod (__id, null); + } finally { + } + } + + static readonly JniPeerMembers _members = new XAPeerMembers ("java/code/IMyInterface", typeof (MyInterface)); + +} + +[Register ("java/code/IMyInterface", DoNotGenerateAcw=true)] +[global::System.Obsolete (@"Use the 'MyInterface' type. This type will be removed in a future release.", error: true)] +public abstract class MyInterfaceConsts : MyInterface { + private MyInterfaceConsts () + { + } + +} + +// Metadata.xml XPath interface reference: path="/api/package[@name='java.code']/interface[@name='IMyInterface']" +[Register ("java/code/IMyInterface", "", "java.code.IMyInterfaceInvoker")] +public partial interface IMyInterface : IJavaObject, IJavaPeerable { + private static readonly JniPeerMembers _members = new XAPeerMembers ("java/code/IMyInterface", typeof (IMyInterface), isInterface: true); + + // Metadata.xml XPath method reference: path="/api/package[@name='java.code']/interface[@name='IMyInterface']/method[@name='DoSomething' and count(parameter)=0]" + [Register ("DoSomething", "()V", "")] + public static unsafe void DoSomething () + { + const string __id = "DoSomething.()V"; + try { + _members.StaticMethods.InvokeVoidMethod (__id, null); + } finally { + } + } + +} + +[global::Android.Runtime.Register ("java/code/IMyInterface", DoNotGenerateAcw=true)] +internal partial class IMyInterfaceInvoker : global::Java.Lang.Object, IMyInterface { + static readonly JniPeerMembers _members = new XAPeerMembers ("java/code/IMyInterface", typeof (IMyInterfaceInvoker)); + + static IntPtr java_class_ref { + get { return _members.JniPeerType.PeerReference.Handle; } + } + + [global::System.Diagnostics.DebuggerBrowsable (global::System.Diagnostics.DebuggerBrowsableState.Never)] + [global::System.ComponentModel.EditorBrowsable (global::System.ComponentModel.EditorBrowsableState.Never)] + public override global::Java.Interop.JniPeerMembers JniPeerMembers { + get { return _members; } + } + + [global::System.Diagnostics.DebuggerBrowsable (global::System.Diagnostics.DebuggerBrowsableState.Never)] + [global::System.ComponentModel.EditorBrowsable (global::System.ComponentModel.EditorBrowsableState.Never)] + protected override IntPtr ThresholdClass { + get { return class_ref; } + } + + [global::System.Diagnostics.DebuggerBrowsable (global::System.Diagnostics.DebuggerBrowsableState.Never)] + [global::System.ComponentModel.EditorBrowsable (global::System.ComponentModel.EditorBrowsableState.Never)] + protected override global::System.Type ThresholdType { + get { return _members.ManagedPeerType; } + } + + IntPtr class_ref; + + public static IMyInterface GetObject (IntPtr handle, JniHandleOwnership transfer) + { + return global::Java.Lang.Object.GetObject (handle, transfer); + } + + static IntPtr Validate (IntPtr handle) + { + if (!JNIEnv.IsInstanceOf (handle, java_class_ref)) + throw new InvalidCastException ($"Unable to convert instance of type '{JNIEnv.GetClassNameFromInstance (handle)}' to type 'java.code.IMyInterface'."); + return handle; + } + + protected override void Dispose (bool disposing) + { + if (this.class_ref != IntPtr.Zero) + JNIEnv.DeleteGlobalRef (this.class_ref); + this.class_ref = IntPtr.Zero; + base.Dispose (disposing); + } + + public IMyInterfaceInvoker (IntPtr handle, JniHandleOwnership transfer) : base (Validate (handle), transfer) + { + IntPtr local_ref = JNIEnv.GetObjectClass (((global::Java.Lang.Object) this).Handle); + this.class_ref = JNIEnv.NewGlobalRef (local_ref); + JNIEnv.DeleteLocalRef (local_ref); + } + +} diff --git a/tests/generator-Tests/Unit-Tests/DefaultInterfaceMethodsTests.cs b/tests/generator-Tests/Unit-Tests/DefaultInterfaceMethodsTests.cs index 9b94acd88..1da494e59 100644 --- a/tests/generator-Tests/Unit-Tests/DefaultInterfaceMethodsTests.cs +++ b/tests/generator-Tests/Unit-Tests/DefaultInterfaceMethodsTests.cs @@ -208,6 +208,22 @@ public void WriteStaticInterfaceMethod () AssertTargetedExpected (nameof (WriteStaticInterfaceMethod), writer.ToString ()); } + [Test] + public void WriteStaticInterfaceMethodWithRegisteredAlternatives () + { + options.RegisterInterfaceAlternativesInOriginalNamespace = true; + + // Create an interface with a static method + var iface = SupportTypeBuilder.CreateEmptyInterface ("java.code.IMyInterface"); + iface.Methods.Add (new TestMethod (iface, "DoSomething").SetStatic ()); + + iface.Validate (options, new GenericParameterDefinitionList (), new CodeGeneratorContext ()); + + generator.WriteType (iface, string.Empty, new GenerationInfo (string.Empty, string.Empty, "MyAssembly")); + + AssertTargetedExpected (nameof (WriteStaticInterfaceMethodWithRegisteredAlternatives), writer.ToString ()); + } + [Test] public void WriteStaticInterfaceProperty () { diff --git a/tests/generator-Tests/expected.xaji/TestInterface/Test.ME.ITestInterface.cs b/tests/generator-Tests/expected.xaji/TestInterface/Test.ME.ITestInterface.cs index 8f2af06b3..e82e59ef6 100644 --- a/tests/generator-Tests/expected.xaji/TestInterface/Test.ME.ITestInterface.cs +++ b/tests/generator-Tests/expected.xaji/TestInterface/Test.ME.ITestInterface.cs @@ -5,7 +5,7 @@ namespace Test.ME { - [Register ("test/me/TestInterface", DoNotGenerateAcw=true)] + [Register ("mono/internal/test/me/TestInterface", DoNotGenerateAcw=true)] public abstract class TestInterface : Java.Lang.Object { internal TestInterface () { @@ -31,7 +31,7 @@ internal TestInterface () } - [Register ("test/me/TestInterface", DoNotGenerateAcw=true)] + [Register ("mono/internal/test/me/TestInterface", DoNotGenerateAcw=true)] [global::System.Obsolete (@"Use the 'TestInterface' type. This type will be removed in a future release.", error: true)] public abstract class TestInterfaceConsts : TestInterface { private TestInterfaceConsts () diff --git a/tools/generator/CodeGenerationOptions.cs b/tools/generator/CodeGenerationOptions.cs index 51f244451..c873f5c6c 100644 --- a/tools/generator/CodeGenerationOptions.cs +++ b/tools/generator/CodeGenerationOptions.cs @@ -67,6 +67,7 @@ public SymbolTable SymbolTable { public bool UseObsoletedOSPlatformAttributes { get; set; } public bool UseRestrictToAttributes { get; set; } public bool FixObsoleteOverrides { get; set; } + public bool RegisterInterfaceAlternativesInOriginalNamespace { get; set; } public bool RemoveConstSugar => BuildingCoreAssembly; bool? buildingCoreAssembly; diff --git a/tools/generator/CodeGenerator.cs b/tools/generator/CodeGenerator.cs index 927b4e24e..76072f5df 100644 --- a/tools/generator/CodeGenerator.cs +++ b/tools/generator/CodeGenerator.cs @@ -87,6 +87,7 @@ static void Run (CodeGeneratorOptions options, DirectoryAssemblyResolver resolve UseRestrictToAttributes = options.UseRestrictToAttributes, EmitLegacyInterfaceInvokers = options.EmitLegacyInterfaceInvokers, FixObsoleteOverrides = options.FixObsoleteOverrides, + RegisterInterfaceAlternativesInOriginalNamespace = options.RegisterInterfaceAlternativesInOriginalNamespace, }; var resolverCache = new TypeDefinitionCache (); diff --git a/tools/generator/CodeGeneratorOptions.cs b/tools/generator/CodeGeneratorOptions.cs index 317f8b93a..d1b11e71e 100644 --- a/tools/generator/CodeGeneratorOptions.cs +++ b/tools/generator/CodeGeneratorOptions.cs @@ -57,6 +57,7 @@ public CodeGeneratorOptions () public bool FixObsoleteOverrides { get; set;} = true; public bool UseLegacyJavaResolver { get; set; } public bool UseObsoletedOSPlatformAttributes { get; set; } + public bool RegisterInterfaceAlternativesInOriginalNamespace { get; set; } public XmldocStyle XmldocStyle { get; set; } = XmldocStyle.IntelliSense; @@ -116,6 +117,7 @@ public static CodeGeneratorOptions Parse (string[] args) opts.UseObsoletedOSPlatformAttributes = v?.Contains ("obsoleted-platform-attributes") == true; opts.UseRestrictToAttributes = v?.Contains ("restrict-to-attributes") == true; opts.FixObsoleteOverrides = v?.Contains ("do-not-fix-obsolete-overrides") == false; + opts.RegisterInterfaceAlternativesInOriginalNamespace = v?.Contains ("register-interface-alternatives-original-ns") == true; }}, { "preserve-enums", "For internal use.", diff --git a/tools/generator/SourceWriters/InterfaceMemberAlternativeClass.cs b/tools/generator/SourceWriters/InterfaceMemberAlternativeClass.cs index 8d99cb8e9..4b92b72d8 100644 --- a/tools/generator/SourceWriters/InterfaceMemberAlternativeClass.cs +++ b/tools/generator/SourceWriters/InterfaceMemberAlternativeClass.cs @@ -39,7 +39,9 @@ public InterfaceMemberAlternativeClass (InterfaceGen iface, CodeGenerationOption SourceWriterExtensions.AddSupportedOSPlatform (Attributes, iface, opt); - Attributes.Add (new RegisterAttr (iface.RawJniName, noAcw: true, additionalProperties: iface.AdditionalAttributeString ()) { AcwLast = true }); + // Place this class in a dummy package that won't conflict the actual interface + var register_jni = (opt.RegisterInterfaceAlternativesInOriginalNamespace ? string.Empty : "mono/internal/") + iface.RawJniName; + Attributes.Add (new RegisterAttr (register_jni, noAcw: true, additionalProperties: iface.AdditionalAttributeString ()) { AcwLast = true }); if (should_obsolete) SourceWriterExtensions.AddObsolete (Attributes, $"Use the '{iface.FullName}' type. This class will be removed in a future release.", opt); @@ -179,7 +181,10 @@ public InterfaceConstsForwardClass (InterfaceGen iface, CodeGenerationOptions op IsPublic = true; IsAbstract = true; - Attributes.Add (new RegisterAttr (iface.RawJniName, noAcw: true, additionalProperties: iface.AdditionalAttributeString ())); + // Place this class in a dummy package that won't conflict the actual interface + var register_jni = (opt.RegisterInterfaceAlternativesInOriginalNamespace ? string.Empty : "mono/internal/") + iface.RawJniName; + Attributes.Add (new RegisterAttr (register_jni, noAcw: true, additionalProperties: iface.AdditionalAttributeString ())); + SourceWriterExtensions.AddObsolete (Attributes, $"Use the '{iface.Name.Substring (1)}' type. This type will be removed in a future release.", opt, isError: true); Constructors.Add (new ConstructorWriter { From e72247fb97486e7d4e35dc3188635a8df9a8543e Mon Sep 17 00:00:00 2001 From: Jonathan Pobst Date: Tue, 20 Aug 2024 10:05:26 -1000 Subject: [PATCH 2/2] Remove opt-out. --- ...erfaceMethodWithRegisteredAlternatives.txt | 104 ------------------ .../DefaultInterfaceMethodsTests.cs | 16 --- tools/generator/CodeGenerationOptions.cs | 1 - tools/generator/CodeGenerator.cs | 1 - tools/generator/CodeGeneratorOptions.cs | 2 - .../InterfaceMemberAlternativeClass.cs | 6 +- 6 files changed, 2 insertions(+), 128 deletions(-) delete mode 100644 tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/XAJavaInterop1/WriteStaticInterfaceMethodWithRegisteredAlternatives.txt diff --git a/tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/XAJavaInterop1/WriteStaticInterfaceMethodWithRegisteredAlternatives.txt b/tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/XAJavaInterop1/WriteStaticInterfaceMethodWithRegisteredAlternatives.txt deleted file mode 100644 index c0e0aab82..000000000 --- a/tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/XAJavaInterop1/WriteStaticInterfaceMethodWithRegisteredAlternatives.txt +++ /dev/null @@ -1,104 +0,0 @@ -[Register ("java/code/IMyInterface", DoNotGenerateAcw=true)] -public abstract class MyInterface : Java.Lang.Object { - internal MyInterface () - { - } - - // Metadata.xml XPath method reference: path="/api/package[@name='java.code']/interface[@name='IMyInterface']/method[@name='DoSomething' and count(parameter)=0]" - [Register ("DoSomething", "()V", "")] - public static unsafe void DoSomething () - { - const string __id = "DoSomething.()V"; - try { - _members.StaticMethods.InvokeVoidMethod (__id, null); - } finally { - } - } - - static readonly JniPeerMembers _members = new XAPeerMembers ("java/code/IMyInterface", typeof (MyInterface)); - -} - -[Register ("java/code/IMyInterface", DoNotGenerateAcw=true)] -[global::System.Obsolete (@"Use the 'MyInterface' type. This type will be removed in a future release.", error: true)] -public abstract class MyInterfaceConsts : MyInterface { - private MyInterfaceConsts () - { - } - -} - -// Metadata.xml XPath interface reference: path="/api/package[@name='java.code']/interface[@name='IMyInterface']" -[Register ("java/code/IMyInterface", "", "java.code.IMyInterfaceInvoker")] -public partial interface IMyInterface : IJavaObject, IJavaPeerable { - private static readonly JniPeerMembers _members = new XAPeerMembers ("java/code/IMyInterface", typeof (IMyInterface), isInterface: true); - - // Metadata.xml XPath method reference: path="/api/package[@name='java.code']/interface[@name='IMyInterface']/method[@name='DoSomething' and count(parameter)=0]" - [Register ("DoSomething", "()V", "")] - public static unsafe void DoSomething () - { - const string __id = "DoSomething.()V"; - try { - _members.StaticMethods.InvokeVoidMethod (__id, null); - } finally { - } - } - -} - -[global::Android.Runtime.Register ("java/code/IMyInterface", DoNotGenerateAcw=true)] -internal partial class IMyInterfaceInvoker : global::Java.Lang.Object, IMyInterface { - static readonly JniPeerMembers _members = new XAPeerMembers ("java/code/IMyInterface", typeof (IMyInterfaceInvoker)); - - static IntPtr java_class_ref { - get { return _members.JniPeerType.PeerReference.Handle; } - } - - [global::System.Diagnostics.DebuggerBrowsable (global::System.Diagnostics.DebuggerBrowsableState.Never)] - [global::System.ComponentModel.EditorBrowsable (global::System.ComponentModel.EditorBrowsableState.Never)] - public override global::Java.Interop.JniPeerMembers JniPeerMembers { - get { return _members; } - } - - [global::System.Diagnostics.DebuggerBrowsable (global::System.Diagnostics.DebuggerBrowsableState.Never)] - [global::System.ComponentModel.EditorBrowsable (global::System.ComponentModel.EditorBrowsableState.Never)] - protected override IntPtr ThresholdClass { - get { return class_ref; } - } - - [global::System.Diagnostics.DebuggerBrowsable (global::System.Diagnostics.DebuggerBrowsableState.Never)] - [global::System.ComponentModel.EditorBrowsable (global::System.ComponentModel.EditorBrowsableState.Never)] - protected override global::System.Type ThresholdType { - get { return _members.ManagedPeerType; } - } - - IntPtr class_ref; - - public static IMyInterface GetObject (IntPtr handle, JniHandleOwnership transfer) - { - return global::Java.Lang.Object.GetObject (handle, transfer); - } - - static IntPtr Validate (IntPtr handle) - { - if (!JNIEnv.IsInstanceOf (handle, java_class_ref)) - throw new InvalidCastException ($"Unable to convert instance of type '{JNIEnv.GetClassNameFromInstance (handle)}' to type 'java.code.IMyInterface'."); - return handle; - } - - protected override void Dispose (bool disposing) - { - if (this.class_ref != IntPtr.Zero) - JNIEnv.DeleteGlobalRef (this.class_ref); - this.class_ref = IntPtr.Zero; - base.Dispose (disposing); - } - - public IMyInterfaceInvoker (IntPtr handle, JniHandleOwnership transfer) : base (Validate (handle), transfer) - { - IntPtr local_ref = JNIEnv.GetObjectClass (((global::Java.Lang.Object) this).Handle); - this.class_ref = JNIEnv.NewGlobalRef (local_ref); - JNIEnv.DeleteLocalRef (local_ref); - } - -} diff --git a/tests/generator-Tests/Unit-Tests/DefaultInterfaceMethodsTests.cs b/tests/generator-Tests/Unit-Tests/DefaultInterfaceMethodsTests.cs index 1da494e59..9b94acd88 100644 --- a/tests/generator-Tests/Unit-Tests/DefaultInterfaceMethodsTests.cs +++ b/tests/generator-Tests/Unit-Tests/DefaultInterfaceMethodsTests.cs @@ -208,22 +208,6 @@ public void WriteStaticInterfaceMethod () AssertTargetedExpected (nameof (WriteStaticInterfaceMethod), writer.ToString ()); } - [Test] - public void WriteStaticInterfaceMethodWithRegisteredAlternatives () - { - options.RegisterInterfaceAlternativesInOriginalNamespace = true; - - // Create an interface with a static method - var iface = SupportTypeBuilder.CreateEmptyInterface ("java.code.IMyInterface"); - iface.Methods.Add (new TestMethod (iface, "DoSomething").SetStatic ()); - - iface.Validate (options, new GenericParameterDefinitionList (), new CodeGeneratorContext ()); - - generator.WriteType (iface, string.Empty, new GenerationInfo (string.Empty, string.Empty, "MyAssembly")); - - AssertTargetedExpected (nameof (WriteStaticInterfaceMethodWithRegisteredAlternatives), writer.ToString ()); - } - [Test] public void WriteStaticInterfaceProperty () { diff --git a/tools/generator/CodeGenerationOptions.cs b/tools/generator/CodeGenerationOptions.cs index c873f5c6c..51f244451 100644 --- a/tools/generator/CodeGenerationOptions.cs +++ b/tools/generator/CodeGenerationOptions.cs @@ -67,7 +67,6 @@ public SymbolTable SymbolTable { public bool UseObsoletedOSPlatformAttributes { get; set; } public bool UseRestrictToAttributes { get; set; } public bool FixObsoleteOverrides { get; set; } - public bool RegisterInterfaceAlternativesInOriginalNamespace { get; set; } public bool RemoveConstSugar => BuildingCoreAssembly; bool? buildingCoreAssembly; diff --git a/tools/generator/CodeGenerator.cs b/tools/generator/CodeGenerator.cs index 76072f5df..927b4e24e 100644 --- a/tools/generator/CodeGenerator.cs +++ b/tools/generator/CodeGenerator.cs @@ -87,7 +87,6 @@ static void Run (CodeGeneratorOptions options, DirectoryAssemblyResolver resolve UseRestrictToAttributes = options.UseRestrictToAttributes, EmitLegacyInterfaceInvokers = options.EmitLegacyInterfaceInvokers, FixObsoleteOverrides = options.FixObsoleteOverrides, - RegisterInterfaceAlternativesInOriginalNamespace = options.RegisterInterfaceAlternativesInOriginalNamespace, }; var resolverCache = new TypeDefinitionCache (); diff --git a/tools/generator/CodeGeneratorOptions.cs b/tools/generator/CodeGeneratorOptions.cs index d1b11e71e..317f8b93a 100644 --- a/tools/generator/CodeGeneratorOptions.cs +++ b/tools/generator/CodeGeneratorOptions.cs @@ -57,7 +57,6 @@ public CodeGeneratorOptions () public bool FixObsoleteOverrides { get; set;} = true; public bool UseLegacyJavaResolver { get; set; } public bool UseObsoletedOSPlatformAttributes { get; set; } - public bool RegisterInterfaceAlternativesInOriginalNamespace { get; set; } public XmldocStyle XmldocStyle { get; set; } = XmldocStyle.IntelliSense; @@ -117,7 +116,6 @@ public static CodeGeneratorOptions Parse (string[] args) opts.UseObsoletedOSPlatformAttributes = v?.Contains ("obsoleted-platform-attributes") == true; opts.UseRestrictToAttributes = v?.Contains ("restrict-to-attributes") == true; opts.FixObsoleteOverrides = v?.Contains ("do-not-fix-obsolete-overrides") == false; - opts.RegisterInterfaceAlternativesInOriginalNamespace = v?.Contains ("register-interface-alternatives-original-ns") == true; }}, { "preserve-enums", "For internal use.", diff --git a/tools/generator/SourceWriters/InterfaceMemberAlternativeClass.cs b/tools/generator/SourceWriters/InterfaceMemberAlternativeClass.cs index 4b92b72d8..c247fceb7 100644 --- a/tools/generator/SourceWriters/InterfaceMemberAlternativeClass.cs +++ b/tools/generator/SourceWriters/InterfaceMemberAlternativeClass.cs @@ -40,8 +40,7 @@ public InterfaceMemberAlternativeClass (InterfaceGen iface, CodeGenerationOption SourceWriterExtensions.AddSupportedOSPlatform (Attributes, iface, opt); // Place this class in a dummy package that won't conflict the actual interface - var register_jni = (opt.RegisterInterfaceAlternativesInOriginalNamespace ? string.Empty : "mono/internal/") + iface.RawJniName; - Attributes.Add (new RegisterAttr (register_jni, noAcw: true, additionalProperties: iface.AdditionalAttributeString ()) { AcwLast = true }); + Attributes.Add (new RegisterAttr ("mono/internal/" + iface.RawJniName, noAcw: true, additionalProperties: iface.AdditionalAttributeString ()) { AcwLast = true }); if (should_obsolete) SourceWriterExtensions.AddObsolete (Attributes, $"Use the '{iface.FullName}' type. This class will be removed in a future release.", opt); @@ -182,8 +181,7 @@ public InterfaceConstsForwardClass (InterfaceGen iface, CodeGenerationOptions op IsAbstract = true; // Place this class in a dummy package that won't conflict the actual interface - var register_jni = (opt.RegisterInterfaceAlternativesInOriginalNamespace ? string.Empty : "mono/internal/") + iface.RawJniName; - Attributes.Add (new RegisterAttr (register_jni, noAcw: true, additionalProperties: iface.AdditionalAttributeString ())); + Attributes.Add (new RegisterAttr ("mono/internal/" + iface.RawJniName, noAcw: true, additionalProperties: iface.AdditionalAttributeString ())); SourceWriterExtensions.AddObsolete (Attributes, $"Use the '{iface.Name.Substring (1)}' type. This type will be removed in a future release.", opt, isError: true);