Skip to content

Commit 3736516

Browse files
Merge pull request #21915 from david-allison/kotlin-dropped-class
Kotlin: Fix findTopLevelPropertyOrWarn for K2 compiler
2 parents b68ce98 + 19dc506 commit 3736516

8 files changed

Lines changed: 44 additions & 1 deletion

File tree

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3905,7 +3905,14 @@ open class KotlinFileExtractor(
39053905

39063906
val prop =
39073907
getPropertiesByFqName(pluginContext, propertyPkg, propertyName)
3908-
.firstOrNull { it.owner.parentClassOrNull?.fqNameWhenAvailable?.asString() == type }
3908+
.firstOrNull {
3909+
val owner = it.owner
3910+
when (val parent = owner.parent) {
3911+
is IrClass -> parent.fqNameWhenAvailable?.asString()
3912+
is IrExternalPackageFragment -> getFileClassFqName(owner)?.asString()
3913+
else -> null
3914+
} == type
3915+
}
39093916
?.owner
39103917

39113918
if (prop != null) {
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* Fixed an issue where `Foo::class.java` arguments were dropped during extraction under the Kotlin K2 compiler, which could cause false positives in queries such as `java/android/implicit-pendingintents`.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| consume | Class<Target> |
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Target
2+
3+
class KClassJavaArg {
4+
fun consume(c: Class<*>) {}
5+
6+
fun test() {
7+
// `Target::class.java` must be extracted as the argument to `consume`.
8+
consume(Target::class.java)
9+
}
10+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import java
2+
3+
from MethodCall mc, Argument arg
4+
where mc.getMethod().hasName("consume") and arg = mc.getAnArgument()
5+
select mc.getMethod().getName(), arg.getType().getName()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| consume | Class<Target> |
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Target
2+
3+
class KClassJavaArg {
4+
fun consume(c: Class<*>) {}
5+
6+
fun test() {
7+
// `Target::class.java` must be extracted as the argument to `consume`.
8+
consume(Target::class.java)
9+
}
10+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import java
2+
3+
from MethodCall mc, Argument arg
4+
where mc.getMethod().hasName("consume") and arg = mc.getAnArgument()
5+
select mc.getMethod().getName(), arg.getType().getName()

0 commit comments

Comments
 (0)