rewritten as global inspection

This commit is contained in:
Alexey Kudravtsev
2013-06-04 13:05:47 +04:00
parent 72990b46fd
commit af8ee7e86f
3 changed files with 62 additions and 62 deletions
@@ -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<Module> modules = new THashSet<Module>();
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;
@@ -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<VirtualFile> libraryRoots = new ArrayList<VirtualFile>();
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<PsiFile, Set<PsiFile>> dependencies = builder.getDependencies();
for (PsiFile file : dependencies.keySet()) {
final VirtualFile virtualFile = file.getVirtualFile();
LOG.assertTrue(virtualFile != null);
for (Iterator<VirtualFile> i = libraryRoots.iterator(); i.hasNext();) {
for (Iterator<VirtualFile> 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<VirtualFile, String>() {
@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;
@@ -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);