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

Duplicate values cannot be added to JavaList via Add(index, item) #9675

Open
AdamEssenmacher opened this issue Jan 12, 2025 · 2 comments
Open
Assignees
Labels
Area: Mono.Android Issues with the Android API binding (Mono.Android.dll).
Milestone

Comments

@AdamEssenmacher
Copy link

AdamEssenmacher commented Jan 12, 2025

Android framework version

net9.0-android

Affected platform version

All Platforms/Versions

Description

When calling either Add(Java.Lang.Object item) or Add(int index, Java.Lang.Object item) on a JavaList, duplicate values cannot be added to the list.

Offending code:

public virtual bool Add (Java.Lang.Object? item)
{
return Add (0, item);
}
public virtual bool Add (int index, Java.Lang.Object? item)
{
if (Contains (item))
return false;
Add ((object?) item);
return true;
}

I believe this is incorrect behavior for two reasons:

  1. JavaList is a wrapper over the native Java ArrayList, and relevant Java documentation indicates that Add() should always return true.
  2. Even if we were to reject the idea that JavaList should match the expected behavior of the underlying ArrayList, JavaList itself is not internally consistent in applying this constraint. Any other way of adding or setting elements to a JavaList does not reject duplicate elements

It looks like it has been like this since at least 2016, and I wouldn't be surprised if there are consumers out there accidentally relying on this behavior. I recommend marking the two offending methods obsolete, and replacing them with correctly behaving methods with close-enough names like AddItem.

Quick Edit: Example of this causing a real problem in the wild (and what brought it to my attention).

Steps to Reproduce

Use either Add(Java.Lang.Object item) or Add(int index, Java.Lang.Object item) to add duplicate values (e.g. "foo") repeatedly to a JavaList.

Did you find any workaround?

Adding or setting elements to JavaList through any other available method (e.g. Insert, Set, Add(JavaList collection) allows duplicate values to be added.

Relevant log output

None

@AdamEssenmacher AdamEssenmacher added Area: Mono.Android Issues with the Android API binding (Mono.Android.dll). needs-triage Issues that need to be assigned. labels Jan 12, 2025
@jpobst
Copy link
Contributor

jpobst commented Jan 13, 2025

For Android team, here's the historical internal commit that added this code:
https://github.com/xamarin/monodroid/commit/8d38265ba00678ff3f3dd6edd8ceb10a97b55abd

There does not seem to be any mention of why it was done this way. It references bugzilla bug #8134, but I don't think we have data from that bug tracker anymore.

@jonpryor
Copy link
Member

It's actually been this way since 2012: xamarin/monodroid@8d38265ba00678ff3f3dd6edd8ceb10a97b55abd

I agree with @AdamEssenmacher, and the Contains() check should be removed from Add(). This will need to be .NET 10+, as I have no idea what it'll do to existing apps, and would rather not chance it on .NET 8 or 9.

@jonpryor jonpryor added this to the .NET 10 milestone Jan 13, 2025
@jpobst jpobst removed the needs-triage Issues that need to be assigned. label Jan 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Mono.Android Issues with the Android API binding (Mono.Android.dll).
Projects
None yet
Development

No branches or pull requests

3 participants