Skip to content

Commit f1b9365

Browse files
authored
[generator] Change generated code to not emit CA1305 warning. (#771)
Fixes: #763 Today we generate this code: ``` throw new InvalidCastException (string.Format ("Unable to convert instance of type '{0}' to type '{1}'.", JNIEnv.GetClassNameFromInstance (handle), "java.code.IMyInterface")); ``` In newer CSC versions (.NET5+), this produces a [CA1305](https://docs.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1305) warning. We can avoid this warning with string interpolation: ``` throw new InvalidCastException ($"Unable to convert instance of type '{JNIEnv.GetClassNameFromInstance (handle)}' to type 'java.code.IMyInterface'."); ```
1 parent 2244407 commit f1b9365

File tree

41 files changed

+46
-46
lines changed

Some content is hidden

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

41 files changed

+46
-46
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ internal partial class AnimatorListenerInvoker : global::Java.Lang.Object, Anima
4747
static IntPtr Validate (IntPtr handle)
4848
{
4949
if (!JNIEnv.IsInstanceOf (handle, java_class_ref))
50-
throw new InvalidCastException (string.Format ("Unable to convert instance of type '{0}' to type '{1}'.", JNIEnv.GetClassNameFromInstance (handle), "java.code.AnimatorListener"));
50+
throw new InvalidCastException ($"Unable to convert instance of type '{JNIEnv.GetClassNameFromInstance (handle)}' to type 'java.code.AnimatorListener'.");
5151
return handle;
5252
}
5353

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ internal partial class IParentInvoker : global::Java.Lang.Object, IParent {
135135
static IntPtr Validate (IntPtr handle)
136136
{
137137
if (!JNIEnv.IsInstanceOf (handle, java_class_ref))
138-
throw new InvalidCastException (string.Format ("Unable to convert instance of type '{0}' to type '{1}'.", JNIEnv.GetClassNameFromInstance (handle), "com.xamarin.android.Parent"));
138+
throw new InvalidCastException ($"Unable to convert instance of type '{JNIEnv.GetClassNameFromInstance (handle)}' to type 'com.xamarin.android.Parent'.");
139139
return handle;
140140
}
141141

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ internal partial class IMyInterfaceInvoker : global::Java.Lang.Object, IMyInterf
7272
static IntPtr Validate (IntPtr handle)
7373
{
7474
if (!JNIEnv.IsInstanceOf (handle, java_class_ref))
75-
throw new InvalidCastException (string.Format ("Unable to convert instance of type '{0}' to type '{1}'.", JNIEnv.GetClassNameFromInstance (handle), "java.code.IMyInterface"));
75+
throw new InvalidCastException ($"Unable to convert instance of type '{JNIEnv.GetClassNameFromInstance (handle)}' to type 'java.code.IMyInterface'.");
7676
return handle;
7777
}
7878

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ internal partial class IMyInterfaceInvoker : global::Java.Lang.Object, IMyInterf
111111
static IntPtr Validate (IntPtr handle)
112112
{
113113
if (!JNIEnv.IsInstanceOf (handle, java_class_ref))
114-
throw new InvalidCastException (string.Format ("Unable to convert instance of type '{0}' to type '{1}'.", JNIEnv.GetClassNameFromInstance (handle), "java.code.IMyInterface"));
114+
throw new InvalidCastException ($"Unable to convert instance of type '{JNIEnv.GetClassNameFromInstance (handle)}' to type 'java.code.IMyInterface'.");
115115
return handle;
116116
}
117117

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ internal partial class IMyInterfaceInvoker : global::Java.Lang.Object, IMyInterf
6868
static IntPtr Validate (IntPtr handle)
6969
{
7070
if (!JNIEnv.IsInstanceOf (handle, java_class_ref))
71-
throw new InvalidCastException (string.Format ("Unable to convert instance of type '{0}' to type '{1}'.", JNIEnv.GetClassNameFromInstance (handle), "java.code.IMyInterface"));
71+
throw new InvalidCastException ($"Unable to convert instance of type '{JNIEnv.GetClassNameFromInstance (handle)}' to type 'java.code.IMyInterface'.");
7272
return handle;
7373
}
7474

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ internal partial class IMyInterfaceInvoker : global::Java.Lang.Object, IMyInterf
9797
static IntPtr Validate (IntPtr handle)
9898
{
9999
if (!JNIEnv.IsInstanceOf (handle, java_class_ref))
100-
throw new InvalidCastException (string.Format ("Unable to convert instance of type '{0}' to type '{1}'.", JNIEnv.GetClassNameFromInstance (handle), "java.code.IMyInterface"));
100+
throw new InvalidCastException ($"Unable to convert instance of type '{JNIEnv.GetClassNameFromInstance (handle)}' to type 'java.code.IMyInterface'.");
101101
return handle;
102102
}
103103

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ internal partial class IMyInterfaceInvoker : global::Java.Lang.Object, IMyInterf
7070
static IntPtr Validate (IntPtr handle)
7171
{
7272
if (!JNIEnv.IsInstanceOf (handle, java_class_ref))
73-
throw new InvalidCastException (string.Format ("Unable to convert instance of type '{0}' to type '{1}'.", JNIEnv.GetClassNameFromInstance (handle), "java.code.IMyInterface"));
73+
throw new InvalidCastException ($"Unable to convert instance of type '{JNIEnv.GetClassNameFromInstance (handle)}' to type 'java.code.IMyInterface'.");
7474
return handle;
7575
}
7676

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ internal partial class IMyInterface2Invoker : global::Java.Lang.Object, IMyInter
4343
static IntPtr Validate (IntPtr handle)
4444
{
4545
if (!JNIEnv.IsInstanceOf (handle, java_class_ref))
46-
throw new InvalidCastException (string.Format ("Unable to convert instance of type '{0}' to type '{1}'.", JNIEnv.GetClassNameFromInstance (handle), "java.code.IMyInterface2"));
46+
throw new InvalidCastException ($"Unable to convert instance of type '{JNIEnv.GetClassNameFromInstance (handle)}' to type 'java.code.IMyInterface2'.");
4747
return handle;
4848
}
4949

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ internal partial class IParentInvoker : global::Java.Lang.Object, IParent {
7878
static IntPtr Validate (IntPtr handle)
7979
{
8080
if (!JNIEnv.IsInstanceOf (handle, java_class_ref))
81-
throw new InvalidCastException (string.Format ("Unable to convert instance of type '{0}' to type '{1}'.", JNIEnv.GetClassNameFromInstance (handle), "com.xamarin.android.Parent"));
81+
throw new InvalidCastException ($"Unable to convert instance of type '{JNIEnv.GetClassNameFromInstance (handle)}' to type 'com.xamarin.android.Parent'.");
8282
return handle;
8383
}
8484

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public partial interface IParent : IJavaObject, IJavaPeerable {
5454
static IntPtr Validate (IntPtr handle)
5555
{
5656
if (!JNIEnv.IsInstanceOf (handle, java_class_ref))
57-
throw new InvalidCastException (string.Format ("Unable to convert instance of type '{0}' to type '{1}'.", JNIEnv.GetClassNameFromInstance (handle), "com.xamarin.android.Parent.Child"));
57+
throw new InvalidCastException ($"Unable to convert instance of type '{JNIEnv.GetClassNameFromInstance (handle)}' to type 'com.xamarin.android.Parent.Child'.");
5858
return handle;
5959
}
6060

@@ -138,7 +138,7 @@ internal partial class IParentInvoker : global::Java.Lang.Object, IParent {
138138
static IntPtr Validate (IntPtr handle)
139139
{
140140
if (!JNIEnv.IsInstanceOf (handle, java_class_ref))
141-
throw new InvalidCastException (string.Format ("Unable to convert instance of type '{0}' to type '{1}'.", JNIEnv.GetClassNameFromInstance (handle), "com.xamarin.android.Parent"));
141+
throw new InvalidCastException ($"Unable to convert instance of type '{JNIEnv.GetClassNameFromInstance (handle)}' to type 'com.xamarin.android.Parent'.");
142142
return handle;
143143
}
144144

0 commit comments

Comments
 (0)