Skip to content

Binding a Kotlin library results in bad code #464

@mattleibow

Description

@mattleibow

I have noticed that binding Kotlin code results in bad code. For example, I am binding okio v2.0.0

There are a few bits of code that cannot be compiled:

  1. It tries to bind classes with a - prefix. This looks to be a way to mark them as deprecated or internal or something. This is easy to remove by:
    <remove-node path="/api/package/class[starts-with(@name,'-')]" />
  2. It tries to bind methods with a - prefix, again easy to remove with:
    <remove-node path="/api/package/class/method[starts-with(@name,'-')]" />
  3. It tries to bind methods with $ in the parameter names:
    public static final int codePointAt(@NotNull byte[] $receiver, int index)
    There are other areas where it works fine, so this looks to be just in the finally blocks. I am able to fix this by setting the managedName:
    <attr path="/api/package[@name='okio']/class/method[parameter[1][@name='$receiver']]/parameter[1]" name="managedName">receiver</attr>
    Without this adjustment, the generator results in partially corrected code:
    • Declaration: public static unsafe int CodePointAt (byte[] _receiver_, int index)
    • Code: try { ... } finally { if ($receiver != null) { ... } }

There are also cases where this could be improved overall:

  1. With Kotlin, you can have extension methods (the reason for the $receiver parameter name). This means that Xamarin could potentially automatically generate extension methods - or have the option to do so.
  2. There are also cases where internal members are public in C#:
    internal val AssertionError.isAndroidGetsocknameError: Boolean get() { ... }
    https://github.com/square/okio/blob/okio-parent-2.0.0/okio/jvm/src/main/java/okio/Okio.kt#L212

I am working on a tool that runs after the api.xml is created to create a transforms.xml file that tries to fix and resolve members. A very rough thing that seems to be working: xamarin/XamarinComponents#642

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions