diff --git a/jvm/jvm-analysis-impl/src/com/intellij/codeInspection/logging/LoggingPlaceholderUtil.kt b/jvm/jvm-analysis-impl/src/com/intellij/codeInspection/logging/LoggingPlaceholderUtil.kt index 56aeadb7b0de..849177e147bf 100644 --- a/jvm/jvm-analysis-impl/src/com/intellij/codeInspection/logging/LoggingPlaceholderUtil.kt +++ b/jvm/jvm-analysis-impl/src/com/intellij/codeInspection/logging/LoggingPlaceholderUtil.kt @@ -16,7 +16,7 @@ const val MAX_BUILDER_LENGTH = 20 const val ADD_ARGUMENT_METHOD_NAME = "addArgument" const val SET_MESSAGE_METHOD_NAME = "setMessage" -interface LoggerTypeSearcher { +internal interface LoggerTypeSearcher { fun findType(expression: UCallExpression, context: LoggerContext): PlaceholderLoggerType? } @@ -30,7 +30,7 @@ private val SLF4J_HOLDER = object : LoggerTypeSearcher { } -val LOG4J_LOG_BUILDER_HOLDER = object : LoggerTypeSearcher { +internal val LOG4J_LOG_BUILDER_HOLDER = object : LoggerTypeSearcher { override fun findType(expression: UCallExpression, context: LoggerContext): PlaceholderLoggerType? { var qualifierExpression = getImmediateLoggerQualifier(expression) if (qualifierExpression is UReferenceExpression) { @@ -54,7 +54,7 @@ val LOG4J_LOG_BUILDER_HOLDER = object : LoggerTypeSearcher { } } -val SLF4J_BUILDER_HOLDER = object : LoggerTypeSearcher { +internal val SLF4J_BUILDER_HOLDER = object : LoggerTypeSearcher { override fun findType(expression: UCallExpression, context: LoggerContext): PlaceholderLoggerType { if (context.log4jAsImplementationForSlf4j) { return PlaceholderLoggerType.SLF4J_EQUAL_PLACEHOLDERS @@ -105,37 +105,37 @@ private val AKKA_PLACEHOLDERS = object : LoggerTypeSearcher { } } -val IDEA_PLACEHOLDERS = object : LoggerTypeSearcher { +internal val IDEA_PLACEHOLDERS = object : LoggerTypeSearcher { override fun findType(expression: UCallExpression, context: LoggerContext): PlaceholderLoggerType? { return null } } -enum class ResultType { +internal enum class ResultType { PARTIAL_PLACE_HOLDER_MISMATCH, PLACE_HOLDER_MISMATCH, INCORRECT_STRING, SUCCESS } -enum class PlaceholderLoggerType { +internal enum class PlaceholderLoggerType { SLF4J, SLF4J_EQUAL_PLACEHOLDERS, LOG4J_OLD_STYLE, LOG4J_FORMATTED_STYLE, LOG4J_EQUAL_PLACEHOLDERS, AKKA_PLACEHOLDERS } -enum class PlaceholdersStatus { +internal enum class PlaceholdersStatus { EXACTLY, PARTIAL, ERROR_TO_PARSE_STRING, EMPTY } -class LoggerContext(val log4jAsImplementationForSlf4j: Boolean) +internal class LoggerContext(val log4jAsImplementationForSlf4j: Boolean) /** * A data class representing the result of a placeholder count operation. * - * @property placeholderRangesInPartHolderList The list of [PlaceholderRangesInPartHolder] instances. + * @property placeholderRangeList The list of the ranges, corresponding to the placeholder * @property status The status of the placeholders ranges extraction. * * @see countBracesPlaceholders */ -data class PlaceholderCountResult(val placeholderRangeList: List, val status: PlaceholdersStatus) { +internal data class PlaceholderCountResult(val placeholderRangeList: List, val status: PlaceholdersStatus) { val count = placeholderRangeList.size } @@ -151,7 +151,7 @@ data class PlaceholderCountResult(val placeholderRangeList: List, va * * @see getPlaceholderContext */ -data class PlaceholderContext( +internal data class PlaceholderContext( val placeholderParameters: List, val logStringArgument: UExpression, val partHolderList: List, @@ -160,21 +160,21 @@ data class PlaceholderContext( val lastArgumentIsSupplier: Boolean, ) -val LOGGER_BUILDER_LOG_TYPE_SEARCHERS: CallMapper = CallMapper() +internal val LOGGER_BUILDER_LOG_TYPE_SEARCHERS: CallMapper = CallMapper() .register(CallMatcher.instanceCall(LoggingUtil.SLF4J_EVENT_BUILDER, "log"), SLF4J_BUILDER_HOLDER) .register(CallMatcher.instanceCall(LoggingUtil.LOG4J_LOG_BUILDER, "log"), LOG4J_LOG_BUILDER_HOLDER) -val LOGGER_RESOLVE_TYPE_SEARCHERS: CallMapper = LOGGER_BUILDER_LOG_TYPE_SEARCHERS +internal val LOGGER_RESOLVE_TYPE_SEARCHERS: CallMapper = LOGGER_BUILDER_LOG_TYPE_SEARCHERS .register(CallMatcher.instanceCall(LoggingUtil.SLF4J_LOGGER, "trace", "debug", "info", "warn", "error"), SLF4J_HOLDER) .register(CallMatcher.instanceCall(LoggingUtil.IDEA_LOGGER, "trace", "debug", "info", "warn", "error"), IDEA_PLACEHOLDERS) .register(CallMatcher.instanceCall(LoggingUtil.LOG4J_LOGGER, "trace", "debug", "info", "warn", "error", "fatal", "log"), LOG4J_HOLDER) -val LOGGER_TYPE_SEARCHERS: CallMapper = LOGGER_RESOLVE_TYPE_SEARCHERS +internal val LOGGER_TYPE_SEARCHERS: CallMapper = LOGGER_RESOLVE_TYPE_SEARCHERS .register(CallMatcher.instanceCall(LoggingUtil.AKKA_LOGGING, "debug", "error", "format", "info", "log", "warning"), AKKA_PLACEHOLDERS) private val BUILDER_CHAIN = setOf("addKeyValue", "addMarker", "setCause") -fun getLogStringIndex(parameters: List): Int? { +internal fun getLogStringIndex(parameters: List): Int? { if (parameters.isEmpty()) return null if (!TypeUtils.isJavaLangString(parameters[0].type)) { if (parameters.size < 2 || !TypeUtils.isJavaLangString(parameters[1].type)) { @@ -189,7 +189,7 @@ fun getLogStringIndex(parameters: List): Int? { } } -fun detectLoggerMethod(uCallExpression: UCallExpression): UCallExpression? { +internal fun detectLoggerMethod(uCallExpression: UCallExpression): UCallExpression? { val name = uCallExpression.methodName return if (name == ADD_ARGUMENT_METHOD_NAME || name == SET_MESSAGE_METHOD_NAME) { detectLoggerBuilderMethod(uCallExpression) ?: return null @@ -214,7 +214,7 @@ private fun getImmediateLoggerQualifier(expression: UCallExpression): UExpressio return result } -fun findMessageSetterStringArg(node: UCallExpression, +internal fun findMessageSetterStringArg(node: UCallExpression, loggerType: LoggerTypeSearcher?): UExpression? { if (loggerType == null) { return null @@ -251,7 +251,7 @@ fun findMessageSetterStringArg(node: UCallExpression, /** * @return The count of additional arguments, or null if it is impossible to count. */ -fun findAdditionalArgumentCount(node: UCallExpression, +internal fun findAdditionalArgumentCount(node: UCallExpression, loggerType: LoggerTypeSearcher, allowIntermediateMessage: Boolean): List? { val uExpressions = mutableListOf() @@ -294,7 +294,7 @@ fun findAdditionalArgumentCount(node: UCallExpression, * * @return PlaceholderCountResult returns the result of either countFormattedPlaceholders or countBracesPlaceholders based on the type of the logger. */ -fun solvePlaceholderCount( +internal fun solvePlaceholderCount( loggerType: PlaceholderLoggerType, argumentCount: Int, holders: List, @@ -345,7 +345,7 @@ private fun countFormattedPlaceholders(holders: List, log4jAsImplementationForSlf4j: Boolean @@ -399,11 +399,11 @@ fun getPlaceholderContext( ) } -fun collectParts(logStringArgument: UExpression): List? { +internal fun collectParts(logStringArgument: UExpression): List? { return LoggingStringPartEvaluator.calculateValue(logStringArgument) } -fun hasThrowableType(lastArgument: UExpression): Boolean { +internal fun hasThrowableType(lastArgument: UExpression): Boolean { val type = lastArgument.getExpressionType() if (type is UastErrorType) { return false diff --git a/jvm/jvm-analysis-impl/src/com/intellij/codeInspection/logging/LoggingStringPartEvaluator.kt b/jvm/jvm-analysis-impl/src/com/intellij/codeInspection/logging/LoggingStringPartEvaluator.kt index cd91fbd06799..392a29d1ce8b 100644 --- a/jvm/jvm-analysis-impl/src/com/intellij/codeInspection/logging/LoggingStringPartEvaluator.kt +++ b/jvm/jvm-analysis-impl/src/com/intellij/codeInspection/logging/LoggingStringPartEvaluator.kt @@ -10,19 +10,19 @@ import com.siyeh.ig.psiutils.TypeUtils import org.jetbrains.uast.* import org.jetbrains.uast.visitor.AbstractUastVisitor -class LoggingStringPartEvaluator { +internal class LoggingStringPartEvaluator { /** * @param text - null if it is a literal, which is not String or Character * @param isConstant - it is a constant */ - data class PartHolder(val text: String?, val isConstant: Boolean, val callPart: CallPart? = null) + internal data class PartHolder(val text: String?, val isConstant: Boolean, val callPart: CallPart? = null) - data class CallPart(val stringArguments: List) + internal data class CallPart(val stringArguments: List) private data class Context(val depth: Int, val maxParts: Int) companion object { - fun calculateValue(expression: UExpression): List? { + internal fun calculateValue(expression: UExpression): List? { if (!isString(expression)) return null val sourcePsi = expression.sourcePsi ?: return null val project = sourcePsi.project diff --git a/jvm/jvm-analysis-impl/src/com/intellij/codeInspection/logging/LoggingStringTemplateAsArgumentInspection.kt b/jvm/jvm-analysis-impl/src/com/intellij/codeInspection/logging/LoggingStringTemplateAsArgumentInspection.kt index 2cd55a2294f0..92bdf3e72356 100644 --- a/jvm/jvm-analysis-impl/src/com/intellij/codeInspection/logging/LoggingStringTemplateAsArgumentInspection.kt +++ b/jvm/jvm-analysis-impl/src/com/intellij/codeInspection/logging/LoggingStringTemplateAsArgumentInspection.kt @@ -3,6 +3,10 @@ package com.intellij.codeInspection.logging import com.intellij.analysis.JvmAnalysisBundle import com.intellij.codeInspection.* +import com.intellij.codeInspection.logging.LoggingUtil.Companion +import com.intellij.codeInspection.logging.LoggingUtil.Companion.LOG_MATCHERS +import com.intellij.codeInspection.logging.LoggingUtil.Companion.countPlaceHolders +import com.intellij.codeInspection.logging.LoggingUtil.Companion.isGuarded import com.intellij.codeInspection.options.OptPane import com.intellij.lang.Language import com.intellij.openapi.project.Project @@ -12,10 +16,6 @@ import com.intellij.psi.PsiType import com.intellij.psi.util.InheritanceUtil import com.intellij.psi.util.TypeConversionUtil import com.intellij.uast.UastHintedVisitorAdapter -import com.intellij.codeInspection.logging.LoggingUtil.Companion -import com.intellij.codeInspection.logging.LoggingUtil.Companion.LOG_MATCHERS -import com.intellij.codeInspection.logging.LoggingUtil.Companion.countPlaceHolders -import com.intellij.codeInspection.logging.LoggingUtil.Companion.isGuarded import org.jetbrains.uast.* import org.jetbrains.uast.expressions.UInjectionHost import org.jetbrains.uast.generate.getUastElementFactory diff --git a/jvm/jvm-analysis-impl/src/com/intellij/codeInspection/logging/LoggingUtil.kt b/jvm/jvm-analysis-impl/src/com/intellij/codeInspection/logging/LoggingUtil.kt index e0cf1a4c6482..a5b7f51c4596 100644 --- a/jvm/jvm-analysis-impl/src/com/intellij/codeInspection/logging/LoggingUtil.kt +++ b/jvm/jvm-analysis-impl/src/com/intellij/codeInspection/logging/LoggingUtil.kt @@ -12,13 +12,13 @@ import org.jetbrains.uast.visitor.AbstractUastVisitor class LoggingUtil { companion object { - const val SLF4J_LOGGER = "org.slf4j.Logger" + internal const val SLF4J_LOGGER = "org.slf4j.Logger" - const val LOG4J_LOGGER = "org.apache.logging.log4j.Logger" + internal const val LOG4J_LOGGER = "org.apache.logging.log4j.Logger" - const val LOG4J_LOG_BUILDER = "org.apache.logging.log4j.LogBuilder" + internal const val LOG4J_LOG_BUILDER = "org.apache.logging.log4j.LogBuilder" - const val SLF4J_EVENT_BUILDER = "org.slf4j.spi.LoggingEventBuilder" + internal const val SLF4J_EVENT_BUILDER = "org.slf4j.spi.LoggingEventBuilder" private const val LEGACY_LOG4J_LOGGER = "org.apache.log4j.Logger" private const val LEGACY_CATEGORY_LOGGER = "org.apache.log4j.Category" @@ -27,7 +27,7 @@ class LoggingUtil { internal const val AKKA_LOGGING = "akka.event.LoggingAdapter" - const val IDEA_LOGGER = "com.intellij.openapi.diagnostic.Logger" + internal const val IDEA_LOGGER = "com.intellij.openapi.diagnostic.Logger" private val LOGGER_CLASSES = setOf(SLF4J_LOGGER, LOG4J_LOGGER) private val LEGACY_LOGGER_CLASSES = setOf(LEGACY_LOG4J_LOGGER, LEGACY_CATEGORY_LOGGER, @@ -39,29 +39,29 @@ class LoggingUtil { private val LOG4J_BUILDER_MATCHER: CallMatcher.Simple = CallMatcher.instanceCall(LOG4J_LOG_BUILDER, "log") private val SLF4J_BUILDER_MATCHER: CallMatcher.Simple = CallMatcher.instanceCall(SLF4J_EVENT_BUILDER, "log") - val LOG_MATCHERS: CallMatcher = CallMatcher.anyOf( + internal val LOG_MATCHERS: CallMatcher = CallMatcher.anyOf( SLF4J_MATCHER, LOG4J_MATCHER, LOG4J_BUILDER_MATCHER, SLF4J_BUILDER_MATCHER, ) - val LOG_MATCHERS_WITHOUT_BUILDERS: CallMatcher = CallMatcher.anyOf( + internal val LOG_MATCHERS_WITHOUT_BUILDERS: CallMatcher = CallMatcher.anyOf( SLF4J_MATCHER, LOG4J_MATCHER, ) internal val FORMATTED_LOG4J: CallMatcher = CallMatcher.staticCall("org.apache.logging.log4j.LogManager", "getFormatterLogger") - const val LOG_4_J_LOGGER = "org.apache.logging.slf4j.Log4jLogger" + internal const val LOG_4_J_LOGGER = "org.apache.logging.slf4j.Log4jLogger" - val LEGACY_LOG_MATCHERS: CallMatcher = CallMatcher.anyOf( - CallMatcher.instanceCall(LEGACY_LOG4J_LOGGER, "trace", "debug", "info", "warn", "error", "fatal", "log", "l7dlog"), - CallMatcher.instanceCall(LEGACY_CATEGORY_LOGGER, "debug", "info", "warn", "error", "fatal", "log", "l7dlog"), - CallMatcher.instanceCall(LEGACY_APACHE_COMMON_LOGGER, "trace", "debug", "info", "warn", "error", "fatal"), - CallMatcher.instanceCall(LEGACY_JAVA_LOGGER, "fine", "log", "finer", "finest", "logp", "logrb", "info", "severe", "warning", "config") + internal val LEGACY_LOG_MATCHERS: CallMatcher = CallMatcher.anyOf( + CallMatcher.instanceCall(LEGACY_LOG4J_LOGGER, "trace", "debug", "info", "warn", "error", "fatal", "log", "l7dlog"), + CallMatcher.instanceCall(LEGACY_CATEGORY_LOGGER, "debug", "info", "warn", "error", "fatal", "log", "l7dlog"), + CallMatcher.instanceCall(LEGACY_APACHE_COMMON_LOGGER, "trace", "debug", "info", "warn", "error", "fatal"), + CallMatcher.instanceCall(LEGACY_JAVA_LOGGER, "fine", "log", "finer", "finest", "logp", "logrb", "info", "severe", "warning", "config") ) - val IDEA_LOG_MATCHER: CallMatcher = CallMatcher.anyOf( + internal val IDEA_LOG_MATCHER: CallMatcher = CallMatcher.anyOf( CallMatcher.instanceCall(IDEA_LOGGER, "trace", "debug", "info", "warn", "error"), ) @@ -73,7 +73,7 @@ class LoggingUtil { private val LEVEL_CLASSES = setOf("org.apache.logging.log4j.Level", "org.slf4j.event.Level") private val LEGACY_LEVEL_CLASSES = setOf("org.apache.logging.log4j.Level", "org.apache.log4j.Priority", "java.util.logging.Level") - fun skipAccordingLevel(node: UCallExpression, myLimitLevelType: LimitLevelType): Boolean { + internal fun skipAccordingLevel(node: UCallExpression, myLimitLevelType: LimitLevelType): Boolean { if (myLimitLevelType != LimitLevelType.ALL) { val loggerLevel = getLoggerLevel(node) if (loggerLevel == null) return true @@ -92,7 +92,7 @@ class LoggingUtil { } } - fun getLoggerType(uCall: UCallExpression?): LoggerType? { + internal fun getLoggerType(uCall: UCallExpression?): LoggerType? { return if (SLF4J_MATCHER.uCallMatches(uCall)) { LoggerType.SLF4J_LOGGER_TYPE } @@ -110,22 +110,22 @@ class LoggingUtil { } } - fun isGuarded(call: UCallExpression): Boolean { + internal fun isGuarded(call: UCallExpression): Boolean { val loggerLevel = getLoggerLevel(call) ?: return false val guardedCondition = getGuardedCondition(call) ?: return false val levelFromCondition = getLevelFromCondition(guardedCondition) ?: return false return isGuardedIn(levelFromCondition, loggerLevel) } - fun isGuardedIn(levelFromCondition: LevelType, loggerLevel: LevelType): Boolean { + internal fun isGuardedIn(levelFromCondition: LevelType, loggerLevel: LevelType): Boolean { return levelFromCondition == loggerLevel } - fun isLegacyGuardedIn(levelFromCondition: LegacyLevelType, loggerLevel: LegacyLevelType): Boolean { + internal fun isLegacyGuardedIn(levelFromCondition: LegacyLevelType, loggerLevel: LegacyLevelType): Boolean { return levelFromCondition == loggerLevel } - fun getLegacyLevelFromCondition(condition: UExpression): LegacyLevelType? { + internal fun getLegacyLevelFromCondition(condition: UExpression): LegacyLevelType? { if (condition is UCallExpression) { val methodName = condition.methodName ?: return null if ("isEnabledFor" == methodName || "isLoggable" == methodName) { @@ -143,7 +143,7 @@ class LoggingUtil { return null } - fun getLevelFromCondition(condition: UExpression): LevelType? { + internal fun getLevelFromCondition(condition: UExpression): LevelType? { if (condition is UCallExpression) { val methodName = condition.methodName ?: return null if ("isEnabled" == methodName || "isEnabledForLevel" == methodName) { @@ -161,7 +161,7 @@ class LoggingUtil { return null } - fun getGuardedCondition(call: UCallExpression?): UExpression? { + internal fun getGuardedCondition(call: UCallExpression?): UExpression? { if (call == null) return null val loggerSource = getLoggerQualifier(call) ?: return null var ifExpression: UIfExpression? = call.getParentOfType() ?: return null @@ -271,7 +271,7 @@ class LoggingUtil { return null } - fun getLegacyLoggerLevel(uCall: UCallExpression?): LegacyLevelType? { + internal fun getLegacyLoggerLevel(uCall: UCallExpression?): LegacyLevelType? { if (uCall == null) { return null } @@ -288,7 +288,7 @@ class LoggingUtil { return findLevelTypeByName(methodName, LEGACY_LEVEL_MAP) } - fun getLoggerLevel(uCall: UCallExpression?, isLog: Boolean = false): LevelType? { + internal fun getLoggerLevel(uCall: UCallExpression?, isLog: Boolean = false): LevelType? { if (uCall == null) { return null } @@ -357,7 +357,7 @@ class LoggingUtil { return null } - fun countPlaceHolders(text: String, loggerType: LoggerType?): Int { + internal fun countPlaceHolders(text: String, loggerType: LoggerType?): Int { var count = 0 var placeHolder = false var escaped = false @@ -385,7 +385,7 @@ class LoggingUtil { return count } - fun getLoggerCalls(guardedCondition: UExpression): List { + internal fun getLoggerCalls(guardedCondition: UExpression): List { val sourcePsi = guardedCondition.sourcePsi ?: return emptyList() return CachedValuesManager.getManager(sourcePsi.project).getCachedValue(sourcePsi, CachedValueProvider { val emptyResult = CachedValueProvider.Result.create(listOf(), PsiModificationTracker.MODIFICATION_COUNT) @@ -420,27 +420,27 @@ class LoggingUtil { }) } - fun hasBridgeFromSlf4jToLog4j2(element: UElement): Boolean { + internal fun hasBridgeFromSlf4jToLog4j2(element: UElement): Boolean { val file = element.getContainingUFile() ?: return true val sourcePsi = file.sourcePsi val project = sourcePsi.project return JavaLibraryUtil.hasLibraryClass(project, LOG_4_J_LOGGER) } - enum class LoggerType { + internal enum class LoggerType { SLF4J_LOGGER_TYPE, SLF4J_BUILDER_TYPE, LOG4J_LOGGER_TYPE, LOG4J_BUILDER_TYPE } - enum class LevelType { + internal enum class LevelType { FATAL, ERROR, WARN, INFO, DEBUG, TRACE } @Suppress("unused") - enum class LegacyLevelType { + internal enum class LegacyLevelType { FATAL, ERROR, SEVERE, WARN, WARNING, INFO, DEBUG, TRACE, CONFIG, FINE, FINER, FINEST } - val GUARD_MAP = mapOf( + val GUARD_MAP = mapOf( Pair("isTraceEnabled", "trace"), Pair("isDebugEnabled", "debug"), Pair("isInfoEnabled", "info"),