Search in sources :

Example 1 with GlobalInspectionContextBase

use of com.intellij.codeInspection.ex.GlobalInspectionContextBase 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 GlobalInspectionContextBase

use of com.intellij.codeInspection.ex.GlobalInspectionContextBase in project intellij-community by JetBrains.

the class CleanupIntention method invoke.

@Override
public void invoke(@NotNull final Project project, final Editor editor, final PsiFile file) throws IncorrectOperationException {
    if (!FileModificationService.getInstance().preparePsiElementForWrite(file))
        return;
    final InspectionManager managerEx = InspectionManager.getInstance(project);
    final GlobalInspectionContextBase globalContext = (GlobalInspectionContextBase) managerEx.createNewGlobalContext(false);
    final AnalysisScope scope = getScope(project, file);
    if (scope != null) {
        globalContext.codeCleanup(scope, InspectionProjectProfileManager.getInstance(project).getCurrentProfile(), getText(), null, false);
    }
}
Also used : AnalysisScope(com.intellij.analysis.AnalysisScope) InspectionManager(com.intellij.codeInspection.InspectionManager) GlobalInspectionContextBase(com.intellij.codeInspection.ex.GlobalInspectionContextBase)

Example 3 with GlobalInspectionContextBase

use of com.intellij.codeInspection.ex.GlobalInspectionContextBase in project intellij-community by JetBrains.

the class CodeCleanupAction method runInspections.

@Override
protected void runInspections(Project project, AnalysisScope scope) {
    final InspectionProfile profile = myExternalProfile != null ? myExternalProfile : InspectionProjectProfileManager.getInstance(project).getCurrentProfile();
    final InspectionManager managerEx = InspectionManager.getInstance(project);
    final GlobalInspectionContextBase globalContext = (GlobalInspectionContextBase) managerEx.createNewGlobalContext(false);
    globalContext.codeCleanup(scope, profile, getTemplatePresentation().getText(), null, false);
}
Also used : InspectionManager(com.intellij.codeInspection.InspectionManager) InspectionProfile(com.intellij.codeInspection.InspectionProfile) GlobalInspectionContextBase(com.intellij.codeInspection.ex.GlobalInspectionContextBase)

Example 4 with GlobalInspectionContextBase

use of com.intellij.codeInspection.ex.GlobalInspectionContextBase in project intellij-community by JetBrains.

the class UnusedDeclarationInspectionBase method runInspection.

