Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/Java.Interop/Java.Interop/JniTypeSignature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,13 @@ public string Name {
public JniTypeSignature (string? simpleReference, int arrayRank = 0, bool keyword = false)
{
if (simpleReference != null) {
Comment on lines 43 to 45
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is simpleReference allowed to be null?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jonathanpeppers: it's a struct, and thus always has a default constructor, i.e. new JniTypeSignature() is always "valid" (can't/won't throw), and couldn't be overridden at the time (that may have since changed, but default provides original semantics). Given that, and that new JniTypeSignature() is semantically identical to new JniTypeSignature(null) and new JniTypeSignature(null, 0, false), why should new JniTypeSigature(null) throw?

Perhaps it should throw, but at the time it didn't make sense to me.

if (simpleReference.IndexOf (".", StringComparison.Ordinal) >= 0)
if (simpleReference.Length < 1)
throw new ArgumentException ("The empty string is not a valid JNI simple reference.", nameof (simpleReference));
if (simpleReference.IndexOf ('.') >= 0)
throw new ArgumentException ("JNI type names do not contain '.', they use '/'. Are you sure you're using a JNI type name?", nameof (simpleReference));
if (simpleReference.StartsWith ("[", StringComparison.Ordinal))
if (simpleReference [0] == '[')
throw new ArgumentException ("To specify an array, use the ArrayRank property.", nameof (simpleReference));
if (simpleReference.StartsWith ("L", StringComparison.Ordinal) && simpleReference.EndsWith (";", StringComparison.Ordinal))
if (simpleReference [0] == 'L' && simpleReference [simpleReference.Length-1] == ';' )
throw new ArgumentException ("JNI type references are not supported.", nameof (simpleReference));
}

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

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

int i = 0;
int r = 0;
Expand Down
15 changes: 15 additions & 0 deletions tests/Java.Interop-Tests/Java.Interop/JniTypeSignatureTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ public void Constructor_Exceptions ()
Assert.Throws<ArgumentException> (() => new JniTypeSignature ("java.lang.Object"));
Assert.Throws<ArgumentException> (() => new JniTypeSignature ("[[I"));
Assert.Throws<ArgumentException> (() => new JniTypeSignature ("Ljava/lang/Object;"));
Assert.Throws<ArgumentException> (() => new JniTypeSignature (""));
}

[Test]
public void DefaultConstructor ()
{
var t = new JniTypeSignature ();
Assert.False (t.IsValid);
}

[Test]
Expand Down Expand Up @@ -55,6 +63,7 @@ public void QualifiedReference ()
public void Parse ()
{
Assert.Throws<ArgumentNullException> (() => JniTypeSignature.Parse ((string) null));
Assert.Throws<ArgumentException> (() => JniTypeSignature.Parse (""));
Assert.Throws<ArgumentException> (() => JniTypeSignature.Parse ("java.lang.String"));
Assert.Throws<ArgumentException> (() => JniTypeSignature.Parse ("Ljava/lang/String;I"));
Assert.Throws<ArgumentException> (() => JniTypeSignature.Parse ("ILjava/lang/String;"));
Expand All @@ -78,6 +87,12 @@ static void AssertGetJniTypeInfoForJniTypeReference (string jniTypeReference, st
Assert.AreEqual (jniTypeName, sig.SimpleReference, "JniTypeName for: " + jniTypeReference);
Assert.AreEqual (arrayRank, sig.ArrayRank, "ArrayRank for: " + jniTypeReference);
}

[Test]
public void TryParse ()
{
Assert.False (JniTypeSignature.TryParse ("", out var _));
}
}
}