From bd1436394fe37a897c11a0ad0b3dd97f9afe7825 Mon Sep 17 00:00:00 2001 From: Madis Pink Date: Sun, 7 Aug 2022 14:01:27 +0300 Subject: [PATCH] Fix NoSuchFieldError in newer Kotlin plugins --- .../intellij/SqlDelightReferenceContributor.kt | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/sqldelight-idea-plugin/src/main/kotlin/app/cash/sqldelight/intellij/SqlDelightReferenceContributor.kt b/sqldelight-idea-plugin/src/main/kotlin/app/cash/sqldelight/intellij/SqlDelightReferenceContributor.kt index 56b13ef320d..8576be4b789 100644 --- a/sqldelight-idea-plugin/src/main/kotlin/app/cash/sqldelight/intellij/SqlDelightReferenceContributor.kt +++ b/sqldelight-idea-plugin/src/main/kotlin/app/cash/sqldelight/intellij/SqlDelightReferenceContributor.kt @@ -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) } @@ -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() +}