Skip to content

Commit 166a6c9

Browse files
jonpryorjpobst
authored andcommitted
[Java.Interop-Tests] Remove Reflection from JavaPrimitiveArrayContract (#521)
Context: dotnet/android#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.
1 parent 09083d9 commit 166a6c9

11 files changed

+119
-19
lines changed

src/Java.Interop/Tests/Java.Interop/JavaBooleanArrayContractTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,21 @@ namespace Java.InteropTests
1111
[TestFixture]
1212
public class JavaBooleanArrayContractTests : JavaPrimitiveArrayContract<JavaBooleanArray, bool>
1313
{
14+
protected override ICollection<bool> CreateCollection (IEnumerable<bool> values)
15+
{
16+
return new JavaBooleanArray (values);
17+
}
18+
19+
protected override ICollection<bool> CreateCollection (IList<bool> values)
20+
{
21+
return new JavaBooleanArray (values);
22+
}
23+
24+
protected override ICollection<bool> CreateCollection (int length)
25+
{
26+
return new JavaBooleanArray (length);
27+
}
28+
1429
protected override bool CreateValueA ()
1530
{
1631
return true;

src/Java.Interop/Tests/Java.Interop/JavaCharArrayContractTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,20 @@ namespace Java.InteropTests
1111
[TestFixture]
1212
public class JavaCharArrayContractTests : JavaPrimitiveArrayContract<JavaCharArray, char>
1313
{
14+
protected override ICollection<char> CreateCollection (IEnumerable<char> values)
15+
{
16+
return new JavaCharArray (values);
17+
}
18+
19+
protected override ICollection<char> CreateCollection (IList<char> values)
20+
{
21+
return new JavaCharArray (values);
22+
}
23+
24+
protected override ICollection<char> CreateCollection (int length)
25+
{
26+
return new JavaCharArray (length);
27+
}
1428
}
1529
}
1630

src/Java.Interop/Tests/Java.Interop/JavaDoubleArrayContractTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,20 @@ namespace Java.InteropTests
1111
[TestFixture]
1212
public class JavaDoubleArrayContractTests : JavaPrimitiveArrayContract<JavaDoubleArray, double>
1313
{
14+
protected override ICollection<double> CreateCollection (IEnumerable<double> values)
15+
{
16+
return new JavaDoubleArray (values);
17+
}
18+
19+
protected override ICollection<double> CreateCollection (IList<double> values)
20+
{
21+
return new JavaDoubleArray (values);
22+
}
23+
24+
protected override ICollection<double> CreateCollection (int length)
25+
{
26+
return new JavaDoubleArray (length);
27+
}
1428
}
1529
}
1630

src/Java.Interop/Tests/Java.Interop/JavaInt16ArrayContractTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,20 @@ namespace Java.InteropTests
1111
[TestFixture]
1212
public class JavaInt16ArrayContractTests : JavaPrimitiveArrayContract<JavaInt16Array, short>
1313
{
14+
protected override ICollection<short> CreateCollection (IEnumerable<short> values)
15+
{
16+
return new JavaInt16Array (values);
17+
}
18+
19+
protected override ICollection<short> CreateCollection (IList<short> values)
20+
{
21+
return new JavaInt16Array (values);
22+
}
23+
24+
protected override ICollection<short> CreateCollection (int length)
25+
{
26+
return new JavaInt16Array (length);
27+
}
1428
}
1529
}
1630

src/Java.Interop/Tests/Java.Interop/JavaInt32ArrayContractTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,20 @@ namespace Java.InteropTests
1111
[TestFixture]
1212
public class JavaInt32ArrayContractTests : JavaPrimitiveArrayContract<JavaInt32Array, int>
1313
{
14+
protected override ICollection<int> CreateCollection (IEnumerable<int> values)
15+
{
16+
return new JavaInt32Array (values);
17+
}
18+
19+
protected override ICollection<int> CreateCollection (IList<int> values)
20+
{
21+
return new JavaInt32Array (values);
22+
}
23+
24+
protected override ICollection<int> CreateCollection (int length)
25+
{
26+
return new JavaInt32Array (length);
27+
}
1428
}
1529
}
1630

src/Java.Interop/Tests/Java.Interop/JavaInt64ArrayContractTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,20 @@ namespace Java.InteropTests
1111
[TestFixture]
1212
public class JavaInt64ArrayContractTests : JavaPrimitiveArrayContract<JavaInt64Array, long>
1313
{
14+
protected override ICollection<long> CreateCollection (IEnumerable<long> values)
15+
{
16+
return new JavaInt64Array (values);
17+
}
18+
19+
protected override ICollection<long> CreateCollection (IList<long> values)
20+
{
21+
return new JavaInt64Array (values);
22+
}
23+
24+
protected override ICollection<long> CreateCollection (int length)
25+
{
26+
return new JavaInt64Array (length);
27+
}
1428
}
1529
}
1630

src/Java.Interop/Tests/Java.Interop/JavaPrimitiveArrayContract.cs

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4-
using System.Reflection;
54

65
using Java.Interop;
76

@@ -22,11 +21,9 @@ protected override TElement CreateValueB ()
2221
return FromInt32 ((int) 'B');
2322
}
2423

25-
protected override ICollection<TElement> CreateCollection (IEnumerable<TElement> values)
26-
{
27-
var array = (JavaPrimitiveArray<TElement>) Activator.CreateInstance (typeof (TArray), values);
28-
return array;
29-
}
24+
protected abstract ICollection<TElement> CreateCollection (IList<TElement> values);
25+
26+
protected abstract ICollection<TElement> CreateCollection (int length);
3027

3128
protected TElement FromInt32 (int value)
3229
{
@@ -36,17 +33,9 @@ protected TElement FromInt32 (int value)
3633
[Test]
3734
public void Constructor_Exceptions ()
3835
{
39-
var ctor = typeof (TArray).GetConstructor (new[]{ typeof (IList<TElement>) });
40-
var ex = Assert.Throws<TargetInvocationException> (() => ctor.Invoke (new object[]{ null }));
41-
Assert.IsTrue (ex.InnerException is ArgumentNullException);
42-
43-
ctor = typeof (TArray).GetConstructor (new[]{ typeof (IEnumerable<TElement>) });
44-
ex = Assert.Throws<TargetInvocationException> (() => ctor.Invoke (new object[]{ null }));
45-
Assert.IsTrue (ex.InnerException is ArgumentNullException);
46-
47-
ctor = typeof (TArray).GetConstructor (new[]{ typeof (int) });
48-
ex = Assert.Throws<TargetInvocationException> (() => ctor.Invoke (new object[]{ -1 }));
49-
Assert.IsTrue (ex.InnerException is ArgumentException);
36+
Assert.Throws<ArgumentNullException>(() => CreateCollection ((IEnumerable<TElement>) null));
37+
Assert.Throws<ArgumentNullException>(() => CreateCollection ((IList<TElement>) null));
38+
Assert.Throws<ArgumentException>(() => CreateCollection (-1));
5039
}
5140

5241
[Test]

src/Java.Interop/Tests/Java.Interop/JavaSByteArrayContractTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,20 @@ namespace Java.InteropTests
1111
[TestFixture]
1212
public class JavaSByteArrayContractTests : JavaPrimitiveArrayContract<JavaSByteArray, sbyte>
1313
{
14+
protected override ICollection<sbyte> CreateCollection (IEnumerable<sbyte> values)
15+
{
16+
return new JavaSByteArray (values);
17+
}
18+
19+
protected override ICollection<sbyte> CreateCollection (IList<sbyte> values)
20+
{
21+
return new JavaSByteArray (values);
22+
}
23+
24+
protected override ICollection<sbyte> CreateCollection (int length)
25+
{
26+
return new JavaSByteArray (length);
27+
}
1428
}
1529
}
1630

src/Java.Interop/Tests/Java.Interop/JavaSingleArrayContractTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,20 @@ namespace Java.InteropTests
1111
[TestFixture]
1212
public class JavaSingleArrayContractTests : JavaPrimitiveArrayContract<JavaSingleArray, float>
1313
{
14+
protected override ICollection<float> CreateCollection (IEnumerable<float> values)
15+
{
16+
return new JavaSingleArray (values);
17+
}
18+
19+
protected override ICollection<float> CreateCollection (IList<float> values)
20+
{
21+
return new JavaSingleArray (values);
22+
}
23+
24+
protected override ICollection<float> CreateCollection (int length)
25+
{
26+
return new JavaSingleArray (length);
27+
}
1428
}
1529
}
1630

src/Java.Interop/Tests/Java.Interop/JniEnvironmentTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Reflection;
32
using System.Runtime.InteropServices;
43

54
using Java.Interop;

0 commit comments

Comments
 (0)