Skip to content

Commit 66312f7

Browse files
jonpryorjonathanpeppers
authored andcommitted
[Java.Interop.Tools.JavaCallableWrappers] Improve missing attribute (#1369)
Context: bc44f08 If "something goes wrong" and the `*.jlo.xml` files contain `//jcw-types/type` elements that are *missing* the `//type/@package` attribute, then an exception is thrown: System.InvalidOperationException: Missing required attribute 'package' at Java.Interop.Tools.JavaCallableWrappers.Extensions.XmlExtensions.GetRequiredAttribute(XElement xml, String name) at Java.Interop.Tools.JavaCallableWrappers.Adapters.XmlImporter.ImportType(XElement xml) at Java.Interop.Tools.JavaCallableWrappers.Adapters.XmlImporter.Import(XElement xml) at Xamarin.Android.Tasks.JavaObjectsXmlFile.Import(String filename, JavaObjectsXmlFileReadType readType) at Xamarin.Android.Tasks.GenerateJavaCallableWrappers.GenerateWrappers(List`1 assemblies) at Xamarin.Android.Tasks.GenerateJavaCallableWrappers.RunTask() at Microsoft.Android.Build.Tasks.AndroidTask.Execute() Which is all well and good, except ***I have no idea what's broken***. *Something* within dotnet/android should be updated so that the file causing the exception is mentioned, but we can also improve the `InvalidOperationException` to contain at least a "breadcrumb" for what's going wrong. Update the `InvalidOperationException` to also include the contents of the `XElement.ToString()` which is missing the required attribute. We can then at least search file contents to find the "offending" file. This provides a more useful exception message: System.InvalidOperationException: Missing required attribute 'package' within element `<type name="SKCanvasElementImpl" package="" application_java_class="android.support.multidex.MultiDexApplication" mono_runtime_initialization="mono.MonoPackageManager.LoadApplication (context);" extends_type="Uno.WinUI.Graphics2DSK.SKCanvasElement" partial_assembly_qualified_name="SKCanvasElementImpl, SamplesApp"> <constructors> <constructor name="SKCanvasElementImpl" method="n_.ctor:(Landroid/content/Context;)V:" jni_signature="(Landroid/content/Context;)V" managed_parameters="Android.Content.Context, Mono.Android" params="android.content.Context p0" retval="void" is_dynamically_registered="True" super_call="p0" activate_call="p0" /> </constructors> </type>`. at Java.Interop.Tools.JavaCallableWrappers.Extensions.XmlExtensions.GetRequiredAttribute(XElement xml, String name) at Java.Interop.Tools.JavaCallableWrappers.Adapters.XmlImporter.ImportType(XElement xml) at Java.Interop.Tools.JavaCallableWrappers.Adapters.XmlImporter.Import(XElement xml) at Xamarin.Android.Tasks.JavaObjectsXmlFile.Import(String filename, JavaObjectsXmlFileReadType readType) at Xamarin.Android.Tasks.GenerateJavaCallableWrappers.GenerateWrappers(List`1 assemblies) at Xamarin.Android.Tasks.GenerateJavaCallableWrappers.RunTask() at Microsoft.Android.Build.Tasks.AndroidTask.Execute() Which shows *two* problems: 1. The `package` attribute *is* present, but 2. The `package` attribute is the empty string. Which should be supported! The global package is A Thing That Exists™; it should be supported! (Has *been* supported?!) Update `XmlImporter.ImportType()` so that `package` is *not* required. Update `tests/Java.Interop.Tools.JavaCallableWrappers-Tests` to add an explicit test for a type in the Java global package.
1 parent 0b7b3e9 commit 66312f7

File tree

4 files changed

+11
-3
lines changed

4 files changed

+11
-3
lines changed

src/Java.Interop.Tools.JavaCallableWrappers/Extensions/XmlExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public static string GetRequiredAttribute (this XElement xml, string name)
2121
var value = xml.Attribute (name)?.Value;
2222

2323
if (string.IsNullOrWhiteSpace (value))
24-
throw new InvalidOperationException ($"Missing required attribute '{name}'");
24+
throw new InvalidOperationException ($"Missing required attribute '{name}' within element `{xml.ToString()}`.");
2525

2626
return value!; // NRT - Guarded by IsNullOrWhiteSpace check above
2727
}

