Skip to content

Commit e61da3b

Browse files
authored
Fix NoSuchFieldError in newer Kotlin plugins (#3422)
1 parent 1967c14 commit e61da3b

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

sqldelight-idea-plugin/src/main/kotlin/app/cash/sqldelight/intellij/SqlDelightReferenceContributor.kt

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ internal class SqlDelightReferenceContributor : PsiReferenceContributor() {
6464
val scope = module.getModuleWithDependenciesAndLibrariesScope(false)
6565
val classNameIndex = KotlinFullClassNameIndex.getInstance()
6666
val ktClass = { classNameIndex[qName, project, scope].firstOrNull() }
67-
val typeAliasFqNameIndex = KotlinTopLevelTypeAliasFqNameIndex.getInstance()
67+
val typeAliasFqNameIndex = getKotlinTopLevelTypeAliasFqNameIndex()
6868
val typeAlias = { typeAliasFqNameIndex[qName, project, scope].firstOrNull() }
6969
val javaPsiFacade = JavaPsiFacade.getInstance(project)
7070
val javaClass = { javaPsiFacade.findClass(qName, scope) }
@@ -74,3 +74,17 @@ internal class SqlDelightReferenceContributor : PsiReferenceContributor() {
7474
private fun typeForThisPackage(file: SqlDelightFile) = "${file.packageName}.${element.text}"
7575
}
7676
}
77+
78+
private fun getKotlinTopLevelTypeAliasFqNameIndex(): KotlinTopLevelTypeAliasFqNameIndex {
79+
// read the INSTANCE variable reflectively first (newer Kotlin plugins)
80+
try {
81+
val instanceField = KotlinTopLevelTypeAliasFqNameIndex::class.java.getField("INSTANCE")
82+
val instance = instanceField.get(null)
83+
if (instance is KotlinTopLevelTypeAliasFqNameIndex) {
84+
return instance
85+
}
86+
} catch (e: Exception) {
87+
/* intentionally empty, fall back to getInstance() call in case of errors */
88+
}
89+
return KotlinTopLevelTypeAliasFqNameIndex.getInstance()
90+
}

0 commit comments

Comments
 (0)