Skip to content

Commit b25dd1b

Browse files
atsushienojonpryor
authored andcommitted
[generator] make sure to escape non-generic parameter name for calls. (#99)
Fixes: https://bugzilla.xamarin.com/show_bug.cgi?id=46454 `Parameter.ToNative()` was not escaping C# identifiers, and then it sometimes resulted in invalid C# code. It can be easily worked around by metadata fixup, but also fixable.
1 parent 46c4dc2 commit b25dd1b

File tree

9 files changed

+406
-187
lines changed

9 files changed

+406
-187
lines changed

tools/generator/Parameter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ public string GetCall (CodeGenerationOptions opt)
4646

4747
public string ToNative (CodeGenerationOptions opt)
4848
{
49-
return NeedsPrep ? sym.Call (opt, Name) : sym.ToNative (opt, Name, null);
49+
var safeName = opt.GetSafeIdentifier (Name);
50+
return NeedsPrep ? sym.Call (opt, safeName) : sym.ToNative (opt, safeName, null);
5051
}
5152

5253
public string GenericType {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using NUnit.Framework;
3+
4+
namespace generatortests
5+
{
6+
[TestFixture]
7+
public class CSharpKeywords : BaseGeneratorTest
8+
{
9+
[Test]
10+
public void GeneratedOK ()
11+
{
12+
RunAllTargets (
13+
outputRelativePath: "CSharpKeywords",
14+
apiDescriptionFile: "expected/CSharpKeywords/CSharpKeywords.xml",
15+
expectedRelativePath: "CSharpKeywords");
16+
}
17+
}
18+
}
19+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<api>
3+
<package name="java.lang">
4+
<class abstract="false" deprecated="not deprecated" final="false" name="Object" static="false" visibility="public">
5+
</class>
6+
<class abstract="false" deprecated="not deprecated" extends="java.lang.Object" final="false" name="Throwable" static="false" visibility="public">
7+
<method abstract="false" deprecated="not deprecated" final="false" name="getMessage" native="false" return="java.lang.String" static="false" synchronized="false" visibility="public">
8+
</method>
9+
</class>
10+
</package>
11+
<package name="xamarin.test">
12+
<class abstract="false" deprecated="not deprecated" final="false" name="CSharpKeywords" static="false" visibility="public">
13+
<method abstract="false" deprecated="not deprecated" final="false" name="usePartial" native="false" return="java.lang.String" static="false" synchronized="false" visibility="public">
14+
<parameter name="partial" type="int">
15+
</parameter>
16+
</method>
17+
</class>
18+
</package>
19+
</api>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<DefineConstants>$(DefineConstants);ANDROID_1;ANDROID_2;ANDROID_3;ANDROID_4</DefineConstants>
5+
</PropertyGroup>
6+
<!-- Classes -->
7+
<ItemGroup>
8+
<Compile Include="$(MSBuildThisFileDirectory)\Java.Interop.__TypeRegistrations.cs" />
9+
<Compile Include="$(MSBuildThisFileDirectory)\Java.Lang.Object.cs" />
10+
<Compile Include="$(MSBuildThisFileDirectory)\Java.Lang.Throwable.cs" />
11+
<Compile Include="$(MSBuildThisFileDirectory)\Xamarin.Test.CSharpKeywords.cs" />
12+
<Compile Include="$(MSBuildThisFileDirectory)\__NamespaceMapping__.cs" />
13+
</ItemGroup>
14+
<!-- Enums -->
15+
<ItemGroup />
16+
</Project>
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Android.Runtime;
4+
using Java.Interop;
5+
6+
namespace Xamarin.Test {
7+
8+
// Metadata.xml XPath class reference: path="/api/package[@name='xamarin.test']/class[@name='CSharpKeywords']"
9+
[global::Android.Runtime.Register ("xamarin/test/CSharpKeywords", DoNotGenerateAcw=true)]
10+
public partial class CSharpKeywords : global::Java.Lang.Object {
11+
12+
internal static readonly JniPeerMembers _members = new JniPeerMembers ("xamarin/test/CSharpKeywords", typeof (CSharpKeywords));
13+
internal static IntPtr class_ref {
14+
get {
15+
return _members.JniPeerType.PeerReference.Handle;
16+
}
17+
}
18+
19+
public override global::Java.Interop.JniPeerMembers JniPeerMembers {
20+
get { return _members; }
21+
}
22+
23+
protected override IntPtr ThresholdClass {
24+
get { return _members.JniPeerType.PeerReference.Handle; }
25+
}
26+
27+
protected override global::System.Type ThresholdType {
28+
get { return _members.ManagedPeerType; }
29+
}
30+
31+
protected CSharpKeywords (IntPtr javaReference, JniHandleOwnership transfer) : base (javaReference, transfer) {}
32+
33+
static Delegate cb_usePartial_I;
34+
#pragma warning disable 0169
35+
static Delegate GetUsePartial_IHandler ()
36+
{
37+
if (cb_usePartial_I == null)
38+
cb_usePartial_I = JNINativeWrapper.CreateDelegate ((Func<IntPtr, IntPtr, int, IntPtr>) n_UsePartial_I);
39+
return cb_usePartial_I;
40+
}
41+
42+
static IntPtr n_UsePartial_I (IntPtr jnienv, IntPtr native__this, int partial_)
43+
{
44+
global::Xamarin.Test.CSharpKeywords __this = global::Java.Lang.Object.GetObject<global::Xamarin.Test.CSharpKeywords> (jnienv, native__this, JniHandleOwnership.DoNotTransfer);
45+
return JNIEnv.NewString (__this.UsePartial (partial_));
46+
}
47+
#pragma warning restore 0169
48+
49+
// Metadata.xml XPath method reference: path="/api/package[@name='xamarin.test']/class[@name='CSharpKeywords']/method[@name='usePartial' and count(parameter)=1 and parameter[1][@type='int']]"
50+
[Register ("usePartial", "(I)Ljava/lang/String;", "GetUsePartial_IHandler")]
51+
public virtual unsafe string UsePartial (int partial_)
52+
{
53+
const string __id = "usePartial.(I)Ljava/lang/String;";
54+
try {
55+
JniArgumentValue* __args = stackalloc JniArgumentValue [1];
56+
__args [0] = new JniArgumentValue (partial_);
57+
var __rm = _members.InstanceMethods.InvokeVirtualObjectMethod (__id, this, __args);
58+
return JNIEnv.GetString (__rm.Handle, JniHandleOwnership.TransferLocalRef);
59+
} finally {
60+
}
61+
}
62+
63+
}
64+
}

0 commit comments

Comments
 (0)