Skip to content
This repository was archived by the owner on Jul 6, 2026. It is now read-only.

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

2 files changed

Lines changed: 22 additions & 3 deletions

File tree

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,13 @@ public string Name {
4343
publicJniTypeSignature(string?simpleReference,intarrayRank=0,boolkeyword=false)
4444
{
4545
if(simpleReference!=null){
46-
if(simpleReference.IndexOf(".",StringComparison.Ordinal)>=0)
46+
if(simpleReference.Length<1)
47+
thrownewArgumentException("The empty string is not a valid JNI simple reference.",nameof(simpleReference));
48+
if(simpleReference.IndexOf('.')>=0)
4749
thrownewArgumentException("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
thrownewArgumentException("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
thrownewArgumentException("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
returnnewArgumentNullException(nameof(signature));
110+
if(signature.Length<1)
111+
returnnewArgumentException("The empty string is not a valid JNI simple reference.",nameof(signature));
108112

109113
inti=0;
110114
intr=0;

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ public void Constructor_Exceptions ()
1515
Assert.Throws<ArgumentException>(()=>newJniTypeSignature("java.lang.Object"));
1616
Assert.Throws<ArgumentException>(()=>newJniTypeSignature("[[I"));
1717
Assert.Throws<ArgumentException>(()=>newJniTypeSignature("Ljava/lang/Object;"));
18+
Assert.Throws<ArgumentException>(()=>newJniTypeSignature(""));
19+
}
20+
21+
[Test]
22+
publicvoidDefaultConstructor()
23+
{
24+
vart=newJniTypeSignature();
25+
Assert.False(t.IsValid);
1826
}
1927

2028
[Test]
@@ -55,6 +63,7 @@ public void QualifiedReference ()
5563
publicvoidParse()
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+
publicvoidTryParse()
93+
{
94+
Assert.False(JniTypeSignature.TryParse("",outvar_));
95+
}
8196
}
8297
}
8398

0 commit comments

Comments
 (0)