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 @@ -106,8 +106,8 @@ public string[] PostCall (CodeGenerationOptions opt, string var_name)
{
string native_name = opt.GetSafeIdentifier (TypeNameUtilities.GetNativeName (var_name));
string[] result = new string [4];
result [0] = String.Format ("if ({0} != null) {{", var_name);
result [1] = String.Format ("\tJNIEnv.CopyArray ({0}, {1});", native_name, var_name);
result [0] = String.Format ("if ({0} != null) {{", opt.GetSafeIdentifier (var_name));
Copy link
Contributor

Choose a reason for hiding this comment

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

Presumably the caller of PostCall() is also using opt.GetSafeIdentifier() -- something somewhere else must be, because this code is just the error checking code! -- so would it instead be better if we called opt.GetSafeIdentifier() once and passed that value around to all the methods that needed it, instead of duplicating opt.GetSafeIdentifier() invocations?

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 think that is a much trickier and dangerous change to get correct. For example the previous line is:

string native_name = opt.GetSafeIdentifier (TypeNameUtilities.GetNativeName (var_name));

If we're passing in a var_name that has already been mangled then getting the native name from it will probably need to be reworked, etc.

result [1] = String.Format ("\tJNIEnv.CopyArray ({0}, {1});", native_name, opt.GetSafeIdentifier (var_name));
result [2] = String.Format ("\tJNIEnv.DeleteLocalRef ({0});", native_name);
result [3] = "}";
return result;
Expand Down
17 changes: 17 additions & 0 deletions tools/generator/Tests/Unit-Tests/CodeGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -862,5 +862,22 @@ public void WriteMethodWithInvalidJavaName ()
Assert.False (result.Contains ("cb_has-hyp$hen"));
Assert.True (result.Contains ("cb_has_x45_hyp_x36_hen"));
}

[Test]
public void WriteMethodWithInvalidParameterName ()
{
var @class = new TestClass ("java.lang.Object", "com.mypackage.foo");
var method = new TestMethod (@class, "DoStuff");

method.Parameters.Add (new Parameter ("$this", "byte[]", "byte[]", false));

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 dollar signs
Assert.False (result.Contains ("$this"));
}
}
}