From 166a6c95260330114174a91e8cf0b68cc0e14479 Mon Sep 17 00:00:00 2001 From: Jonathan Pryor Date: Tue, 19 Nov 2019 02:57:32 +0100 Subject: [PATCH] [Java.Interop-Tests] Remove Reflection from JavaPrimitiveArrayContract (#521) Context: https://github.com/xamarin/xamarin-android/pull/3393 Context: https://devdiv.visualstudio.com/DevDiv/_build/results?buildId=3247796&view=ms.vss-test-web.build-test-results-tab&runId=9790200&resultId=100160&paneView=debug In attempting to get Xamarin.Android to run the full compliement of Java.Interop unit tests, there is one set of tests which are still failing: the `JavaPrimitiveArrayContract.Constructor_Exceptions()` tests fail because the *linker* is executed, and the linker is removing the constructors which `Constructor_Exceptions()` relies on. Make `JavaPrimitiveArrayContract` linker-safe, and instead of using `System.Type.GetConstructor()` to get the constructor, introduce new `abstract` methods on `JavaPrimitiveArrayContract.CreateCollection()` which create instances of the appropriate type, using the appropriate constructor. This is more "linker friendly", as the linker can "see" that the constructors are used, which in turn should allow the tests to pass. --- .../JavaBooleanArrayContractTests.cs | 15 ++++++++++++ .../JavaCharArrayContractTests.cs | 14 +++++++++++ .../JavaDoubleArrayContractTests.cs | 14 +++++++++++ .../JavaInt16ArrayContractTests.cs | 14 +++++++++++ .../JavaInt32ArrayContractTests.cs | 14 +++++++++++ .../JavaInt64ArrayContractTests.cs | 14 +++++++++++ .../JavaPrimitiveArrayContract.cs | 23 +++++-------------- .../JavaSByteArrayContractTests.cs | 14 +++++++++++ .../JavaSingleArrayContractTests.cs | 14 +++++++++++ .../Tests/Java.Interop/JniEnvironmentTests.cs | 1 - .../Tests/Java.Interop/JniTransitionTest.cs | 1 - 11 files changed, 119 insertions(+), 19 deletions(-) diff --git a/src/Java.Interop/Tests/Java.Interop/JavaBooleanArrayContractTests.cs b/src/Java.Interop/Tests/Java.Interop/JavaBooleanArrayContractTests.cs index 73241f76e..3f13eaa42 100644 --- a/src/Java.Interop/Tests/Java.Interop/JavaBooleanArrayContractTests.cs +++ b/src/Java.Interop/Tests/Java.Interop/JavaBooleanArrayContractTests.cs @@ -11,6 +11,21 @@ namespace Java.InteropTests [TestFixture] public class JavaBooleanArrayContractTests : JavaPrimitiveArrayContract { + protected override ICollection CreateCollection (IEnumerable values) + { + return new JavaBooleanArray (values); + } + + protected override ICollection CreateCollection (IList values) + { + return new JavaBooleanArray (values); + } + + protected override ICollection CreateCollection (int length) + { + return new JavaBooleanArray (length); + } + protected override bool CreateValueA () { return true; diff --git a/src/Java.Interop/Tests/Java.Interop/JavaCharArrayContractTests.cs b/src/Java.Interop/Tests/Java.Interop/JavaCharArrayContractTests.cs index 343d93288..350df67fa 100644 --- a/src/Java.Interop/Tests/Java.Interop/JavaCharArrayContractTests.cs +++ b/src/Java.Interop/Tests/Java.Interop/JavaCharArrayContractTests.cs @@ -11,6 +11,20 @@ namespace Java.InteropTests [TestFixture] public class JavaCharArrayContractTests : JavaPrimitiveArrayContract { + protected override ICollection CreateCollection (IEnumerable values) + { + return new JavaCharArray (values); + } + + protected override ICollection CreateCollection (IList values) + { + return new JavaCharArray (values); + } + + protected override ICollection CreateCollection (int length) + { + return new JavaCharArray (length); + } } } diff --git a/src/Java.Interop/Tests/Java.Interop/JavaDoubleArrayContractTests.cs b/src/Java.Interop/Tests/Java.Interop/JavaDoubleArrayContractTests.cs index ef263b525..6f00e3ccc 100644 --- a/src/Java.Interop/Tests/Java.Interop/JavaDoubleArrayContractTests.cs +++ b/src/Java.Interop/Tests/Java.Interop/JavaDoubleArrayContractTests.cs @@ -11,6 +11,20 @@ namespace Java.InteropTests [TestFixture] public class JavaDoubleArrayContractTests : JavaPrimitiveArrayContract { + protected override ICollection CreateCollection (IEnumerable values) + { + return new JavaDoubleArray (values); + } + + protected override ICollection CreateCollection (IList values) + { + return new JavaDoubleArray (values); + } + + protected override ICollection CreateCollection (int length) + { + return new JavaDoubleArray (length); + } } } diff --git a/src/Java.Interop/Tests/Java.Interop/JavaInt16ArrayContractTests.cs b/src/Java.Interop/Tests/Java.Interop/JavaInt16ArrayContractTests.cs index 1462c449f..1705edd84 100644 --- a/src/Java.Interop/Tests/Java.Interop/JavaInt16ArrayContractTests.cs +++ b/src/Java.Interop/Tests/Java.Interop/JavaInt16ArrayContractTests.cs @@ -11,6 +11,20 @@ namespace Java.InteropTests [TestFixture] public class JavaInt16ArrayContractTests : JavaPrimitiveArrayContract { + protected override ICollection CreateCollection (IEnumerable values) + { + return new JavaInt16Array (values); + } + + protected override ICollection CreateCollection (IList values) + { + return new JavaInt16Array (values); + } + + protected override ICollection CreateCollection (int length) + { + return new JavaInt16Array (length); + } } } diff --git a/src/Java.Interop/Tests/Java.Interop/JavaInt32ArrayContractTests.cs b/src/Java.Interop/Tests/Java.Interop/JavaInt32ArrayContractTests.cs index 1dd9f8e6a..ddc2bc911 100644 --- a/src/Java.Interop/Tests/Java.Interop/JavaInt32ArrayContractTests.cs +++ b/src/Java.Interop/Tests/Java.Interop/JavaInt32ArrayContractTests.cs @@ -11,6 +11,20 @@ namespace Java.InteropTests [TestFixture] public class JavaInt32ArrayContractTests : JavaPrimitiveArrayContract { + protected override ICollection CreateCollection (IEnumerable values) + { + return new JavaInt32Array (values); + } + + protected override ICollection CreateCollection (IList values) + { + return new JavaInt32Array (values); + } + + protected override ICollection CreateCollection (int length) + { + return new JavaInt32Array (length); + } } } diff --git a/src/Java.Interop/Tests/Java.Interop/JavaInt64ArrayContractTests.cs b/src/Java.Interop/Tests/Java.Interop/JavaInt64ArrayContractTests.cs index 32c8586bf..f384760e8 100644 --- a/src/Java.Interop/Tests/Java.Interop/JavaInt64ArrayContractTests.cs +++ b/src/Java.Interop/Tests/Java.Interop/JavaInt64ArrayContractTests.cs @@ -11,6 +11,20 @@ namespace Java.InteropTests [TestFixture] public class JavaInt64ArrayContractTests : JavaPrimitiveArrayContract { + protected override ICollection CreateCollection (IEnumerable values) + { + return new JavaInt64Array (values); + } + + protected override ICollection CreateCollection (IList values) + { + return new JavaInt64Array (values); + } + + protected override ICollection CreateCollection (int length) + { + return new JavaInt64Array (length); + } } } diff --git a/src/Java.Interop/Tests/Java.Interop/JavaPrimitiveArrayContract.cs b/src/Java.Interop/Tests/Java.Interop/JavaPrimitiveArrayContract.cs index 59b9d4334..d20c52a86 100644 --- a/src/Java.Interop/Tests/Java.Interop/JavaPrimitiveArrayContract.cs +++ b/src/Java.Interop/Tests/Java.Interop/JavaPrimitiveArrayContract.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Reflection; using Java.Interop; @@ -22,11 +21,9 @@ protected override TElement CreateValueB () return FromInt32 ((int) 'B'); } - protected override ICollection CreateCollection (IEnumerable values) - { - var array = (JavaPrimitiveArray) Activator.CreateInstance (typeof (TArray), values); - return array; - } + protected abstract ICollection CreateCollection (IList values); + + protected abstract ICollection CreateCollection (int length); protected TElement FromInt32 (int value) { @@ -36,17 +33,9 @@ protected TElement FromInt32 (int value) [Test] public void Constructor_Exceptions () { - var ctor = typeof (TArray).GetConstructor (new[]{ typeof (IList) }); - var ex = Assert.Throws (() => ctor.Invoke (new object[]{ null })); - Assert.IsTrue (ex.InnerException is ArgumentNullException); - - ctor = typeof (TArray).GetConstructor (new[]{ typeof (IEnumerable) }); - ex = Assert.Throws (() => ctor.Invoke (new object[]{ null })); - Assert.IsTrue (ex.InnerException is ArgumentNullException); - - ctor = typeof (TArray).GetConstructor (new[]{ typeof (int) }); - ex = Assert.Throws (() => ctor.Invoke (new object[]{ -1 })); - Assert.IsTrue (ex.InnerException is ArgumentException); + Assert.Throws(() => CreateCollection ((IEnumerable) null)); + Assert.Throws(() => CreateCollection ((IList) null)); + Assert.Throws(() => CreateCollection (-1)); } [Test] diff --git a/src/Java.Interop/Tests/Java.Interop/JavaSByteArrayContractTests.cs b/src/Java.Interop/Tests/Java.Interop/JavaSByteArrayContractTests.cs index 4f897fe15..64228bd27 100644 --- a/src/Java.Interop/Tests/Java.Interop/JavaSByteArrayContractTests.cs +++ b/src/Java.Interop/Tests/Java.Interop/JavaSByteArrayContractTests.cs @@ -11,6 +11,20 @@ namespace Java.InteropTests [TestFixture] public class JavaSByteArrayContractTests : JavaPrimitiveArrayContract { + protected override ICollection CreateCollection (IEnumerable values) + { + return new JavaSByteArray (values); + } + + protected override ICollection CreateCollection (IList values) + { + return new JavaSByteArray (values); + } + + protected override ICollection CreateCollection (int length) + { + return new JavaSByteArray (length); + } } } diff --git a/src/Java.Interop/Tests/Java.Interop/JavaSingleArrayContractTests.cs b/src/Java.Interop/Tests/Java.Interop/JavaSingleArrayContractTests.cs index 66c510a0c..7d3d3c303 100644 --- a/src/Java.Interop/Tests/Java.Interop/JavaSingleArrayContractTests.cs +++ b/src/Java.Interop/Tests/Java.Interop/JavaSingleArrayContractTests.cs @@ -11,6 +11,20 @@ namespace Java.InteropTests [TestFixture] public class JavaSingleArrayContractTests : JavaPrimitiveArrayContract { + protected override ICollection CreateCollection (IEnumerable values) + { + return new JavaSingleArray (values); + } + + protected override ICollection CreateCollection (IList values) + { + return new JavaSingleArray (values); + } + + protected override ICollection CreateCollection (int length) + { + return new JavaSingleArray (length); + } } } diff --git a/src/Java.Interop/Tests/Java.Interop/JniEnvironmentTests.cs b/src/Java.Interop/Tests/Java.Interop/JniEnvironmentTests.cs index 3164c216b..b24626161 100644 --- a/src/Java.Interop/Tests/Java.Interop/JniEnvironmentTests.cs +++ b/src/Java.Interop/Tests/Java.Interop/JniEnvironmentTests.cs @@ -1,5 +1,4 @@ using System; -using System.Reflection; using System.Runtime.InteropServices; using Java.Interop; diff --git a/src/Java.Interop/Tests/Java.Interop/JniTransitionTest.cs b/src/Java.Interop/Tests/Java.Interop/JniTransitionTest.cs index 7fedd4158..565d100a8 100644 --- a/src/Java.Interop/Tests/Java.Interop/JniTransitionTest.cs +++ b/src/Java.Interop/Tests/Java.Interop/JniTransitionTest.cs @@ -1,5 +1,4 @@ using System; -using System.Reflection; using Java.Interop;