-
Notifications
You must be signed in to change notification settings - Fork 64
[XA.Tools.Bytecode] Add Kotlin parsing and honor Kotlin class visibility #499
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
Conversation
213ad21 to
7ca54f5
Compare
|
See also: 39a3b87 This PR will also need to update ...once the new XML elements are emitted... |
#502) Context: #466 Context: #467 Context: #499 Add support for parsing the [`RuntimeVisibleAnnotations`][0] and [`RuntimeInvisibleAnnotations`][1] attributes in Java bytecode. These attributes store Java programming language [annotations][2] placed on language constructs such as types, fields, and methods, and are analogous with C# custom attributes. Kotlin stores various bits of information in these attributes, and it will be necessary to parse these attributes to read that information; see also PR #499. [0]: https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.7.16 [1]: https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.7.17 [2]: https://docs.oracle.com/javase/specs/jls/se7/html/jls-9.html#jls-9.7
7ca54f5 to
b6e01bc
Compare
|
|
||
| namespace Xamarin.Android.Tools.Bytecode.Kotlin | ||
| { | ||
| public static class KotlinFixups |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this need to be public?
| foreach (var c in classes) { | ||
| // See if this is a Kotlin class | ||
| var attr = c.Attributes.OfType<RuntimeVisibleAnnotationsAttribute> ().FirstOrDefault (); | ||
| var kotlin = attr?.Annotations.FirstOrDefault (a => a.Type == "Lkotlin/Metadata;"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should use .SingleOrDefault().
|
Summary: Body: |
Kotlin Metadata
The bulk of this PR is adding plumbing support for the metadata that Kotlin adds to compiled classes.
This metadata is added to a
RuntimeVisibleAnnotationon the class with a key ofLkotlin/Metadata. This Annotation can contain the following key/values:k- The kind of metadata annotation (required)mv- The version of metadata (required)bv- The version of the byte code (required)d1- The encoded metadata (optional)d2- Additional strings (optional)As of this PR, we only support
Kind==Class.The
Classmetadata is encoded as an array of strings. First we have to decode thed1data into bytes using a variety of potential encodings. Once we have bytes, they are formatted as:lengthbytesThe string data table provides an indexed lookup table used by the Protobuf metadata to retrieve strings from one of:
d2fieldThe Protobuf encoded data is relatively straightforward. All fields that would be strings are instead indexes into the string data table. It provides metadata about the class itself and every field/method/property/parameter/etc for the class.
Metadata Consumption
With this PR we are starting small to consume this metadata:
privateif it isKotlin-PrivateorKotlin-Internal..classfiles for. Since our code doesn't understand them yet they do not generate usable bindings. Until we have completed our support for these constructs this PR marks them asprivateso we do not attempt to bind them. If a user really wants to bind these constructs they can mark them aspublicinmetadatawhen adding the othermetadataneeded to make them bindable.Protobuf Generated File
https://github.com/xamarin/java.interop/blob/b6e01bc2bea72f5524f89e0f1f6fede6c13183b2/src/Xamarin.Android.Tools.Bytecode/Kotlin/KotlinProtobufDefinition.cs is generated using this tool from Protobuf.Net:
https://protogen.marcgravell.com/
Paste in this file:
https://github.com/JetBrains/kotlin/blob/master/core/metadata.jvm/src/jvm_metadata.proto
The tool does not support
#importso you'll need to replace those lines with the contents of the files:Generate with the default options to create the .cs file.