Skip to content

Commit 7b56ed3

Browse files
authored
[Xamarin.Android.Tools.ApiXmlAdjuster] Attributes are optional (dotnet#340)
Commit 0881acc [broke][0] the [build][1] via unit test failure: System.Exception : /var/folders/l8/73t5whsx7dj5v5gh_bn7zxlr0000gn/T/tmp28cf44e7.tmp (5,5): Element 'constructor' requires attribute 'jni-signature' at Xamarin.Android.Tools.ApiXmlAdjuster.XmlUtil.GetRequiredAttribute (System.Xml.XmlReader reader, System.String name) at Xamarin.Android.Tools.ApiXmlAdjuster.JavaApiLoaderExtensions.LoadMemberAttributes (Xamarin.Android.Tools.ApiXmlAdjuster.JavaMember member, System.Xml.XmlReader reader) ... This was generated from e.g. `make run-test-generator-core` and processing a "legacy" file in the form of `tools/generator/Tests-Core/api-cp.xml` (whereby "legacy" means "didn't contain the newly required `//constructor/@jni-signature` XML attribute). There are two plausible fixes: 1. Update `api-cp.xml` so that it provides the attribute that 0881acc requires, or 2. Make the attributes *not* required attributes. Since I don't know what the larger ecosystem may be doing with these API XML files, I chose the conservative approach (2). [0]: https://jenkins.mono-project.com/view/Xamarin.Android/job/Java.Interop/261/testReport/junit/generatortests/AdjusterTests/Process/ [1]: https://jenkins.mono-project.com/view/Xamarin.Android/job/Java.Interop/261/
1 parent bac23f9 commit 7b56ed3

File tree

2 files changed

+31
-13
lines changed

2 files changed

+31
-13
lines changed

src/Xamarin.Android.Tools.ApiXmlAdjuster/JavaApiXmlGeneratorExtensions.cs

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ public static void Save (this JavaApi api, XmlWriter writer)
2929
continue;
3030
writer.WriteStartElement ("package");
3131
writer.WriteAttributeString ("name", pkg.Name);
32-
writer.WriteAttributeString ("jni-name", pkg.JniName);
32+
if (!string.IsNullOrEmpty (pkg.JniName)) {
33+
writer.WriteAttributeString ("jni-name", pkg.JniName);
34+
}
3335
foreach (var type in pkg.Types) {
3436
if (type.IsReferenceOnly)
3537
continue; // skip reference only types
@@ -69,13 +71,17 @@ static void SaveTypeCommon (this JavaType cls, XmlWriter writer, string elementN
6971
writer.WriteAttributeString ("name", cls.Name);
7072
writer.WriteAttributeString ("static", XmlConvert.ToString (cls.Static));
7173
writer.WriteAttributeString ("visibility", cls.Visibility);
72-
writer.WriteAttributeString ("jni-signature", cls.ExtendedJniSignature);
73-
74+
if (!string.IsNullOrEmpty (cls.ExtendedJniSignature)) {
75+
writer.WriteAttributeString ("jni-signature", cls.ExtendedJniSignature);
76+
}
77+
7478
foreach (var imp in cls.Implements.OrderBy (i => i.Name, StringComparer.Ordinal)) {
7579
writer.WriteStartElement ("implements");
7680
writer.WriteAttributeString ("name", imp.Name);
7781
writer.WriteAttributeString ("name-generic-aware", imp.NameGeneric);
78-
writer.WriteAttributeString ("jni-type", imp.ExtendedJniType);
82+
if (!string.IsNullOrEmpty (imp.ExtendedJniType)) {
83+
writer.WriteAttributeString ("jni-type", imp.ExtendedJniType);
84+
}
7985
writer.WriteString ("\n ");
8086
writer.WriteFullEndElement ();
8187
}
@@ -99,10 +105,18 @@ static void Save (this JavaTypeParameters typeParameters, XmlWriter writer, stri
99105
foreach (var tp in typeParameters.TypeParameters) {
100106
writer.WriteStartElement ("typeParameter");
101107
writer.WriteAttributeString ("name", tp.Name);
102-
writer.WriteAttributeString ("classBound", tp.ExtendedClassBound);
103-
writer.WriteAttributeString ("jni-classBound", tp.ExtendedJniClassBound);
104-
writer.WriteAttributeString ("interfaceBounds", tp.ExtendedInterfaceBounds);
105-
writer.WriteAttributeString ("jni-interfaceBounds", tp.ExtendedJniInterfaceBounds);
108+
if (!string.IsNullOrEmpty (tp.ExtendedClassBound)) {
109+
writer.WriteAttributeString ("classBound", tp.ExtendedClassBound);
110+
}
111+
if (!string.IsNullOrEmpty (tp.ExtendedJniClassBound)) {
112+
writer.WriteAttributeString ("jni-classBound", tp.ExtendedJniClassBound);
113+
}
114+
if (!string.IsNullOrEmpty (tp.ExtendedInterfaceBounds)) {
115+
writer.WriteAttributeString ("interfaceBounds", tp.ExtendedInterfaceBounds);
116+
}
117+
if (!string.IsNullOrEmpty (tp.ExtendedJniInterfaceBounds)) {
118+
writer.WriteAttributeString ("jni-interfaceBounds", tp.ExtendedJniInterfaceBounds);
119+
}
106120

107121
if (tp.GenericConstraints != null) {
108122
// If there is only one generic constraint that specifies java.lang.Object,
@@ -259,7 +273,9 @@ static void SaveCommon (this JavaMember m, XmlWriter writer, string elementName,
259273
writer.WriteStartElement ("parameter");
260274
writer.WriteAttributeString ("name", p.Name);
261275
writer.WriteAttributeString ("type", p.GetVisibleTypeName ());
262-
writer.WriteAttributeString ("jni-type", p.JniType);
276+
if (!string.IsNullOrEmpty (p.JniType)) {
277+
writer.WriteAttributeString ("jni-type", p.JniType);
278+
}
263279
writer.WriteString ("\n ");
264280
writer.WriteFullEndElement ();
265281
}
@@ -270,7 +286,9 @@ static void SaveCommon (this JavaMember m, XmlWriter writer, string elementName,
270286
writer.WriteStartElement ("exception");
271287
writer.WriteAttributeString ("name", e.Name.Substring (e.Name.LastIndexOf ('/') + 1).Replace ('$', '.'));
272288
writer.WriteAttributeString ("type", e.Type);
273-
writer.WriteAttributeString ("type-generic-aware", e.TypeGenericAware);
289+
if (!string.IsNullOrEmpty (e.TypeGenericAware)) {
290+
writer.WriteAttributeString ("type-generic-aware", e.TypeGenericAware);
291+
}
274292
writer.WriteString ("\n ");
275293
writer.WriteFullEndElement ();
276294
}

src/Xamarin.Android.Tools.ApiXmlAdjuster/JavaApiXmlLoaderExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ public static void LoadMemberAttributes (this JavaMember member, XmlReader reade
204204
member.Name = XmlUtil.GetRequiredAttribute (reader, "name");
205205
member.Static = XmlConvert.ToBoolean (XmlUtil.GetRequiredAttribute (reader, "static"));
206206
member.Visibility = XmlUtil.GetRequiredAttribute (reader, "visibility");
207-
member.ExtendedJniSignature = XmlUtil.GetRequiredAttribute (reader, "jni-signature");
207+
member.ExtendedJniSignature = reader.GetAttribute ("jni-signature");
208208
}
209209

210210
public static void Load (this JavaField field, XmlReader reader)
@@ -283,15 +283,15 @@ internal static void Load (this JavaParameter p, XmlReader reader)
283283
{
284284
p.Name = XmlUtil.GetRequiredAttribute (reader, "name");
285285
p.Type = XmlUtil.GetRequiredAttribute (reader, "type");
286-
p.JniType = XmlUtil.GetRequiredAttribute (reader, "jni-type");
286+
p.JniType = reader.GetAttribute ("jni-type");
287287
reader.Skip ();
288288
}
289289

290290
internal static void Load (this JavaException e, XmlReader reader)
291291
{
292292
e.Name = XmlUtil.GetRequiredAttribute (reader, "name");
293293
e.Type = XmlUtil.GetRequiredAttribute (reader, "type");
294-
e.Type = XmlUtil.GetRequiredAttribute (reader, "type-generic-aware");
294+
e.Type = reader.GetAttribute ("type-generic-aware");
295295
reader.Skip ();
296296
}
297297

0 commit comments

Comments
 (0)