Files
kotlin/compiler/testData/codegen/boxWithStdlib/platformNames/classMembers.kt
dnpetrov 50ea67ba13 KT-5524 Support [platformName] annotation for class members
@platformName is now supported for final non-overriding class member functions
(including property accessors).
Front-end provides diagnostics for inapplicable annotation cases.
Code generation updated:
- ignore kotlin.platform.platformName annotation for Java class methods;
- bridges generation generates proper JVM declarations in case of methods renamed with @platformName.
@platformName-related tests added.

#KT-5524 Fixed
2015-06-04 17:54:08 +03:00

122 lines
3.9 KiB
Kotlin
Vendored

import kotlin.platform.*
// See:
// http://kotlinlang.org/docs/reference/java-interop.html#handling-signature-clashes-with-platformname
// https://youtrack.jetbrains.com/issue/KT-5524
val strs = listOf("abc", "def")
val ints = listOf(1, 2, 3)
class C {
// Instance methods
@platformName("instMethodStr")
fun instMethod(list: List<String>): String = "instMethodStr"
@platformName("instMethodInt")
fun instMethod(list: List<Int>): String = "instMethodInt"
// Properties
var rwProperty: Int
@platformName("get_rwProperty")
get() = 123
@platformName("set_rwProperty")
set(v) {}
var rwValue = 111
fun getRwProperty(): Int = rwValue
fun setRwProperty(v: Int) {
rwValue = v
}
// Extension methods
class Inner
@platformName("extMethodWithGenericParamStr")
fun Inner.extMethodWithGenericParam(list: List<String>): String = "extMethodWithGenericParamStr"
@platformName("extMethodWithGenericParamInt")
fun Inner.extMethodWithGenericParam(list: List<Int>): String = "extMethodWithGenericParamInt"
// This is already covered by extMethodWithGenericParam(), but might be relevant for a platform
// with extension method code generation strategy different from Java 6.
@platformName("extMethodWithGenericReceiverStr")
fun List<String>.extMethodWithGenericReceiver(): String = "extMethodWithGenericReceiverStr"
@platformName("extMethodWithGenericReceiverInt")
fun List<Int>.extMethodWithGenericReceiver(): String = "extMethodWithGenericReceiverInt"
// Extension method vs instance method
@platformName("ambigMethod1")
fun ambigMethod(str: String): String = "ambigMethod1"
@platformName("ambigMethod2")
fun String.ambigMethod(): String = "ambigMethod2"
}
fun box(): String {
val c = C()
// Instance methods:
// method signatures with erased types SHOULD NOT clash
val test1 = c.instMethod(strs)
if (test1 != "instMethodStr") return "Fail: c.instMethod(strs)==$test1"
val test2 = c.instMethod(ints)
if (test2 != "instMethodInt") return "Fail: c.instMethod(ints)==$test2"
// Properties:
// property accessors SHOULD NOT clash with class methods
val test3 = c.rwProperty
if (test3 != 123) return "Fail: c.rwProperty==$test3"
val test3a = c.getRwProperty()
if (test3a != 111) return "Fail: c.getRwProperty()==$test3a"
c.setRwProperty(444)
val test3b = c.rwProperty
if (test3b != 123) return "Fail: c.rwProperty==$test3b after c.setRwProperty(1234)"
val test3c = c.getRwProperty()
if (test3c != 444) return "Fail: c.getRwProperty()==$test3c after c.setRwProperty(1234)"
// Extension methods:
// method signatures with erased types SHOULD NOT clash
val test4 = with(c) { C.Inner().extMethodWithGenericParam(strs) }
if (test4 != "extMethodWithGenericParamStr") return "Fail: with(c) { C.Inner().extMethodWithGenericParam(strs) }==$test4"
val test5 = with(c) { C.Inner().extMethodWithGenericParam(ints) }
if (test5 != "extMethodWithGenericParamInt") return "Fail: with(c) { C.Inner().extMethodWithGenericParam(ints) }==$test5"
val test6 = with(c) { strs.extMethodWithGenericReceiver() }
if (test6 != "extMethodWithGenericReceiverStr") return "Fail: with(c) { strs.extMethodWithGenericReceiver() }==$test6"
val test7 = with(c) { ints.extMethodWithGenericReceiver() }
if (test7 != "extMethodWithGenericReceiverInt") return "Fail: with(c) { ints.extMethodWithGenericReceiver() }==$test7"
// Extension method SHOULD NOT clash with instance method with the same Java signature.
val str = "abc"
val test8 = with(c) { ambigMethod(str) }
if (test8 != "ambigMethod1") return "Fail: with(c) { ambigMethod(str) }==$test8"
val test9 = with(c) { str.ambigMethod() }
if (test9 != "ambigMethod2") return "Fail: with(c) { str.ambigMethod() }==$test9"
// Everything is fine.
return "OK"
}