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
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Java.Interop.Tools.JavaCallableWrappers\Crc64.cs" />
<Compile Include="Java.Interop.Tools.JavaCallableWrappers\IdentifierValidator.cs" />
<Compile Include="Java.Interop.Tools.JavaCallableWrappers\JavaCallableWrapperGenerator.cs" />
<Compile Include="Java.Interop.Tools.JavaCallableWrappers\JavaTypeScanner.cs" />
<Compile Include="Java.Interop.Tools.JavaCallableWrappers\TypeNameMapGenerator.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Java.Interop.Tools.JavaCallableWrappers
{
public static class IdentifierValidator
{
// https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/lexical-structure#identifiers
private const string FormattingCharacter = @"\p{Cf}";
private const string ConnectingCharacter = @"\p{Pc}";
private const string DecimalDigitCharacter = @"\p{Nd}";
private const string CombiningCharacter = @"\p{Mn}\p{Mc}";
private const string LetterCharacter = @"\p{Lu}\p{Ll}\p{Lt}\p{Lm}\p{Lo}\p{Nl}";

private const string IdentifierPartCharacter = LetterCharacter +
DecimalDigitCharacter +
ConnectingCharacter +
CombiningCharacter +
FormattingCharacter;

private const string IdentifierStartCharacter = "(" + LetterCharacter + "_)";

private const string Identifier = IdentifierStartCharacter + "(" + IdentifierPartCharacter + ")";

// We use [^ ...] to detect any character that is NOT a match.
static Regex validIdentifier = new Regex ($"[^{Identifier}]", RegexOptions.Compiled);

public static string CreateValidIdentifier (string identifier, bool useEncodedReplacements = false)
{
if (string.IsNullOrWhiteSpace (identifier)) return string.Empty;

var normalizedIdentifier = identifier.Normalize ();

if (useEncodedReplacements)
return validIdentifier.Replace (normalizedIdentifier, new MatchEvaluator (EncodeReplacement));

return validIdentifier.Replace (normalizedIdentifier, "_");
}

// Makes uglier but unique identifiers by encoding each invalid character with its character value
static string EncodeReplacement (Match match) => $"_x{(ushort) match.Value [0]}_";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
<ItemGroup>
<Compile Include="Java.Interop.Tools.JavaCallableWrappers\JavaCallableWrapperGeneratorTests.cs" />
<Compile Include="Java.Interop.Tools.JavaCallableWrappers\Crc64Tests.cs" />
<Compile Include="Java.Interop.Tools.JavaCallableWrappers\IdentifierValidatorTests.cs" />
<Compile Include="Java.Interop.Tools.JavaCallableWrappers\JavaNativeTypeManagerTests.cs" />
<Compile Include="Java.Interop.Tools.JavaCallableWrappers\TypeNameMapGeneratorTests.cs" />
<Compile Include="Java.Interop.Tools.JavaCallableWrappers\SupportDeclarations.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Java.Interop.Tools.JavaCallableWrappers;
using Java.Interop.Tools.TypeNameMappings;
using NUnit.Framework;

namespace Java.Interop.Tools.JavaCallableWrappersTests
{
[TestFixture]
public class IdentifierValidatorTests
{
[Test]
public void CreateValidIdentifier_Simple ()
{
Assert.AreEqual ("my_identifier_test", IdentifierValidator.CreateValidIdentifier ("my-identifier$test"));
}

[Test]
public void CreateValidIdentifier_Encoded ()
{
Assert.AreEqual ("my_x45_identifier_x36_test", IdentifierValidator.CreateValidIdentifier ("my-identifier$test", true));
Assert.AreEqual ("myidentifier_x55357__x56842_test", IdentifierValidator.CreateValidIdentifier ("myidentifier😊test", true));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Linq;
using Java.Interop.Tools.JavaCallableWrappers;
using MonoDroid.Generation.Utilities;

namespace MonoDroid.Generation
Expand Down Expand Up @@ -105,7 +106,7 @@ internal string CalculateEventName (Func<string, bool> checkNameDuplicate)

public string ConnectorName => $"Get{Name}{IDSignature}Handler";

public string EscapedCallbackName => $"cb_{JavaName}{IDSignature}";
public string EscapedCallbackName => IdentifierValidator.CreateValidIdentifier ($"cb_{JavaName}{IDSignature}", true);

public string EscapedIdName => "id_" + JavaName.Replace ("<", "_x60_").Replace (">", "_x62_") + IDSignature;
Copy link
Member

Choose a reason for hiding this comment

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

I see in this instance you only replace the JavaName and not the signature. Does it matter?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure I understand?

Copy link
Member

Choose a reason for hiding this comment

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

This line is JavaName.Replace ("<", "_x60_").Replace (">", "_x62_")
and then new one is "cb_{JavaName}{IDSignature}".Replace (...

Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if this is relevant: #498 (comment)

Have a "single" "ensure the string is a valid C# identifier" method and use that consistently ~everywhere.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'll be merging this PR, but on the whole I believe @mattleibow is correct, and EscapedIdName should likewise use IdentifierValidator.CreateValidIdentifier() as well.

However, AFAICT EscapedIdName is only used for the legacy JNIEnv-based output format, not the newer XAJavaInterop1 type, so I'm not entirely concerned about this. We should prefer XAJavaInterop.


Expand Down
18 changes: 18 additions & 0 deletions tools/generator/Tests/Unit-Tests/CodeGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -844,5 +844,23 @@ public void WritePropertyInvoker ()

Assert.AreEqual (GetExpected (nameof (WritePropertyInvoker)), writer.ToString ().NormalizeLineEndings ());
}

[Test]
public void WriteMethodWithInvalidJavaName ()
{
var @class = new TestClass ("java.lang.Object", "com.mypackage.foo");
var method = new TestMethod (@class, "has-hyp$hen");

method.Name = "nohyphen";

Assert.IsTrue (method.Validate (options, new GenericParameterDefinitionList (), new CodeGeneratorContext ()), "method.Validate failed!");
generator.WriteMethod (method, string.Empty, @class, true);

var result = writer.ToString ().NormalizeLineEndings ();

// Ensure we escape hyphens/dollar signs in callback names
Assert.False (result.Contains ("cb_has-hyp$hen"));
Assert.True (result.Contains ("cb_has_x45_hyp_x36_hen"));
}
}
}