Skip to content

Commit

Permalink
Fix NoSuchFieldError in newer Kotlin plugins (#3422)
Browse files Browse the repository at this point in the history
  • Loading branch information
madisp committed Aug 8, 2022
1 parent 1967c14 commit e61da3b
Showing 1 changed file with 15 additions and 1 deletion.
Expand Up @@ -64,7 +64,7 @@ internal class SqlDelightReferenceContributor : PsiReferenceContributor() {
val scope = module.getModuleWithDependenciesAndLibrariesScope(false)
val classNameIndex = KotlinFullClassNameIndex.getInstance()
val ktClass = { classNameIndex[qName, project, scope].firstOrNull() }
val typeAliasFqNameIndex = KotlinTopLevelTypeAliasFqNameIndex.getInstance()
val typeAliasFqNameIndex = getKotlinTopLevelTypeAliasFqNameIndex()
val typeAlias = { typeAliasFqNameIndex[qName, project, scope].firstOrNull() }
val javaPsiFacade = JavaPsiFacade.getInstance(project)
val javaClass = { javaPsiFacade.findClass(qName, scope) }
Expand All @@ -74,3 +74,17 @@ internal class SqlDelightReferenceContributor : PsiReferenceContributor() {
private fun typeForThisPackage(file: SqlDelightFile) = "${file.packageName}.${element.text}"
}
}

private fun getKotlinTopLevelTypeAliasFqNameIndex(): KotlinTopLevelTypeAliasFqNameIndex {
// read the INSTANCE variable reflectively first (newer Kotlin plugins)
try {
val instanceField = KotlinTopLevelTypeAliasFqNameIndex::class.java.getField("INSTANCE")
val instance = instanceField.get(null)
if (instance is KotlinTopLevelTypeAliasFqNameIndex) {
return instance
}
} catch (e: Exception) {
/* intentionally empty, fall back to getInstance() call in case of errors */
}
return KotlinTopLevelTypeAliasFqNameIndex.getInstance()
}

0 comments on commit e61da3b

Please sign in to comment.