Skip to content

Commit 0a63551

Browse files
committed
[generator] Qualify object.GetType() invocations
Context: https://bugzilla.xamarin.com/show_bug.cgi?id=45203 Context: https://bugzilla.xamarin.com/show_bug.cgi?id=44263#c8 (Sorry; private bugs.) The `getType()` method isn't special; any Java library could contain such a method: // Java public class Example { public int[] getType() {return null;} } (It's a wonder that we haven't hit such a thing before!) Unfortunately, should such a method (1) exist, and (2) be bound as `GetType()` instead of as a `Type` property -- in the above example, we use an `int[]` return type because if a Java method has an array return type, we won't turn it into a property -- the resulting code wouldn't compile: // error CS0019: Operator `==' cannot be applied to operands of type `int[]' and `System.Type' if (GetType () == ThresholdType) ... To fix this, *qualify* all use of the `GetType()` method so that we explicitly use `System.Object.GetType()`: if ((object) this).GetType () == ThresholdType) ... This has no performance-impact, IL-wise, as the C# compiler is able to statically determine that we want non-virtual invocation of the `System.Object.GetType()` method. No runtime cast is performed.
1 parent f915754 commit 0a63551

35 files changed

+233
-83
lines changed

tools/generator/JavaInteropCodeGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ internal override void WriteConstructorBody (Ctor ctor, StreamWriter sw, string
124124
var oldindent = indent;
125125
indent += "\t";
126126
ctor.Parameters.WriteCallArgs (sw, indent, opt, invoker:false);
127-
sw.WriteLine ("{0}var __r = _members.InstanceMethods.StartCreateInstance (__id, GetType (){1});", indent, ctor.Parameters.GetCallArgs (opt, invoker:false));
127+
sw.WriteLine ("{0}var __r = _members.InstanceMethods.StartCreateInstance (__id, ((object) this).GetType (){1});", indent, ctor.Parameters.GetCallArgs (opt, invoker:false));
128128
sw.WriteLine ("{0}SetHandle (__r.Handle, JniHandleOwnership.TransferLocalRef);", indent);
129129
sw.WriteLine ("{0}_members.InstanceMethods.FinishCreateInstance (__id, this{1});", indent, ctor.Parameters.GetCallArgs (opt, invoker:false));
130130
indent = oldindent;

tools/generator/Tests-Core/expected.ji/Android.Text.SpannableString.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public unsafe SpannableString (Java.Lang.ICharSequence source)
4444
try {
4545
JniArgumentValue* __args = stackalloc JniArgumentValue [1];
4646
__args [0] = new JniArgumentValue (native_source);
47-
var __r = _members.InstanceMethods.StartCreateInstance (__id, GetType (), __args);
47+
var __r = _members.InstanceMethods.StartCreateInstance (__id, ((object) this).GetType (), __args);
4848
SetHandle (__r.Handle, JniHandleOwnership.TransferLocalRef);
4949
_members.InstanceMethods.FinishCreateInstance (__id, this, __args);
5050
} finally {
@@ -65,7 +65,7 @@ public unsafe SpannableString (string source)
6565
try {
6666
JniArgumentValue* __args = stackalloc JniArgumentValue [1];
6767
__args [0] = new JniArgumentValue (native_source);
68-
var __r = _members.InstanceMethods.StartCreateInstance (__id, GetType (), __args);
68+
var __r = _members.InstanceMethods.StartCreateInstance (__id, ((object) this).GetType (), __args);
6969
SetHandle (__r.Handle, JniHandleOwnership.TransferLocalRef);
7070
_members.InstanceMethods.FinishCreateInstance (__id, this, __args);
7171
} finally {

tools/generator/Tests-Core/expected/Android.Text.SpannableString.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ public unsafe SpannableString (Java.Lang.ICharSequence source)
3838
try {
3939
JValue* __args = stackalloc JValue [1];
4040
__args [0] = new JValue (native_source);
41-
if (GetType () != typeof (SpannableString)) {
41+
if (((object) this).GetType () != typeof (SpannableString)) {
4242
SetHandle (
43-
global::Android.Runtime.JNIEnv.StartCreateInstance (GetType (), "(Ljava/lang/CharSequence;)V", __args),
43+
global::Android.Runtime.JNIEnv.StartCreateInstance (((object) this).GetType (), "(Ljava/lang/CharSequence;)V", __args),
4444
JniHandleOwnership.TransferLocalRef);
4545
global::Android.Runtime.JNIEnv.FinishCreateInstance (((global::Java.Lang.Object) this).Handle, "(Ljava/lang/CharSequence;)V", __args);
4646
return;
@@ -68,9 +68,9 @@ public unsafe SpannableString (string source)
6868
try {
6969
JValue* __args = stackalloc JValue [1];
7070
__args [0] = new JValue (native_source);
71-
if (GetType () != typeof (SpannableString)) {
71+
if (((object) this).GetType () != typeof (SpannableString)) {
7272
SetHandle (
73-
global::Android.Runtime.JNIEnv.StartCreateInstance (GetType (), "(Ljava/lang/CharSequence;)V", __args),
73+
global::Android.Runtime.JNIEnv.StartCreateInstance (((object) this).GetType (), "(Ljava/lang/CharSequence;)V", __args),
7474
JniHandleOwnership.TransferLocalRef);
7575
global::Android.Runtime.JNIEnv.FinishCreateInstance (((global::Java.Lang.Object) this).Handle, "(Ljava/lang/CharSequence;)V", __args);
7676
return;
@@ -117,7 +117,7 @@ public override unsafe Android.Text.SpanTypes GetSpanFlags (Java.Lang.Object wha
117117
__args [0] = new JValue (what);
118118

119119
Android.Text.SpanTypes __ret;
120-
if (GetType () == ThresholdType)
120+
if (((object) this).GetType () == ThresholdType)
121121
__ret = (Android.Text.SpanTypes) JNIEnv.CallIntMethod (((global::Java.Lang.Object) this).Handle, id_getSpanFlags_Ljava_lang_Object_, __args);
122122
else
123123
__ret = (Android.Text.SpanTypes) JNIEnv.CallNonvirtualIntMethod (((global::Java.Lang.Object) this).Handle, ThresholdClass, JNIEnv.GetMethodID (ThresholdClass, "getSpanFlags", "(Ljava/lang/Object;)I"), __args);

tools/generator/Tests-Core/expected/Android.Text.SpannableStringInternal.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public virtual unsafe Android.Text.SpanTypes GetSpanFlags (Java.Lang.Object p0)
5656
__args [0] = new JValue (p0);
5757

5858
Android.Text.SpanTypes __ret;
59-
if (GetType () == ThresholdType)
59+
if (((object) this).GetType () == ThresholdType)
6060
__ret = (Android.Text.SpanTypes) JNIEnv.CallIntMethod (((global::Java.Lang.Object) this).Handle, id_getSpanFlags_Ljava_lang_Object_, __args);
6161
else
6262
__ret = (Android.Text.SpanTypes) JNIEnv.CallNonvirtualIntMethod (((global::Java.Lang.Object) this).Handle, ThresholdClass, JNIEnv.GetMethodID (ThresholdClass, "getSpanFlags", "(Ljava/lang/Object;)I"), __args);

tools/generator/Tests-Core/expected/Android.Views.View.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public virtual unsafe void SetOnClickListener (Android.Views.View.IOnClickListen
164164
JValue* __args = stackalloc JValue [1];
165165
__args [0] = new JValue (l);
166166

167-
if (GetType () == ThresholdType)
167+
if (((object) this).GetType () == ThresholdType)
168168
JNIEnv.CallVoidMethod (((global::Java.Lang.Object) this).Handle, id_setOnClickListener_Landroid_view_View_OnClickListener_, __args);
169169
else
170170
JNIEnv.CallNonvirtualVoidMethod (((global::Java.Lang.Object) this).Handle, ThresholdClass, JNIEnv.GetMethodID (ThresholdClass, "setOnClickListener", "(Landroid/view/View$OnClickListener;)V"), __args);
@@ -201,7 +201,7 @@ public virtual unsafe void AddTouchables (System.Collections.Generic.IList<Andro
201201
JValue* __args = stackalloc JValue [1];
202202
__args [0] = new JValue (native_views);
203203

204-
if (GetType () == ThresholdType)
204+
if (((object) this).GetType () == ThresholdType)
205205
JNIEnv.CallVoidMethod (((global::Java.Lang.Object) this).Handle, id_addTouchables_Ljava_util_ArrayList_, __args);
206206
else
207207
JNIEnv.CallNonvirtualVoidMethod (((global::Java.Lang.Object) this).Handle, ThresholdClass, JNIEnv.GetMethodID (ThresholdClass, "addTouchables", "(Ljava/util/ArrayList;)V"), __args);

tools/generator/Tests/expected.ji/Constructors/Xamarin.Test.SomeObject.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public unsafe SomeObject ()
4141
return;
4242

4343
try {
44-
var __r = _members.InstanceMethods.StartCreateInstance (__id, GetType (), null);
44+
var __r = _members.InstanceMethods.StartCreateInstance (__id, ((object) this).GetType (), null);
4545
SetHandle (__r.Handle, JniHandleOwnership.TransferLocalRef);
4646
_members.InstanceMethods.FinishCreateInstance (__id, this, null);
4747
} finally {
@@ -61,7 +61,7 @@ public unsafe SomeObject (int aint)
6161
try {
6262
JniArgumentValue* __args = stackalloc JniArgumentValue [1];
6363
__args [0] = new JniArgumentValue (aint);
64-
var __r = _members.InstanceMethods.StartCreateInstance (__id, GetType (), __args);
64+
var __r = _members.InstanceMethods.StartCreateInstance (__id, ((object) this).GetType (), __args);
6565
SetHandle (__r.Handle, JniHandleOwnership.TransferLocalRef);
6666
_members.InstanceMethods.FinishCreateInstance (__id, this, __args);
6767
} finally {

tools/generator/Tests/expected.ji/NestedTypes/Xamarin.Test.NotificationCompatBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public unsafe InstanceInner (global::Xamarin.Test.NotificationCompatBase __self)
163163
try {
164164
JniArgumentValue* __args = stackalloc JniArgumentValue [1];
165165
__args [0] = new JniArgumentValue ((__self == null) ? IntPtr.Zero : ((global::Java.Lang.Object) __self).Handle);
166-
var __r = _members.InstanceMethods.StartCreateInstance (__id, GetType (), __args);
166+
var __r = _members.InstanceMethods.StartCreateInstance (__id, ((object) this).GetType (), __args);
167167
SetHandle (__r.Handle, JniHandleOwnership.TransferLocalRef);
168168
_members.InstanceMethods.FinishCreateInstance (__id, this, __args);
169169
} finally {

tools/generator/Tests/expected.ji/NormalMethods/Xamarin.Test.SomeObject.cs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,41 @@ public unsafe SomeObject (global::Java.Lang.Class c)
4343
try {
4444
JniArgumentValue* __args = stackalloc JniArgumentValue [1];
4545
__args [0] = new JniArgumentValue ((c == null) ? IntPtr.Zero : ((global::Java.Lang.Object) c).Handle);
46-
var __r = _members.InstanceMethods.StartCreateInstance (__id, GetType (), __args);
46+
var __r = _members.InstanceMethods.StartCreateInstance (__id, ((object) this).GetType (), __args);
4747
SetHandle (__r.Handle, JniHandleOwnership.TransferLocalRef);
4848
_members.InstanceMethods.FinishCreateInstance (__id, this, __args);
4949
} finally {
5050
}
5151
}
5252

53+
static Delegate cb_getType;
54+
#pragma warning disable 0169
55+
static Delegate GetGetTypeHandler ()
56+
{
57+
if (cb_getType == null)
58+
cb_getType = JNINativeWrapper.CreateDelegate ((Func<IntPtr, IntPtr, IntPtr>) n_GetType);
59+
return cb_getType;
60+
}
61+
62+
static IntPtr n_GetType (IntPtr jnienv, IntPtr native__this)
63+
{
64+
global::Xamarin.Test.SomeObject __this = global::Java.Lang.Object.GetObject<global::Xamarin.Test.SomeObject> (jnienv, native__this, JniHandleOwnership.DoNotTransfer);
65+
return JNIEnv.NewArray (__this.GetType ());
66+
}
67+
#pragma warning restore 0169
68+
69+
// Metadata.xml XPath method reference: path="/api/package[@name='xamarin.test']/class[@name='SomeObject']/method[@name='getType' and count(parameter)=0]"
70+
[Register ("getType", "()[I", "GetGetTypeHandler")]
71+
public virtual unsafe int[] GetType ()
72+
{
73+
const string __id = "getType.()[I";
74+
try {
75+
var __rm = _members.InstanceMethods.InvokeVirtualObjectMethod (__id, this, null);
76+
return (int[]) JNIEnv.GetArray (__rm.Handle, JniHandleOwnership.TransferLocalRef, typeof (int));
77+
} finally {
78+
}
79+
}
80+
5381
static Delegate cb_handle_Ljava_lang_Object_Ljava_lang_Throwable_;
5482
#pragma warning disable 0169
5583
static Delegate GetHandle_Ljava_lang_Object_Ljava_lang_Throwable_Handler ()

tools/generator/Tests/expected.ji/Streams/Java.IO.FilterOutputStream.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public unsafe FilterOutputStream (global::System.IO.Stream @out)
4444
try {
4545
JniArgumentValue* __args = stackalloc JniArgumentValue [1];
4646
__args [0] = new JniArgumentValue (native__out);
47-
var __r = _members.InstanceMethods.StartCreateInstance (__id, GetType (), __args);
47+
var __r = _members.InstanceMethods.StartCreateInstance (__id, ((object) this).GetType (), __args);
4848
SetHandle (__r.Handle, JniHandleOwnership.TransferLocalRef);
4949
_members.InstanceMethods.FinishCreateInstance (__id, this, __args);
5050
} finally {

tools/generator/Tests/expected.ji/Streams/Java.IO.InputStream.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public unsafe InputStream ()
4141
return;
4242

4343
try {
44-
var __r = _members.InstanceMethods.StartCreateInstance (__id, GetType (), null);
44+
var __r = _members.InstanceMethods.StartCreateInstance (__id, ((object) this).GetType (), null);
4545
SetHandle (__r.Handle, JniHandleOwnership.TransferLocalRef);
4646
_members.InstanceMethods.FinishCreateInstance (__id, this, null);
4747
} finally {

0 commit comments

Comments
 (0)