Search in sources :

Example 1 with RefManager

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);
}
Also used : java.util(java.util) ActionListener(java.awt.event.ActionListener) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) JobLauncher(com.intellij.concurrency.JobLauncher) VirtualFile(com.intellij.openapi.vfs.VirtualFile) URL(java.net.URL) Document(com.intellij.openapi.editor.Document) THashSet(gnu.trove.THashSet) LowLevelSearchUtil(com.intellij.psi.impl.search.LowLevelSearchUtil) StringSearcher(com.intellij.util.text.StringSearcher) RefManager(com.intellij.codeInspection.reference.RefManager) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException) Comparing(com.intellij.openapi.util.Comparing) ProgressWrapper(com.intellij.openapi.progress.util.ProgressWrapper) PsiElement(com.intellij.psi.PsiElement) PsiFile(com.intellij.psi.PsiFile) Logger(com.intellij.openapi.diagnostic.Logger) Module(com.intellij.openapi.module.Module) ProgressManager(com.intellij.openapi.progress.ProgressManager) Property(com.intellij.lang.properties.psi.Property) PsiSearchHelper(com.intellij.psi.search.PsiSearchHelper) PropertiesBundle(com.intellij.lang.properties.PropertiesBundle) ModuleUtilCore(com.intellij.openapi.module.ModuleUtilCore) MalformedURLException(java.net.MalformedURLException) StringUtil(com.intellij.openapi.util.text.StringUtil) Processors(com.intellij.util.Processors) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) com.intellij.codeInspection(com.intellij.codeInspection) ActionEvent(java.awt.event.ActionEvent) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) IProperty(com.intellij.lang.properties.IProperty) GlobalInspectionContextBase(com.intellij.codeInspection.ex.GlobalInspectionContextBase) NotNull(org.jetbrains.annotations.NotNull) CharArrayUtil(com.intellij.util.text.CharArrayUtil) javax.swing(javax.swing) THashSet(gnu.trove.THashSet) PsiSearchHelper(com.intellij.psi.search.PsiSearchHelper) IProperty(com.intellij.lang.properties.IProperty) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) Module(com.intellij.openapi.module.Module) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException)

Example 2 with RefManager

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();
    }
}
Also used : QuickFix(com.intellij.codeInspection.QuickFix) BatchQuickFix(com.intellij.codeInspection.BatchQuickFix) BatchQuickFix(com.intellij.codeInspection.BatchQuickFix) CommonProblemDescriptor(com.intellij.codeInspection.CommonProblemDescriptor) RefManager(com.intellij.codeInspection.reference.RefManager) ArrayList(java.util.ArrayList) PsiElement(com.intellij.psi.PsiElement)

Example 3 with RefManager

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;
}
Also used : RefManager(com.intellij.codeInspection.reference.RefManager) ArrayList(java.util.ArrayList) ModuleRootManager(com.intellij.openapi.roots.ModuleRootManager) RefModule(com.intellij.codeInspection.reference.RefModule) ModuleOrderEntry(com.intellij.openapi.roots.ModuleOrderEntry) OrderEntry(com.intellij.openapi.roots.OrderEntry) ModuleOrderEntry(com.intellij.openapi.roots.ModuleOrderEntry) RefModule(com.intellij.codeInspection.reference.RefModule) Module(com.intellij.openapi.module.Module)

Example 4 with RefManager

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);
            }
        }
    }
}
Also used : RefManager(com.intellij.codeInspection.reference.RefManager) PsiElementVisitor(com.intellij.psi.PsiElementVisitor) THashSet(gnu.trove.THashSet) RefModule(com.intellij.codeInspection.reference.RefModule) UnnecessaryModuleDependencyInspection(com.intellij.codeInspection.unnecessaryModuleDependency.UnnecessaryModuleDependencyInspection) LanguageLevel(com.intellij.pom.java.LanguageLevel) RefModule(com.intellij.codeInspection.reference.RefModule) Module(com.intellij.openapi.module.Module) PsiElement(com.intellij.psi.PsiElement)

Example 5 with RefManager

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);
        }
    });
}
Also used : RefJavaVisitor(com.intellij.codeInspection.reference.RefJavaVisitor) RefClass(com.intellij.codeInspection.reference.RefClass) Set(java.util.Set) RefManager(com.intellij.codeInspection.reference.RefManager)

Aggregations

RefManager (com.intellij.codeInspection.reference.RefManager)5 Module (com.intellij.openapi.module.Module)3 PsiElement (com.intellij.psi.PsiElement)3 RefModule (com.intellij.codeInspection.reference.RefModule)2 THashSet (gnu.trove.THashSet)2 ArrayList (java.util.ArrayList)2 com.intellij.codeInspection (com.intellij.codeInspection)1 BatchQuickFix (com.intellij.codeInspection.BatchQuickFix)1 CommonProblemDescriptor (com.intellij.codeInspection.CommonProblemDescriptor)1 QuickFix (com.intellij.codeInspection.QuickFix)1 GlobalInspectionContextBase (com.intellij.codeInspection.ex.GlobalInspectionContextBase)1 RefClass (com.intellij.codeInspection.reference.RefClass)1 RefJavaVisitor (com.intellij.codeInspection.reference.RefJavaVisitor)1 UnnecessaryModuleDependencyInspection (com.intellij.codeInspection.unnecessaryModuleDependency.UnnecessaryModuleDependencyInspection)1 JobLauncher (com.intellij.concurrency.JobLauncher)1 IProperty (com.intellij.lang.properties.IProperty)1 PropertiesBundle (com.intellij.lang.properties.PropertiesBundle)1 PropertiesFile (com.intellij.lang.properties.psi.PropertiesFile)1 Property (com.intellij.lang.properties.psi.Property)1 Logger (com.intellij.openapi.diagnostic.Logger)1