[gradle] IDEA-363330 prevent NPE in SyntheticVersionCatalogAccessor: check if the delegate is null

There are some cases of NPE while <init> of the SyntheticVersionCatalogAccessor. Probably they were caused by the usage of `!!` operator with found delegate that could be null

Code review: IJ-CR-153929
(cherry picked from commit 636fc616b7148b82d645872fdef13f3b42e37c99)

GitOrigin-RevId: b3b707895ad3a5887bcde084bb35e0b87b78cc1d
This commit is contained in:
Nikita Biriukov
2024-12-24 18:19:33 +01:00
committed by intellij-monorepo-bot
parent 76b5d499de
commit 041c829211
2 changed files with 21 additions and 4 deletions

View File

@@ -35,7 +35,7 @@ class GradleDslVersionCatalogHandler : GradleVersionCatalogHandler {
val buildModel = getBuildModel(module) ?: return null
val versionCatalogModel = buildModel.versionCatalogsModel
if (versionCatalogModel.getVersionCatalogModel(catalogName) == null) return null
return SyntheticVersionCatalogAccessor(project, scope, versionCatalogModel, catalogName)
return SyntheticVersionCatalogAccessor.create(project, scope, versionCatalogModel, catalogName)
}
override fun getAccessorsForAllCatalogs(context: PsiElement): Map<String, PsiClass> {
@@ -45,7 +45,8 @@ class GradleDslVersionCatalogHandler : GradleVersionCatalogHandler {
val catalogsModel = getBuildModel(module)?.versionCatalogsModel ?: return emptyMap()
val result = mutableMapOf<String, PsiClass>()
catalogsModel.catalogNames().forEach { catalogName ->
result.putIfAbsent(catalogName, SyntheticVersionCatalogAccessor(project, scope, catalogsModel, catalogName))
val accessor = SyntheticVersionCatalogAccessor.create(project, scope, catalogsModel, catalogName)
accessor?.let { result.putIfAbsent(catalogName, accessor) }
}
return result
}

View File

@@ -18,8 +18,13 @@ import org.jetbrains.plugins.gradle.service.resolve.GradleCommonClassNames
/**
* Serves as a client for PSI infrastructure and as a layer over TOML version catalog files at the same time
*/
class SyntheticVersionCatalogAccessor(project: Project, scope: GlobalSearchScope, model: GradleVersionCatalogsModel, className: String) :
LightClass(JavaPsiFacade.getInstance(project).findClass(CommonClassNames.JAVA_LANG_OBJECT, scope)!!) {
class SyntheticVersionCatalogAccessor(
project: Project,
scope: GlobalSearchScope,
model: GradleVersionCatalogsModel,
className: String,
delegate: PsiClass,
) : LightClass(delegate) {
private val libraries: Array<PsiMethod> =
SyntheticAccessorBuilder(project, scope, className, Kind.LIBRARY)
@@ -48,6 +53,17 @@ class SyntheticVersionCatalogAccessor(project: Project, scope: GlobalSearchScope
override fun getName(): String = className
companion object {
fun create(
project: Project,
scope: GlobalSearchScope,
model: GradleVersionCatalogsModel,
className: String,
): SyntheticVersionCatalogAccessor? {
val delegate = JavaPsiFacade.getInstance(project).findClass(CommonClassNames.JAVA_LANG_OBJECT, scope) ?: return null
return SyntheticVersionCatalogAccessor(project, scope, model, className, delegate)
}
private enum class Kind(val prefix: String) {
LIBRARY("Library"), PLUGIN("Plugin"), BUNDLE("Bundle"), VERSION("Version")
}