Skip to content

Commit 8f3fe62

Browse files
authored
[generator] Fix an NRE when cloning a method with generic arguments (#1089)
Context: #1080 Context: 9e0a469 When building an updated `com.google.firebase.firebase-components.csproj` in xamarin/GooglePlayServicesComponents using a *post*- 9e0a469 `generator`, the build hits an NullReferenceException when attempting to `Method.Clone (…)` a method with generic arguments: error BG0000: System.NullReferenceException: Object reference not set to an instance of an object. at MonoDroid.Generation.Method.Clone(GenBase declaringType) in C:\code\xamarin-android\external\Java.Interop\tools\generator\Java.Interop.Tools.Generator.ObjectModel\Method.cs:line 131 at MonoDroid.Generation.Method.Clone(GenBase declaringType) in C:\code\xamarin-android\external\Java.Interop\tools\generator\Java.Interop.Tools.Generator.ObjectModel\Method.cs:line 131 at MonoDroid.Generation.ClassGen.FixupAccessModifiers(CodeGenerationOptions opt) in C:\code\xamarin-android\external\Java.Interop\tools\generator\Java.Interop.Tools.Generator.ObjectModel\ClassGen.cs:line 67 at MonoDroid.Generation.ClassGen.FixupAccessModifiers(CodeGenerationOptions opt) in C:\code\xamarin-android\external\Java.Interop\tools\generator\Java.Interop.Tools.Generator.ObjectModel\ClassGen.cs:line 67 at Xamarin.Android.Binder.CodeGenerator.Validate(List`1 gens, CodeGenerationOptions opt, CodeGeneratorContext context) in C:\code\xamarin-android\external\Java.Interop\tools\generator\CodeGenerator.cs:line 300 at Xamarin.Android.Binder.CodeGenerator.Validate(List`1 gens, CodeGenerationOptions opt, CodeGeneratorContext context) in C:\code\xamarin-android\external\Java.Interop\tools\generator\CodeGenerator.cs:line 300 at Xamarin.Android.Binder.CodeGenerator.Run(CodeGeneratorOptions options, DirectoryAssemblyResolver resolver) in C:\code\xamarin-android\external\Java.Interop\tools\generator\CodeGenerator.cs:line 208 at Xamarin.Android.Binder.CodeGenerator.Run(CodeGeneratorOptions options, DirectoryAssemblyResolver resolver) in C:\code\xamarin-android\external\Java.Interop\tools\generator\CodeGenerator.cs:line 208 at Xamarin.Android.Binder.CodeGenerator.Run(CodeGeneratorOptions options) in C:\code\xamarin-android\external\Java.Interop\tools\generator\CodeGenerator.cs:line 50 at Xamarin.Android.Binder.CodeGenerator.Run(CodeGeneratorOptions options) in C:\code\xamarin-android\external\Java.Interop\tools\generator\CodeGenerator.cs:line 50 at Xamarin.Android.Binder.CodeGenerator.Main(String[] args) in C:\code\xamarin-android\external\Java.Interop\tools\generator\CodeGenerator.cs:line 33 at Xamarin.Android.Binder.CodeGenerator.Main(String[] args) in C:\code\xamarin-android\external\Java.Interop\tools\generator\CodeGenerator.cs:line 33 This is because the `MethodBase.GenericArguments` collection is `null` unless it has items. Thus we need to create it before populating it with items in `Method.Clone(GenBase)`.
1 parent 53bfb4a commit 8f3fe62

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

tests/generator-Tests/Unit-Tests/GenBaseTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,22 @@ public void TestEqualsMethodsWithOneParameter ()
9191
Assert.False (c.RequiresNew (m.Name, m));
9292
}
9393

94+
[Test]
95+
public void TestMethodClone_GenericArguments ()
96+
{
97+
var c = SupportTypeBuilder.CreateClass ("java.myClass", options);
98+
var m = SupportTypeBuilder.CreateMethod (c, "DoStuff", options);
99+
100+
m.GenericArguments = new GenericParameterDefinitionList {
101+
new GenericParameterDefinition ("T", null)
102+
};
103+
104+
var clone = m.Clone (c);
105+
106+
Assert.AreEqual (1, clone.GenericArguments.Count);
107+
Assert.AreEqual ("T", clone.GenericArguments [0].Name);
108+
}
109+
94110
void TestParameterlessMethods (string name)
95111
{
96112
var c = SupportTypeBuilder.CreateClass ("java.myClass", options);

tools/generator/Java.Interop.Tools.Generator.ObjectModel/Method.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,13 @@ public Method Clone (GenBase declaringType)
126126
clone.SourceFile = SourceFile;
127127
clone.JavadocInfo = JavadocInfo;
128128

129-
if (GenericArguments != null)
129+
if (GenericArguments != null) {
130+
if (clone.GenericArguments is null)
131+
clone.GenericArguments = new GenericParameterDefinitionList ();
132+
130133
foreach (var ga in GenericArguments)
131134
clone.GenericArguments.Add (ga.Clone ());
135+
}
132136

133137
foreach (var p in Parameters)
134138
clone.Parameters.Add (p.Clone ());

0 commit comments

Comments
 (0)