Merge pull request #10297 from tamasvajk/kotlin-fix-kotlin-to-java-fn-names

Kotlin: Lookup getter methods based on special JVM method mapping
This commit is contained in:
Tamás Vajk
2022-09-07 08:56:19 +02:00
committed by GitHub
3 changed files with 20 additions and 13 deletions

View File

@@ -1202,10 +1202,11 @@ open class KotlinUsesExtractor(
else else
f.parentClassOrNull?.let { parentClass -> f.parentClassOrNull?.let { parentClass ->
getJavaEquivalentClass(parentClass)?.let { javaClass -> getJavaEquivalentClass(parentClass)?.let { javaClass ->
if (javaClass != parentClass) if (javaClass != parentClass) {
val jvmName = getJvmName(f) ?: f.name.asString()
// Look for an exact type match... // Look for an exact type match...
javaClass.declarations.findSubType<IrFunction> { decl -> javaClass.declarations.findSubType<IrFunction> { decl ->
decl.name == f.name && decl.name.asString() == jvmName &&
decl.valueParameters.size == f.valueParameters.size && decl.valueParameters.size == f.valueParameters.size &&
// Note matching by classifier not the whole type so that generic arguments are allowed to differ, // Note matching by classifier not the whole type so that generic arguments are allowed to differ,
// as they always will for method type parameters occurring in parameter types (e.g. <T> toArray(T[] array) // as they always will for method type parameters occurring in parameter types (e.g. <T> toArray(T[] array)
@@ -1214,7 +1215,7 @@ open class KotlinUsesExtractor(
} ?: } ?:
// Or if there is none, look for the only viable overload // Or if there is none, look for the only viable overload
javaClass.declarations.singleOrNullSubType<IrFunction> { decl -> javaClass.declarations.singleOrNullSubType<IrFunction> { decl ->
decl.name == f.name && decl.name.asString() == jvmName &&
decl.valueParameters.size == f.valueParameters.size decl.valueParameters.size == f.valueParameters.size
} ?: } ?:
// Or check property accessors: // Or check property accessors:
@@ -1234,6 +1235,7 @@ open class KotlinUsesExtractor(
} }
null null
} }
}
else else
null null
} }

View File

@@ -1,9 +1,11 @@
| test.kt:1:84:1:89 | length(...) | length | diag
| test.kt:1:97:1:100 | size(...) | size | #select
| test.kt:1:108:1:111 | size(...) | size | | test.kt:1:84:1:89 | length(...) | java.lang.CharSequence | length |
| test.kt:1:119:1:122 | keySet(...) | keySet | | test.kt:1:97:1:100 | size(...) | java.util.Collection<String> | size |
| test.kt:1:124:1:127 | size(...) | size | | test.kt:1:108:1:111 | size(...) | java.util.Map<String,String> | size |
| test.kt:1:135:1:140 | values(...) | values | | test.kt:1:119:1:122 | keySet(...) | java.util.Map<String,String> | keySet |
| test.kt:1:142:1:145 | size(...) | size | | test.kt:1:124:1:127 | size(...) | java.util.Set<String> | size |
| test.kt:1:153:1:159 | entrySet(...) | entrySet | | test.kt:1:135:1:140 | values(...) | java.util.Map<String,String> | values |
| test.kt:1:161:1:164 | size(...) | size | | test.kt:1:142:1:145 | size(...) | java.util.Collection<String> | size |
| test.kt:1:153:1:159 | entrySet(...) | java.util.Map<String,String> | entrySet |
| test.kt:1:161:1:164 | size(...) | java.util.Set<Entry<String,String>> | size |

View File

@@ -1,4 +1,7 @@
import java import java
import semmle.code.java.Diagnostics
from MethodAccess ma from MethodAccess ma
select ma, ma.getCallee().toString() select ma, ma.getCallee().getDeclaringType().getQualifiedName(), ma.getCallee().getName()
query predicate diag(Diagnostic d) { any() }