Skip to content

Commit 7f77a70

Browse files
committed
Fix property setter generation
Context: dotnet/android#8339 While testing on dotnet/android#8339, we hit this error (among others, to be addressed later): src/Mono.Android/obj/Debug/net8.0/android-34/mcw/Android.Views.IWindowInsetsController.cs(304,41): error CS0103: The name 'behavior' does not exist in the current context This was caused because of code such as: public partial interface IWindowInsetsController { public unsafe int SystemBarsBehavior { get { const string __id = "getSystemBarsBehavior.()I"; try { var __rm = _members_IWindowInsetsController.InstanceMethods.InvokeAbstractInt32Method (__id, this, null); return __rm; } finally { } } set { const string __id = "setSystemBarsBehavior.(I)V"; try { JniArgumentValue* __args = stackalloc JniArgumentValue [1]; __args [0] = new JniArgumentValue (behavior); _members_IWindowInsetsController.InstanceMethods.InvokeAbstractVoidMethod (__id, this, __args); } finally { } } } } This happened because when emitting the property setter, we need to update the `set*` method's parameter name to be `value` so that the normal property setter body is emitted properly. Update `InterfaceInvokerProperty.cs` so that the parameter name is set to `value`. Update `tests/generator-Tests/Integration-Tests/Interfaces.cs` so that we test interface generation for JavaInterop1. Update `tests/generator-Tests/SupportFiles/*.cs` so that `Interfaces.cs` test output can be compiled. Update `tests/generator-Tests/expected.ji/TestInterface/TestInterface.xml` so that some parameter names for `set*()` methods are *not* already named `value`, to induce the original error. Flush `tests/generator-Tests/expected.ji/TestInterface`.
1 parent 4a5bbae commit 7f77a70

19 files changed

+280
-78
lines changed

tests/generator-Tests/Integration-Tests/Interfaces.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace generatortests
66
[TestFixture]
77
public class Interfaces : BaseGeneratorTest
88
{
9-
protected override bool TryJavaInterop1 => false;
9+
protected override bool TryJavaInterop1 => true;
1010

1111
[Test]
1212
public void Generated_OK ()
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
#if !JAVA_INTEROP1
2-
1+

32
using System;
4-
using Android.Runtime;
3+
using Java.Interop;
54

65
namespace Java.Lang {
76

8-
public partial interface ICharSequence : IJavaObject
7+
public partial interface ICharSequence : IJavaPeerable
8+
#if !JAVA_INTEROP1
9+
, Android.Runtime.IJavaObject
10+
#endif // !JAVA_INTEROP1
911
{
1012
char CharAt (int index);
1113
int Length ();
1214
Java.Lang.ICharSequence SubSequenceFormatted (int start, int end);
1315
string ToString ();
1416
}
1517
}
16-
17-
#endif // !JAVA_INTEROP1

tests/generator-Tests/SupportFiles/Java_Lang_String.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
#if !JAVA_INTEROP1
2-
3-
using System;
1+
using System;
42
using System.Collections;
53
using System.Collections.Generic;
64

75
namespace Java.Lang {
86

9-
public sealed partial class String : global::Java.Lang.Object, Java.Lang.ICharSequence
7+
public sealed partial class String : global::Java.Lang.Object, Java.Lang.ICharSequence, IEnumerable
108
{
11-
public String (string value)
9+
public unsafe String (string value)
10+
#if JAVA_INTEROP1
11+
: base (ref *InvalidJniObjectReference, Java.Interop.JniObjectReferenceOptions.None)
12+
#endif // JAVA_INTEROP1
1213
{
1314
}
1415

@@ -43,5 +44,3 @@ IEnumerator IEnumerable.GetEnumerator ()
4344
}
4445
}
4546
}
46-
47-
#endif // !JAVA_INTEROP1

tests/generator-Tests/expected.ji/TestInterface/Java.Lang.Object.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by a tool.
4+
//
5+
// Changes to this file may cause incorrect behavior and will be lost if
6+
// the code is regenerated.
7+
// </auto-generated>
8+
//------------------------------------------------------------------------------
9+
10+
#nullable restore
111
using System;
212
using System.Collections.Generic;
313
using Java.Interop;

tests/generator-Tests/expected.ji/TestInterface/Java.Lang.String.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by a tool.
4+
//
5+
// Changes to this file may cause incorrect behavior and will be lost if
6+
// the code is regenerated.
7+
// </auto-generated>
8+
//------------------------------------------------------------------------------
9+
10+
#nullable restore
111
using System;
212
using System.Collections.Generic;
313
using Java.Interop;

