Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Xamarin.Android.Build.Tasks] Invokers after abstracts in typemaps #9545

Merged
merged 3 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions src/Xamarin.Android.Build.Tasks/Utilities/TypeMapGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ internal sealed class TypeMapDebugEntry
public TypeDefinition TypeDefinition;
public bool SkipInJavaToManaged;
public TypeMapDebugEntry DuplicateForJavaToManaged;

public override string ToString ()
{
return $"TypeMapDebugEntry{{JavaName={JavaName}, ManagedName={ManagedName}, JavaIndex={JavaIndex}, ManagedIndex={ManagedIndex}, SkipInJavaToManaged={SkipInJavaToManaged}, DuplicateForJavaToManaged={DuplicateForJavaToManaged}}}";
}
}

// Widths include the terminating nul character but not the padding!
Expand Down Expand Up @@ -305,13 +310,18 @@ void HandleDebugDuplicates (Dictionary<string, List<TypeMapDebugEntry>> javaDupl
if (!javaDuplicates.TryGetValue (entry.JavaName, out duplicates)) {
javaDuplicates.Add (entry.JavaName, new List<TypeMapDebugEntry> { entry });
} else {
duplicates.Add (entry);
TypeMapDebugEntry oldEntry = duplicates[0];
if (td.IsAbstract || td.IsInterface || oldEntry.TypeDefinition.IsAbstract || oldEntry.TypeDefinition.IsInterface) {
if (td.IsAssignableFrom (oldEntry.TypeDefinition, cache)) {
oldEntry.TypeDefinition = td;
oldEntry.ManagedName = GetManagedTypeName (td);
}
if ((td.IsAbstract || td.IsInterface) &&
!oldEntry.TypeDefinition.IsAbstract &&
!oldEntry.TypeDefinition.IsInterface &&
td.IsAssignableFrom (oldEntry.TypeDefinition, cache)) {
// We found the `Invoker` type *before* the declared type
// Fix things up so the abstract type is first, and the `Invoker` is considered a duplicate.
duplicates.Insert (0, entry);
oldEntry.SkipInJavaToManaged = false;
} else {
// ¯\_(ツ)_/¯
duplicates.Add (entry);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ public void JavaCast_BaseToGenericWrapper ()
public void JavaCast_InterfaceCast ()
{
IntPtr g;
using (var n = new Java.Lang.Integer (42)) {
g = JNIEnv.NewGlobalRef (n.Handle);
using (var vp = global::Net.Dot.Android.Test.Example.ValueProvider) {
g = JNIEnv.NewGlobalRef (vp.Handle);
}
// We want a Java.Lang.Object so that we create an IComparableInvoker
// We want a Java.Lang.Object so that we create an IIValueProviderInvoker
// instead of just getting back the original instance.
using (var o = Java.Lang.Object.GetObject<Java.Lang.Object> (g, JniHandleOwnership.TransferGlobalRef)) {
var c = JavaObjectExtensions.JavaCast<Java.Lang.IComparable> (o);
var c = JavaObjectExtensions.JavaCast<global::Net.Dot.Android.Test.IValueProvider> (o);
Assert.AreEqual (42, c.Value);
c.Dispose ();
}
}
Expand Down Expand Up @@ -111,3 +112,7 @@ public MyObject (IntPtr handle, JniHandleOwnership transfer)
}
}

namespace Net.Dot.Android.Test {
partial class IValueProviderInvoker {
}
}
6 changes: 6 additions & 0 deletions tests/Mono.Android-Tests/Mono.Android-Test.Shared.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@
<Link>Assets\subfolder\asset2.txt</Link>
</AndroidAsset>
</ItemGroup>

<ItemGroup>
<AndroidJavaSource Include="$(MSBuildThisFileDirectory)java/net/dot/android/test/ValueProvider.java" />
<AndroidJavaSource Include="$(MSBuildThisFileDirectory)java/net/dot/android/test/Example.java" />
</ItemGroup>

<ItemGroup>
<Folder Include="$(MSBuildThisFileDirectory)LinkedAssets\" />
</ItemGroup>
Expand Down
12 changes: 12 additions & 0 deletions tests/Mono.Android-Tests/java/net/dot/android/test/Example.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package net.dot.android.test;

public class Example {
public static ValueProvider getValueProvider() {
return new ValueProvider() {
@Override
public int getValue() {
return 42;
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package net.dot.android.test;

public interface ValueProvider {
int getValue();
}
Loading