From 303a9b3bf9f19f6f47e4ecc543cfb18947b094fd Mon Sep 17 00:00:00 2001 From: Alexey Kudravtsev Date: Thu, 31 Aug 2023 14:01:50 +0200 Subject: [PATCH] notnull GitOrigin-RevId: 14c079597f5e076f2ae7d333b4abd7fd12619289 --- .../codeInspection/SuppressManagerImpl.java | 2 +- .../codeInspection/RedundantSuppressTest.java | 2 +- .../RedundantSuppressionDetector.java | 2 +- .../RedundantSuppressInspectionBase.java | 92 ++++++++++--------- .../offline/OfflineProblemDescriptor.java | 18 ++-- .../OfflineDescriptorResolveResult.java | 28 +++--- 6 files changed, 76 insertions(+), 68 deletions(-) diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/SuppressManagerImpl.java b/java/java-analysis-impl/src/com/intellij/codeInspection/SuppressManagerImpl.java index de58bbddf43a..eb69fb19bd21 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInspection/SuppressManagerImpl.java +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/SuppressManagerImpl.java @@ -83,7 +83,7 @@ public class SuppressManagerImpl extends SuppressManager implements RedundantSup } @Override - public LocalQuickFix createRemoveRedundantSuppressionFix(@NotNull String toolId) { + public @NotNull LocalQuickFix createRemoveRedundantSuppressionFix(@NotNull String toolId) { return new RemoveSuppressWarningAction(toolId); } } diff --git a/java/java-tests/testSrc/com/intellij/java/codeInspection/RedundantSuppressTest.java b/java/java-tests/testSrc/com/intellij/java/codeInspection/RedundantSuppressTest.java index 16cce3c7c9b6..6442c777422a 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInspection/RedundantSuppressTest.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInspection/RedundantSuppressTest.java @@ -47,7 +47,7 @@ public class RedundantSuppressTest extends JavaInspectionTestCase { myWrapper = new GlobalInspectionToolWrapper(new RedundantSuppressInspection() { @Override - protected @NotNull List> getInspectionTools(PsiElement psiElement, @NotNull InspectionProfile profile) { + protected @NotNull List> getInspectionTools(@NotNull PsiElement psiElement, @NotNull InspectionProfile profile) { return myInspectionToolWrappers; } }); diff --git a/platform/analysis-api/src/com/intellij/codeInspection/RedundantSuppressionDetector.java b/platform/analysis-api/src/com/intellij/codeInspection/RedundantSuppressionDetector.java index 50c509887db5..cf6dbfa8da00 100644 --- a/platform/analysis-api/src/com/intellij/codeInspection/RedundantSuppressionDetector.java +++ b/platform/analysis-api/src/com/intellij/codeInspection/RedundantSuppressionDetector.java @@ -20,7 +20,7 @@ public interface RedundantSuppressionDetector { /** * @return quickfix to remove {@code toolId} suppression from list of suppressions */ - @Nullable + @NotNull LocalQuickFix createRemoveRedundantSuppressionFix(@NotNull String toolId); /** diff --git a/platform/analysis-impl/src/com/intellij/codeInspection/RedundantSuppressInspectionBase.java b/platform/analysis-impl/src/com/intellij/codeInspection/RedundantSuppressInspectionBase.java index 368f927c3d3c..65a93ededdd3 100644 --- a/platform/analysis-impl/src/com/intellij/codeInspection/RedundantSuppressInspectionBase.java +++ b/platform/analysis-impl/src/com/intellij/codeInspection/RedundantSuppressInspectionBase.java @@ -57,13 +57,13 @@ public abstract class RedundantSuppressInspectionBase extends GlobalSimpleInspec InspectionSuppressor extension = ContainerUtil.find(LanguageInspectionSuppressors.INSTANCE.allForLanguage(file.getLanguage()), s -> s instanceof RedundantSuppressionDetector); if (!(extension instanceof RedundantSuppressionDetector redundantSuppressionDetector)) return; InspectionProfileImpl profile = getProfile(manager, globalContext); - final CommonProblemDescriptor[] descriptors = checkElement(file, redundantSuppressionDetector, manager, profile); + CommonProblemDescriptor[] descriptors = checkElement(file, redundantSuppressionDetector, manager, profile); for (CommonProblemDescriptor descriptor : descriptors) { if (descriptor instanceof ProblemDescriptor) { - final PsiElement psiElement = ((ProblemDescriptor)descriptor).getPsiElement(); + PsiElement psiElement = ((ProblemDescriptor)descriptor).getPsiElement(); if (psiElement != null) { - final PsiElement member = globalContext.getRefManager().getContainerElement(psiElement); - final RefElement reference = globalContext.getRefManager().getReference(member); + PsiElement member = globalContext.getRefManager().getContainerElement(psiElement); + RefElement reference = globalContext.getRefManager().getReference(member); if (reference != null) { problemDescriptionsProcessor.addProblemElement(reference, descriptor); } @@ -77,11 +77,11 @@ public abstract class RedundantSuppressInspectionBase extends GlobalSimpleInspec } } - private ProblemDescriptor @NotNull [] checkElement(final @NotNull PsiFile psiFile, - RedundantSuppressionDetector extension, - final @NotNull InspectionManager manager, + private ProblemDescriptor @NotNull [] checkElement(@NotNull PsiFile psiFile, + @NotNull RedundantSuppressionDetector extension, + @NotNull InspectionManager manager, @NotNull InspectionProfile profile) { - final Map> suppressedScopes = new HashMap<>(); + Map> suppressedScopes = new HashMap<>(); psiFile.accept(new PsiRecursiveElementWalkingVisitor() { @Override public void visitElement(@NotNull PsiElement element) { @@ -98,8 +98,7 @@ public abstract class RedundantSuppressInspectionBase extends GlobalSimpleInspec for (Collection ids : suppressedScopes.values()) { for (Iterator iterator = ids.iterator(); iterator.hasNext(); ) { String suppressId = iterator.next().trim(); - List> reportingWrappers = - findReportingTools(toolWrappers, suppressId, language); + List> reportingWrappers = findReportingTools(toolWrappers, suppressId, language); if (reportingWrappers.isEmpty()) { iterator.remove(); } @@ -111,17 +110,18 @@ public abstract class RedundantSuppressInspectionBase extends GlobalSimpleInspec } } - final AnalysisScope scope = new AnalysisScope(psiFile); + AnalysisScope scope = new AnalysisScope(psiFile); - final GlobalInspectionContextBase globalContext = createContext(psiFile); + GlobalInspectionContextBase globalContext = createContext(psiFile); globalContext.setCurrentScope(scope); List result = new ArrayList<>(); ((RefManagerImpl)globalContext.getRefManager()).runInsideInspectionReadAction(() -> { try { - for (InspectionToolWrapper toolWrapper : suppressedTools.keySet()) { - String toolId = suppressedTools.get(toolWrapper); + for (Map.Entry, String> entry : suppressedTools.entrySet()) { + InspectionToolWrapper toolWrapper = entry.getKey(); + String toolId = entry.getValue(); toolWrapper.initialize(globalContext); - final Collection descriptors; + Collection descriptors; if (toolWrapper instanceof LocalInspectionToolWrapper local) { if (local.isUnfair()) { continue; // can't work with passes other than LocalInspectionPass @@ -145,8 +145,9 @@ public abstract class RedundantSuppressInspectionBase extends GlobalSimpleInspec else { continue; } - for (PsiElement suppressedScope : suppressedScopes.keySet()) { - Collection suppressedIds = suppressedScopes.get(suppressedScope); + for (Map.Entry> e : suppressedScopes.entrySet()) { + Collection suppressedIds = e.getValue(); + PsiElement suppressedScope = e.getKey(); if (!suppressedIds.contains(toolId)) continue; for (CommonProblemDescriptor descriptor : descriptors) { if (!(descriptor instanceof ProblemDescriptor)) continue; @@ -160,8 +161,9 @@ public abstract class RedundantSuppressInspectionBase extends GlobalSimpleInspec } } } - for (PsiElement suppressedScope : suppressedScopes.keySet()) { - Collection suppressedIds = suppressedScopes.get(suppressedScope); + for (Map.Entry> entry : suppressedScopes.entrySet()) { + PsiElement suppressedScope = entry.getKey(); + Collection suppressedIds = entry.getValue(); for (String toolId : suppressedIds) { PsiElement documentedElement = globalContext.getRefManager().getContainerElement(suppressedScope); if (documentedElement != null && documentedElement.isValid()) { @@ -198,25 +200,25 @@ public abstract class RedundantSuppressInspectionBase extends GlobalSimpleInspec } @NotNull - public LocalInspectionTool createLocalTool(RedundantSuppressionDetector suppressor, - Map> toolToSuppressScopes, - Set activeTools) { + public LocalInspectionTool createLocalTool(@NotNull RedundantSuppressionDetector suppressor, + @NotNull Map> toolToSuppressScopes, + @NotNull Set activeTools) { return new LocalRedundantSuppressionInspection(suppressor, activeTools, toolToSuppressScopes); } - protected @NotNull List> getInspectionTools(PsiElement psiElement, @NotNull InspectionProfile profile) { + protected @NotNull List> getInspectionTools(@NotNull PsiElement psiElement, @NotNull InspectionProfile profile) { return profile.getInspectionTools(psiElement); } @Override - public synchronized @Nullable QuickFix getQuickFix(final String hint) { + public synchronized @Nullable QuickFix getQuickFix(String hint) { return myQuickFixes != null ? myQuickFixes.get(hint) : createQuickFix(hint); } @Override - public synchronized @Nullable String getHint(final @NotNull QuickFix fix) { + public synchronized @Nullable String getHint(@NotNull QuickFix fix) { if (myQuickFixes != null) { - final List list = myQuickFixes.getKeysByValue(fix); + List list = myQuickFixes.getKeysByValue(fix); if (list != null) { LOG.assertTrue(list.size() == 1); return list.get(0); @@ -230,12 +232,13 @@ public abstract class RedundantSuppressInspectionBase extends GlobalSimpleInspec return false; } - protected GlobalInspectionContextBase createContext(PsiFile file) { - final InspectionManager inspectionManagerEx = InspectionManager.getInstance(file.getProject()); + @NotNull + protected static GlobalInspectionContextBase createContext(@NotNull PsiFile file) { + InspectionManager inspectionManagerEx = InspectionManager.getInstance(file.getProject()); return (GlobalInspectionContextBase)inspectionManagerEx.createNewGlobalContext(); } - private static InspectionProfileImpl getProfile(InspectionManager manager, GlobalInspectionContext globalContext) { + private static @NotNull InspectionProfileImpl getProfile(@NotNull InspectionManager manager, @NotNull GlobalInspectionContext globalContext) { if (globalContext instanceof GlobalInspectionContextBase) { InspectionProfileImpl profile = ((GlobalInspectionContextBase)globalContext).getCurrentProfile(); if (profile.getSingleTool() == null) { @@ -247,9 +250,10 @@ public abstract class RedundantSuppressInspectionBase extends GlobalSimpleInspec return ObjectUtils.notNull(profileManager.getProfile(currentProfileName, false), profileManager.getCurrentProfile()); } + @NotNull private static List> findReportingTools(@NotNull List> toolWrappers, - String suppressedId, - Language language) { + @NotNull String suppressedId, + @NotNull Language language) { List> wrappers = Collections.emptyList(); List mergedToolName = InspectionElementsMerger.getMergedToolNames(suppressedId); for (InspectionToolWrapper toolWrapper : toolWrappers) { @@ -279,14 +283,14 @@ public abstract class RedundantSuppressInspectionBase extends GlobalSimpleInspec } private static boolean collectSuppressions(@NotNull PsiElement element, - Map> suppressedScopes, + @NotNull Map> suppressedScopes, boolean ignoreAll, - RedundantSuppressionDetector suppressor) { + @NotNull RedundantSuppressionDetector suppressor) { String idsString = suppressor.getSuppressionIds(element); if (idsString != null && !idsString.isEmpty()) { List ids = new ArrayList<>(); StringUtil.tokenize(idsString, "[, ]").forEach(ids::add); - boolean isSuppressAll = ids.stream().anyMatch(id -> id.equalsIgnoreCase(SuppressionUtil.ALL)); + boolean isSuppressAll = ContainerUtil.exists(ids, id -> id.equalsIgnoreCase(SuppressionUtil.ALL)); if (ignoreAll && isSuppressAll) { return false; } @@ -307,7 +311,7 @@ public abstract class RedundantSuppressInspectionBase extends GlobalSimpleInspec return false; } - private static QuickFix createQuickFix(String key) { + private static QuickFix createQuickFix(@NotNull String key) { String[] toolAndLang = key.split(";"); Language language = toolAndLang.length < 2 ? null : Language.findLanguageByID(toolAndLang[1]); if (language == null) return null; @@ -317,13 +321,16 @@ public abstract class RedundantSuppressInspectionBase extends GlobalSimpleInspec } private final class LocalRedundantSuppressionInspection extends LocalInspectionTool implements UnfairLocalInspectionTool { + @NotNull private final RedundantSuppressionDetector mySuppressor; + @NotNull private final Set myActiveTools; - private final Map> myToolToSuppressScopes; + @NotNull + private final Map> myToolToSuppressScopes; - private LocalRedundantSuppressionInspection(RedundantSuppressionDetector suppressor, - Set activeTools, - Map> toolToSuppressScopes) { + private LocalRedundantSuppressionInspection(@NotNull RedundantSuppressionDetector suppressor, + @NotNull Set activeTools, + @NotNull Map> toolToSuppressScopes) { mySuppressor = suppressor; myActiveTools = activeTools; myToolToSuppressScopes = toolToSuppressScopes; @@ -352,8 +359,7 @@ public abstract class RedundantSuppressInspectionBase extends GlobalSimpleInspec if (myActiveTools.contains(suppressId) && !isSuppressedFor(element, suppressId, myToolToSuppressScopes.get(suppressId)) && //suppression in local pass is intentionally disabled to pass ALL - !SuppressionUtil.inspectionResultSuppressed(element, - LocalRedundantSuppressionInspection.this)) { + !SuppressionUtil.inspectionResultSuppressed(element, LocalRedundantSuppressionInspection.this)) { TextRange highlightingRange = mySuppressor.getHighlightingRange(element, suppressId); if (highlightingRange != null) { holder.registerProblem(element, highlightingRange, @@ -365,9 +371,9 @@ public abstract class RedundantSuppressInspectionBase extends GlobalSimpleInspec } } - private boolean isSuppressedFor(PsiElement element, String suppressId, Set suppressedPlaces) { + private boolean isSuppressedFor(@NotNull PsiElement element, @NotNull String suppressId, Set suppressedPlaces) { return suppressedPlaces != null && - suppressedPlaces.stream().anyMatch(place -> mySuppressor.isSuppressionFor(element, place, suppressId)); + ContainerUtil.exists(suppressedPlaces, place -> mySuppressor.isSuppressionFor(element, place, suppressId)); } }; } diff --git a/platform/lang-api/src/com/intellij/codeInspection/offline/OfflineProblemDescriptor.java b/platform/lang-api/src/com/intellij/codeInspection/offline/OfflineProblemDescriptor.java index ac329085a9a3..df5d942fccc0 100644 --- a/platform/lang-api/src/com/intellij/codeInspection/offline/OfflineProblemDescriptor.java +++ b/platform/lang-api/src/com/intellij/codeInspection/offline/OfflineProblemDescriptor.java @@ -28,14 +28,14 @@ import java.util.List; import java.util.Objects; public class OfflineProblemDescriptor { - public String myType; - public String myFQName; - public @InspectionMessage String myDescription; - public List myHints; - public int myProblemIndex; - public int myLine; - public int myOffset; - public String myModuleName; + private String myType; + private String myFQName; + private @InspectionMessage String myDescription; + private List myHints; + private int myProblemIndex; + private int myLine; + private int myOffset; + private String myModuleName; public String getType() { return myType; @@ -65,7 +65,7 @@ public class OfflineProblemDescriptor { return myHints; } - public void setHints(List hints) { + public void setHints(@NotNull List<@NotNull String> hints) { myHints = hints; } diff --git a/platform/lang-impl/src/com/intellij/codeInspection/offlineViewer/OfflineDescriptorResolveResult.java b/platform/lang-impl/src/com/intellij/codeInspection/offlineViewer/OfflineDescriptorResolveResult.java index cb3913899a7a..51b9b7be2099 100644 --- a/platform/lang-impl/src/com/intellij/codeInspection/offlineViewer/OfflineDescriptorResolveResult.java +++ b/platform/lang-impl/src/com/intellij/codeInspection/offlineViewer/OfflineDescriptorResolveResult.java @@ -38,8 +38,6 @@ import org.jetbrains.annotations.Nullable; import java.util.*; -import static com.intellij.openapi.util.Predicates.nonNull; - /** * @author Dmitry Batkovich */ @@ -49,7 +47,7 @@ public final class OfflineDescriptorResolveResult { private final CommonProblemDescriptor myResolvedDescriptor; private volatile boolean myExcluded; - private OfflineDescriptorResolveResult(RefEntity resolvedEntity, CommonProblemDescriptor resolvedDescriptor) { + private OfflineDescriptorResolveResult(@Nullable RefEntity resolvedEntity, @Nullable CommonProblemDescriptor resolvedDescriptor) { myResolvedEntity = resolvedEntity; myResolvedDescriptor = resolvedDescriptor; } @@ -104,16 +102,14 @@ public final class OfflineDescriptorResolveResult { if (element instanceof RefElement) { PsiElement psiElement = ((RefElement)element).getPsiElement(); if (psiElement != null) { - ProblemDescriptor descriptor = ProgressManager.getInstance().runProcess( + return ProgressManager.getInstance().runProcess( () -> runLocalTool(psiElement, offlineDescriptor, (LocalInspectionToolWrapper)toolWrapper, presentation.getContext()), new DaemonProgressIndicator()); - if (descriptor != null) return descriptor; } return null; } - } CommonProblemDescriptor descriptor = createProblemDescriptorFromOfflineDescriptor(element, @@ -222,7 +218,7 @@ public final class OfflineDescriptorResolveResult { return null; } - private static PsiElement @NotNull [] getElementsIntersectingRange(PsiFile file, int startOffset, int endOffset) { + private static PsiElement @NotNull [] getElementsIntersectingRange(@NotNull PsiFile file, int startOffset, int endOffset) { FileViewProvider viewProvider = file.getViewProvider(); Set result = new LinkedHashSet<>(); for (Language language : viewProvider.getLanguages()) { @@ -235,8 +231,9 @@ public final class OfflineDescriptorResolveResult { } private static @NotNull QuickFix @Nullable [] getFixes(@NotNull CommonProblemDescriptor descriptor, - RefEntity entity, - InspectionToolPresentation presentation, List hints) { + @Nullable RefEntity entity, + @NotNull InspectionToolPresentation presentation, + @Nullable List hints) { List> fixes = new ArrayList<>(hints == null ? 1 : hints.size()); if (hints == null) { addFix(descriptor, entity, fixes, null, presentation); @@ -249,10 +246,15 @@ public final class OfflineDescriptorResolveResult { return fixes.isEmpty() ? null : fixes.toArray(QuickFix.EMPTY_ARRAY); } - private static void addFix(@NotNull CommonProblemDescriptor descriptor, RefEntity entity, List> fixes, String hint, @NotNull InspectionToolPresentation presentation) { + private static void addFix(@NotNull CommonProblemDescriptor descriptor, + @Nullable RefEntity entity, + @NotNull List> fixes, + @Nullable String hint, + @NotNull InspectionToolPresentation presentation) { ContainerUtil.addAllNotNull(fixes, presentation.findQuickFixes(descriptor, entity, hint)); } + @NotNull private static CommonProblemDescriptor createRerunGlobalToolDescriptor(@NotNull GlobalInspectionToolWrapper wrapper, @Nullable RefEntity entity, @NotNull OfflineProblemDescriptor offlineDescriptor, @@ -285,7 +287,7 @@ public final class OfflineDescriptorResolveResult { }; List hints = offlineDescriptor.getHints(); if (hints != null && entity instanceof RefModule) { - List fixes = hints.stream().map(hint -> wrapper.getTool().getQuickFix(hint)).filter(nonNull()).toList(); + List fixes = ContainerUtil.mapNotNull(hints, hint -> wrapper.getTool().getQuickFix(hint)); return InspectionManager.getInstance(project).createProblemDescriptor(offlineDescriptor.getDescription(), ((RefModule)entity).getModule(), ArrayUtil.append(fixes.toArray(QuickFix.EMPTY_ARRAY), rerunFix)); } return InspectionManager.getInstance(project).createProblemDescriptor(offlineDescriptor.getDescription(), rerunFix); @@ -296,8 +298,8 @@ public final class OfflineDescriptorResolveResult { private final OfflineProblemDescriptor myOfflineProblemDescriptor; private final QuickFix[] myFixes; - private ProblemDescriptorBackedByRefElement(RefElement element, - OfflineProblemDescriptor descriptor, + private ProblemDescriptorBackedByRefElement(@NotNull RefElement element, + @NotNull OfflineProblemDescriptor descriptor, @NotNull QuickFix @NotNull [] fixes) { myElement = element; myOfflineProblemDescriptor = descriptor;