Skip to content

Commit 99cf719

Browse files
jpobstjonpryor
authored andcommitted
[generator] Call explicit unsigned NewArray() methods (#558)
Fixes: #556 Context: dotnet/android#4119 Call new `JNIEnv.NewArray(...)` overloads that explicitly take unsigned types instead of using the signed counterparts such as `JNIEnv.NewArray((int[])(object)value`. This allows users to get a linker time error if they are using an older Java.Interop.dll that does not support unsigned types, instead of a runtime error.
1 parent 2c77a8d commit 99cf719

File tree

3 files changed

+7
-22
lines changed

3 files changed

+7
-22
lines changed

tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/JavaInterop1/WriteKotlinUnsignedArrayTypeMethodsClass.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public partial class MyClass {
1616
public unsafe uint[] Echo (uint[] value)
1717
{
1818
const string __id = "Echo.([I)[I";
19-
IntPtr native_value = JNIEnv.NewArray ((int[])(object)value);
19+
IntPtr native_value = JNIEnv.NewArray (value);
2020
try {
2121
JniArgumentValue* __args = stackalloc JniArgumentValue [1];
2222
__args [0] = new JniArgumentValue (native_value);
@@ -35,7 +35,7 @@ public partial class MyClass {
3535
public unsafe ushort[] Echo (ushort[] value)
3636
{
3737
const string __id = "Echo.([S)[S";
38-
IntPtr native_value = JNIEnv.NewArray ((short[])(object)value);
38+
IntPtr native_value = JNIEnv.NewArray (value);
3939
try {
4040
JniArgumentValue* __args = stackalloc JniArgumentValue [1];
4141
__args [0] = new JniArgumentValue (native_value);
@@ -54,7 +54,7 @@ public partial class MyClass {
5454
public unsafe ulong[] Echo (ulong[] value)
5555
{
5656
const string __id = "Echo.([J)[J";
57-
IntPtr native_value = JNIEnv.NewArray ((long[])(object)value);
57+
IntPtr native_value = JNIEnv.NewArray (value);
5858
try {
5959
JniArgumentValue* __args = stackalloc JniArgumentValue [1];
6060
__args [0] = new JniArgumentValue (native_value);

tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/JavaInterop1/WriteKotlinUnsignedArrayTypePropertiesClass.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public partial class MyClass {
2626
[Register ("set_UIntProp", "([I)V", "")]
2727
set {
2828
const string __id = "set_UIntProp.([I)V";
29-
IntPtr native_value = JNIEnv.NewArray ((int[])(object)value);
29+
IntPtr native_value = JNIEnv.NewArray (value);
3030
try {
3131
JniArgumentValue* __args = stackalloc JniArgumentValue [1];
3232
__args [0] = new JniArgumentValue (native_value);
@@ -55,7 +55,7 @@ public partial class MyClass {
5555
[Register ("set_UShortProp", "([S)V", "")]
5656
set {
5757
const string __id = "set_UShortProp.([S)V";
58-
IntPtr native_value = JNIEnv.NewArray ((short[])(object)value);
58+
IntPtr native_value = JNIEnv.NewArray (value);
5959
try {
6060
JniArgumentValue* __args = stackalloc JniArgumentValue [1];
6161
__args [0] = new JniArgumentValue (native_value);
@@ -84,7 +84,7 @@ public partial class MyClass {
8484
[Register ("set_ULongProp", "([J)V", "")]
8585
set {
8686
const string __id = "set_ULongProp.([J)V";
87-
IntPtr native_value = JNIEnv.NewArray ((long[])(object)value);
87+
IntPtr native_value = JNIEnv.NewArray (value);
8888
try {
8989
JniArgumentValue* __args = stackalloc JniArgumentValue [1];
9090
__args [0] = new JniArgumentValue (native_value);

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

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -122,25 +122,10 @@ public string[] PreCallback (CodeGenerationOptions opt, string var_name, bool ow
122122

123123
public string[] PreCall (CodeGenerationOptions opt, string var_name)
124124
{
125-
126-
return new string[] { String.Format ("IntPtr {0} = JNIEnv.NewArray ({2}{1});", opt.GetSafeIdentifier (TypeNameUtilities.GetNativeName (var_name)), opt.GetSafeIdentifier (var_name), GetPreCallCast ()) };
125+
return new string[] { String.Format ("IntPtr {0} = JNIEnv.NewArray ({1});", opt.GetSafeIdentifier (TypeNameUtilities.GetNativeName (var_name)), opt.GetSafeIdentifier (var_name)) };
127126
}
128127

129128
public bool NeedsPrep { get { return true; } }
130-
131-
string GetPreCallCast ()
132-
{
133-
switch (sym.FullName) {
134-
case "uint":
135-
return "(int[])(object)";
136-
case "ushort":
137-
return "(short[])(object)";
138-
case "ulong":
139-
return "(long[])(object)";
140-
default:
141-
return string.Empty;
142-
}
143-
}
144129
}
145130
}
146131

0 commit comments

Comments
 (0)