1

I'm trying to figure out what happens with internal classes when seen from Java's perspective.

Found this in the docs:

Members of internal classes go through name mangling, to make it harder to accidentally use them from Java and to allow overloading for members with the same signature that don’t see each other according to Kotlin rules

So I was very curious to see how it looks like in practice.

I created a simple Kotlin class:

internal class Foo(i : Int) {}

Built a project, unpacked the jar and used javap to have a look at the actual class... and it displayed a standard public class with the original name:

Compiled from "Foo.kt"
public final class Foo {
    public Foo(int);
} 

Am I missing something? or is it just the docs that are misleading?

Docs mention members of internal classes, but I tried that as well:

internal class Foo(someInt : Int) {
    var someString : String
        get() {
            TODO()
        }
        set(value) {}

    fun foo() { }

    class Bar { }
}

And got the expected output:

Compiled from "Foo.kt"
public final class Foo {
  public Foo(int);
  public final java.lang.String getSomeString();
  public final void setSomeString(java.lang.String);
  public final void foo();
}

and:

Compiled from "Foo.kt"
public final class Foo$Bar {
  public Foo$Bar();
}
Grzegorz Piwowarek
  • 13,172
  • 8
  • 62
  • 93
  • The quote says "_members_ of internal classes...". Your internal class has no members. – Sweeper Jul 02 '21 at 05:47
  • It should have said "`internal` members of classes go through name mangling..." because that's what I'm seeing. It doesn't seem to matter whether the class is `public` or `internal`. – Sweeper Jul 02 '21 at 06:04
  • @Sweeper Good note. I will try to play further and witness the name mangling – Grzegorz Piwowarek Jul 02 '21 at 06:37
  • @Sweeper tried it, didn't work, updated the question – Grzegorz Piwowarek Jul 02 '21 at 06:45
  • Read my comment again, please. I said "`internal` members". Declarations without modifiers are public by default. You kind of omitted some context from the documentation in your quote. That whole paragraph is talking about `internal` declarations, so it is not that much stretch (though I think the wording could be improved) to interpret "members of internal classes" as "_internal_ members of internal classes". Anyway, that's what's actually mangled. – Sweeper Jul 02 '21 at 06:52
  • @Sweeper I read your comment again and then read the docs as well and noticed the emphasized _internal_ like it's supposed to mean the keyword itself and no mention about the fact that members need to be _internal_ as well. In such case, I consider docs to be misleading. I will accept your answer if you want to make extra effort and write it down here – Grzegorz Piwowarek Jul 02 '21 at 07:07

0 Answers0