Skip to content

Commit 99c68a8

Browse files
authored
[Java.Interop] JniTypeSignature & CultureInfo, empty strings (#1002)
Fixes: #335 Context: 920ea64 Commit 920ea64 optimized `JniTypeManager.AssertSimpleReference()` by using `string.IndexOf(char)`. This dovetails with Issue #335, which wanted to optimize `JniTypeSignature` by removing calls to `string.Contains(string)`. Fix #335, and update the `JniTypeSignature` constructor to use `string.IndexOf(char)` instead of `string.Contains(string)`, and use string indexers instead of `string.StartsWith()` and `string.EndsWith()`. `Java.Interop.dll` has no remaining usage of `string.StartsWith()` and `string.EndsWith()`. Additionally, update `JniTypeSignature` so that the empty string `""` is *not* treated as a valid type signature. This is arguably a breaking change, but the empty string never made sense, *and* would throw an `IndexOutOfRangeException` with `JniTypeManager.Parse()`.
1 parent 920ea64 commit 99c68a8

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

src/Java.Interop/Java.Interop/JniTypeSignature.cs

+7-3
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,13 @@ public string Name {
4343
public JniTypeSignature (string? simpleReference, int arrayRank = 0, bool keyword = false)
4444
{
4545
if (simpleReference != null) {
46-
if (simpleReference.IndexOf (".", StringComparison.Ordinal) >= 0)
46+
if (simpleReference.Length < 1)
47+
throw new ArgumentException ("The empty string is not a valid JNI simple reference.", nameof (simpleReference));
48+
if (simpleReference.IndexOf ('.') >= 0)
4749
throw new ArgumentException ("JNI type names do not contain '.', they use '/'. Are you sure you're using a JNI type name?", nameof (simpleReference));
48-
if (simpleReference.StartsWith ("[", StringComparison.Ordinal))
50+
if (simpleReference [0] == '[')
4951
throw new ArgumentException ("To specify an array, use the ArrayRank property.", nameof (simpleReference));
50-
if (simpleReference.StartsWith ("L", StringComparison.Ordinal) && simpleReference.EndsWith (";", StringComparison.Ordinal))
52+
if (simpleReference [0] == 'L' && simpleReference [simpleReference.Length-1] == ';' )
5153
throw new ArgumentException ("JNI type references are not supported.", nameof (simpleReference));
5254
}
5355

@@ -105,6 +107,8 @@ public static bool TryParse (string signature, [NotNullWhen (true)] out JniTypeS
105107

106108
if (signature == null)
107109
return new ArgumentNullException (nameof (signature));
110+
if (signature.Length < 1)
111+
return new ArgumentException ("The empty string is not a valid JNI simple reference.", nameof (signature));
108112

109113
int i = 0;
110114
int r = 0;

tests/Java.Interop-Tests/Java.Interop/JniTypeSignatureTest.cs

+15
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ public void Constructor_Exceptions ()
1515
Assert.Throws<ArgumentException> (() => new JniTypeSignature ("java.lang.Object"));
1616
Assert.Throws<ArgumentException> (() => new JniTypeSignature ("[[I"));
1717
Assert.Throws<ArgumentException> (() => new JniTypeSignature ("Ljava/lang/Object;"));
18+
Assert.Throws<ArgumentException> (() => new JniTypeSignature (""));
19+
}
20+
21+
[Test]
22+
public void DefaultConstructor ()
23+
{
24+
var t = new JniTypeSignature ();
25+
Assert.False (t.IsValid);
1826
}
1927

2028
[Test]
@@ -55,6 +63,7 @@ public void QualifiedReference ()
5563
public void Parse ()
5664
{
5765
Assert.Throws<ArgumentNullException> (() => JniTypeSignature.Parse ((string) null));
66+
Assert.Throws<ArgumentException> (() => JniTypeSignature.Parse (""));
5867
Assert.Throws<ArgumentException> (() => JniTypeSignature.Parse ("java.lang.String"));
5968
Assert.Throws<ArgumentException> (() => JniTypeSignature.Parse ("Ljava/lang/String;I"));
6069
Assert.Throws<ArgumentException> (() => JniTypeSignature.Parse ("ILjava/lang/String;"));
@@ -78,6 +87,12 @@ static void AssertGetJniTypeInfoForJniTypeReference (string jniTypeReference, st
7887
Assert.AreEqual (jniTypeName, sig.SimpleReference, "JniTypeName for: " + jniTypeReference);
7988
Assert.AreEqual (arrayRank, sig.ArrayRank, "ArrayRank for: " + jniTypeReference);
8089
}
90+
91+
[Test]
92+
public void TryParse ()
93+
{
94+
Assert.False (JniTypeSignature.TryParse ("", out var _));
95+
}
8196
}
8297
}
8398

0 commit comments

Comments
 (0)