Skip to content

[Byecode] Hide internal Kotlin fields marked with @JvmField. #1004

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 3 commits into from
Jul 7, 2022
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
2 changes: 1 addition & 1 deletion src/Xamarin.Android.Tools.Bytecode/ClassFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public ClassFile (Stream stream)
thisClass = stream.ReadNetworkUInt16 ();
superClass = stream.ReadNetworkUInt16 ();
Interfaces = new Interfaces (ConstantPool, stream);
Fields = new Fields (ConstantPool, stream);
Fields = new Fields (ConstantPool, this, stream);
Methods = new Methods (ConstantPool, this, stream);
Attributes = new AttributeCollection (ConstantPool, stream);

Expand Down
15 changes: 11 additions & 4 deletions src/Xamarin.Android.Tools.Bytecode/Fields.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public sealed class Fields : Collection<FieldInfo> {

public ConstantPool ConstantPool {get; private set;}

public Fields (ConstantPool constantPool, Stream stream)
public Fields (ConstantPool constantPool, ClassFile declaringClass, Stream stream)
{
if (constantPool == null)
throw new ArgumentNullException ("constantPool");
Expand All @@ -20,7 +20,7 @@ public Fields (ConstantPool constantPool, Stream stream)
ConstantPool = constantPool;
var count = stream.ReadNetworkUInt16 ();
for (int i = 0; i < count; ++i) {
Add (new FieldInfo (constantPool, stream));
Add (new FieldInfo (constantPool, declaringClass, stream));
}
}
}
Expand All @@ -31,13 +31,15 @@ public sealed class FieldInfo {
ushort descriptorIndex;

public ConstantPool ConstantPool {get; private set;}
public FieldAccessFlags AccessFlags {get; private set;}
public ClassFile DeclaringType {get; private set;}
public FieldAccessFlags AccessFlags {get; set;}
public AttributeCollection Attributes {get; private set;}
public string? KotlinType {get; set;}

public FieldInfo (ConstantPool constantPool, Stream stream)
public FieldInfo (ConstantPool constantPool, ClassFile declaringType, Stream stream)
{
ConstantPool = constantPool;
DeclaringType = declaringType;
AccessFlags = (FieldAccessFlags) stream.ReadNetworkUInt16 ();
nameIndex = stream.ReadNetworkUInt16 ();
descriptorIndex = stream.ReadNetworkUInt16 ();
Expand All @@ -61,6 +63,8 @@ public string Descriptor {
var signature = Attributes.Get<SignatureAttribute> ();
return signature != null ? signature.Value : null;
}

public bool IsPubliclyVisible => AccessFlags.HasFlag (FieldAccessFlags.Public) || AccessFlags.HasFlag (FieldAccessFlags.Protected);
}

[Flags]
Expand All @@ -74,5 +78,8 @@ public enum FieldAccessFlags {
Transient = 0x0080,
Synthetic = 0x1000,
Enum = 0x4000,

// This is not a real Java FieldAccessFlags, it is used to denote Kotlin "internal" access.
Internal = 0x10000000,
}
}
23 changes: 23 additions & 0 deletions src/Xamarin.Android.Tools.Bytecode/Kotlin/KotlinFixups.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,20 @@ static MethodAccessFlags SetVisibility (MethodAccessFlags existing, MethodAccess
return existing;
}

static FieldAccessFlags SetVisibility (FieldAccessFlags existing, FieldAccessFlags newVisibility)
{
// First we need to remove any existing visibility flags,
// without modifying other flags like Abstract
existing = (existing ^ FieldAccessFlags.Public) & existing;
existing = (existing ^ FieldAccessFlags.Protected) & existing;
existing = (existing ^ FieldAccessFlags.Private) & existing;
existing = (existing ^ FieldAccessFlags.Internal) & existing;

existing |= newVisibility;

return existing;
}

static void FixupJavaMethods (Methods methods)
{
// We do the following method level fixups here because we can operate on all methods,
Expand Down Expand Up @@ -294,6 +308,15 @@ static void FixupField (FieldInfo? field, KotlinProperty metadata)
if (field is null)
return;

// Hide field if it isn't Public/Protected
if (!metadata.Flags.IsPubliclyVisible ()) {

if (field.IsPubliclyVisible) {
Log.Debug ($"Kotlin: Hiding internal field {field.DeclaringType?.ThisClass.Name.Value} - {field.Name}");
field.AccessFlags = SetVisibility (field.AccessFlags, FieldAccessFlags.Internal);
}
}

// Handle erasure of Kotlin unsigned types
field.KotlinType = GetKotlinType (field.Descriptor, metadata.ReturnType?.ClassName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,8 @@ static string EscapeLiteral (string value)

static string GetVisibility (FieldAccessFlags accessFlags)
{
if (accessFlags.HasFlag (FieldAccessFlags.Internal))
return "kotlin-internal";
if ((accessFlags & FieldAccessFlags.Public) != 0)
return "public";
if ((accessFlags & FieldAccessFlags.Protected) != 0)
Expand Down
16 changes: 16 additions & 0 deletions tests/Xamarin.Android.Tools.Bytecode-Tests/KotlinFixupsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,22 @@ public void HideInternalMethod ()
Assert.True (output.Contains ("visibility=\"kotlin-internal\""));
}

[Test]
public void HideInternalField ()
{
var klass = LoadClassFile ("InternalField.class");
var field = klass.Fields.First (m => m.Name == "city");

Assert.True (field.AccessFlags.HasFlag (FieldAccessFlags.Public));

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

Assert.False (field.AccessFlags.HasFlag (FieldAccessFlags.Public));

var output = new XmlClassDeclarationBuilder (klass).ToXElement ().ToString ();
Assert.True (output.Contains ("visibility=\"kotlin-internal\""));
}

[Test]
public void ParameterName ()
{
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class InternalField {
@JvmField
internal val city: String = "London"
}