Repository navigation
[Mono.Android] Remove all matching JavaList elements - #13508
Conversation
Context: https://developer.android.com/reference/java/util/Collection#removeAll(java.util.Collection<?>) RemoveAll previously removed only one destination occurrence per input item, leaving duplicates behind and failing for self-removal. Scan destination elements and retain virtual Contains/Remove dispatch, including a false result from an overridden Remove. Keep Java peers for strings and boxed values without leaving temporary peers interned or disposing caller-owned peers. Cover duplicate inputs and destination values, nulls, empty/no-match results, self-removal, virtual dispatch, and peer lifetime for both JavaList fixtures. Validated with the rebuilt local SDK and all 52 JavaList device tests on emulator-5570. Co-authored-by: Copilot App <[email protected]>
There was a problem hiding this comment.
🟡 Changes recommended
A critical ownership flaw can cause the list lookup to dispose another caller’s shared peer after a PeekObject() miss, requiring ownership-aware acquisition and a concurrent or reentrant lookup regression test.
1 open issue
What changed in this PR
Updates JavaList.RemoveAll() in the Android runtime bindings to remove every matching destination element, aligning its behavior with Java collection semantics.
Changes:
- Scans the destination to handle duplicates and self-removal while preserving survivor order and virtual dispatch.
- Adds null-collection validation and temporary JNI reference and peer cleanup.
- Adds regression tests for matching behavior, nulls, boxed values, virtual overrides, and existing-peer lifetime.
| File | Description |
|---|---|
| tests/Mono.Android-Tests/Mono.Android-Tests/Java.Interop/JavaListTest.cs | Adds bulk-removal regressions and a virtual-dispatch test helper. |
| src/Mono.Android/Android.Runtime/JavaList.cs | Reworks removal iteration, input validation, and JNI cleanup. |
Review effort: Balanced
Reuse a peeked peer directly and construct an explicitly owned, unregistered Object wrapper on a miss. A lookup miss does not confer ownership of a later GetObject result, which may be the registry winner from a concurrent or reentrant activation. Add deterministic Contains callback lookups, with and without reentrant activation. All four new fixture cases fail against the previous implementation; all 56 JavaList cases pass with the fix on an arm64 CoreCLR emulator. Co-authored-by: Copilot App <[email protected]>
|
/review |
|
✅ Android PR Reviewer completed successfully!
|
There was a problem hiding this comment.
Code assessment: no blocking findings. 0 ❌ errors · 0
Scanning the destination fixes duplicate removal and self-removal while preserving survivor order. The loop advances when virtual Remove declines removal, reports changes only after successful removal, and distinguishes borrowed registered peers from owned temporary wrappers. The regression coverage includes duplicates, nulls, boxed values, empty/no-match collections, virtual dispatch, and peer identity/lifetime for both fixtures.
dotnet-android check for Azure build 1677348. Direct Azure build/timeline verification could not complete: the build query failed during CLI cache initialization and the timeline query required Azure authentication. I therefore cannot independently confirm the Azure build result or inspect its stage/test details, and am not issuing an unconditional CI-green/LGTM verdict. Device tests were not rerun during this review.
Generated by Android PR Reviewer for #13508 · copilot · gpt61 · 28.4 AIC · ⌖ 7.96 AIC · ⊞ 19.5K
Comment /review to run again
Resolve the JavaListTest.cs conflict with #13508 by converting its new RemoveAll tests from NUnit to MSTest: [Test] -> [TestMethod], [TestCase] -> [TestMethod] + [DataRow], [NonParallelizable] -> [DoNotParallelize], and Assert.Throws<T> -> Assert.ThrowsExactly<T>. BulkRemovalJavaList is kept next to the concrete MSTest classes. Co-authored-by: Copilot App <[email protected]>

Context: https://developer.android.com/reference/java/util/Collection#removeAll(java.util.Collection%3C?%3E)
Collection.removeAll(c)removes every element of the destination that is contained inc.JavaList.RemoveAll()instead looped over the removal collection and called the single-itemRemove(item)once per input:Remove(item)removes only the first occurrence, so the number of removals was bounded by the size ofcollection, not by the number of matches:Calling
list.RemoveAll (list)also enumerated the list while mutating it.Fix this by scanning the destination instead: for each index
i, ifcollection.Contains(item)andRemove(item)succeeds, keepito inspect the element that shifted into its place; otherwise advance. This preserves survivor order, keeps virtualContains(Java.Lang.Object)/Remove(Java.Lang.Object)dispatch for subclasses, terminates when an override'sRemove()returnsfalse, and returnstrueonly when something was removed. Anullcollection now throwsArgumentNullException, including for an empty destination.JNI ownership
Each element is obtained as a Java object, not through
Get()/InternalGet(), because those convertjava.lang.Stringand boxed primitives into managed values that would not round-trip throughContains()/Remove()as the same Java instance. If a registered peer already exists (PeekObject()), it is passed through and never disposed. Otherwise an explicitly owned, unregistered (DoNotRegister)Java.Lang.Objectwrapper is created and disposed afterward, soRemoveAll()neither disposes a peer that a concurrent or reentrant lookup registered nor leaves temporary wrappers interned.Compatibility notes
These only affect subclasses overriding
Contains(Java.Lang.Object)orRemove(Java.Lang.Object):Java.Lang.Objectwrapper rather than a typed peer such asJava.Lang.String, and that wrapper is disposed afterRemoveAll()finishes with it.Remove(item)still removes the first equal element. This matches indexiunless an override refused to remove an earlier equal element.Each match costs
Remove()'s linearIndexOf()scan, as before; callingRemoveAt(i)directly would avoid it but bypassRemove()overrides.Tests
Add
JavaListandJavaList<string>regressions for duplicate destination elements and removal inputs, nulls, boxed values, empty/no-match results, self-removal, null collections, virtual dispatch, existing-peer lifetime, and peers acquired by lookups (with and without reentrant activation) duringContains().Validation (Debug, arm64 CoreCLR emulator, API 29):
make prepare— passed.make leeroy CONFIGURATION=Debug— passed. (make allalone failed inConfigureLocalWorkloadbecause extra API 37.1 reference assemblies are only built bymake leeroy.)ANDROID_SERIAL=emulator-5570 ./dotnet-local.sh build -t:Install -c Debug -p:IncludeCategories=JavaList tests/Mono.Android-Tests/Mono.Android-Tests/Mono.Android.NET-Tests.csproj— passed.tests/Mono.Android-Tests/Mono.Android-Tests:ANDROID_SERIAL=emulator-5570 ../../../dotnet-local.sh test Mono.Android.NET-Tests.csproj --no-build -c Debug -p:IncludeCategories=JavaList --device emulator-5570 --report-trx --results-directory ../../../bin/TestDebug/TestResults— 52/52 passed at 7af6db7.