Commit 6bd7ae4
authored
[Xamarin.Android.Tools.Bytecode] Kotlin internal prop visibility (#1151)
Fixes: #1139
Context: https://jetbrains.gitbooks.io/kotlin-reference-for-kindle/content/properties.html
Kotlin does not allow classes to explicitly contain fields:
> Classes in Kotlin cannot have fields.
They can *implicitly* contain fields, but not explicitly.
Syntax that *look like* a field to those who don't know Kotlin:
/* partial */ class EmojiPickerView {
private var onEmojiPickedListener: Consumer<EmojiViewItem>? = null
}
are in fact *properties*, which may or may not involve a compiler-
generated backing field.
When a property is declared in Kotlin, Java `get*` and/or `set*`
methods are generated as needed. In the case where the property is
`internal` -- which is not supported by Java -- `public` getters or
setters are generated. We wish to "hide" these as they are not
intended to be part of the public API. That is, they would not be
callable by Kotlin code.
However, consider these code fragments from [`EmojiPickerView.kt`][0]:
/* partial */ class EmojiPickerView {
// Line 100
private var onEmojiPickedListener: Consumer<EmojiViewItem>? = null
// Lines 317-319
fun setOnEmojiPickedListener(onEmojiPickedListener: Consumer<EmojiViewItem>?) {
this.onEmojiPickedListener = onEmojiPickedListener
}
}
Default member visibility in Kotlin is `public`, so this declares a
private `onEmojiPickedListener` property and a public
`setOnEmojiPickedListener()` method.
However, we were incorrectly determining visibility (678c4bd): we
treated `onEmojiPickedListener` and `setOnEmojiPickedListener()` as
if they were part of the same property. As part of this association,
we saw that `onEmojiPickedListener` was private, and marked
`setOnEmojiPickedListener()` as private to follow suit.
This was incorrect, because `private` properties do not have setters
*at all*, so we should not attempt to hide anything for `private`
properties, only for `internal` properties.
With that incorrect association broken, that allows
`setOnEmojiPickedListener()` to be considered separately, and found
to have `public` visibility.
For example, this Kotlin code:
private var type = 0
internal var itype = 0
generates Java code equivalent to:
private int type;
private int itype;
public final int getItype$main() {
return this.itype;
}
public final void setItype$main(int <set-?>) {
this.itype = <set-?>;
Additionally, when matching `internal` properties to their getters
or setters, the generated name differs from the names given to
`public` getters and setters.
// Kotlin: public var type = 0
// Java:
public final int getType () { ... }
// Kotlin: internal var type = 0
// Java:
public final int getType$main () { ... }
Fix this scenario:
* Do not attempt to hide getters/setters for `private` Kotlin
properties.
* Improve matching of generated names for `internal` Kotlin
properties.
Additionally, our existing unit test in `NameShadowing.kt` was written
incorrectly:
// Incorrect, return type of a function
fun setType(type: Int) = { println (type); }
// Correct, return type of void
fun setType(type: Int) { println (type); }
The fixed `NameShadowing.kt` unit test fails without the other
changes here. Additionally, add several more unit test cases to
cover `internal` properties and mangled getter/setter names.
Additionally, some enum values in `KotlinPropertyFlags` were
specified incorrectly which has been fixed.
[0]: https://github.com/androidx/androidx/blob/0d655214d339e006f4e13a85f55c78770c885f2e/emoji2/emoji2-emojipicker/src/main/java/androidx/emoji2/emojipicker/EmojiPickerView.kt1 parent 3c83179 commit 6bd7ae4
File tree
6 files changed
+96
-19
lines changed- src/Xamarin.Android.Tools.Bytecode/Kotlin
- tests/Xamarin.Android.Tools.Bytecode-Tests
- kotlin
6 files changed
+96
-19
lines changedLines changed: 15 additions & 9 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
318 | 318 | | |
319 | 319 | | |
320 | 320 | | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
321 | 325 | | |
322 | 326 | | |
323 | 327 | | |
| |||
688 | 692 | | |
689 | 693 | | |
690 | 694 | | |
| 695 | + | |
| 696 | + | |
691 | 697 | | |
692 | 698 | | |
693 | 699 | | |
| |||
698 | 704 | | |
699 | 705 | | |
700 | 706 | | |
701 | | - | |
702 | | - | |
703 | | - | |
704 | | - | |
705 | | - | |
706 | | - | |
707 | | - | |
708 | | - | |
709 | | - | |
| 707 | + | |
| 708 | + | |
| 709 | + | |
| 710 | + | |
| 711 | + | |
| 712 | + | |
| 713 | + | |
| 714 | + | |
| 715 | + | |
710 | 716 | | |
711 | 717 | | |
712 | 718 | | |
| |||
Lines changed: 28 additions & 8 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
272 | 272 | | |
273 | 273 | | |
274 | 274 | | |
275 | | - | |
276 | | - | |
| 275 | + | |
| 276 | + | |
277 | 277 | | |
278 | 278 | | |
279 | 279 | | |
| |||
384 | 384 | | |
385 | 385 | | |
386 | 386 | | |
387 | | - | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
388 | 398 | | |
389 | 399 | | |
390 | 400 | | |
| |||
394 | 404 | | |
395 | 405 | | |
396 | 406 | | |
397 | | - | |
398 | | - | |
399 | | - | |
400 | | - | |
401 | | - | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
402 | 422 | | |
403 | 423 | | |
404 | 424 | | |
| |||
Lines changed: 13 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
3 | 4 | | |
4 | 5 | | |
5 | 6 | | |
| |||
8 | 9 | | |
9 | 10 | | |
10 | 11 | | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
11 | 24 | | |
12 | 25 | | |
13 | 26 | | |
| |||
Lines changed: 22 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
324 | 324 | | |
325 | 325 | | |
326 | 326 | | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
327 | 331 | | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
328 | 350 | | |
329 | 351 | | |
330 | 352 | | |
| |||
Binary file not shown.
Lines changed: 18 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
11 | | - | |
| 11 | + | |
12 | 12 | | |
13 | | - | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
14 | 30 | | |
0 commit comments