Skip to content

Commit 7228af0

Browse files
authored
[Xamarin.Android.Tools.Bytecode] Allow null enclosing method (#479)
Context: dotnet/android#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.<ToXElement>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
1 parent 39a3b87 commit 7228af0

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

src/Xamarin.Android.Tools.Bytecode/ClassFile.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ public bool TryGetEnclosingMethodInfo (out string declaringClass, out string dec
115115
}
116116

117117
declaringClass = enclosingMethod.Class.Name.Value;
118-
declaringMethod = enclosingMethod.Method.Name.Value;
119-
declaringDescriptor = enclosingMethod.Method.Descriptor.Value;
118+
declaringMethod = enclosingMethod.Method?.Name.Value;
119+
declaringDescriptor = enclosingMethod.Method?.Descriptor.Value;
120120
return true;
121121
}
122122

src/Xamarin.Android.Tools.Bytecode/XmlClassDeclarationBuilder.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,18 @@ static string GetDeprecatedValue (AttributeCollection attributes)
6565
return "deprecated";
6666
}
6767

68-
XAttribute[] GetEnclosingMethod ()
68+
IEnumerable<XAttribute> GetEnclosingMethod ()
6969
{
7070
string declaringClass, declaringMethod, declaringDescriptor;
7171
if (!classFile.TryGetEnclosingMethodInfo (out declaringClass, out declaringMethod, out declaringDescriptor)) {
72-
return null;
72+
yield break;
7373
}
74-
return new []{
75-
new XAttribute ("enclosing-method-jni-type", "L" + declaringClass + ";"),
76-
new XAttribute ("enclosing-method-name", declaringMethod),
77-
new XAttribute ("enclosing-method-signature", declaringDescriptor),
78-
};
74+
if (declaringClass != null)
75+
yield return new XAttribute ("enclosing-method-jni-type", "L" + declaringClass + ";");
76+
if (declaringMethod != null)
77+
yield return new XAttribute ("enclosing-method-name", declaringMethod);
78+
if (declaringDescriptor != null)
79+
yield return new XAttribute ("enclosing-method-signature", declaringDescriptor);
7980
}
8081

8182
XAttribute[] GetExtends ()

0 commit comments

Comments
 (0)