Skip to content

Bump to dotnet/java-interop/main@d3d3a1bf #9921

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
13 changes: 10 additions & 3 deletions src/Mono.Android/Android.Runtime/AndroidRuntime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,13 @@ public override IntPtr ReleaseLocalReference (ref JniObjectReference value, ref
public override void WriteLocalReferenceLine (string format, params object?[] args)
{
RuntimeNativeMethods._monodroid_gref_log ("[LREF] " + string.Format (CultureInfo.InvariantCulture, format, args));
RuntimeNativeMethods._monodroid_gref_log ("\n");
}

public override void WriteGlobalReferenceLine (string format, params object?[] args)
{
RuntimeNativeMethods._monodroid_gref_log (string.Format (CultureInfo.InvariantCulture, format, args));
RuntimeNativeMethods._monodroid_gref_log ("\n");
}

public override JniObjectReference CreateGlobalReference (JniObjectReference value)
Expand Down Expand Up @@ -689,7 +691,7 @@ internal void AddPeer (IJavaPeerable value, JniObjectReference reference, IntPtr
for (int i = 0; i < targets.Count; ++i) {
IJavaPeerable? target;
var wref = targets [i];
if (ShouldReplaceMapping (wref!, reference, out target)) {
if (ShouldReplaceMapping (wref!, reference, value, out target)) {
found = true;
targets [i] = IdentityHashTargets.CreateWeakReference (value);
break;
Expand Down Expand Up @@ -747,7 +749,7 @@ internal void AddPeer (IJavaPeerable value, IntPtr handle, JniHandleOwnership tr
}
}

bool ShouldReplaceMapping (WeakReference<IJavaPeerable> current, JniObjectReference reference, out IJavaPeerable? target)
bool ShouldReplaceMapping (WeakReference<IJavaPeerable> current, JniObjectReference reference, IJavaPeerable value, out IJavaPeerable? target)
{
target = null;

Expand All @@ -771,12 +773,17 @@ bool ShouldReplaceMapping (WeakReference<IJavaPeerable> current, JniObjectRefere
// we want the 2nd MCW to replace the 1st, as the 2nd is
// the one the dev created; the 1st is an implicit intermediary.
//
// Meanwhile, a new "replaceable" instance should *not* replace an
// existing "replaceable" instance; see dotnet/android#9862.
//
// [0]: If Java ctor invokes overridden virtual method, we'll
// transition into managed code w/o a registered instance, and
// thus will create an "intermediary" via
// (IntPtr, JniHandleOwnership) .ctor.
if ((target.JniManagedPeerState & JniManagedPeerStates.Replaceable) == JniManagedPeerStates.Replaceable)
if (target.JniManagedPeerState.HasFlag (JniManagedPeerStates.Replaceable) &&
!value.JniManagedPeerState.HasFlag (JniManagedPeerStates.Replaceable)) {
return true;
}

return false;
}
Expand Down
1 change: 1 addition & 0 deletions src/Mono.Android/Android.Runtime/JNIEnv.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1070,6 +1070,7 @@ static int _GetArrayLength (IntPtr array_ptr)
ret [i] = targetType == null || targetType.IsInstanceOfType (value)
? value
: Convert.ChangeType (value, targetType, CultureInfo.InvariantCulture);
GC.KeepAlive (value);
}

return ret;
Expand Down
21 changes: 15 additions & 6 deletions src/Mono.Android/Java.Interop/TypeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -359,9 +359,6 @@ static Type monovm_typemap_java_to_managed (string java_type_name)

try {
result = (IJavaPeerable) CreateProxy (type, handle, transfer);
if (Runtime.IsGCUserPeer (result.PeerReference.Handle)) {
result.SetJniManagedPeerState (JniManagedPeerStates.Replaceable | JniManagedPeerStates.Activatable);
}
} catch (MissingMethodException e) {
var key_handle = JNIEnv.IdentityHash (handle);
JNIEnv.DeleteRef (handle, transfer);
Expand All @@ -385,19 +382,31 @@ internal static object CreateProxy (
BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
var c = type.GetConstructor (flags, null, XAConstructorSignature, null);
if (c != null) {
return c.Invoke (new object [] { handle, transfer });
var self = GetUninitializedObject (type);
c.Invoke (self, new object [] { handle, transfer });
return self;
}
c = type.GetConstructor (flags, null, JIConstructorSignature, null);
if (c != null) {
var self = GetUninitializedObject (type);
JniObjectReference r = new JniObjectReference (handle);
JniObjectReferenceOptions o = JniObjectReferenceOptions.Copy;
var peer = (IJavaPeerable) c.Invoke (new object [] { r, o });
c.Invoke (self, new object [] { r, o });
JNIEnv.DeleteRef (handle, transfer);
return peer;
return self;
}
throw new MissingMethodException (
"No constructor found for " + type.FullName + "::.ctor(System.IntPtr, Android.Runtime.JniHandleOwnership)",
CreateJavaLocationException ());

static IJavaPeerable GetUninitializedObject (
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)]
Type type)
{
var v = (IJavaPeerable) System.Runtime.CompilerServices.RuntimeHelpers.GetUninitializedObject (type);
v.SetJniManagedPeerState (JniManagedPeerStates.Replaceable | JniManagedPeerStates.Activatable);
return v;
}
}

public static void RegisterType (string java_class, Type t)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,8 @@ public override List<JniSurfacedPeerInfo> GetSurfacedPeers ()

static readonly Type[] XAConstructorSignature = new Type [] { typeof (IntPtr), typeof (JniHandleOwnership) };

protected override IJavaPeerable? TryCreatePeer (
protected override bool TryConstructPeer (
IJavaPeerable self,
ref JniObjectReference reference,
JniObjectReferenceOptions options,
[DynamicallyAccessedMembers (Constructors)]
Expand All @@ -277,10 +278,10 @@ public override List<JniSurfacedPeerInfo> GetSurfacedPeers ()
reference.Handle,
JniHandleOwnership.DoNotTransfer,
};
var p = (IJavaPeerable) c.Invoke (args);
c.Invoke (self, args);
JniObjectReference.Dispose (ref reference, options);
return p;
return true;
}
return base.TryCreatePeer (ref reference, options, type);
return base.TryConstructPeer (self, ref reference, options, type);
}
}
4 changes: 1 addition & 3 deletions src/native/mono/runtime-base/util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,10 @@ Util::path_combine (const char *path1, const char *path2)
void
Util::create_public_directory (const char *dir)
{
mode_t m = umask (0);
int ret = mkdir (dir, 0777);
int ret = create_directory (dir, 0777);
if (ret < 0) {
log_warn (LOG_DEFAULT, "Failed to create directory '{}'. {}", dir, std::strerror (errno));
}
umask (m);
}

int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@
<_DefaultValueAttributeSupport Condition="'$(TrimMode)' == 'full'">true</_DefaultValueAttributeSupport>
</PropertyGroup>

<ItemGroup Condition=" '$(Configuration)' == 'Debug' ">
<!-- trying to track:
JNI ERROR (app bug): accessed deleted Global 0x3056
-->
<AndroidEnvironment Include="env.txt" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\TestRunner.Core\TestRunner.Core.NET.csproj" />
<ProjectReference Include="..\..\TestRunner.NUnit\TestRunner.NUnit.NET.csproj" />
Expand Down
3 changes: 3 additions & 0 deletions tests/Mono.Android-Tests/Mono.Android-Tests/env.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Environment Variables and system properties
# debug.mono.log=gref,default
debug.mono.debug=1
Loading