Skip to content

Commit 572b119

Browse files
jonpryoratsushieno
authored andcommitted
[generator] Qualify object.GetType() invocations (#95)
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 572b119

File tree

54 files changed

+537
-83
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+537
-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);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<DefineConstants>$(DefineConstants);ANDROID_1;ANDROID_2;ANDROID_3;ANDROID_4</DefineConstants>
5+
</PropertyGroup>
6+
<!-- Classes -->
7+
<ItemGroup>
8+
<Compile Include="$(MSBuildThisFileDirectory)\Java.Interop.__TypeRegistrations.cs" />
9+
<Compile Include="$(MSBuildThisFileDirectory)\Java.Lang.Object.cs" />
10+
<Compile Include="$(MSBuildThisFileDirectory)\Xamarin.Test.AbsSpinner.cs" />
11+
<Compile Include="$(MSBuildThisFileDirectory)\Xamarin.Test.AdapterView.cs" />
12+
<Compile Include="$(MSBuildThisFileDirectory)\Xamarin.Test.GenericReturnObject.cs" />
13+
<Compile Include="$(MSBuildThisFileDirectory)\Xamarin.Test.IAdapter.cs" />
14+
<Compile Include="$(MSBuildThisFileDirectory)\Xamarin.Test.ISpinnerAdapter.cs" />
15+
<Compile Include="$(MSBuildThisFileDirectory)\__NamespaceMapping__.cs" />
16+
</ItemGroup>
17+
<!-- Enums -->
18+
<ItemGroup />
19+
</Project>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<DefineConstants>$(DefineConstants);ANDROID_1;ANDROID_2;ANDROID_3;ANDROID_4</DefineConstants>
5+
</PropertyGroup>
6+
<!-- Classes -->
7+
<ItemGroup>
8+
<Compile Include="$(MSBuildThisFileDirectory)\Java.Interop.__TypeRegistrations.cs" />
9+
<Compile Include="$(MSBuildThisFileDirectory)\Java.Lang.Object.cs" />
10+
<Compile Include="$(MSBuildThisFileDirectory)\Xamarin.Test.SomeObject.cs" />
11+
<Compile Include="$(MSBuildThisFileDirectory)\__NamespaceMapping__.cs" />
12+
</ItemGroup>
13+
<!-- Enums -->
14+
<ItemGroup />
15+
</Project>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<DefineConstants>$(DefineConstants);ANDROID_1;ANDROID_2;ANDROID_3;ANDROID_4</DefineConstants>
5+
</PropertyGroup>
6+
<!-- Classes -->
7+
<ItemGroup>
8+
<Compile Include="$(MSBuildThisFileDirectory)\Java.Interop.__TypeRegistrations.cs" />
9+
<Compile Include="$(MSBuildThisFileDirectory)\Java.Lang.Object.cs" />
10+
<Compile Include="$(MSBuildThisFileDirectory)\Xamarin.Test.SomeObject.cs" />
11+
<Compile Include="$(MSBuildThisFileDirectory)\__NamespaceMapping__.cs" />
12+
</ItemGroup>
13+
<!-- Enums -->
14+
<ItemGroup />
15+
</Project>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<DefineConstants>$(DefineConstants);ANDROID_1;ANDROID_2;ANDROID_3;ANDROID_4</DefineConstants>
5+
</PropertyGroup>
6+
<!-- Classes -->
7+
<ItemGroup>
8+
<Compile Include="$(MSBuildThisFileDirectory)\Java.Interop.__TypeRegistrations.cs" />
9+
<Compile Include="$(MSBuildThisFileDirectory)\Java.Lang.Object.cs" />
10+
<Compile Include="$(MSBuildThisFileDirectory)\Xamarin.Test.SomeObject.cs" />
11+
<Compile Include="$(MSBuildThisFileDirectory)\__NamespaceMapping__.cs" />
12+
</ItemGroup>
13+
<!-- Enums -->
14+
<ItemGroup />
15+
</Project>

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 {

0 commit comments

Comments
 (0)