From 7d42821c8a849f4e1bd2c20b1b36b822e50dc8d0 Mon Sep 17 00:00:00 2001 From: Jonathan Pobst Date: Thu, 3 Oct 2019 10:28:57 -0500 Subject: [PATCH 1/5] [generator] Escape hyphens/dollar signs for callback names so they're valid C#. --- .../Method.cs | 2 +- .../Tests/Unit-Tests/CodeGeneratorTests.cs | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/tools/generator/Java.Interop.Tools.Generator.ObjectModel/Method.cs b/tools/generator/Java.Interop.Tools.Generator.ObjectModel/Method.cs index 06a09de2e..ca40817bb 100644 --- a/tools/generator/Java.Interop.Tools.Generator.ObjectModel/Method.cs +++ b/tools/generator/Java.Interop.Tools.Generator.ObjectModel/Method.cs @@ -105,7 +105,7 @@ internal string CalculateEventName (Func checkNameDuplicate) public string ConnectorName => $"Get{Name}{IDSignature}Handler"; - public string EscapedCallbackName => $"cb_{JavaName}{IDSignature}"; + public string EscapedCallbackName => $"cb_{JavaName}{IDSignature}".Replace ("-", "_x45_").Replace ("$", "_x36_"); public string EscapedIdName => "id_" + JavaName.Replace ("<", "_x60_").Replace (">", "_x62_") + IDSignature; diff --git a/tools/generator/Tests/Unit-Tests/CodeGeneratorTests.cs b/tools/generator/Tests/Unit-Tests/CodeGeneratorTests.cs index 511c23362..81849d6a1 100644 --- a/tools/generator/Tests/Unit-Tests/CodeGeneratorTests.cs +++ b/tools/generator/Tests/Unit-Tests/CodeGeneratorTests.cs @@ -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")); + } } } From 803f946b8b9c6e2e1c72674a8917fbe71d803ef5 Mon Sep 17 00:00:00 2001 From: Jonathan Pobst Date: Mon, 7 Oct 2019 13:34:39 -0500 Subject: [PATCH 2/5] [generator] Ensure we escape all invalid identifier characters. --- .../Method.cs | 2 +- .../Unit-Tests/TypeNameUtilitiesTests.cs | 15 +++++++- .../generator/Utilities/TypeNameUtilities.cs | 37 +++++++++++++++++++ 3 files changed, 52 insertions(+), 2 deletions(-) diff --git a/tools/generator/Java.Interop.Tools.Generator.ObjectModel/Method.cs b/tools/generator/Java.Interop.Tools.Generator.ObjectModel/Method.cs index ca40817bb..b7e5d5ef9 100644 --- a/tools/generator/Java.Interop.Tools.Generator.ObjectModel/Method.cs +++ b/tools/generator/Java.Interop.Tools.Generator.ObjectModel/Method.cs @@ -105,7 +105,7 @@ internal string CalculateEventName (Func checkNameDuplicate) public string ConnectorName => $"Get{Name}{IDSignature}Handler"; - public string EscapedCallbackName => $"cb_{JavaName}{IDSignature}".Replace ("-", "_x45_").Replace ("$", "_x36_"); + public string EscapedCallbackName => TypeNameUtilities.CreateValidIdentifier ($"cb_{JavaName}{IDSignature}", true); public string EscapedIdName => "id_" + JavaName.Replace ("<", "_x60_").Replace (">", "_x62_") + IDSignature; diff --git a/tools/generator/Tests/Unit-Tests/TypeNameUtilitiesTests.cs b/tools/generator/Tests/Unit-Tests/TypeNameUtilitiesTests.cs index edc034bdc..5822eb359 100644 --- a/tools/generator/Tests/Unit-Tests/TypeNameUtilitiesTests.cs +++ b/tools/generator/Tests/Unit-Tests/TypeNameUtilitiesTests.cs @@ -1,4 +1,4 @@ -using System; +using System; using MonoDroid.Generation; using NUnit.Framework; @@ -19,5 +19,18 @@ public void MangleName () Assert.AreEqual ("byte_var", TypeNameUtilities.MangleName ("byte_var")); Assert.AreEqual ("foo", TypeNameUtilities.MangleName ("foo")); } + + [Test] + public void CreateValidIdentifier_Simple () + { + Assert.AreEqual ("my_identifier_test", TypeNameUtilities.CreateValidIdentifier ("my-identifier$test")); + } + + [Test] + public void CreateValidIdentifier_Encoded () + { + Assert.AreEqual ("my_x45_identifier_x36_test", TypeNameUtilities.CreateValidIdentifier ("my-identifier$test", true)); + Assert.AreEqual ("myidentifier_x55357__x56842_test", TypeNameUtilities.CreateValidIdentifier ("myidentifier😊test", true)); + } } } diff --git a/tools/generator/Utilities/TypeNameUtilities.cs b/tools/generator/Utilities/TypeNameUtilities.cs index 371ab068e..ffb9f25dd 100644 --- a/tools/generator/Utilities/TypeNameUtilities.cs +++ b/tools/generator/Utilities/TypeNameUtilities.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using System.Text.RegularExpressions; using System.Threading.Tasks; namespace MonoDroid.Generation @@ -101,5 +102,41 @@ public static string StudlyCase (string name) } return builder.ToString (); } + + // From https://github.com/xamarin/xamarin-android/blob/master/src/Xamarin.Android.Build.Tasks/Utilities/ResourceIdentifier.cs + // 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]}_"; } } From 6fee2a2e6fcb63fb4edc158d2d3848b2796b99e3 Mon Sep 17 00:00:00 2001 From: Jonathan Pobst Date: Mon, 7 Oct 2019 16:47:55 -0500 Subject: [PATCH 3/5] [Java.Interop.Tools.JavaCallableWrappers] Create common method for validating identifier names. --- ....Interop.Tools.JavaCallableWrappers.csproj | 1 + .../IdentifierValidator.cs | 47 +++++++++++++++++++ ...op.Tools.JavaCallableWrappers-Tests.csproj | 1 + .../IdentifierValidatorTests.cs | 28 +++++++++++ .../Method.cs | 3 +- .../Unit-Tests/TypeNameUtilitiesTests.cs | 13 ----- .../generator/Utilities/TypeNameUtilities.cs | 37 --------------- 7 files changed, 79 insertions(+), 51 deletions(-) create mode 100644 src/Java.Interop.Tools.JavaCallableWrappers/Java.Interop.Tools.JavaCallableWrappers/IdentifierValidator.cs create mode 100644 src/Java.Interop.Tools.JavaCallableWrappers/Test/Java.Interop.Tools.JavaCallableWrappers/IdentifierValidatorTests.cs diff --git a/src/Java.Interop.Tools.JavaCallableWrappers/Java.Interop.Tools.JavaCallableWrappers.csproj b/src/Java.Interop.Tools.JavaCallableWrappers/Java.Interop.Tools.JavaCallableWrappers.csproj index 3103b1558..087160981 100644 --- a/src/Java.Interop.Tools.JavaCallableWrappers/Java.Interop.Tools.JavaCallableWrappers.csproj +++ b/src/Java.Interop.Tools.JavaCallableWrappers/Java.Interop.Tools.JavaCallableWrappers.csproj @@ -34,6 +34,7 @@ + diff --git a/src/Java.Interop.Tools.JavaCallableWrappers/Java.Interop.Tools.JavaCallableWrappers/IdentifierValidator.cs b/src/Java.Interop.Tools.JavaCallableWrappers/Java.Interop.Tools.JavaCallableWrappers/IdentifierValidator.cs new file mode 100644 index 000000000..d3158b19b --- /dev/null +++ b/src/Java.Interop.Tools.JavaCallableWrappers/Java.Interop.Tools.JavaCallableWrappers/IdentifierValidator.cs @@ -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]}_"; + } +} diff --git a/src/Java.Interop.Tools.JavaCallableWrappers/Test/Java.Interop.Tools.JavaCallableWrappers-Tests.csproj b/src/Java.Interop.Tools.JavaCallableWrappers/Test/Java.Interop.Tools.JavaCallableWrappers-Tests.csproj index 17934c1b8..1ae521ced 100644 --- a/src/Java.Interop.Tools.JavaCallableWrappers/Test/Java.Interop.Tools.JavaCallableWrappers-Tests.csproj +++ b/src/Java.Interop.Tools.JavaCallableWrappers/Test/Java.Interop.Tools.JavaCallableWrappers-Tests.csproj @@ -39,6 +39,7 @@ + diff --git a/src/Java.Interop.Tools.JavaCallableWrappers/Test/Java.Interop.Tools.JavaCallableWrappers/IdentifierValidatorTests.cs b/src/Java.Interop.Tools.JavaCallableWrappers/Test/Java.Interop.Tools.JavaCallableWrappers/IdentifierValidatorTests.cs new file mode 100644 index 000000000..abb1593bb --- /dev/null +++ b/src/Java.Interop.Tools.JavaCallableWrappers/Test/Java.Interop.Tools.JavaCallableWrappers/IdentifierValidatorTests.cs @@ -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)); + } + } +} diff --git a/tools/generator/Java.Interop.Tools.Generator.ObjectModel/Method.cs b/tools/generator/Java.Interop.Tools.Generator.ObjectModel/Method.cs index b7e5d5ef9..67e857b92 100644 --- a/tools/generator/Java.Interop.Tools.Generator.ObjectModel/Method.cs +++ b/tools/generator/Java.Interop.Tools.Generator.ObjectModel/Method.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using Java.Interop.Tools.JavaCallableWrappers; using MonoDroid.Generation.Utilities; namespace MonoDroid.Generation @@ -105,7 +106,7 @@ internal string CalculateEventName (Func checkNameDuplicate) public string ConnectorName => $"Get{Name}{IDSignature}Handler"; - public string EscapedCallbackName => TypeNameUtilities.CreateValidIdentifier ($"cb_{JavaName}{IDSignature}", true); + public string EscapedCallbackName => IdentifierValidator.CreateValidIdentifier ($"cb_{JavaName}{IDSignature}", true); public string EscapedIdName => "id_" + JavaName.Replace ("<", "_x60_").Replace (">", "_x62_") + IDSignature; diff --git a/tools/generator/Tests/Unit-Tests/TypeNameUtilitiesTests.cs b/tools/generator/Tests/Unit-Tests/TypeNameUtilitiesTests.cs index 5822eb359..6b4c9faf7 100644 --- a/tools/generator/Tests/Unit-Tests/TypeNameUtilitiesTests.cs +++ b/tools/generator/Tests/Unit-Tests/TypeNameUtilitiesTests.cs @@ -19,18 +19,5 @@ public void MangleName () Assert.AreEqual ("byte_var", TypeNameUtilities.MangleName ("byte_var")); Assert.AreEqual ("foo", TypeNameUtilities.MangleName ("foo")); } - - [Test] - public void CreateValidIdentifier_Simple () - { - Assert.AreEqual ("my_identifier_test", TypeNameUtilities.CreateValidIdentifier ("my-identifier$test")); - } - - [Test] - public void CreateValidIdentifier_Encoded () - { - Assert.AreEqual ("my_x45_identifier_x36_test", TypeNameUtilities.CreateValidIdentifier ("my-identifier$test", true)); - Assert.AreEqual ("myidentifier_x55357__x56842_test", TypeNameUtilities.CreateValidIdentifier ("myidentifier😊test", true)); - } } } diff --git a/tools/generator/Utilities/TypeNameUtilities.cs b/tools/generator/Utilities/TypeNameUtilities.cs index ffb9f25dd..371ab068e 100644 --- a/tools/generator/Utilities/TypeNameUtilities.cs +++ b/tools/generator/Utilities/TypeNameUtilities.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using System.Linq; using System.Text; -using System.Text.RegularExpressions; using System.Threading.Tasks; namespace MonoDroid.Generation @@ -102,41 +101,5 @@ public static string StudlyCase (string name) } return builder.ToString (); } - - // From https://github.com/xamarin/xamarin-android/blob/master/src/Xamarin.Android.Build.Tasks/Utilities/ResourceIdentifier.cs - // 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]}_"; } } From 1f926a2c21484acf6bc217ac3447eaba00682e25 Mon Sep 17 00:00:00 2001 From: Jonathan Pryor Date: Mon, 7 Oct 2019 21:46:13 -0400 Subject: [PATCH 4/5] Remove BOM no whitespace changes! --- tools/generator/Tests/Unit-Tests/TypeNameUtilitiesTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/generator/Tests/Unit-Tests/TypeNameUtilitiesTests.cs b/tools/generator/Tests/Unit-Tests/TypeNameUtilitiesTests.cs index 6b4c9faf7..edc034bdc 100644 --- a/tools/generator/Tests/Unit-Tests/TypeNameUtilitiesTests.cs +++ b/tools/generator/Tests/Unit-Tests/TypeNameUtilitiesTests.cs @@ -1,4 +1,4 @@ -using System; +using System; using MonoDroid.Generation; using NUnit.Framework; From a5c3acd0bbd011f0c92d410e0381aeb06eefdd13 Mon Sep 17 00:00:00 2001 From: Jonathan Pryor Date: Mon, 7 Oct 2019 21:46:58 -0400 Subject: [PATCH 5/5] Sort filenames --- .../Java.Interop.Tools.JavaCallableWrappers.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Java.Interop.Tools.JavaCallableWrappers/Java.Interop.Tools.JavaCallableWrappers.csproj b/src/Java.Interop.Tools.JavaCallableWrappers/Java.Interop.Tools.JavaCallableWrappers.csproj index 087160981..4a0e46e81 100644 --- a/src/Java.Interop.Tools.JavaCallableWrappers/Java.Interop.Tools.JavaCallableWrappers.csproj +++ b/src/Java.Interop.Tools.JavaCallableWrappers/Java.Interop.Tools.JavaCallableWrappers.csproj @@ -34,9 +34,9 @@ - +