From a8653a361b1c7b054ff5ebd61b31c98ed11dcc04 Mon Sep 17 00:00:00 2001 From: Jonathan Pryor Date: Sat, 17 Aug 2019 09:23:28 -0400 Subject: [PATCH] [Xamarin.Android.Tools.Bytecode] Allow null enclosing method Context: https://github.com/xamarin/xamarin-android/pull/3504 Context: https://devdiv.visualstudio.com/DevDiv/_build/results?buildId=2951424&view=ms.vss-test-web.build-test-results-tab&runId=7990780&resultId=100107&paneView=attachments The attempt to bump the Java.Interop submodule within xamarin-android hit a snag, as unit tests failed: The "ClassParse" task failed unexpectedly. System.NullReferenceException: Object reference not set to an instance of an object at Xamarin.Android.Tools.Bytecode.ClassFile.TryGetEnclosingMethodInfo (System.String& declaringClass, System.String& declaringMethod, System.String& declaringDescriptor) at Xamarin.Android.Tools.Bytecode.XmlClassDeclarationBuilder.GetEnclosingMethod () at Xamarin.Android.Tools.Bytecode.XmlClassDeclarationBuilder.ToXElement () at Xamarin.Android.Tools.Bytecode.ClassPath+<>c.b__36_3 (Xamarin.Android.Tools.Bytecode.ClassFile c) at System.Linq.Enumerable+SelectIPartitionIterator`2[TSource,TResult].MoveNext () at System.Xml.Linq.XContainer.AddContentSkipNotify (System.Object content) at System.Xml.Linq.XContainer.AddContentSkipNotify (System.Object content) The cause? The [`material-menu`][0] library has `.class` files which have an `EnclosingMethod` attribute blob, but doesn't mention method name information, only an enclosing type: $ mono class-parse.exe com/balysv/material/drawable/menu/MaterialMenuDrawable\$5.class --dump ... 1: EnclosingMethod(Class(nameIndex=58 Name="com/balysv/material/drawable/menu/MaterialMenuDrawable"), ) # Behold! No method information! Support generating an XML description of this type by adding appropriate `null` checks around `EnclosingMethodAttribute`. [0]: https://repo.jfrog.org/artifactory/libs-release-bintray/com/balysv/material-menu/1.1.0/material-menu-1.1.0.aar --- src/Xamarin.Android.Tools.Bytecode/ClassFile.cs | 4 ++-- .../XmlClassDeclarationBuilder.cs | 15 ++++++++------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/Xamarin.Android.Tools.Bytecode/ClassFile.cs b/src/Xamarin.Android.Tools.Bytecode/ClassFile.cs index 1e50293e8..6968509e7 100644 --- a/src/Xamarin.Android.Tools.Bytecode/ClassFile.cs +++ b/src/Xamarin.Android.Tools.Bytecode/ClassFile.cs @@ -115,8 +115,8 @@ public bool TryGetEnclosingMethodInfo (out string declaringClass, out string dec } declaringClass = enclosingMethod.Class.Name.Value; - declaringMethod = enclosingMethod.Method.Name.Value; - declaringDescriptor = enclosingMethod.Method.Descriptor.Value; + declaringMethod = enclosingMethod.Method?.Name.Value; + declaringDescriptor = enclosingMethod.Method?.Descriptor.Value; return true; } diff --git a/src/Xamarin.Android.Tools.Bytecode/XmlClassDeclarationBuilder.cs b/src/Xamarin.Android.Tools.Bytecode/XmlClassDeclarationBuilder.cs index a9626a5ea..4d41072a2 100644 --- a/src/Xamarin.Android.Tools.Bytecode/XmlClassDeclarationBuilder.cs +++ b/src/Xamarin.Android.Tools.Bytecode/XmlClassDeclarationBuilder.cs @@ -65,17 +65,18 @@ static string GetDeprecatedValue (AttributeCollection attributes) return "deprecated"; } - XAttribute[] GetEnclosingMethod () + IEnumerable GetEnclosingMethod () { string declaringClass, declaringMethod, declaringDescriptor; if (!classFile.TryGetEnclosingMethodInfo (out declaringClass, out declaringMethod, out declaringDescriptor)) { - return null; + yield break; } - return new []{ - new XAttribute ("enclosing-method-jni-type", "L" + declaringClass + ";"), - new XAttribute ("enclosing-method-name", declaringMethod), - new XAttribute ("enclosing-method-signature", declaringDescriptor), - }; + if (declaringClass != null) + yield return new XAttribute ("enclosing-method-jni-type", "L" + declaringClass + ";"); + if (declaringMethod != null) + yield return new XAttribute ("enclosing-method-name", declaringMethod); + if (declaringDescriptor != null) + yield return new XAttribute ("enclosing-method-signature", declaringDescriptor); } XAttribute[] GetExtends ()