Skip to content

Commit 1c21ce0

Browse files
committed
Kotlin: Lookup getter methods based on special JVM method mapping
1 parent 6a90db9 commit 1c21ce0

File tree

3 files changed

+14
-18
lines changed

3 files changed

+14
-18
lines changed

java/kotlin-extractor/src/main/kotlin/KotlinUsesExtractor.kt

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,20 +1191,22 @@ open class KotlinUsesExtractor(
11911191
else
11921192
f.parentClassOrNull?.let { parentClass ->
11931193
getJavaEquivalentClass(parentClass)?.let { javaClass ->
1194-
if (javaClass != parentClass)
1194+
if (javaClass != parentClass) {
1195+
val jvmName = getSpecialJvmName(f) ?: f.name.asString()
11951196
// Look for an exact type match...
11961197
javaClass.declarations.findSubType<IrFunction> { decl ->
1197-
decl.name == f.name &&
1198-
decl.valueParameters.size == f.valueParameters.size &&
1199-
// Note matching by classifier not the whole type so that generic arguments are allowed to differ,
1200-
// as they always will for method type parameters occurring in parameter types (e.g. <T> toArray(T[] array)
1201-
// Differing only by nullability would also be insignificant if it came up.
1202-
decl.valueParameters.zip(f.valueParameters).all { p -> p.first.type.classifierOrNull == p.second.type.classifierOrNull }
1198+
decl.name.asString() == jvmName &&
1199+
decl.valueParameters.size == f.valueParameters.size &&
1200+
// Note matching by classifier not the whole type so that generic arguments are allowed to differ,
1201+
// as they always will for method type parameters occurring in parameter types (e.g. <T> toArray(T[] array)
1202+
// Differing only by nullability would also be insignificant if it came up.
1203+
decl.valueParameters.zip(f.valueParameters)
1204+
.all { p -> p.first.type.classifierOrNull == p.second.type.classifierOrNull }
12031205
} ?:
12041206
// Or if there is none, look for the only viable overload
12051207
javaClass.declarations.singleOrNullSubType<IrFunction> { decl ->
1206-
decl.name == f.name &&
1207-
decl.valueParameters.size == f.valueParameters.size
1208+
decl.name.asString() == jvmName &&
1209+
decl.valueParameters.size == f.valueParameters.size
12081210
} ?:
12091211
// Or check property accessors:
12101212
(f.propertyIfAccessor as? IrProperty)?.let { kotlinProp ->
@@ -1223,6 +1225,7 @@ open class KotlinUsesExtractor(
12231225
}
12241226
null
12251227
}
1228+
}
12261229
else
12271230
null
12281231
}

java/kotlin-extractor/src/main/kotlin/utils/JvmNames.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ private val specialFunctions = mapOf(
5757

5858
private val specialFunctionShortNames = specialFunctions.keys.map { it.functionName }.toSet()
5959

60-
private fun getSpecialJvmName(f: IrFunction): String? {
60+
fun getSpecialJvmName(f: IrFunction): String? {
6161
if (specialFunctionShortNames.contains(f.name) && f is IrSimpleFunction) {
6262
f.allOverriddenIncludingSelf().forEach { overriddenFunc ->
6363
overriddenFunc.parentClassOrNull?.fqNameWhenAvailable?.let { parentFqName ->

java/ql/test/kotlin/library-tests/special-method-getters/test.expected

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
diag
2-
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.CharSequence.<get-length> in java.lang.CharSequence |
3-
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.collections.Collection.<get-size> in java.util.Collection |
4-
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.collections.Collection.<get-size> in java.util.Collection |
5-
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.collections.Map.<get-entries> in java.util.Map |
6-
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.collections.Map.<get-keys> in java.util.Map |
7-
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.collections.Map.<get-size> in java.util.Map |
8-
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.collections.Map.<get-values> in java.util.Map |
92
#select
10-
| test.kt:1:84:1:89 | length(...) | kotlin.CharSequence | length |
3+
| test.kt:1:84:1:89 | length(...) | java.lang.CharSequence | length |
114
| test.kt:1:97:1:100 | size(...) | java.util.Collection<String> | size |
125
| test.kt:1:108:1:111 | size(...) | java.util.Map<String,String> | size |
136
| test.kt:1:119:1:122 | keySet(...) | java.util.Map<String,String> | keySet |

0 commit comments

Comments
 (0)