[Java. Logging] Move logger import exclusion check to JvmLogger

IDEA-345098

GitOrigin-RevId: 4c9e81289b0d98c6ed844a00865c070bdad665a3
This commit is contained in:
Georgii Ustinov
2024-02-12 16:54:00 +02:00
committed by intellij-monorepo-bot
parent 89c3856151
commit 0b90553973
5 changed files with 37 additions and 16 deletions

View File

@@ -1,28 +1,21 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.codeInsight.generation
import com.intellij.codeInsight.completion.JavaCompletionUtil
import com.intellij.lang.logging.JvmLogger
import com.intellij.openapi.module.Module
import com.intellij.openapi.project.Project
import com.intellij.psi.*
import com.intellij.psi.search.GlobalSearchScope
import com.intellij.psi.PsiAnonymousClass
import com.intellij.psi.PsiClass
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiImplicitClass
import com.intellij.psi.util.parentsOfType
object GenerateLoggerUtil {
fun findSuitableLoggers(module: Module?, filterByImportExclusion : Boolean = false): List<JvmLogger> {
val project = module?.project ?: return emptyList()
return JvmLogger.getAllLoggers(false).filter {
it.isAvailable(module) && !(filterByImportExclusion && isLoggerExcluded(project, it))
it.isAvailable(module) && !(filterByImportExclusion && it.isExcludedFromImport(module?.project))
}
}
private fun isLoggerExcluded(project: Project, logger: JvmLogger) : Boolean {
val clazz = JavaPsiFacade.getInstance(project).findClass(logger.loggerTypeName, GlobalSearchScope.everythingScope(project))
?: return true
return JavaCompletionUtil.isInExcludedPackage(clazz, false)
}
fun getAllNestedClasses(element: PsiElement) = element.parentsOfType(PsiClass::class.java, true)
.filter { clazz -> clazz !is PsiAnonymousClass && clazz !is PsiImplicitClass }

View File

@@ -1,12 +1,15 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.lang.logging
import com.intellij.codeInsight.completion.JavaCompletionUtil
import com.intellij.lang.logging.UnspecifiedLogger.Companion.UNSPECIFIED_LOGGER_NAME
import com.intellij.openapi.extensions.ExtensionPointName
import com.intellij.openapi.module.Module
import com.intellij.openapi.project.Project
import com.intellij.psi.JavaPsiFacade
import com.intellij.psi.PsiClass
import com.intellij.psi.PsiElement
import com.intellij.psi.search.GlobalSearchScope
import org.jetbrains.annotations.ApiStatus
/**
@@ -36,7 +39,7 @@ interface JvmLogger {
fun isOnlyOnStartup() = false
/**
* Inner method for inserting the logger at the specified class. Should only be invoked inside WriteAction.
* Method for inserting the logger at the specified class. Should only be invoked inside WriteAction.
* @param project the project context
* @param clazz the class where the logger element will be inserted
* @param logger PsiElement, corresponding to the logger to be inserted
@@ -49,7 +52,7 @@ interface JvmLogger {
* @param project the project context
* @return true if the logger is available, false otherwise
*/
fun isAvailable(project: Project?) : Boolean
fun isAvailable(project: Project?): Boolean
/**
* Determines if the logger is available for the given module. Should only be invoked inside ReadAction.
@@ -57,7 +60,15 @@ interface JvmLogger {
* @param module the module context
* @return true if the logger is available, false otherwise
*/
fun isAvailable(module: Module?) : Boolean
fun isAvailable(module: Module?): Boolean
/**
* Determines if the logger is excluded from import in the current project.
*
* @param project the project context
* @return true if the logger class is excluded for import in the project, false otherwise
*/
fun isExcludedFromImport(project: Project?): Boolean
/**
* Determines if it is possible to place a logger at the specified class.
@@ -65,7 +76,7 @@ interface JvmLogger {
* @param clazz the class where the logger will be placed
* @return true if it is possible to place a logger, false otherwise
*/
fun isPossibleToPlaceLoggerAtClass(clazz: PsiClass) : Boolean
fun isPossibleToPlaceLoggerAtClass(clazz: PsiClass): Boolean
/**
* Creates a logger element for inserting into a class.
@@ -91,5 +102,14 @@ interface JvmLogger {
if (loggerName == UNSPECIFIED_LOGGER_NAME) return null
return EP_NAME.extensionList.find { it.toString() == loggerName }
}
fun areLoggerTypesExcluded(project: Project?, loggerTypes: List<String>): Boolean {
if (project == null) return true
val facade = JavaPsiFacade.getInstance(project)
return loggerTypes.any { classFqn ->
val clazz = facade.findClass(classFqn, GlobalSearchScope.everythingScope(project)) ?: return true
JavaCompletionUtil.isInExcludedPackage(clazz, false)
}
}
}
}

View File

@@ -36,6 +36,9 @@ class JvmLoggerFieldDelegate(
override fun isAvailable(module: Module?): Boolean = JavaLibraryUtil.hasLibraryClass(module, loggerTypeName)
override fun isExcludedFromImport(project: Project?): Boolean =
JvmLogger.areLoggerTypesExcluded(project, listOf(factoryName, loggerTypeName))
override fun isPossibleToPlaceLoggerAtClass(clazz: PsiClass): Boolean = clazz
.fields.any { it.name == LOGGER_IDENTIFIER || it.type.canonicalText == loggerTypeName }.not()

View File

@@ -23,6 +23,8 @@ class UnspecifiedLogger : JvmLogger {
override fun isAvailable(module: Module?): Boolean = false
override fun isExcludedFromImport(project: Project?): Boolean = true
override fun isPossibleToPlaceLoggerAtClass(clazz: PsiClass): Boolean = false
override fun createLogger(project: Project, clazz: PsiClass): PsiElement = throw UnsupportedOperationException()

View File

@@ -40,6 +40,9 @@ class JvmLoggerAnnotationDelegate(
return module != null && JavaLibraryUtil.hasLibraryClass(module, fieldLoggerName) && LombokLibraryUtil.hasLombokClasses(module)
}
override fun isExcludedFromImport(project: Project?): Boolean =
JvmLogger.areLoggerTypesExcluded(project, listOf(fieldLoggerName, loggerTypeName))
override fun isPossibleToPlaceLoggerAtClass(clazz: PsiClass): Boolean = clazz.hasAnnotation(loggerTypeName).not()
override fun createLogger(project: Project, clazz: PsiClass): PsiAnnotation {