Skip to content

Commit 05eddd9

Browse files
authored
[generator] Add string cast to prevent CS1503 (#970)
Fixes: #967 Context: dotnet/android-libraries#504 Context: #424 Context: #586 Context: #918 Java generics continues to be a "difficulty" in creating bindings. Consider [`ActivityResultContracts.RequestPermission`][0]: // Java public abstract /* partial */ class ActivityResultContract<I, O> { public abstract Intent createIntent(Context context, I input); } public /* partial */ class /* ActivityResultContracts. */ RequestPermission extends ActivityResultContract<String, Boolean> { @OverRide public Intent createIntent(Context context, String input) {…} } The JNI Signature for `ActivityResultContracts.RequestPermission.createIntent()` is `(Landroid/content/Context;Ljava/lang/String;)Landroid/content/Intent;`, i.e. `class-parse` & `generator` believe that the `input` parameter is of type `String`, which we bind by default as `string`. Thus: // C# public abstract partial class ActivityResultContract { public abstract Intent CreateIntent (Context? context, Java.Lang.Object? input) => … } public partial class /* ActivityResultContracts. */ RequestPermission { public override Intent CreateIntent (Context? context, string? input) => … } This fails to compile with a [CS0115][1]: 'RequestPermission.CreateIntent(Context?, string?)': no suitable method found to override as the `input` parameter of `RequestPermission.CreateIntent()` changes from `Java.Lang.Object?` to `string?`. We can attempt to address this via Metadata: <attr path="/api/package[@name='androidx.activity.result.contract']/class[@name='ActivityResultContracts.RequestPermission']/method[@name='createIntent' and count(parameter)=2 and parameter[1][@type='android.content.Context'] and parameter[2][@type='java.lang.String']]" name="managedType" >Java.Lang.Object</attr> This fixes one error, as `generator` now emits: public partial class /* ActivityResultContracts. */ RequestPermission { [Register ("createIntent", "(Landroid/content/Context;Ljava/lang/String;)Landroid/content/Intent;", "")] public override unsafe global::Android.Content.Intent CreateIntent (global::Android.Content.Context context, global::Java.Lang.Object input) { const string __id = "createIntent.(Landroid/content/Context;Ljava/lang/String;)Landroid/content/Intent;"; IntPtr native_input = JNIEnv.NewString (input); try { JniArgumentValue* __args = stackalloc JniArgumentValue [2]; __args [0] = new JniArgumentValue ((context == null) ? IntPtr.Zero : ((global::Java.Lang.Object) context).Handle); __args [1] = new JniArgumentValue (native_input); var __rm = _members.InstanceMethods.InvokeAbstractObjectMethod (__id, this, __args); return global::Java.Lang.Object.GetObject<global::Android.Content.Intent> (__rm.Handle, JniHandleOwnership.TransferLocalRef); } finally { JNIEnv.DeleteLocalRef (native_input); global::System.GC.KeepAlive (context); global::System.GC.KeepAlive (input); } } } The `override` method declaration is correct. However, this introduces a [CS1503][2] error: IntPtr native_input = JNIEnv.NewString (input); // Error CS1503 Argument 1: cannot convert from 'Java.Lang.Object' to 'string?' The workaround is to remove `createIntent()` ~entirely, and manually bind it, as done in dotnet/android-libraries#512. Fix this issue by always emitting a cast to `(string)` as part of the `JNIEnv.NewString()` invocation, instead emitting: IntPtr native_input = JNIEnv.NewString ((string?) input); This works because `Java.Lang.Object` defines an [explicit conversion to `string?`][3], and if a `Java.Lang.String` instance is provided to the `input` parameter, it's equivalent to calling `.ToString()`. This fix allows the original suggested Metadata solution to work. [0]: https://developer.android.com/reference/androidx/activity/result/contract/ActivityResultContracts.RequestPermission [1]: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/cs0115 [2]: https://docs.microsoft.com/en-us/dotnet/csharp/misc/cs1503 [3]: https://github.com/xamarin/xamarin-android/blob/a58d4e9706455227eabb6e5b5103b25da716688b/src/Mono.Android/Java.Lang/Object.cs#L434-L439
1 parent 37cff25 commit 05eddd9

29 files changed

+39
-39
lines changed

tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/Common/WriteInterfaceMethodInvokers.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public unsafe int GetCountForKey (string key)
2121
{
2222
if (id_GetCountForKey_Ljava_lang_String_ == IntPtr.Zero)
2323
id_GetCountForKey_Ljava_lang_String_ = JNIEnv.GetMethodID (class_ref, "GetCountForKey", "(Ljava/lang/String;)I");
24-
IntPtr native_key = JNIEnv.NewString (key);
24+
IntPtr native_key = JNIEnv.NewString ((string)key);
2525
JValue* __args = stackalloc JValue [1];
2626
__args [0] = new JValue (native_key);
2727
var __ret = JNIEnv.CallIntMethod (((global::Java.Lang.Object) this).Handle, id_GetCountForKey_Ljava_lang_String_, __args);

tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/Common/WriteInterfaceMethodInvokersWithSkips.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public unsafe int GetCountForKey (string key)
2121
{
2222
if (id_GetCountForKey_Ljava_lang_String_ == IntPtr.Zero)
2323
id_GetCountForKey_Ljava_lang_String_ = JNIEnv.GetMethodID (class_ref, "GetCountForKey", "(Ljava/lang/String;)I");
24-
IntPtr native_key = JNIEnv.NewString (key);
24+
IntPtr native_key = JNIEnv.NewString ((string)key);
2525
JValue* __args = stackalloc JValue [1];
2626
__args [0] = new JValue (native_key);
2727
var __ret = JNIEnv.CallIntMethod (((global::Java.Lang.Object) this).Handle, id_GetCountForKey_Ljava_lang_String_, __args);

tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/Common/WriteInterfacePropertyInvokers.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public unsafe string Key {
9191
set {
9292
if (id_set_Key_Ljava_lang_String_ == IntPtr.Zero)
9393
id_set_Key_Ljava_lang_String_ = JNIEnv.GetMethodID (class_ref, "set_Key", "(Ljava/lang/String;)V");
94-
IntPtr native_value = JNIEnv.NewString (value);
94+
IntPtr native_value = JNIEnv.NewString ((string)value);
9595
JValue* __args = stackalloc JValue [1];
9696
__args [0] = new JValue (native_value);
9797
JNIEnv.CallVoidMethod (((global::Java.Lang.Object) this).Handle, id_set_Key_Ljava_lang_String_, __args);

tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/Common/WriteInterfacePropertyInvokersWithSkips.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public unsafe string Key {
4242
set {
4343
if (id_set_Key_Ljava_lang_String_ == IntPtr.Zero)
4444
id_set_Key_Ljava_lang_String_ = JNIEnv.GetMethodID (class_ref, "set_Key", "(Ljava/lang/String;)V");
45-
IntPtr native_value = JNIEnv.NewString (value);
45+
IntPtr native_value = JNIEnv.NewString ((string)value);
4646
JValue* __args = stackalloc JValue [1];
4747
__args [0] = new JValue (native_value);
4848
JNIEnv.CallVoidMethod (((global::Java.Lang.Object) this).Handle, id_set_Key_Ljava_lang_String_, __args);

tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/XAJavaInterop1-NRT/WriteClass.txt

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public partial class MyClass {
3737
if (((global::Java.Lang.Object) this).Handle != IntPtr.Zero)
3838
return;
3939

40-
IntPtr native_p0 = JNIEnv.NewString (p0);
40+
IntPtr native_p0 = JNIEnv.NewString ((string?)p0);
4141
try {
4242
JniArgumentValue* __args = stackalloc JniArgumentValue [1];
4343
__args [0] = new JniArgumentValue (native_p0);
@@ -153,7 +153,7 @@ public partial class MyClass {
153153
[Register ("set_Key", "(Ljava/lang/String;)V", "Getset_Key_Ljava_lang_String_Handler")]
154154
set {
155155
const string __id = "set_Key.(Ljava/lang/String;)V";
156-
IntPtr native_value = JNIEnv.NewString (value);
156+
IntPtr native_value = JNIEnv.NewString ((string?)value);
157157
try {
158158
JniArgumentValue* __args = stackalloc JniArgumentValue [1];
159159
__args [0] = new JniArgumentValue (native_value);
@@ -253,7 +253,7 @@ public partial class MyClass {
253253
public virtual unsafe int GetCountForKey (string? key)
254254
{
255255
const string __id = "GetCountForKey.(Ljava/lang/String;)I";
256-
IntPtr native_key = JNIEnv.NewString (key);
256+
IntPtr native_key = JNIEnv.NewString ((string?)key);
257257
try {
258258
JniArgumentValue* __args = stackalloc JniArgumentValue [1];
259259
__args [0] = new JniArgumentValue (native_key);

tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/XAJavaInterop1-NRT/WriteInterface.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ internal partial class IMyInterfaceInvoker : global::Java.Lang.Object, IMyInterf
223223
set {
224224
if (id_set_Key_Ljava_lang_String_ == IntPtr.Zero)
225225
id_set_Key_Ljava_lang_String_ = JNIEnv.GetMethodID (class_ref, "set_Key", "(Ljava/lang/String;)V");
226-
IntPtr native_value = JNIEnv.NewString (value);
226+
IntPtr native_value = JNIEnv.NewString ((string?)value);
227227
JValue* __args = stackalloc JValue [1];
228228
__args [0] = new JValue (native_value);
229229
JNIEnv.CallVoidMethod (((global::Java.Lang.Object) this).Handle, id_set_Key_Ljava_lang_String_, __args);
@@ -303,7 +303,7 @@ internal partial class IMyInterfaceInvoker : global::Java.Lang.Object, IMyInterf
303303
{
304304
if (id_GetCountForKey_Ljava_lang_String_ == IntPtr.Zero)
305305
id_GetCountForKey_Ljava_lang_String_ = JNIEnv.GetMethodID (class_ref, "GetCountForKey", "(Ljava/lang/String;)I");
306-
IntPtr native_key = JNIEnv.NewString (key);
306+
IntPtr native_key = JNIEnv.NewString ((string?)key);
307307
JValue* __args = stackalloc JValue [1];
308308
__args [0] = new JValue (native_key);
309309
var __ret = JNIEnv.CallIntMethod (((global::Java.Lang.Object) this).Handle, id_GetCountForKey_Ljava_lang_String_, __args);

tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/XAJavaInterop1/WriteClass.txt

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public partial class MyClass {
3737
if (((global::Java.Lang.Object) this).Handle != IntPtr.Zero)
3838
return;
3939

40-
IntPtr native_p0 = JNIEnv.NewString (p0);
40+
IntPtr native_p0 = JNIEnv.NewString ((string)p0);
4141
try {
4242
JniArgumentValue* __args = stackalloc JniArgumentValue [1];
4343
__args [0] = new JniArgumentValue (native_p0);
@@ -153,7 +153,7 @@ public partial class MyClass {
153153
[Register ("set_Key", "(Ljava/lang/String;)V", "Getset_Key_Ljava_lang_String_Handler")]
154154
set {
155155
const string __id = "set_Key.(Ljava/lang/String;)V";
156-
IntPtr native_value = JNIEnv.NewString (value);
156+
IntPtr native_value = JNIEnv.NewString ((string)value);
157157
try {
158158
JniArgumentValue* __args = stackalloc JniArgumentValue [1];
159159
__args [0] = new JniArgumentValue (native_value);
@@ -253,7 +253,7 @@ public partial class MyClass {
253253
public virtual unsafe int GetCountForKey (string key)
254254
{
255255
const string __id = "GetCountForKey.(Ljava/lang/String;)I";
256-
IntPtr native_key = JNIEnv.NewString (key);
256+
IntPtr native_key = JNIEnv.NewString ((string)key);
257257
try {
258258
JniArgumentValue* __args = stackalloc JniArgumentValue [1];
259259
__args [0] = new JniArgumentValue (native_key);

tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/XAJavaInterop1/WriteInterface.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ internal partial class IMyInterfaceInvoker : global::Java.Lang.Object, IMyInterf
223223
set {
224224
if (id_set_Key_Ljava_lang_String_ == IntPtr.Zero)
225225
id_set_Key_Ljava_lang_String_ = JNIEnv.GetMethodID (class_ref, "set_Key", "(Ljava/lang/String;)V");
226-
IntPtr native_value = JNIEnv.NewString (value);
226+
IntPtr native_value = JNIEnv.NewString ((string)value);
227227
JValue* __args = stackalloc JValue [1];
228228
__args [0] = new JValue (native_value);
229229
JNIEnv.CallVoidMethod (((global::Java.Lang.Object) this).Handle, id_set_Key_Ljava_lang_String_, __args);
@@ -303,7 +303,7 @@ internal partial class IMyInterfaceInvoker : global::Java.Lang.Object, IMyInterf
303303
{
304304
if (id_GetCountForKey_Ljava_lang_String_ == IntPtr.Zero)
305305
id_GetCountForKey_Ljava_lang_String_ = JNIEnv.GetMethodID (class_ref, "GetCountForKey", "(Ljava/lang/String;)I");
306-
IntPtr native_key = JNIEnv.NewString (key);
306+
IntPtr native_key = JNIEnv.NewString ((string)key);
307307
JValue* __args = stackalloc JValue [1];
308308
__args [0] = new JValue (native_key);
309309
var __ret = JNIEnv.CallIntMethod (((global::Java.Lang.Object) this).Handle, id_GetCountForKey_Ljava_lang_String_, __args);

tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/XamarinAndroid/WriteClass.txt

+3-3
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public partial class MyClass {
4848
if (((global::Java.Lang.Object) this).Handle != IntPtr.Zero)
4949
return;
5050

51-
IntPtr native_p0 = JNIEnv.NewString (p0);
51+
IntPtr native_p0 = JNIEnv.NewString ((string)p0);
5252
try {
5353
JValue* __args = stackalloc JValue [1];
5454
__args [0] = new JValue (native_p0);
@@ -193,7 +193,7 @@ public partial class MyClass {
193193
set {
194194
if (id_set_Key_Ljava_lang_String_ == IntPtr.Zero)
195195
id_set_Key_Ljava_lang_String_ = JNIEnv.GetMethodID (class_ref, "set_Key", "(Ljava/lang/String;)V");
196-
IntPtr native_value = JNIEnv.NewString (value);
196+
IntPtr native_value = JNIEnv.NewString ((string)value);
197197
try {
198198
JValue* __args = stackalloc JValue [1];
199199
__args [0] = new JValue (native_value);
@@ -299,7 +299,7 @@ public partial class MyClass {
299299
{
300300
if (id_GetCountForKey_Ljava_lang_String_ == IntPtr.Zero)
301301
id_GetCountForKey_Ljava_lang_String_ = JNIEnv.GetMethodID (class_ref, "GetCountForKey", "(Ljava/lang/String;)I");
302-
IntPtr native_key = JNIEnv.NewString (key);
302+
IntPtr native_key = JNIEnv.NewString ((string)key);
303303
try {
304304
JValue* __args = stackalloc JValue [1];
305305
__args [0] = new JValue (native_key);

tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/XamarinAndroid/WriteClassConstructors.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ static IntPtr id_ctor_Ljava_lang_String_;
3737
if (((global::Java.Lang.Object) this).Handle != IntPtr.Zero)
3838
return;
3939

40-
IntPtr native_p0 = JNIEnv.NewString (p0);
40+
IntPtr native_p0 = JNIEnv.NewString ((string)p0);
4141
try {
4242
JValue* __args = stackalloc JValue [1];
4343
__args [0] = new JValue (native_p0);

tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/XamarinAndroid/WriteClassMethods.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public virtual unsafe int GetCountForKey (string key)
2323
{
2424
if (id_GetCountForKey_Ljava_lang_String_ == IntPtr.Zero)
2525
id_GetCountForKey_Ljava_lang_String_ = JNIEnv.GetMethodID (class_ref, "GetCountForKey", "(Ljava/lang/String;)I");
26-
IntPtr native_key = JNIEnv.NewString (key);
26+
IntPtr native_key = JNIEnv.NewString ((string)key);
2727
try {
2828
JValue* __args = stackalloc JValue [1];
2929
__args [0] = new JValue (native_key);

tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/XamarinAndroid/WriteClassProperties.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public virtual unsafe string Key {
120120
set {
121121
if (id_set_Key_Ljava_lang_String_ == IntPtr.Zero)
122122
id_set_Key_Ljava_lang_String_ = JNIEnv.GetMethodID (class_ref, "set_Key", "(Ljava/lang/String;)V");
123-
IntPtr native_value = JNIEnv.NewString (value);
123+
IntPtr native_value = JNIEnv.NewString ((string)value);
124124
try {
125125
JValue* __args = stackalloc JValue [1];
126126
__args [0] = new JValue (native_value);

tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/XamarinAndroid/WriteFieldSetBody.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
if (bar_jfieldId == IntPtr.Zero)
22
bar_jfieldId = JNIEnv.GetFieldID (class_ref, "bar", "Ljava/lang/String;");
3-
IntPtr native_value = JNIEnv.NewString (value);
3+
IntPtr native_value = JNIEnv.NewString ((string)value);
44
try {
55
JNIEnv.SetField (((global::Java.Lang.Object) this).Handle, bar_jfieldId, native_value);
66
} finally {

tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/XamarinAndroid/WriteFieldString.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public string bar {
1212
set {
1313
if (bar_jfieldId == IntPtr.Zero)
1414
bar_jfieldId = JNIEnv.GetFieldID (class_ref, "bar", "Ljava/lang/String;");
15-
IntPtr native_value = JNIEnv.NewString (value);
15+
IntPtr native_value = JNIEnv.NewString ((string)value);
1616
try {
1717
JNIEnv.SetField (((global::Java.Lang.Object) this).Handle, bar_jfieldId, native_value);
1818
} finally {

tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/XamarinAndroid/WriteInterface.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ internal partial class IMyInterfaceInvoker : global::Java.Lang.Object, IMyInterf
205205
set {
206206
if (id_set_Key_Ljava_lang_String_ == IntPtr.Zero)
207207
id_set_Key_Ljava_lang_String_ = JNIEnv.GetMethodID (class_ref, "set_Key", "(Ljava/lang/String;)V");
208-
IntPtr native_value = JNIEnv.NewString (value);
208+
IntPtr native_value = JNIEnv.NewString ((string)value);
209209
JValue* __args = stackalloc JValue [1];
210210
__args [0] = new JValue (native_value);
211211
JNIEnv.CallVoidMethod (((global::Java.Lang.Object) this).Handle, id_set_Key_Ljava_lang_String_, __args);
@@ -285,7 +285,7 @@ internal partial class IMyInterfaceInvoker : global::Java.Lang.Object, IMyInterf
285285
{
286286
if (id_GetCountForKey_Ljava_lang_String_ == IntPtr.Zero)
287287
id_GetCountForKey_Ljava_lang_String_ = JNIEnv.GetMethodID (class_ref, "GetCountForKey", "(Ljava/lang/String;)I");
288-
IntPtr native_key = JNIEnv.NewString (key);
288+
IntPtr native_key = JNIEnv.NewString ((string)key);
289289
JValue* __args = stackalloc JValue [1];
290290
__args [0] = new JValue (native_key);
291291
var __ret = JNIEnv.CallIntMethod (((global::Java.Lang.Object) this).Handle, id_GetCountForKey_Ljava_lang_String_, __args);

tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/XamarinAndroid/WriteInterfaceInvoker.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ internal partial class IMyInterfaceInvoker : global::Java.Lang.Object, IMyInterf
134134
set {
135135
if (id_set_Key_Ljava_lang_String_ == IntPtr.Zero)
136136
id_set_Key_Ljava_lang_String_ = JNIEnv.GetMethodID (class_ref, "set_Key", "(Ljava/lang/String;)V");
137-
IntPtr native_value = JNIEnv.NewString (value);
137+
IntPtr native_value = JNIEnv.NewString ((string)value);
138138
JValue* __args = stackalloc JValue [1];
139139
__args [0] = new JValue (native_value);
140140
JNIEnv.CallVoidMethod (((global::Java.Lang.Object) this).Handle, id_set_Key_Ljava_lang_String_, __args);
@@ -214,7 +214,7 @@ internal partial class IMyInterfaceInvoker : global::Java.Lang.Object, IMyInterf
214214
{
215215
if (id_GetCountForKey_Ljava_lang_String_ == IntPtr.Zero)
216216
id_GetCountForKey_Ljava_lang_String_ = JNIEnv.GetMethodID (class_ref, "GetCountForKey", "(Ljava/lang/String;)I");
217-
IntPtr native_key = JNIEnv.NewString (key);
217+
IntPtr native_key = JNIEnv.NewString ((string)key);
218218
JValue* __args = stackalloc JValue [1];
219219
__args [0] = new JValue (native_key);
220220
var __ret = JNIEnv.CallIntMethod (((global::Java.Lang.Object) this).Handle, id_GetCountForKey_Ljava_lang_String_, __args);

tests/generator-Tests/expected.xaji/CSharpKeywords/Xamarin.Test.CSharpKeywords.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public virtual unsafe string UsePartial (int partial)
7171
public static unsafe string UseThis (string this_)
7272
{
7373
const string __id = "useThis.(Ljava/lang/String;)Ljava/lang/String;";
74-
IntPtr native_this = JNIEnv.NewString (this_);
74+
IntPtr native_this = JNIEnv.NewString ((string)this_);
7575
try {
7676
JniArgumentValue* __args = stackalloc JniArgumentValue [1];
7777
__args [0] = new JniArgumentValue (native_this);

tests/generator-Tests/expected.xaji/GenericArguments/Com.Google.Android.Exoplayer.Drm.FrameworkMediaCrypto.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public unsafe FrameworkMediaCrypto () : base (IntPtr.Zero, JniHandleOwnership.Do
5858
public unsafe bool RequiresSecureDecoderComponent (string p0)
5959
{
6060
const string __id = "requiresSecureDecoderComponent.(Ljava/lang/String;)Z";
61-
IntPtr native_p0 = JNIEnv.NewString (p0);
61+
IntPtr native_p0 = JNIEnv.NewString ((string)p0);
6262
try {
6363
JniArgumentValue* __args = stackalloc JniArgumentValue [1];
6464
__args [0] = new JniArgumentValue (native_p0);

tests/generator-Tests/expected.xaji/GenericArguments/Com.Google.Android.Exoplayer.Drm.IExoMediaCrypto.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public unsafe bool RequiresSecureDecoderComponent (string p0)
9292
{
9393
if (id_requiresSecureDecoderComponent_Ljava_lang_String_ == IntPtr.Zero)
9494
id_requiresSecureDecoderComponent_Ljava_lang_String_ = JNIEnv.GetMethodID (class_ref, "requiresSecureDecoderComponent", "(Ljava/lang/String;)Z");
95-
IntPtr native_p0 = JNIEnv.NewString (p0);
95+
IntPtr native_p0 = JNIEnv.NewString ((string)p0);
9696
JValue* __args = stackalloc JValue [1];
9797
__args [0] = new JValue (native_p0);
9898
var __ret = JNIEnv.CallBooleanMethod (((global::Java.Lang.Object) this).Handle, id_requiresSecureDecoderComponent_Ljava_lang_String_, __args);

tests/generator-Tests/expected.xaji/NormalMethods/Xamarin.Test.SomeObject.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ static void n_VoidMethodWithParams_Ljava_lang_String_ILjava_lang_Object_ (IntPtr
254254
public virtual unsafe void VoidMethodWithParams (string astring, int anint, global::Java.Lang.Object anObject)
255255
{
256256
const string __id = "VoidMethodWithParams.(Ljava/lang/String;ILjava/lang/Object;)V";
257-
IntPtr native_astring = JNIEnv.NewString (astring);
257+
IntPtr native_astring = JNIEnv.NewString ((string)astring);
258258
try {
259259
JniArgumentValue* __args = stackalloc JniArgumentValue [3];
260260
__args [0] = new JniArgumentValue (native_astring);

tests/generator-Tests/expected.xaji/NormalProperties/Xamarin.Test.SomeObject.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ public override unsafe string SomeString {
250250
[Register ("setSomeString", "(Ljava/lang/String;)V", "GetSetSomeString_Ljava_lang_String_Handler")]
251251
set {
252252
const string __id = "setSomeString.(Ljava/lang/String;)V";
253-
IntPtr native_value = JNIEnv.NewString (value);
253+
IntPtr native_value = JNIEnv.NewString ((string)value);
254254
try {
255255
JniArgumentValue* __args = stackalloc JniArgumentValue [1];
256256
__args [0] = new JniArgumentValue (native_value);

tests/generator-Tests/expected.xaji/StaticProperties/Xamarin.Test.SomeObject.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public static unsafe string SomeString {
7575
[Register ("setSomeString", "(Ljava/lang/String;)V", "")]
7676
set {
7777
const string __id = "setSomeString.(Ljava/lang/String;)V";
78-
IntPtr native_value = JNIEnv.NewString (value);
78+
IntPtr native_value = JNIEnv.NewString ((string)value);
7979
try {
8080
JniArgumentValue* __args = stackalloc JniArgumentValue [1];
8181
__args [0] = new JniArgumentValue (native_value);

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public virtual unsafe string Object {
101101
[Register ("SetObject", "(Ljava/lang/String;)V", "GetSetObject_Ljava_lang_String_Handler")]
102102
set {
103103
const string __id = "SetObject.(Ljava/lang/String;)V";
104-
IntPtr native_value = JNIEnv.NewString (value);
104+
IntPtr native_value = JNIEnv.NewString ((string)value);
105105
try {
106106
JniArgumentValue* __args = stackalloc JniArgumentValue [1];
107107
__args [0] = new JniArgumentValue (native_value);

tests/generator-Tests/expected/CSharpKeywords/Xamarin.Test.CSharpKeywords.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public static unsafe string UseThis (string this_)
6767
{
6868
if (id_useThis_Ljava_lang_String_ == IntPtr.Zero)
6969
id_useThis_Ljava_lang_String_ = JNIEnv.GetStaticMethodID (class_ref, "useThis", "(Ljava/lang/String;)Ljava/lang/String;");
70-
IntPtr native_this = JNIEnv.NewString (this_);
70+
IntPtr native_this = JNIEnv.NewString ((string)this_);
7171
try {
7272
JValue* __args = stackalloc JValue [1];
7373
__args [0] = new JValue (native_this);

tests/generator-Tests/expected/NormalMethods/Xamarin.Test.SomeObject.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ public virtual unsafe void VoidMethodWithParams (string astring, int anint, glob
286286
{
287287
if (id_VoidMethodWithParams_Ljava_lang_String_ILjava_lang_Object_ == IntPtr.Zero)
288288
id_VoidMethodWithParams_Ljava_lang_String_ILjava_lang_Object_ = JNIEnv.GetMethodID (class_ref, "VoidMethodWithParams", "(Ljava/lang/String;ILjava/lang/Object;)V");
289-
IntPtr native_astring = JNIEnv.NewString (astring);
289+
IntPtr native_astring = JNIEnv.NewString ((string)astring);
290290
try {
291291
JValue* __args = stackalloc JValue [3];
292292
__args [0] = new JValue (native_astring);

tests/generator-Tests/expected/NormalProperties/Xamarin.Test.SomeObject.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ public override unsafe string SomeString {
227227
set {
228228
if (id_setSomeString_Ljava_lang_String_ == IntPtr.Zero)
229229
id_setSomeString_Ljava_lang_String_ = JNIEnv.GetMethodID (class_ref, "setSomeString", "(Ljava/lang/String;)V");
230-
IntPtr native_value = JNIEnv.NewString (value);
230+
IntPtr native_value = JNIEnv.NewString ((string)value);
231231
try {
232232
JValue* __args = stackalloc JValue [1];
233233
__args [0] = new JValue (native_value);

tests/generator-Tests/expected/StaticProperties/Xamarin.Test.SomeObject.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public static unsafe string SomeString {
7070
set {
7171
if (id_setSomeString_Ljava_lang_String_ == IntPtr.Zero)
7272
id_setSomeString_Ljava_lang_String_ = JNIEnv.GetStaticMethodID (class_ref, "setSomeString", "(Ljava/lang/String;)V");
73-
IntPtr native_value = JNIEnv.NewString (value);
73+
IntPtr native_value = JNIEnv.NewString ((string)value);
7474
try {
7575
JValue* __args = stackalloc JValue [1];
7676
__args [0] = new JValue (native_value);

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public virtual unsafe string Object {
108108
set {
109109
if (id_SetObject_Ljava_lang_String_ == IntPtr.Zero)
110110
id_SetObject_Ljava_lang_String_ = JNIEnv.GetMethodID (class_ref, "SetObject", "(Ljava/lang/String;)V");
111-
IntPtr native_value = JNIEnv.NewString (value);
111+
IntPtr native_value = JNIEnv.NewString ((string)value);
112112
try {
113113
JValue* __args = stackalloc JValue [1];
114114
__args [0] = new JValue (native_value);

tools/generator/Java.Interop.Tools.Generator.ObjectModel/Symbols/StringSymbol.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public string[] PreCall (CodeGenerationOptions opt, string var_name)
123123
};
124124
}
125125
return new[]{
126-
$"IntPtr {native_name} = JNIEnv.NewString ({managed_name});",
126+
$"IntPtr {native_name} = JNIEnv.NewString ((string{opt.NullableOperator}){managed_name});",
127127
};
128128
}
129129

0 commit comments

Comments
 (0)