From af8ee7e86f4f922d899ca5931d2b1bb9361a9259 Mon Sep 17 00:00:00 2001 From: Alexey Kudravtsev Date: Tue, 4 Jun 2013 13:05:47 +0400 Subject: [PATCH] rewritten as global inspection --- .../InconsistentLanguageLevelInspection.java | 31 ++----- .../UnusedLibrariesInspection.java | 91 +++++++++++-------- .../ex/GlobalInspectionContextImpl.java | 2 +- 3 files changed, 62 insertions(+), 62 deletions(-) diff --git a/java/java-impl/src/com/intellij/codeInspection/inconsistentLanguageLevel/InconsistentLanguageLevelInspection.java b/java/java-impl/src/com/intellij/codeInspection/inconsistentLanguageLevel/InconsistentLanguageLevelInspection.java index 9ff80b54409d..4c0e2227e038 100644 --- a/java/java-impl/src/com/intellij/codeInspection/inconsistentLanguageLevel/InconsistentLanguageLevelInspection.java +++ b/java/java-impl/src/com/intellij/codeInspection/inconsistentLanguageLevel/InconsistentLanguageLevelInspection.java @@ -22,15 +22,10 @@ package com.intellij.codeInspection.inconsistentLanguageLevel; import com.intellij.analysis.AnalysisScope; import com.intellij.codeInsight.daemon.GroupNames; -import com.intellij.codeInspection.CommonProblemDescriptor; -import com.intellij.codeInspection.GlobalInspectionContext; -import com.intellij.codeInspection.InspectionManager; -import com.intellij.codeInspection.QuickFix; -import com.intellij.codeInspection.ex.DescriptorProviderInspection; -import com.intellij.codeInspection.ex.JobDescriptor; +import com.intellij.codeInspection.*; +import com.intellij.codeInspection.reference.RefManager; import com.intellij.codeInspection.reference.RefModule; import com.intellij.codeInspection.unnecessaryModuleDependency.UnnecessaryModuleDependencyInspection; -import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.module.Module; import com.intellij.openapi.module.ModuleUtil; import com.intellij.openapi.project.Project; @@ -47,11 +42,12 @@ import org.jetbrains.annotations.NotNull; import java.util.Set; -public class InconsistentLanguageLevelInspection extends DescriptorProviderInspection{ - private static final Logger LOGGER = Logger.getInstance("#" + InconsistentLanguageLevelInspection.class.getName()); - +public class InconsistentLanguageLevelInspection extends GlobalInspectionTool { @Override - public void runInspection(@NotNull AnalysisScope scope, @NotNull InspectionManager manager) { + public void runInspection(@NotNull AnalysisScope scope, + @NotNull InspectionManager manager, + @NotNull GlobalInspectionContext globalContext, + @NotNull ProblemDescriptionsProcessor problemProcessor) { final Set modules = new THashSet(); scope.accept(new PsiElementVisitor(){ @Override @@ -69,8 +65,8 @@ public class InconsistentLanguageLevelInspection extends DescriptorProviderInspe if (languageLevel == null) { languageLevel = projectLanguageLevel; } - LOGGER.assertTrue(languageLevel != null); - final RefModule refModule = getRefManager().getRefModule(module); + RefManager refManager = globalContext.getRefManager(); + final RefModule refModule = refManager.getRefModule(module); for (OrderEntry entry : ModuleRootManager.getInstance(module).getOrderEntries()) { if (!(entry instanceof ModuleOrderEntry)) continue; final Module dependantModule = ((ModuleOrderEntry)entry).getModule(); @@ -79,25 +75,18 @@ public class InconsistentLanguageLevelInspection extends DescriptorProviderInspe if (dependantLanguageLevel == null) { dependantLanguageLevel = projectLanguageLevel; } - LOGGER.assertTrue(dependantLanguageLevel != null); if (languageLevel.compareTo(dependantLanguageLevel) < 0) { final CommonProblemDescriptor problemDescriptor = manager.createProblemDescriptor( "Inconsistent language level settings: module " + module.getName() + " with language level " + languageLevel + " depends on module " + dependantModule.getName() +" with language level " + dependantLanguageLevel, new UnnecessaryModuleDependencyInspection.RemoveModuleDependencyFix(module, dependantModule), new OpenModuleSettingsFix(module)); - addProblemElement(refModule, problemDescriptor); + problemProcessor.addProblemElement(refModule, problemDescriptor); } } } } - @Override - @NotNull - public JobDescriptor[] getJobDescriptors(@NotNull GlobalInspectionContext globalInspectionContext) { - return JobDescriptor.EMPTY_ARRAY; - } - @Override public boolean isEnabledByDefault() { return false; diff --git a/java/java-impl/src/com/intellij/codeInspection/unusedLibraries/UnusedLibrariesInspection.java b/java/java-impl/src/com/intellij/codeInspection/unusedLibraries/UnusedLibrariesInspection.java index c77d57989141..288202f9089f 100644 --- a/java/java-impl/src/com/intellij/codeInspection/unusedLibraries/UnusedLibrariesInspection.java +++ b/java/java-impl/src/com/intellij/codeInspection/unusedLibraries/UnusedLibrariesInspection.java @@ -23,7 +23,7 @@ package com.intellij.codeInspection.unusedLibraries; import com.intellij.analysis.AnalysisScope; import com.intellij.codeInsight.daemon.GroupNames; import com.intellij.codeInspection.*; -import com.intellij.codeInspection.ex.DescriptorProviderInspection; +import com.intellij.codeInspection.ex.GlobalInspectionContextImpl; import com.intellij.codeInspection.ex.JobDescriptor; import com.intellij.codeInspection.reference.RefManager; import com.intellij.codeInspection.reference.RefModule; @@ -60,13 +60,17 @@ import org.jetbrains.annotations.NotNull; import java.util.*; -public class UnusedLibrariesInspection extends DescriptorProviderInspection { +public class UnusedLibrariesInspection extends GlobalInspectionTool { private static final Logger LOG = Logger.getInstance("#" + UnusedLibrariesInspection.class.getName()); private final JobDescriptor BACKWARD_ANALYSIS = new JobDescriptor(InspectionsBundle.message("unused.library.backward.analysis.job.description")); @Override - public void runInspection(@NotNull final AnalysisScope scope, @NotNull final InspectionManager manager) { - final Project project = getContext().getProject(); + public void runInspection(@NotNull AnalysisScope scope, + @NotNull InspectionManager manager, + @NotNull final GlobalInspectionContext globalContext, + @NotNull ProblemDescriptionsProcessor problemProcessor) { + ((GlobalInspectionContextImpl)globalContext).appendJobDescriptor(BACKWARD_ANALYSIS); + final Project project = manager.getProject(); final ArrayList libraryRoots = new ArrayList(); if (scope.getScopeType() == AnalysisScope.PROJECT) { ContainerUtil.addAll(libraryRoots, LibraryUtil.getLibraryRoots(project, false, false)); @@ -110,33 +114,43 @@ public class UnusedLibrariesInspection extends DescriptorProviderInspection { final ProgressIndicator progressIndicator = ProgressManager.getInstance().getProgressIndicator(); BACKWARD_ANALYSIS.setTotalAmount(builder.getTotalFileCount()); - ((ProgressManagerImpl)ProgressManager.getInstance()).executeProcessUnderProgress(new Runnable(){ - @Override - public void run() { - builder.analyze(); - } - }, new ProgressIndicatorBase() { - @Override - public void setFraction(final double fraction) { - super.setFraction(fraction); - int nextAmount = (int)(fraction * BACKWARD_ANALYSIS.getTotalAmount()); - if (nextAmount > BACKWARD_ANALYSIS.getDoneAmount() && nextAmount < BACKWARD_ANALYSIS.getTotalAmount()) { - BACKWARD_ANALYSIS.setDoneAmount(nextAmount); - getContext().incrementJobDoneAmount(BACKWARD_ANALYSIS, getText2()); - } - } + ((ProgressManagerImpl)ProgressManager.getInstance()).executeProcessUnderProgress(new Runnable() { + @Override + public void run() { + builder.analyze(); + } + }, new ProgressIndicatorBase() { + @Override + public void setFraction(final double fraction) { + super.setFraction(fraction); + int nextAmount = (int)(fraction * + BACKWARD_ANALYSIS + .getTotalAmount()); + if (nextAmount > + BACKWARD_ANALYSIS.getDoneAmount() && + nextAmount < + BACKWARD_ANALYSIS.getTotalAmount()) { + BACKWARD_ANALYSIS.setDoneAmount(nextAmount); + globalContext + .incrementJobDoneAmount(BACKWARD_ANALYSIS, + getText2()); + } + } - @Override - public boolean isCanceled() { - return progressIndicator != null && progressIndicator.isCanceled() || super.isCanceled(); - } - }); + @Override + public boolean isCanceled() { + return progressIndicator != null && + progressIndicator.isCanceled() || + super.isCanceled(); + } + } + ); BACKWARD_ANALYSIS.setDoneAmount(BACKWARD_ANALYSIS.getTotalAmount()); final Map> dependencies = builder.getDependencies(); for (PsiFile file : dependencies.keySet()) { final VirtualFile virtualFile = file.getVirtualFile(); LOG.assertTrue(virtualFile != null); - for (Iterator i = libraryRoots.iterator(); i.hasNext();) { + for (Iterator i = libraryRoots.iterator(); i.hasNext(); ) { if (VfsUtil.isAncestor(i.next(), virtualFile, false)) { i.remove(); } @@ -158,7 +172,7 @@ public class UnusedLibrariesInspection extends DescriptorProviderInspection { files.add(libraryRoot); } } - final RefManager refManager = getRefManager(); + final RefManager refManager = globalContext.getRefManager(); for (OrderEntry orderEntry : unusedLibs.keySet()) { if (!(orderEntry instanceof LibraryOrderEntry)) continue; final RefModule refModule = refManager.getRefModule(orderEntry.getOwnerModule()); @@ -166,27 +180,24 @@ public class UnusedLibrariesInspection extends DescriptorProviderInspection { final VirtualFile[] roots = ((LibraryOrderEntry)orderEntry).getRootFiles(OrderRootType.CLASSES); if (files.size() < roots.length) { final String unusedLibraryRoots = StringUtil.join(files, new Function() { - @Override - public String fun(final VirtualFile file) { - return file.getPresentableName(); - } - }, ","); - String message = InspectionsBundle.message("unused.library.roots.problem.descriptor", unusedLibraryRoots, orderEntry.getPresentableName()); - addProblemElement(refModule, manager.createProblemDescriptor(message, new RemoveUnusedLibrary(refModule, orderEntry, files))); + @Override + public String fun(final VirtualFile file) { + return file.getPresentableName(); + } + }, ","); + String message = + InspectionsBundle.message("unused.library.roots.problem.descriptor", unusedLibraryRoots, orderEntry.getPresentableName()); + problemProcessor.addProblemElement(refModule, + manager.createProblemDescriptor(message, new RemoveUnusedLibrary(refModule, orderEntry, files))); } else { String message = InspectionsBundle.message("unused.library.problem.descriptor", orderEntry.getPresentableName()); - addProblemElement(refModule, manager.createProblemDescriptor(message, new RemoveUnusedLibrary(refModule, orderEntry, null))); + problemProcessor.addProblemElement(refModule, + manager.createProblemDescriptor(message, new RemoveUnusedLibrary(refModule, orderEntry, null))); } } } - @Override - @NotNull - public JobDescriptor[] getJobDescriptors(@NotNull GlobalInspectionContext globalInspectionContext) { - return new JobDescriptor[] {BACKWARD_ANALYSIS}; - } - @Override public boolean isEnabledByDefault() { return false; diff --git a/platform/lang-impl/src/com/intellij/codeInspection/ex/GlobalInspectionContextImpl.java b/platform/lang-impl/src/com/intellij/codeInspection/ex/GlobalInspectionContextImpl.java index e56386b5e52f..e8549bd7f37a 100644 --- a/platform/lang-impl/src/com/intellij/codeInspection/ex/GlobalInspectionContextImpl.java +++ b/platform/lang-impl/src/com/intellij/codeInspection/ex/GlobalInspectionContextImpl.java @@ -799,7 +799,7 @@ public class GlobalInspectionContextImpl extends UserDataHolderBase implements G return myTools; } - private void appendJobDescriptor(@NotNull JobDescriptor job) { + public void appendJobDescriptor(@NotNull JobDescriptor job) { if (!myJobDescriptors.contains(job)) { myJobDescriptors.add(job); job.setDoneAmount(0);