@Override
public void runInspection(@NotNull final AnalysisScope scope, @NotNull InspectionManager manager, @NotNull final GlobalInspectionContext globalContext, @NotNull ProblemDescriptionsProcessor problemDescriptionsProcessor) {
    globalContext.getRefManager().iterate(new RefJavaVisitor() {

        @Override
        public void visitElement(@NotNull final RefEntity refEntity) {
            if (refEntity instanceof RefElementImpl) {
                final RefElementImpl refElement = (RefElementImpl) refEntity;
                if (!refElement.isSuspicious())
                    return;
                PsiFile file = refElement.getContainingFile();
                if (file == null)
                    return;
                final boolean isSuppressed = refElement.isSuppressed(getShortName(), ALTERNATIVE_ID);
                if (isSuppressed || !((GlobalInspectionContextBase) globalContext).isToCheckFile(file, UnusedDeclarationInspectionBase.this)) {
                    if (isSuppressed || !scope.contains(file)) {
                        getEntryPointsManager(globalContext).addEntryPoint(refElement, false);
                    }
                }
            }
        }
    });
    if (isAddNonJavaUsedEnabled()) {
        checkForReachableRefs(globalContext);
        final StrictUnreferencedFilter strictUnreferencedFilter = new StrictUnreferencedFilter(this, globalContext);
        ProgressManager.getInstance().runProcess(new Runnable() {

            @Override
            public void run() {
                final RefManager refManager = globalContext.getRefManager();
                final PsiSearchHelper helper = PsiSearchHelper.SERVICE.getInstance(refManager.getProject());
                refManager.iterate(new RefJavaVisitor() {

                    @Override
                    public void visitElement(@NotNull final RefEntity refEntity) {
                        if (refEntity instanceof RefClass && strictUnreferencedFilter.accepts((RefClass) refEntity)) {
                            findExternalClassReferences((RefClass) refEntity);
                        } else if (refEntity instanceof RefMethod) {
                            RefMethod refMethod = (RefMethod) refEntity;
                            if (refMethod.isConstructor() && strictUnreferencedFilter.accepts(refMethod)) {
                                findExternalClassReferences(refMethod.getOwnerClass());
                            }
                        }
                    }

                    private void findExternalClassReferences(final RefClass refElement) {
                        final PsiClass psiClass = refElement.getElement();
                        String qualifiedName = psiClass != null ? psiClass.getQualifiedName() : null;
                        if (qualifiedName != null) {
                            final GlobalSearchScope projectScope = GlobalSearchScope.projectScope(globalContext.getProject());
                            final PsiNonJavaFileReferenceProcessor processor = (file, startOffset, endOffset) -> {
                                getEntryPointsManager(globalContext).addEntryPoint(refElement, false);
                                return false;
                            };
                            final DelegatingGlobalSearchScope globalSearchScope = new DelegatingGlobalSearchScope(projectScope) {

                                @Override
                                public boolean contains(@NotNull VirtualFile file) {
                                    return file.getFileType() != JavaFileType.INSTANCE && super.contains(file);
                                }
                            };
                            if (helper.processUsagesInNonJavaFiles(qualifiedName, processor, globalSearchScope)) {
                                final PsiReference reference = ReferencesSearch.search(psiClass, globalSearchScope).findFirst();
                                if (reference != null) {
                                    getEntryPointsManager(globalContext).addEntryPoint(refElement, false);
                                    for (PsiMethod method : psiClass.getMethods()) {
                                        final RefElement refMethod = refManager.getReference(method);
                                        if (refMethod != null) {
                                            getEntryPointsManager(globalContext).addEntryPoint(refMethod, false);
                                        }
                                    }
                                }
                            }
                        }
                    }
                });
            }
        }, null);
    }
    myProcessedSuspicious = new HashSet<>();
    myPhase = 1;
}
Also used : GroupNames(com.intellij.codeInsight.daemon.GroupNames) PsiClassImplUtil(com.intellij.psi.impl.PsiClassImplUtil) java.util(java.util) JobDescriptor(com.intellij.codeInspection.ex.JobDescriptor) VirtualFile(com.intellij.openapi.vfs.VirtualFile) HashMap(com.intellij.util.containers.HashMap) ToolExtensionPoints(com.intellij.ToolExtensionPoints) InvalidDataException(com.intellij.openapi.util.InvalidDataException) ContainerUtil(com.intellij.util.containers.ContainerUtil) UnusedSymbolLocalInspectionBase(com.intellij.codeInspection.unusedSymbol.UnusedSymbolLocalInspectionBase) DelegatingGlobalSearchScope(com.intellij.psi.search.DelegatingGlobalSearchScope) Project(com.intellij.openapi.project.Project) com.intellij.codeInspection.reference(com.intellij.codeInspection.reference) Logger(com.intellij.openapi.diagnostic.Logger) RefFilter(com.intellij.codeInspection.util.RefFilter) Extensions(com.intellij.openapi.extensions.Extensions) ProgressManager(com.intellij.openapi.progress.ProgressManager) ReferencesSearch(com.intellij.psi.search.searches.ReferencesSearch) PsiSearchHelper(com.intellij.psi.search.PsiSearchHelper) ExtensionPoint(com.intellij.openapi.extensions.ExtensionPoint) AnalysisScope(com.intellij.analysis.AnalysisScope) HighlightInfoType(com.intellij.codeInsight.daemon.impl.HighlightInfoType) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) com.intellij.codeInspection(com.intellij.codeInspection) JavaFileType(com.intellij.ide.highlighter.JavaFileType) PsiNonJavaFileReferenceProcessor(com.intellij.psi.search.PsiNonJavaFileReferenceProcessor) TestOnly(org.jetbrains.annotations.TestOnly) Nullable(org.jetbrains.annotations.Nullable) EntryPointsManager(com.intellij.codeInspection.ex.EntryPointsManager) PsiMethodUtil(com.intellij.psi.util.PsiMethodUtil) ApplicationManager(com.intellij.openapi.application.ApplicationManager) GlobalInspectionContextBase(com.intellij.codeInspection.ex.GlobalInspectionContextBase) HighlightUtilBase(com.intellij.codeInsight.daemon.impl.analysis.HighlightUtilBase) com.intellij.psi(com.intellij.psi) WriteExternalException(com.intellij.openapi.util.WriteExternalException) NotNull(org.jetbrains.annotations.NotNull) Element(org.jdom.Element) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiNonJavaFileReferenceProcessor(com.intellij.psi.search.PsiNonJavaFileReferenceProcessor) NotNull(org.jetbrains.annotations.NotNull) PsiSearchHelper(com.intellij.psi.search.PsiSearchHelper) DelegatingGlobalSearchScope(com.intellij.psi.search.DelegatingGlobalSearchScope) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) DelegatingGlobalSearchScope(com.intellij.psi.search.DelegatingGlobalSearchScope)

Aggregations

GlobalInspectionContextBase (com.intellij.codeInspection.ex.GlobalInspectionContextBase)4 AnalysisScope (com.intellij.analysis.AnalysisScope)2 com.intellij.codeInspection (com.intellij.codeInspection)2 InspectionManager (com.intellij.codeInspection.InspectionManager)2 Logger (com.intellij.openapi.diagnostic.Logger)2 ProgressManager (com.intellij.openapi.progress.ProgressManager)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)2 PsiSearchHelper (com.intellij.psi.search.PsiSearchHelper)2 java.util (java.util)2 NotNull (org.jetbrains.annotations.NotNull)2 ToolExtensionPoints (com.intellij.ToolExtensionPoints)1 GroupNames (com.intellij.codeInsight.daemon.GroupNames)1 HighlightInfoType (com.intellij.codeInsight.daemon.impl.HighlightInfoType)1 HighlightUtilBase (com.intellij.codeInsight.daemon.impl.analysis.HighlightUtilBase)1 InspectionProfile (com.intellij.codeInspection.InspectionProfile)1 EntryPointsManager (com.intellij.codeInspection.ex.EntryPointsManager)1 JobDescriptor (com.intellij.codeInspection.ex.JobDescriptor)1 com.intellij.codeInspection.reference (com.intellij.codeInspection.reference)1 RefManager (com.intellij.codeInspection.reference.RefManager)1