From f639e50b5cc1c20308cf72d85f18c534fa7fce31 Mon Sep 17 00:00:00 2001 From: Jonathan Pobst Date: Mon, 8 Jun 2020 10:22:26 -0500 Subject: [PATCH] [generator] Don't invalidate interface if we invalidate a static method on it. --- .../DefaultInterfaceMethodsTests.cs | 35 +++++++++++++++++++ .../GenBase.cs | 6 ++-- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/tests/generator-Tests/Unit-Tests/DefaultInterfaceMethodsTests.cs b/tests/generator-Tests/Unit-Tests/DefaultInterfaceMethodsTests.cs index d7b4f82db..25f737f5c 100644 --- a/tests/generator-Tests/Unit-Tests/DefaultInterfaceMethodsTests.cs +++ b/tests/generator-Tests/Unit-Tests/DefaultInterfaceMethodsTests.cs @@ -396,5 +396,40 @@ public void RespectNoAlternativesForInterfaces () Assert.False (writer.ToString ().Contains ("class ParentConsts")); Assert.False (writer.ToString ().Contains ("class Parent")); } + + [Test] + public void DontInvalidateInterfaceDueToStaticOrDefaultMethods () + { + // This interface contains a static and a default interface method that cannot + // be bound due to an unknown return type. However the user doesn't have to + // provide an implementation for these methods, so it's ok to bind the interface. + + var xml = @" + + + + + + + + "; + + var gens = ParseApiDefinition (xml); + var iface = gens.OfType ().Single (); + + var result = iface.Validate (options, new GenericParameterDefinitionList (), new CodeGeneratorContext ()); + + // Inteface should pass validation despite invalid static/default methods + Assert.True (result); + + generator.WriteInterface (iface, string.Empty, new GenerationInfo (string.Empty, string.Empty, "MyAssembly")); + + var generated = writer.ToString (); + + Assert.True (generated.Contains ("interface IParent")); + Assert.True (generated.Contains ("NormalMethod")); + Assert.False (generated.Contains ("StaticMethod")); + Assert.False (generated.Contains ("DefaultMethod")); + } } } diff --git a/tools/generator/Java.Interop.Tools.Generator.ObjectModel/GenBase.cs b/tools/generator/Java.Interop.Tools.Generator.ObjectModel/GenBase.cs index 95ce07828..255a37e91 100644 --- a/tools/generator/Java.Interop.Tools.Generator.ObjectModel/GenBase.cs +++ b/tools/generator/Java.Interop.Tools.Generator.ObjectModel/GenBase.cs @@ -677,11 +677,11 @@ protected virtual bool OnValidate (CodeGenerationOptions opt, GenericParameterDe } Fields = valid_fields; - // If we can't validate a default interface method it's ok to ignore it and still bind the interface - var method_cnt = Methods.Where (m => !m.IsInterfaceDefaultMethod).Count (); + // If we can't validate a static or default interface method it's ok to ignore it and still bind the interface + var method_cnt = Methods.Where (m => !m.IsInterfaceDefaultMethod && !m.IsStatic).Count (); Methods = Methods.Where (m => ValidateMethod (opt, m, context)).ToList (); - MethodValidationFailed = method_cnt != Methods.Where (m => !m.IsInterfaceDefaultMethod).Count (); + MethodValidationFailed = method_cnt != Methods.Where (m => !m.IsInterfaceDefaultMethod && !m.IsStatic).Count (); foreach (Method m in Methods) { if (m.IsVirtual)