Skip to content

Commit b55fa21

Browse files
atsushienojonpryor
authored andcommitted
[generator] Emit [IntDefinition (JniField = ...)] to enum members (#127)
It will be helpful when people read binding dlls and understand the enum conversion, as well as tooling.
1 parent d4853e2 commit b55fa21

File tree

6 files changed

+30
-2
lines changed

6 files changed

+30
-2
lines changed

tools/generator/EnumMappings.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,7 @@ internal List<string> WriteEnumerations (string output_dir, Dictionary<string, E
152152

153153
foreach (var member in enu.Value.Members) {
154154
var managedMember = FindManagedMember (ns, enoom, enu.Value, member.Key, gens);
155-
if (managedMember != null)
156-
sw.WriteLine (" [global::Android.Runtime.IntDefinition (\"" + managedMember + "\")]");
155+
sw.WriteLine (" [global::Android.Runtime.IntDefinition (" + (managedMember != null? "\"" + managedMember + "\"" : "null") + ", JniField = \"" + StripExtraInterfaceSpec (enu.Value.JniNames [member.Key]) + "\")]");
157156
sw.WriteLine (" {0} = {1},", member.Key.Trim (), member.Value.Trim ());
158157
}
159158
sw.WriteLine (" }");
@@ -163,6 +162,11 @@ internal List<string> WriteEnumerations (string output_dir, Dictionary<string, E
163162
return files;
164163
}
165164

165+
string StripExtraInterfaceSpec (string jniFieldSpec)
166+
{
167+
return jniFieldSpec.StartsWith ("I:", StringComparison.Ordinal) ? jniFieldSpec.Substring (2) : jniFieldSpec;
168+
}
169+
166170
string FindManagedMember (string ns, string enumName, EnumDescription desc, string enumFieldName, IEnumerable<GenBase> gens)
167171
{
168172
if (desc.FieldsRemoved)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace Android.Text {
22
public enum SpanTypes {
3+
[global::Android.Runtime.IntDefinition (null, JniField = "android/text/Spanned.SPAN_COMPOSING")]
34
Composing = 256,
45
}
56
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace Android.Text {
22
public enum SpanTypes {
3+
[global::Android.Runtime.IntDefinition (null, JniField = "android/text/Spanned.SPAN_COMPOSING")]
34
Composing = 256,
45
}
56
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
3+
namespace Android.Runtime
4+
{
5+
[AttributeUsage (AttributeTargets.Field)]
6+
public class IntDefinitionAttribute : Attribute
7+
{
8+
public IntDefinitionAttribute (string constantMember)
9+
{
10+
ConstantMember = constantMember;
11+
}
12+
13+
public string ConstantMember { get; set; }
14+
public string JniField { get; set; }
15+
}
16+
}
17+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
namespace Xamarin.Test {
22
public enum SomeValues {
3+
[global::Android.Runtime.IntDefinition (null, JniField = "xamarin/test/SomeObject.SOME_VALUE")]
34
SomeValue = 0,
5+
[global::Android.Runtime.IntDefinition (null, JniField = "xamarin/test/SomeObject.SOME_VALUE2")]
46
SomeValue2 = 1,
57
}
68
}

tools/generator/Tests/generator-Tests.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@
101101
<Content Include="SupportFiles\IJavaObject.cs">
102102
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
103103
</Content>
104+
<Content Include="SupportFiles\IntDefinitionAttribute.cs">
105+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
106+
</Content>
104107
<Content Include="SupportFiles\JavaArray.cs">
105108
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
106109
</Content>

0 commit comments

Comments
 (0)