From 445748da66b1d78e23eb0024dd0f8d24dacbfafd Mon Sep 17 00:00:00 2001 From: Jonathan Pobst Date: Tue, 23 Jun 2020 13:57:04 -0500 Subject: [PATCH] [generator] Mark protected types nested in sealed classes as private. --- .../Unit-Tests/SealedProtectedFixupsTests.cs | 15 +++++++++++++++ .../GenBase.cs | 5 ++++- .../SealedProtectedFixups.cs | 3 +++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/tests/generator-Tests/Unit-Tests/SealedProtectedFixupsTests.cs b/tests/generator-Tests/Unit-Tests/SealedProtectedFixupsTests.cs index 91d3b499c..5b9897d0c 100644 --- a/tests/generator-Tests/Unit-Tests/SealedProtectedFixupsTests.cs +++ b/tests/generator-Tests/Unit-Tests/SealedProtectedFixupsTests.cs @@ -68,6 +68,21 @@ public void FixProtectedField () Assert.AreEqual ("private", field.Visibility); } + [Test] + public void FixProtectedType () + { + var klass = CreateSealedClass (); + + var type = SupportTypeBuilder.CreateClass ("my.example.class.inner", options); + type.Visibility = "protected"; + + klass.NestedTypes.Add (type); + + SealedProtectedFixups.Fixup (new [] { (GenBase) klass }.ToList ()); + + Assert.AreEqual ("private", type.Visibility); + } + private ClassGen CreateSealedClass () { var klass = SupportTypeBuilder.CreateClass ("my.example.class", options); diff --git a/tools/generator/Java.Interop.Tools.Generator.ObjectModel/GenBase.cs b/tools/generator/Java.Interop.Tools.Generator.ObjectModel/GenBase.cs index 255a37e91..c971ede66 100644 --- a/tools/generator/Java.Interop.Tools.Generator.ObjectModel/GenBase.cs +++ b/tools/generator/Java.Interop.Tools.Generator.ObjectModel/GenBase.cs @@ -867,6 +867,9 @@ public bool Validate (CodeGenerationOptions opt, GenericParameterDefinitionList bool ValidateMethod (CodeGenerationOptions opt, Method m, CodeGeneratorContext context) => m.Validate (opt, TypeParameters, context); - public string Visibility => string.IsNullOrEmpty (support.Visibility) ? "public" : support.Visibility; + public string Visibility { + get => string.IsNullOrEmpty (support.Visibility) ? "public" : support.Visibility; + set => support.Visibility = value; + } } } diff --git a/tools/generator/Java.Interop.Tools.Generator.Transformation/SealedProtectedFixups.cs b/tools/generator/Java.Interop.Tools.Generator.Transformation/SealedProtectedFixups.cs index 6933a729d..d0454ecc7 100644 --- a/tools/generator/Java.Interop.Tools.Generator.Transformation/SealedProtectedFixups.cs +++ b/tools/generator/Java.Interop.Tools.Generator.Transformation/SealedProtectedFixups.cs @@ -21,6 +21,9 @@ public static void Fixup (List gens) foreach (var p in c.Fields.Where (f => f.Visibility == "protected")) p.Visibility = "private"; + + foreach (var p in c.NestedTypes.Where (t => t.Visibility == "protected")) + p.Visibility = "private"; } } }