Skip to content

Commit f5d29c3

Browse files
jpobstjonpryor
authored andcommitted
[generator] Ensure all usage of parameter names are escaped the same (#506)
If a method parameter contains a dollar sign, `generator` sometimes escapes it properly and sometimes it does not: public unsafe global::Okio.ByteString Of (byte[] _this_toByteString_, int offset, int byteCount) { // ... if ($this$toByteString != null) { JNIEnv.CopyArray (native__this_toByteString_, $this$toByteString); JNIEnv.DeleteLocalRef (native__this_toByteString_); } That contains both `_this_toByteString_` and `$this$toByteString`. Ensures all instances use the same `opt.GetSafeIdentifier(string)` escaping method so that `$` doesn't remain within generated sources.
1 parent 17cdf54 commit f5d29c3

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

tools/generator/Java.Interop.Tools.Generator.ObjectModel/Symbols/ArraySymbol.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ public string[] PostCall (CodeGenerationOptions opt, string var_name)
106106
{
107107
string native_name = opt.GetSafeIdentifier (TypeNameUtilities.GetNativeName (var_name));
108108
string[] result = new string [4];
109-
result [0] = String.Format ("if ({0} != null) {{", var_name);
110-
result [1] = String.Format ("\tJNIEnv.CopyArray ({0}, {1});", native_name, var_name);
109+
result [0] = String.Format ("if ({0} != null) {{", opt.GetSafeIdentifier (var_name));
110+
result [1] = String.Format ("\tJNIEnv.CopyArray ({0}, {1});", native_name, opt.GetSafeIdentifier (var_name));
111111
result [2] = String.Format ("\tJNIEnv.DeleteLocalRef ({0});", native_name);
112112
result [3] = "}";
113113
return result;

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -862,5 +862,22 @@ public void WriteMethodWithInvalidJavaName ()
862862
Assert.False (result.Contains ("cb_has-hyp$hen"));
863863
Assert.True (result.Contains ("cb_has_x45_hyp_x36_hen"));
864864
}
865+
866+
[Test]
867+
public void WriteMethodWithInvalidParameterName ()
868+
{
869+
var @class = new TestClass ("java.lang.Object", "com.mypackage.foo");
870+
var method = new TestMethod (@class, "DoStuff");
871+
872+
method.Parameters.Add (new Parameter ("$this", "byte[]", "byte[]", false));
873+
874+
Assert.IsTrue (method.Validate (options, new GenericParameterDefinitionList (), new CodeGeneratorContext ()), "method.Validate failed!");
875+
generator.WriteMethod (method, string.Empty, @class, true);
876+
877+
var result = writer.ToString ().NormalizeLineEndings ();
878+
879+
// Ensure we escape dollar signs
880+
Assert.False (result.Contains ("$this"));
881+
}
865882
}
866883
}

0 commit comments

Comments
 (0)