use of com.intellij.codeInspection.reference.RefManager in project intellij-community by JetBrains.
the class DuplicatePropertyInspection method checkFile.
private void checkFile(final PsiFile file, final InspectionManager manager, GlobalInspectionContextBase context, final RefManager refManager, final ProblemDescriptionsProcessor processor) {
if (!(file instanceof PropertiesFile))
return;
if (!context.isToCheckFile(file, this) || SuppressionUtil.inspectionResultSuppressed(file, this))
return;
final PsiSearchHelper searchHelper = PsiSearchHelper.SERVICE.getInstance(file.getProject());
final PropertiesFile propertiesFile = (PropertiesFile) file;
final List<IProperty> properties = propertiesFile.getProperties();
Module module = ModuleUtilCore.findModuleForPsiElement(file);
if (module == null)
return;
final GlobalSearchScope scope = CURRENT_FILE ? GlobalSearchScope.fileScope(file) : MODULE_WITH_DEPENDENCIES ? GlobalSearchScope.moduleWithDependenciesScope(module) : GlobalSearchScope.projectScope(file.getProject());
final Map<String, Set<PsiFile>> processedValueToFiles = Collections.synchronizedMap(new HashMap<String, Set<PsiFile>>());
final Map<String, Set<PsiFile>> processedKeyToFiles = Collections.synchronizedMap(new HashMap<String, Set<PsiFile>>());
final ProgressIndicator original = ProgressManager.getInstance().getProgressIndicator();
final ProgressIndicator progress = ProgressWrapper.wrap(original);
ProgressManager.getInstance().runProcess(() -> {
if (!JobLauncher.getInstance().invokeConcurrentlyUnderProgress(properties, progress, false, property -> {
if (original != null) {
if (original.isCanceled())
return false;
original.setText2(PropertiesBundle.message("searching.for.property.key.progress.text", property.getUnescapedKey()));
}
processTextUsages(processedValueToFiles, property.getValue(), processedKeyToFiles, searchHelper, scope);
processTextUsages(processedKeyToFiles, property.getUnescapedKey(), processedValueToFiles, searchHelper, scope);
return true;
}))
throw new ProcessCanceledException();
List<ProblemDescriptor> problemDescriptors = new ArrayList<>();
Map<String, Set<String>> keyToDifferentValues = new HashMap<>();
if (CHECK_DUPLICATE_KEYS || CHECK_DUPLICATE_KEYS_WITH_DIFFERENT_VALUES) {
prepareDuplicateKeysByFile(processedKeyToFiles, manager, keyToDifferentValues, problemDescriptors, file, original);
}
if (CHECK_DUPLICATE_VALUES)
prepareDuplicateValuesByFile(processedValueToFiles, manager, problemDescriptors, file, original);
if (CHECK_DUPLICATE_KEYS_WITH_DIFFERENT_VALUES) {
processDuplicateKeysWithDifferentValues(keyToDifferentValues, processedKeyToFiles, problemDescriptors, manager, file, original);
}
if (!problemDescriptors.isEmpty()) {
processor.addProblemElement(refManager.getReference(file), problemDescriptors.toArray(new ProblemDescriptor[problemDescriptors.size()]));
}
}, progress);
}
use of com.intellij.codeInspection.reference.RefManager in project intellij-community by JetBrains.
the class LocalQuickFixWrapper method applyFix.
@Override
protected void applyFix(@NotNull final Project project, @NotNull final GlobalInspectionContextImpl context, @NotNull final CommonProblemDescriptor[] descriptors, @NotNull final Set<PsiElement> ignoredElements) {
if (myFix instanceof BatchQuickFix) {
final List<PsiElement> collectedElementsToIgnore = new ArrayList<>();
final Runnable refreshViews = () -> {
DaemonCodeAnalyzer.getInstance(project).restart();
for (CommonProblemDescriptor descriptor : descriptors) {
ignore(ignoredElements, descriptor, getWorkingQuickFix(descriptor.getFixes()), context);
}
final RefManager refManager = context.getRefManager();
final RefElement[] refElements = new RefElement[collectedElementsToIgnore.size()];
for (int i = 0, collectedElementsToIgnoreSize = collectedElementsToIgnore.size(); i < collectedElementsToIgnoreSize; i++) {
refElements[i] = refManager.getReference(collectedElementsToIgnore.get(i));
}
removeElements(refElements, project, myToolWrapper);
};
((BatchQuickFix) myFix).applyFix(project, descriptors, collectedElementsToIgnore, refreshViews);
return;
}
boolean restart = false;
for (CommonProblemDescriptor descriptor : descriptors) {
if (descriptor == null)
continue;
final QuickFix[] fixes = descriptor.getFixes();
if (fixes != null) {
final QuickFix fix = getWorkingQuickFix(fixes);
if (fix != null) {
//CCE here means QuickFix was incorrectly inherited, is there a way to signal (plugin) it is wrong?
fix.applyFix(project, descriptor);
restart = true;
ignore(ignoredElements, descriptor, fix, context);
}
}
}
if (restart) {
DaemonCodeAnalyzer.getInstance(project).restart();
}
}
use of com.intellij.codeInspection.reference.RefManager in project intellij-community by JetBrains.
the class UnnecessaryModuleDependencyInspection method checkElement.
@Override
public CommonProblemDescriptor[] checkElement(@NotNull RefEntity refEntity, @NotNull AnalysisScope scope, @NotNull InspectionManager manager, @NotNull final GlobalInspectionContext globalContext) {
if (refEntity instanceof RefModule) {
final RefModule refModule = (RefModule) refEntity;
final Module module = refModule.getModule();
if (module.isDisposed() || !scope.containsModule(module))
return CommonProblemDescriptor.EMPTY_ARRAY;
final ModuleRootManager moduleRootManager = ModuleRootManager.getInstance(module);
final OrderEntry[] declaredDependencies = moduleRootManager.getOrderEntries();
final Module[] declaredModuleDependencies = moduleRootManager.getDependencies();
List<CommonProblemDescriptor> descriptors = new ArrayList<>();
final Set<Module> modules = refModule.getUserData(UnnecessaryModuleDependencyAnnotator.DEPENDENCIES);
Graph<Module> graph = myGraph.get();
if (graph == null) {
graph = ModuleManager.getInstance(globalContext.getProject()).moduleGraph();
myGraph = new SoftReference<>(graph);
}
final RefManager refManager = globalContext.getRefManager();
for (final OrderEntry entry : declaredDependencies) {
if (entry instanceof ModuleOrderEntry) {
final Module dependency = ((ModuleOrderEntry) entry).getModule();
if (dependency != null) {
if (modules == null || !modules.contains(dependency)) {
List<String> dependenciesThroughExported = null;
if (((ModuleOrderEntry) entry).isExported()) {
final Iterator<Module> iterator = graph.getOut(module);
while (iterator.hasNext()) {
final Module dep = iterator.next();
final RefModule depRefModule = refManager.getRefModule(dep);
if (depRefModule != null) {
final Set<Module> neededModules = depRefModule.getUserData(UnnecessaryModuleDependencyAnnotator.DEPENDENCIES);
if (neededModules != null && neededModules.contains(dependency)) {
if (dependenciesThroughExported == null) {
dependenciesThroughExported = new ArrayList<>();
}
dependenciesThroughExported.add(dep.getName());
}
}
}
}
if (modules != null) {
List<String> transitiveDependencies = new ArrayList<>();
final OrderEntry[] dependenciesOfDependencies = ModuleRootManager.getInstance(dependency).getOrderEntries();
for (OrderEntry secondDependency : dependenciesOfDependencies) {
if (secondDependency instanceof ModuleOrderEntry && ((ModuleOrderEntry) secondDependency).isExported()) {
final Module mod = ((ModuleOrderEntry) secondDependency).getModule();
if (mod != null && modules.contains(mod) && ArrayUtil.find(declaredModuleDependencies, mod) < 0) {
transitiveDependencies.add(mod.getName());
}
}
}
if (!transitiveDependencies.isEmpty()) {
final String exported = StringUtil.join(transitiveDependencies, ", ");
descriptors.add(manager.createProblemDescriptor(InspectionsBundle.message("unnecessary.module.dependency.exported.problem.descriptor1", module.getName(), dependency.getName(), exported)));
continue;
}
}
descriptors.add(createDescriptor(scope, manager, module, dependency, dependenciesThroughExported));
}
}
}
}
return descriptors.isEmpty() ? null : descriptors.toArray(new CommonProblemDescriptor[descriptors.size()]);
}
return null;
}
use of com.intellij.codeInspection.reference.RefManager in project intellij-community by JetBrains.
the class InconsistentLanguageLevelInspection method runInspection.
@Override
public void runInspection(@NotNull AnalysisScope scope, @NotNull InspectionManager manager, @NotNull GlobalInspectionContext globalContext, @NotNull ProblemDescriptionsProcessor problemProcessor) {
final Set<Module> modules = new THashSet<>();
scope.accept(new PsiElementVisitor() {
@Override
public void visitElement(PsiElement element) {
final Module module = ModuleUtilCore.findModuleForPsiElement(element);
if (module != null) {
modules.add(module);
}
}
});
LanguageLevel projectLanguageLevel = LanguageLevelProjectExtension.getInstance(manager.getProject()).getLanguageLevel();
for (Module module : modules) {
LanguageLevel languageLevel = LanguageLevelModuleExtensionImpl.getInstance(module).getLanguageLevel();
if (languageLevel == null) {
languageLevel = projectLanguageLevel;
}
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();
if (dependantModule == null)
continue;
LanguageLevel dependantLanguageLevel = LanguageLevelModuleExtensionImpl.getInstance(dependantModule).getLanguageLevel();
if (dependantLanguageLevel == null) {
dependantLanguageLevel = projectLanguageLevel;
}
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), (QuickFix) QuickFixFactory.getInstance().createShowModulePropertiesFix(module));
problemProcessor.addProblemElement(refModule, problemDescriptor);
}
}
}
}
use of com.intellij.codeInspection.reference.RefManager in project intellij-community by JetBrains.
the class ClassWithTooManyDependenciesInspection method runInspection.
@Override
public void runInspection(@NotNull AnalysisScope scope, @NotNull final InspectionManager inspectionManager, @NotNull GlobalInspectionContext globalInspectionContext, @NotNull final ProblemDescriptionsProcessor problemDescriptionsProcessor) {
final RefManager refManager = globalInspectionContext.getRefManager();
refManager.iterate(new RefJavaVisitor() {
@Override
public void visitClass(@NotNull RefClass refClass) {
super.visitClass(refClass);
if (refClass.getOwner() instanceof RefClass) {
return;
}
final Set<RefClass> dependencies = DependencyUtils.calculateDependenciesForClass(refClass);
final int numDependencies = dependencies.size();
if (numDependencies <= limit) {
return;
}
final String errorString = InspectionGadgetsBundle.message("class.with.too.many.dependencies.problem.descriptor", refClass.getName(), numDependencies, limit);
final CommonProblemDescriptor[] descriptors = { inspectionManager.createProblemDescriptor(errorString) };
problemDescriptionsProcessor.addProblemElement(refClass, descriptors);
}
});
}
Aggregations