Skip to content

Commit bbd75c1

Browse files
jpobstjonpryor
authored andcommitted
[generator] Replace hyphens in parameter names (#498)
Context: #464 Context: 7a309c4 Use of [Kotlin][0] can result in some "weird" method names and parameter names in Java bytecode, in particular names which contain spaces: // Kotlin abstract class Example { public fun `method spaces`(`parameter spaces`: Double) { } } Run that through `kotlinc` and `javap`, and...there's a parameter name with spaces! $ kotlinc -java-parameters hello.kt $ javap Example.class Classfile Example.class ... public abstract class Example ... public final void method spaces(double); ... MethodParameters: Name Flags parameter spaces ...which is a sure fire way to confuse things. Update `XmlApiImporter.EnsureValidIdentifier()` so that it uses `IdentifierValidator.CreateValidIdentifier()` (7a309c4) to turn potentially invalid strings such as `method spaces` into valid C# identifiers such as `method_spaces`, which also ensures that method names and constructor names are appropriately fixed. Additionally, update `XmlApiImporter.CreateParameter()` so that it now uses `EnsureValidIdentifier()`, ensuring it is consistent with method and constructor name correction. [0]: https://kotlinlang.org
1 parent 68062a7 commit bbd75c1

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

tools/generator/Java.Interop.Tools.Generator.Importers/XmlApiImporter.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Linq;
33
using System.Text.RegularExpressions;
44
using System.Xml.Linq;
5+
using Java.Interop.Tools.JavaCallableWrappers;
56
using MonoDroid.Utils;
67
using Xamarin.Android.Tools;
78

@@ -270,7 +271,7 @@ public static Method CreateMethod (GenBase declaringType, XElement elem)
270271
public static Parameter CreateParameter (XElement elem)
271272
{
272273
string managedName = elem.XGetAttribute ("managedName");
273-
string name = !string.IsNullOrEmpty (managedName) ? managedName : EnsureValidIdentifer (TypeNameUtilities.MangleName (elem.XGetAttribute ("name")));
274+
string name = !string.IsNullOrEmpty (managedName) ? managedName : TypeNameUtilities.MangleName (EnsureValidIdentifer (elem.XGetAttribute ("name")));
274275
string java_type = elem.XGetAttribute ("type");
275276
string enum_type = elem.Attribute ("enumType") != null ? elem.XGetAttribute ("enumType") : null;
276277
string managed_type = elem.Attribute ("managedType") != null ? elem.XGetAttribute ("managedType") : null;
@@ -294,7 +295,7 @@ static string EnsureValidIdentifer (string name)
294295
if (string.IsNullOrWhiteSpace (name))
295296
return name;
296297

297-
name = name.Replace ('$', '_');
298+
name = IdentifierValidator.CreateValidIdentifier (name);
298299

299300
if (char.IsNumber (name [0]))
300301
name = $"_{name}";

tools/generator/Tests/Unit-Tests/XmlApiImporterTests.cs

+9
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,15 @@ public void CreateMethod_EnsureValidName ()
8080
Assert.AreEqual ("_3", klass.Methods [0].Name);
8181
}
8282

83+
[Test]
84+
public void CreateMethod_EnsureValidNameHyphen ()
85+
{
86+
var xml = XDocument.Parse ("<package name=\"com.example.test\" jni-name=\"com/example/test\"><class name=\"test\"><method name=\"-3\" /></class></package>");
87+
var klass = XmlApiImporter.CreateClass (xml.Root, xml.Root.Element ("class"));
88+
89+
Assert.AreEqual ("_3", klass.Methods [0].Name);
90+
}
91+
8392
[Test]
8493
public void CreateParameter_EnsureValidName ()
8594
{

0 commit comments

Comments
 (0)