tests/generator-Tests/expected.ji/TestInterface/Test.ME.GenericImplementation.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by a tool.
4+
//
5+
// Changes to this file may cause incorrect behavior and will be lost if
6+
// the code is regenerated.
7+
// </auto-generated>
8+
//------------------------------------------------------------------------------
9+
10+
#nullable restore
111
using System;
212
using System.Collections.Generic;
313
using Java.Interop;
@@ -36,22 +46,27 @@ public unsafe GenericImplementation () : base (ref *InvalidJniObjectReference, J
3646
}
3747

3848
// Metadata.xml XPath method reference: path="/api/package[@name='test.me']/class[@name='GenericImplementation']/method[@name='SetObject' and count(parameter)=1 and parameter[1][@type='byte[]']]"
49+
[global::Java.Interop.JniMethodSignature ("SetObject", "([B)V")]
3950
public virtual unsafe void SetObject (global::Java.Interop.JavaSByteArray value)
4051
{
4152
const string __id = "SetObject.([B)V";
53+
var native_value = global::Java.Interop.JniEnvironment.Arrays.CreateMarshalSByteArray (value);
4254
try {
4355
JniArgumentValue* __args = stackalloc JniArgumentValue [1];
44-
__args [0] = new JniArgumentValue (value);
56+
__args [0] = new JniArgumentValue (native_value);
4557
_members.InstanceMethods.InvokeVirtualVoidMethod (__id, this, __args);
4658
} finally {
59+
if (native_value != null) {
60+
native_value.DisposeUnlessReferenced ();
61+
}
4762
global::System.GC.KeepAlive (value);
4863
}
4964
}
5065

5166
// This method is explicitly implemented as a member of an instantiated Test.ME.IGenericInterface
5267
void global::Test.ME.IGenericInterface.SetObject (global::Java.Lang.Object value)
5368
{
54-
SetObject (global::Java.Interop.JavaObjectExtensions.JavaCast<global::Java.Interop.JavaSByteArray>(value));
69+
SetObject (global::Java.Interop.JniEnvironment.Runtime.ValueManager.GetValue<global::Java.Interop.JavaSByteArray>((value?.PeerReference ?? default).Handle));
5570
}
5671

5772
}

tests/generator-Tests/expected.ji/TestInterface/Test.ME.GenericObjectPropertyImplementation.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by a tool.
4+
//
5+
// Changes to this file may cause incorrect behavior and will be lost if
6+
// the code is regenerated.
7+
// </auto-generated>
8+
//------------------------------------------------------------------------------
9+
10+
#nullable restore
111
using System;
212
using System.Collections.Generic;
313
using Java.Interop;

tests/generator-Tests/expected.ji/TestInterface/Test.ME.GenericStringImplementation.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by a tool.
4+
//
5+
// Changes to this file may cause incorrect behavior and will be lost if
6+
// the code is regenerated.
7+
// </auto-generated>
8+
//------------------------------------------------------------------------------
9+
10+
#nullable restore
111
using System;
212
using System.Collections.Generic;
313
using Java.Interop;
@@ -36,22 +46,27 @@ public unsafe GenericStringImplementation () : base (ref *InvalidJniObjectRefere
3646
}
3747

3848
// Metadata.xml XPath method reference: path="/api/package[@name='test.me']/class[@name='GenericStringImplementation']/method[@name='SetObject' and count(parameter)=1 and parameter[1][@type='java.lang.String[]']]"
49+
[global::Java.Interop.JniMethodSignature ("SetObject", "([Ljava/lang/String;)V")]
3950
public virtual unsafe void SetObject (global::Java.Interop.JavaObjectArray<string> value)
4051
{
4152
const string __id = "SetObject.([Ljava/lang/String;)V";
53+
var native_value = global::Java.Interop.JniEnvironment.Arrays.CreateMarshalObjectArray<string> (value);
4254
try {
4355
JniArgumentValue* __args = stackalloc JniArgumentValue [1];
44-
__args [0] = new JniArgumentValue (value);
56+
__args [0] = new JniArgumentValue (native_value);
4557
_members.InstanceMethods.InvokeVirtualVoidMethod (__id, this, __args);
4658
} finally {
59+
if (native_value != null) {
60+
native_value.DisposeUnlessReferenced ();
61+
}
4762
global::System.GC.KeepAlive (value);
4863
}
4964
}
5065