src/Java.Interop.Tools.JavaCallableWrappers/Java.Interop.Tools.JavaCallableWrappers.Adapters/XmlImporter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ public static List<CallableWrapperType> Import (XElement xml)
4949
public static CallableWrapperType ImportType (XElement xml)
5050
{
5151
var name = xml.GetRequiredAttribute ("name");
52-
var package = xml.GetRequiredAttribute ("package");
52+
var package = xml.GetAttributeOrDefault ("package", (string?) "");
5353
var partial_assembly_qualified_name = xml.GetRequiredAttribute ("partial_assembly_qualified_name");
5454

55-
var type = new CallableWrapperType (name, package, partial_assembly_qualified_name) {
55+
var type = new CallableWrapperType (name, package ?? "", partial_assembly_qualified_name) {
5656
ApplicationJavaClass = xml.GetAttributeOrDefault ("application_java_class", (string?) null),
5757
ExtendsType = xml.GetAttributeOrDefault ("extends_type", (string?) null),
5858
GenerateOnCreateOverrides = xml.GetAttributeOrDefault ("generate_on_create_overrides", false),

tests/Java.Interop.Tools.JavaCallableWrappers-Tests/Java.Interop.Tools.JavaCallableWrappers/SupportDeclarations.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ static class SupportDeclarations
109109
typeof (ExampleInstrumentation),
110110
typeof (ExampleOuterClass),
111111
typeof (ExampleOuterClass.ExampleInnerClass),
112+
typeof (GlobalClass),
112113
typeof (InstrumentationName),
113114
typeof (NonStaticOuterClass),
114115
typeof (NonStaticOuterClass.NonStaticInnerClass),
@@ -370,3 +371,8 @@ public JavaInteropExample (int a, int b) {}
370371
public void Example () {}
371372
}
372373
}
374+
375+
[Register (nameof (GlobalClass))]
376+
class GlobalClass : Java.Lang.Object
377+
{
378+
}

tests/Java.Interop.Tools.JavaCallableWrappers-Tests/Java.Interop.Tools.JavaCallableWrappers/TypeNameMapGeneratorTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public void WriteJavaToManaged ()
5757
$"entry-count={types.Count - 1}\u0000" +
5858
"entry-len=" + length + "\u0000" +
5959
"value-offset=" + offset + "\u0000" +
60+
GetJ2MEntryLine (typeof (GlobalClass), "GlobalClass", offset, length) +
6061
GetJ2MEntryLine (typeof (ActivityName), "activity/Name", offset, length) +
6162
GetJ2MEntryLine (typeof (ApplicationName), "application/Name", offset, length) +
6263
GetJ2MEntryLine (typeof (ApplicationName.ActivityLifecycleCallbacks), "application/Name_ActivityLifecycleCallbacks", offset, length) +
@@ -136,6 +137,7 @@ public void WriteManagedToJava ()
136137
$"entry-count={types.Count}\u0000" +
137138
"entry-len=" + length + "\u0000" +
138139
"value-offset=" + offset + "\u0000" +
140+
GetM2JEntryLine (typeof (GlobalClass), "GlobalClass", offset, length) +
139141
GetM2JEntryLine (typeof (AbstractClass), "my/AbstractClass", offset, length) +
140142
GetM2JEntryLine (typeof (AbstractClassInvoker), "my/AbstractClass", offset, length) +
141143
GetM2JEntryLine (typeof (ActivityName), "activity/Name", offset, length) +

0 commit comments

Comments
 (0)