Skip to content

Commit

Permalink
[generator] Fix an NRE when cloning a method with generic arguments (#…
Browse files Browse the repository at this point in the history
…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)`.
  • Loading branch information
jpobst authored Mar 30, 2023
1 parent 53bfb4a commit 8f3fe62
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
16 changes: 16 additions & 0 deletions tests/generator-Tests/Unit-Tests/GenBaseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,22 @@ public void TestEqualsMethodsWithOneParameter ()
Assert.False (c.RequiresNew (m.Name, m));
}

[Test]
public void TestMethodClone_GenericArguments ()
{
var c = SupportTypeBuilder.CreateClass ("java.myClass", options);
var m = SupportTypeBuilder.CreateMethod (c, "DoStuff", options);

m.GenericArguments = new GenericParameterDefinitionList {
new GenericParameterDefinition ("T", null)
};

var clone = m.Clone (c);

Assert.AreEqual (1, clone.GenericArguments.Count);
Assert.AreEqual ("T", clone.GenericArguments [0].Name);
}

void TestParameterlessMethods (string name)
{
var c = SupportTypeBuilder.CreateClass ("java.myClass", options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,13 @@ public Method Clone (GenBase declaringType)
clone.SourceFile = SourceFile;
clone.JavadocInfo = JavadocInfo;

if (GenericArguments != null)
if (GenericArguments != null) {
if (clone.GenericArguments is null)
clone.GenericArguments = new GenericParameterDefinitionList ();

foreach (var ga in GenericArguments)
clone.GenericArguments.Add (ga.Clone ());
}

foreach (var p in Parameters)
clone.Parameters.Add (p.Clone ());
Expand Down

0 comments on commit 8f3fe62

Please sign in to comment.