Skip to content

[XAT.Bytecode] Fix issue where Kotlin public types inside internal types were not being hidden. #827

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions src/Xamarin.Android.Tools.Bytecode/Kotlin/KotlinFixups.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static void Fixup (IList<ClassFile> classes)
var class_metadata = (metadata as KotlinClass);

if (class_metadata != null) {
FixupClassVisibility (c, class_metadata);
FixupClassVisibility (c, class_metadata, classes);

if (!c.AccessFlags.IsPubliclyVisible ())
continue;
Expand Down Expand Up @@ -62,7 +62,7 @@ public static void Fixup (IList<ClassFile> classes)
}
}

static void FixupClassVisibility (ClassFile klass, KotlinClass metadata)
static void FixupClassVisibility (ClassFile klass, KotlinClass metadata, IList<ClassFile> classes)
{
// Hide class if it isn't Public/Protected
if (klass.AccessFlags.IsPubliclyVisible () && !metadata.Visibility.IsPubliclyVisible ()) {
Expand All @@ -83,15 +83,33 @@ static void FixupClassVisibility (ClassFile klass, KotlinClass metadata)
Log.Debug ($"Kotlin: Hiding internal class {klass.ThisClass.Name.Value}");
klass.AccessFlags = SetVisibility (klass.AccessFlags, ClassAccessFlags.Private);

foreach (var ic in klass.InnerClasses) {
Log.Debug ($"Kotlin: Hiding nested internal type {ic.InnerClass.Name.Value}");
ic.InnerClassAccessFlags = SetVisibility (ic.InnerClassAccessFlags, ClassAccessFlags.Private);
}
foreach (var ic in klass.InnerClasses)
HideInternalInnerClass (ic, classes);

return;
}
}

static void HideInternalInnerClass (InnerClassInfo klass, IList<ClassFile> classes)
{
var existing = klass.InnerClassAccessFlags;

klass.InnerClassAccessFlags = SetVisibility (existing, ClassAccessFlags.Private);
Log.Debug ($"Kotlin: Hiding nested internal type {klass.InnerClass.Name.Value}");

// Setting the inner class access flags above doesn't technically do anything, because we output
// from the inner class's ClassFile, so we need to find that and set it there.
var kf = classes.FirstOrDefault (c => c.ThisClass.Name.Value == klass.InnerClass.Name.Value);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if classes should become a KeyedCollection<string, ClassFile>? I'm just worried that if classes gets "big", .FirstOrDefault() may become a time sink.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potentially, though this should be a rarely used code path.


if (kf != null) {
kf.AccessFlags = SetVisibility (kf.AccessFlags, ClassAccessFlags.Private);
kf.InnerClass.InnerClassAccessFlags = SetVisibility (kf.InnerClass.InnerClassAccessFlags, ClassAccessFlags.Private);

foreach (var inner_class in kf.InnerClasses.Where (c => c.OuterClass.Name.Value == kf.ThisClass.Name.Value))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this .Where() cause all of kf.InnerClasses to be included? How could kf.InnerClasses contain a type for which the inner type's c.OuterClass doesn't match the containing type?

Wouldn't this more easily & efficiently be expressed as:

foreach (var inner_class in kf.InnerClasses) {
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

…or does kf.InnerClasses contain "grand-children" as well? For example, in the case of HidePublicInterfaceInInternalClass, will InternalClassWithNestedInterface.class specify InternalClassWithNestedInterface$NestedInterface$DoubleNestedInterface.class as an inner class, even though it's an inner class of InternalClassWithNestedInterface$NestedInterface.class?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reason the InnerClasses collection for an inner class contains itself. Without this check it infinitely recurses into itself.

That is, the InnerClasses for InternalClassWithNestedInterface$NestedInterface contains InternalClassWithNestedInterface$NestedInterface.

HideInternalInnerClass (inner_class, classes);
}
}

// Passing null for 'newVisibility' parameter means 'package-private'
static ClassAccessFlags SetVisibility (ClassAccessFlags existing, ClassAccessFlags? newVisibility)
{
Expand Down
19 changes: 19 additions & 0 deletions tests/Xamarin.Android.Tools.Bytecode-Tests/KotlinFixupsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -310,5 +310,24 @@ public void HandleKotlinNameShadowing ()
Assert.True (klass.Methods.Single (m => m.Name == "hitCount").AccessFlags.HasFlag (MethodAccessFlags.Public));
Assert.True (klass.Methods.Single (m => m.Name == "setType").AccessFlags.HasFlag (MethodAccessFlags.Public));
}

[Test]
public void HidePublicInterfaceInInternalClass ()
{
var klass = LoadClassFile ("InternalClassWithNestedInterface.class");
var inner_interface = LoadClassFile ("InternalClassWithNestedInterface$NestedInterface.class");
var inner_inner_interface = LoadClassFile ("InternalClassWithNestedInterface$NestedInterface$DoubleNestedInterface.class");

KotlinFixups.Fixup (new [] { klass, inner_interface, inner_inner_interface });

var output = new XmlClassDeclarationBuilder (klass).ToXElement ().ToString ();
Assert.True (output.Contains ("visibility=\"public\""));

var output2 = new XmlClassDeclarationBuilder (inner_interface).ToXElement ().ToString ();
Assert.True (output2.Contains ("visibility=\"private\""));

var output3 = new XmlClassDeclarationBuilder (inner_inner_interface).ToXElement ().ToString ();
Assert.True (output3.Contains ("visibility=\"private\""));
}
}
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
internal class InternalClassWithNestedInterface {
public interface NestedInterface {
public interface DoubleNestedInterface
}
}