Skip to content

[Mono.Android] Remove all matching JavaList elements - #13508

Merged
simonrozsival merged 2 commits into
mainfrom
simonrozsival-list-bulk-removal
Nov 13, 2026
Merged

simonrozsival merged 2 commits into
mainfrom
simonrozsival-list-bulk-removal

Conversation

@simonrozsival

@simonrozsival simonrozsival commented Nov 11, 2026 •

Copy link
Copy Markdown
Member

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 in c. JavaList.RemoveAll() instead looped over the removal collection and called the single-item Remove(item) once per input:

public virtual bool RemoveAll (JavaList collection)
{
	bool ret = false;
	foreach (Java.Lang.Object? item in collection)
		ret |= Remove (item);
	return ret;
}

Remove(item) removes only the first occurrence, so the number of removals was bounded by the size of collection, not by the number of matches:

var list = new JavaList { "a", "a", "b" };
list.RemoveAll (new JavaList { "a" });
// Expected: [b]
// Actual:   [a, b]

Calling list.RemoveAll (list) also enumerated the list while mutating it.

Fix this by scanning the destination instead: for each index i, if collection.Contains(item) and Remove(item) succeeds, keep i to inspect the element that shifted into its place; otherwise advance. This preserves survivor order, keeps virtual Contains(Java.Lang.Object) / Remove(Java.Lang.Object) dispatch for subclasses, terminates when an override's Remove() returns false, and returns true only when something was removed. A null collection now throws ArgumentNullException, including for an empty destination.

JNI ownership

Each element is obtained as a Java object, not through Get()/InternalGet(), because those convert java.lang.String and boxed primitives into managed values that would not round-trip through Contains()/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.Object wrapper is created and disposed afterward, so RemoveAll() 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) or Remove(Java.Lang.Object):

  • When no peer exists, overrides receive a plain Java.Lang.Object wrapper rather than a typed peer such as Java.Lang.String, and that wrapper is disposed after RemoveAll() finishes with it.
  • Remove(item) still removes the first equal element. This matches index i unless an override refused to remove an earlier equal element.

Each match costs Remove()'s linear IndexOf() scan, as before; calling RemoveAt(i) directly would avoid it but bypass Remove() overrides.

Tests

Add JavaList and JavaList<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) during Contains().

Validation (Debug, arm64 CoreCLR emulator, API 29):

  • make prepare — passed.
  • make leeroy CONFIGURATION=Debug — passed. (make all alone failed in ConfigureLocalWorkload because extra API 37.1 reference assemblies are only built by make 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.
  • From 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.
  • After f0ec593, the focused JavaList device run passed 56/56; the four new reentrant-lookup cases fail against 7af6db7.
  • CI: all 40 checks pass at f0ec593.

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]>
Copilot AI balanced review requested due to automatic review settings November 11, 2026 14:55

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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]>
@simonrozsival

Copy link
Copy Markdown
Member Author

/review

@github-actions

github-actions Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

✅ Android PR Reviewer completed successfully!

Generated by Android PR Reviewer for #13508

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code assessment: no blocking findings. 0 ❌ errors · 0 ⚠️ warnings · 1 💡 inline testing suggestion.

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.

⚠️ CI verification incomplete. All 40 GitHub checks for the current head report success, including the aggregate 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

@simonrozsival simonrozsival added the ready-to-review This PR is ready to review/merge, I think any CI failures are just flaky (ignorable). label Nov 13, 2026
@simonrozsival
simonrozsival merged commit 1a9fc8a into main Nov 13, 2026
40 checks passed
@simonrozsival
simonrozsival deleted the simonrozsival-list-bulk-removal branch November 13, 2026 19:27
jonathanpeppers added a commit that referenced this pull request Nov 16, 2026
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-to-review This PR is ready to review/merge, I think any CI failures are just flaky (ignorable).

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants