From ef0255c10db196a6aeb3bbbce8eca7315aa6dd6e Mon Sep 17 00:00:00 2001 From: Georgii Ustinov Date: Mon, 29 Apr 2024 15:37:40 +0300 Subject: [PATCH] [Java. Logging] Add javadoc for some methods LoggingArgumentSymbolReferenceProvider.kt and rename DefUsage to PsiElementUsage IDEA-342484 GitOrigin-RevId: e40968673369c6b796f4a71dce087561bc7aad95 --- .../{DefUsage.java => PsiElementUsage.java} | 8 ++++-- .../ig/format/StringFormatUsageSearcher.java | 2 +- .../LoggingPlaceholderAnnotator.kt | 4 +-- .../LoggingArgumentSymbolReferenceProvider.kt | 26 +++++++++++++++++-- .../resolve/LoggingArgumentUsageSearcher.kt | 4 +-- 5 files changed, 35 insertions(+), 9 deletions(-) rename java/java-impl/src/com/siyeh/ig/format/{DefUsage.java => PsiElementUsage.java} (80%) diff --git a/java/java-impl/src/com/siyeh/ig/format/DefUsage.java b/java/java-impl/src/com/siyeh/ig/format/PsiElementUsage.java similarity index 80% rename from java/java-impl/src/com/siyeh/ig/format/DefUsage.java rename to java/java-impl/src/com/siyeh/ig/format/PsiElementUsage.java index 44eff74f875b..41e214382ca3 100644 --- a/java/java-impl/src/com/siyeh/ig/format/DefUsage.java +++ b/java/java-impl/src/com/siyeh/ig/format/PsiElementUsage.java @@ -8,10 +8,14 @@ import com.intellij.psi.PsiElement; import com.intellij.psi.PsiFile; import org.jetbrains.annotations.NotNull; -public class DefUsage implements PsiUsage { +/** + * A class, which represents a PsiUsage, based on concrete PsiElement. + * @see PsiUsage + */ +public class PsiElementUsage implements PsiUsage { private final @NotNull PsiElement myArg; - public DefUsage(@NotNull PsiElement arg) { + public PsiElementUsage(@NotNull PsiElement arg) { myArg = arg; } diff --git a/java/java-impl/src/com/siyeh/ig/format/StringFormatUsageSearcher.java b/java/java-impl/src/com/siyeh/ig/format/StringFormatUsageSearcher.java index 73f028405806..24846ab0e9fd 100644 --- a/java/java-impl/src/com/siyeh/ig/format/StringFormatUsageSearcher.java +++ b/java/java-impl/src/com/siyeh/ig/format/StringFormatUsageSearcher.java @@ -36,7 +36,7 @@ public final class StringFormatUsageSearcher implements UsageSearcher { .flatMap(function) .filter(ref -> ref.resolvesTo(symbol)) .map(PsiUsage::textUsage) - .append(List.of(new DefUsage(arg))) + .append(List.of(new PsiElementUsage(arg))) .toList(); } } diff --git a/jvm/jvm-analysis-impl/src/com/intellij/analysis/logging/highlighting/LoggingPlaceholderAnnotator.kt b/jvm/jvm-analysis-impl/src/com/intellij/analysis/logging/highlighting/LoggingPlaceholderAnnotator.kt index 5627a62c5e7f..808ddd714479 100644 --- a/jvm/jvm-analysis-impl/src/com/intellij/analysis/logging/highlighting/LoggingPlaceholderAnnotator.kt +++ b/jvm/jvm-analysis-impl/src/com/intellij/analysis/logging/highlighting/LoggingPlaceholderAnnotator.kt @@ -2,7 +2,7 @@ package com.intellij.analysis.logging.highlighting import com.intellij.analysis.customization.console.ClassFinderConsoleColorsPage -import com.intellij.analysis.logging.resolve.getAlignedPlaceholderCount +import com.intellij.analysis.logging.resolve.getAdjustedPlaceholderList import com.intellij.analysis.logging.resolve.getContext import com.intellij.analysis.logging.resolve.getPlaceholderRanges import com.intellij.lang.annotation.AnnotationHolder @@ -43,6 +43,6 @@ class LoggingPlaceholderAnnotator : Annotator { ranges.filterNotNull() } - return getAlignedPlaceholderCount(textRangeList, context) + return getAdjustedPlaceholderList(textRangeList, context) } } \ No newline at end of file diff --git a/jvm/jvm-analysis-impl/src/com/intellij/analysis/logging/resolve/LoggingArgumentSymbolReferenceProvider.kt b/jvm/jvm-analysis-impl/src/com/intellij/analysis/logging/resolve/LoggingArgumentSymbolReferenceProvider.kt index ae32064fb63d..c05d0aab9e42 100644 --- a/jvm/jvm-analysis-impl/src/com/intellij/analysis/logging/resolve/LoggingArgumentSymbolReferenceProvider.kt +++ b/jvm/jvm-analysis-impl/src/com/intellij/analysis/logging/resolve/LoggingArgumentSymbolReferenceProvider.kt @@ -36,9 +36,15 @@ fun getLogArgumentReferences(uExpression: UExpression): List } }.flatten() - return getAlignedPlaceholderCount(loggerReferenceList, context) + return getAdjustedPlaceholderList(loggerReferenceList, context) } +/** + * Retrieves the context of a placeholder in a logger statement. + * + * @param uExpression The UExpression representing the placeholder. + * @return The PlaceholderContext object if the placeholder context is found, otherwise null. + */ internal fun getContext(uExpression: UExpression): PlaceholderContext? { val uCallExpression = uExpression.getParentOfType() ?: return null val logMethod = detectLoggerMethod(uCallExpression) ?: return null @@ -48,7 +54,15 @@ internal fun getContext(uExpression: UExpression): PlaceholderContext? { return context } -internal fun getAlignedPlaceholderCount(placeholderList: List, context: PlaceholderContext): List? { +/** + * Retrieves a list of placeholders, for which there is a resolve argument exists. + * This list might be different from initial input, because, for example, the number of arguments is less, + * than the number of placeholders or last argument could be an exception. + * @param placeholderList The list of placeholders. It might be a text ranges or + * @param context The placeholder context. + * @return The adjusted list of placeholders, or null if the logger type is not supported. + */ +internal fun getAdjustedPlaceholderList(placeholderList: List, context: PlaceholderContext): List? { val placeholderParametersSize = context.placeholderParameters.size return when (context.loggerType) { SLF4J -> { @@ -69,6 +83,14 @@ internal fun getAlignedPlaceholderCount(placeholderList: List, context: P } } +/** + * Retrieves a list of placeholder ranges from the given `context`. + * + * @param context The [PlaceholderContext] object containing the necessary data for retrieving placeholder ranges. + * @return A list of PlaceholderRanges or null if the logStringArgument is null or the number of placeholders is not exact. + * @see PlaceholderContext + * @see PlaceholderRanges + */ internal fun getPlaceholderRanges(context: PlaceholderContext): List? { val logStringText = context.logStringArgument.sourcePsi?.text ?: return null val type = if (isKotlinMultilineString(context.logStringArgument, logStringText)) { diff --git a/jvm/jvm-analysis-impl/src/com/intellij/analysis/logging/resolve/LoggingArgumentUsageSearcher.kt b/jvm/jvm-analysis-impl/src/com/intellij/analysis/logging/resolve/LoggingArgumentUsageSearcher.kt index 2855a2d641dd..3040cd9ec7e8 100644 --- a/jvm/jvm-analysis-impl/src/com/intellij/analysis/logging/resolve/LoggingArgumentUsageSearcher.kt +++ b/jvm/jvm-analysis-impl/src/com/intellij/analysis/logging/resolve/LoggingArgumentUsageSearcher.kt @@ -7,7 +7,7 @@ import com.intellij.find.usages.api.UsageSearchParameters import com.intellij.find.usages.api.UsageSearcher import com.intellij.model.psi.PsiSymbolReference import com.intellij.util.Query -import com.siyeh.ig.format.DefUsage +import com.siyeh.ig.format.PsiElementUsage class LoggingArgumentUsageSearcher : UsageSearcher { override fun collectSearchRequests(parameters: UsageSearchParameters): Collection> { @@ -16,7 +16,7 @@ class LoggingArgumentUsageSearcher : UsageSearcher { val uLiteralExpression = target.getPlaceholderString() ?: return emptyList() return getLogArgumentReferences(uLiteralExpression)?.let { it.filter { ref: PsiSymbolReference -> ref.resolvesTo(target) } - .flatMap { usage -> listOf(PsiUsage.textUsage(usage), DefUsage(target.expression)) } + .flatMap { usage -> listOf(PsiUsage.textUsage(usage), PsiElementUsage(target.expression)) } .map { psiUsage -> LoggingArgumentPsiUsageQuery(psiUsage) } } ?: emptyList() }