Skip to content

Commit fd2c94e

Browse files
authored
[generator] Mark protected types nested in sealed classes as private (#672)
In commit 47201c4 we marked `protected` members in `sealed` classes as `private` in order to prevent `csc` warnings. However, we did address nested types which can also cause this warning: sealed class Foo { protected interface Bar { } protected class Bar2 { } } which results in CS0628 warnings: warning CS0628: 'Foo.Bar': new protected member declared in sealed class warning CS0628: 'Foo.Bar2': new protected member declared in sealed class `Mono.Android.dll` in API-30 added 28 new protected interfaces in the `sealed` classes `CalendarContract` and `ContactContract`, which introduced new warnings in our builds: obj\Release\monoandroid10\android-30\mcw\Android.Provider.CalendarContract.cs(614,40): warning CS0628: 'CalendarContract.IAttendeesColumns': new protected member declared in sealed class Avoid the CS0628 warnings by marking nested types as `private` if the parent class is `sealed`.
1 parent 11b7f85 commit fd2c94e

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,21 @@ public void FixProtectedField ()
6868
Assert.AreEqual ("private", field.Visibility);
6969
}
7070

71+
[Test]
72+
public void FixProtectedType ()
73+
{
74+
var klass = CreateSealedClass ();
75+
76+
var type = SupportTypeBuilder.CreateClass ("my.example.class.inner", options);
77+
type.Visibility = "protected";
78+
79+
klass.NestedTypes.Add (type);
80+
81+
SealedProtectedFixups.Fixup (new [] { (GenBase) klass }.ToList ());
82+
83+
Assert.AreEqual ("private", type.Visibility);
84+
}
85+
7186
private ClassGen CreateSealedClass ()
7287
{
7388
var klass = SupportTypeBuilder.CreateClass ("my.example.class", options);

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,9 @@ public bool Validate (CodeGenerationOptions opt, GenericParameterDefinitionList
867867
bool ValidateMethod (CodeGenerationOptions opt, Method m, CodeGeneratorContext context) =>
868868
m.Validate (opt, TypeParameters, context);
869869

870-
public string Visibility => string.IsNullOrEmpty (support.Visibility) ? "public" : support.Visibility;
870+
public string Visibility {
871+
get => string.IsNullOrEmpty (support.Visibility) ? "public" : support.Visibility;
872+
set => support.Visibility = value;
873+
}
871874
}
872875
}

tools/generator/Java.Interop.Tools.Generator.Transformation/SealedProtectedFixups.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ public static void Fixup (List<GenBase> gens)
2121

2222
foreach (var p in c.Fields.Where (f => f.Visibility == "protected"))
2323
p.Visibility = "private";
24+
25+
foreach (var p in c.NestedTypes.Where (t => t.Visibility == "protected"))
26+
p.Visibility = "private";
2427
}
2528
}
2629
}

0 commit comments

Comments
 (0)