5166
// This method is explicitly implemented as a member of an instantiated Test.ME.IGenericInterface
5267
void global::Test.ME.IGenericInterface.SetObject (global::Java.Lang.Object value)
5368
{
54-
SetObject (global::Java.Interop.JavaObjectExtensions.JavaCast<global::Java.Interop.JavaObjectArray<string>>(value));
69+
SetObject (global::Java.Interop.JniEnvironment.Runtime.ValueManager.GetValue<global::Java.Interop.JavaObjectArray<string>>((value?.PeerReference ?? default).Handle));
5570
}
5671

5772
}

tests/generator-Tests/expected.ji/TestInterface/Test.ME.GenericStringPropertyImplementation.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by a tool.
4+
//
5+
// Changes to this file may cause incorrect behavior and will be lost if
6+
// the code is regenerated.
7+
// </auto-generated>
8+
//------------------------------------------------------------------------------
9+
10+
#nullable restore
111
using System;
212
using System.Collections.Generic;
313
using Java.Interop;
@@ -62,11 +72,9 @@ public virtual unsafe string Object {
6272
// This method is explicitly implemented as a member of an instantiated Test.ME.IGenericPropertyInterface
6373
global::Java.Lang.Object global::Test.ME.IGenericPropertyInterface.Object {
6474
// Metadata.xml XPath method reference: path="/api/package[@name='test.me']/interface[@name='GenericPropertyInterface']/method[@name='getObject' and count(parameter)=0]"
65-
[Register ("getObject", "()Ljava/lang/Object;", "GetGetObjectHandler:Test.ME.IGenericPropertyInterfaceInvoker, Mono.Android, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null")]
66-
get { return Object; }
75+
get { return new Java.Lang.String (Object); }
6776
// Metadata.xml XPath method reference: path="/api/package[@name='test.me']/interface[@name='GenericPropertyInterface']/method[@name='setObject' and count(parameter)=1 and parameter[1][@type='T']]"
68-
[Register ("setObject", "(Ljava/lang/Object;)V", "GetSetObject_Ljava_lang_Object_Handler:Test.ME.IGenericPropertyInterfaceInvoker, Mono.Android, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null")]
69-
set { Object = value.ToString (); }
77+
set { Object = value?.ToString (); }
7078
}
7179

7280
}

tests/generator-Tests/expected.ji/TestInterface/Test.ME.IGenericInterface.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,37 @@ namespace Test.ME {
99
[global::Java.Interop.JavaTypeParameters (new string [] {"T"})]
1010
public partial interface IGenericInterface : IJavaPeerable {
1111
// Metadata.xml XPath method reference: path="/api/package[@name='test.me']/interface[@name='GenericInterface']/method[@name='SetObject' and count(parameter)=1 and parameter[1][@type='T']]"
12+
[global::Java.Interop.JniMethodSignature ("SetObject", "(Ljava/lang/Object;)V")]
1213
void SetObject (global::Java.Lang.Object value);
1314

1415
}
16+
17+
[global::Java.Interop.JniTypeSignature ("test/me/GenericInterface", GenerateJavaPeer=false)]
18+
internal partial class IGenericInterfaceInvoker : global::Java.Lang.Object, IGenericInterface {
19+
[global::System.Diagnostics.DebuggerBrowsable (global::System.Diagnostics.DebuggerBrowsableState.Never)]
20+
[global::System.ComponentModel.EditorBrowsable (global::System.ComponentModel.EditorBrowsableState.Never)]
21+
public override global::Java.Interop.JniPeerMembers JniPeerMembers {
22+
get { return _members_IGenericInterface; }
23+
}
24+
25+
static readonly JniPeerMembers _members_IGenericInterface = new JniPeerMembers ("test/me/GenericInterface", typeof (IGenericInterfaceInvoker));
26+
27+
public IGenericInterfaceInvoker (ref JniObjectReference reference, JniObjectReferenceOptions options) : base (ref reference, options)
28+
{
29+
}
30+
31+
public unsafe void SetObject (global::Java.Lang.Object value)
32+
{
33+
const string __id = "SetObject.(Ljava/lang/Object;)V";
34+
var native_value = (value?.PeerReference ?? default);
35+
try {
36+
JniArgumentValue* __args = stackalloc JniArgumentValue [1];
37+
__args [0] = new JniArgumentValue (native_value);
38+
_members_IGenericInterface.InstanceMethods.InvokeAbstractVoidMethod (__id, this, __args);
39+
} finally {
40+
global::System.GC.KeepAlive (value);
41+
}
42+
}
43+
44+
}
1545
}

0 commit comments

Comments
 (0)