Search in sources :

Example 1 with PsiNonJavaFileReferenceProcessor

use of com.intellij.psi.search.PsiNonJavaFileReferenceProcessor in project intellij-community by JetBrains.

the class ExtensionPointLocator method isRegisteredExtension.

public static boolean isRegisteredExtension(@NotNull PsiClass psiClass) {
    String name = psiClass.getQualifiedName();
    if (name == null)
        return false;
    Project project = psiClass.getProject();
    GlobalSearchScope scope = getCandidatesScope(project);
    return !PsiSearchHelper.SERVICE.getInstance(project).processUsagesInNonJavaFiles(name, new PsiNonJavaFileReferenceProcessor() {

        @Override
        public boolean process(PsiFile file, int startOffset, int endOffset) {
            PsiElement at = file.findElementAt(startOffset);
            String tokenText = at instanceof XmlToken ? at.getText() : null;
            if (!StringUtil.equals(name, tokenText))
                return true;
            XmlTag tag = PsiTreeUtil.getParentOfType(at, XmlTag.class);
            if (tag == null)
                return true;
            DomElement dom = DomUtil.getDomElement(tag);
            return !(dom instanceof Extension && ((Extension) dom).getExtensionPoint() != null);
        }
    }, scope);
}
Also used : Extension(org.jetbrains.idea.devkit.dom.Extension) Project(com.intellij.openapi.project.Project) DomElement(com.intellij.util.xml.DomElement) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) PsiNonJavaFileReferenceProcessor(com.intellij.psi.search.PsiNonJavaFileReferenceProcessor) XmlToken(com.intellij.psi.xml.XmlToken) XmlTag(com.intellij.psi.xml.XmlTag)

Example 2 with PsiNonJavaFileReferenceProcessor

use of com.intellij.psi.search.PsiNonJavaFileReferenceProcessor 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)

Example 3 with PsiNonJavaFileReferenceProcessor

use of com.intellij.psi.search.PsiNonJavaFileReferenceProcessor in project intellij-community by JetBrains.

the class TestNGRelatedFilesProvider method getItems.

@NotNull
@Override
public List<? extends GotoRelatedItem> getItems(@NotNull PsiElement context) {
    PsiClass psiClass = PsiTreeUtil.getParentOfType(context, PsiClass.class, false);
    if (psiClass != null) {
        final Project project = psiClass.getProject();
        while (psiClass != null && TestNGUtil.hasTest(psiClass) && PsiClassUtil.isRunnableClass(psiClass, true)) {
            final String qName = psiClass.getQualifiedName();
            if (qName != null) {
                final String packageQName = ((PsiJavaFile) psiClass.getContainingFile()).getPackageName();
                final String packageName = StringUtil.getShortName(packageQName);
                final String[] names;
                if (packageQName.length() > 0) {
                    final String pName = packageName.length() > 0 ? packageName : packageQName;
                    names = new String[] { qName, pName };
                } else {
                    names = new String[] { qName };
                }
                final List<PsiElement> tags = new ArrayList<>();
                for (final String name : names) {
                    PsiSearchHelper.SERVICE.getInstance(project).processUsagesInNonJavaFiles(name, new PsiNonJavaFileReferenceProcessor() {

                        public boolean process(final PsiFile file, final int startOffset, final int endOffset) {
                            final PsiReference referenceAt = file.findReferenceAt(startOffset);
                            if (referenceAt != null) {
                                if (packageQName.endsWith(name)) {
                                    //special package tag required
                                    final XmlTag tag = PsiTreeUtil.getParentOfType(file.findElementAt(startOffset), XmlTag.class);
                                    if (tag == null || !tag.getName().equals("package")) {
                                        return true;
                                    }
                                    final XmlAttribute attribute = tag.getAttribute("name");
                                    if (attribute == null)
                                        return true;
                                    final String value = attribute.getValue();
                                    if (value == null)
                                        return true;
                                    if (!(value.equals(StringUtil.getQualifiedName(packageQName, "*")) || value.equals(packageQName)))
                                        return true;
                                }
                                tags.add(referenceAt.getElement());
                            }
                            return true;
                        }
                    }, new TestNGSearchScope(project));
                }
                if (!tags.isEmpty()) {
                    return GotoRelatedItem.createItems(tags, "TestNG");
                }
            }
            psiClass = PsiTreeUtil.getParentOfType(psiClass, PsiClass.class);
        }
    }
    return Collections.emptyList();
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) PsiNonJavaFileReferenceProcessor(com.intellij.psi.search.PsiNonJavaFileReferenceProcessor) ArrayList(java.util.ArrayList) Project(com.intellij.openapi.project.Project) TestNGSearchScope(com.theoryinpractice.testng.inspection.TestNGSearchScope) XmlTag(com.intellij.psi.xml.XmlTag) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with PsiNonJavaFileReferenceProcessor

use of com.intellij.psi.search.PsiNonJavaFileReferenceProcessor in project intellij-community by JetBrains.

the class ExtensionPointLocator method findExtensionPointCandidates.

private static void findExtensionPointCandidates(PsiClass psiClass, final List<ExtensionPointCandidate> list) {
    String name = psiClass.getQualifiedName();
    if (name == null)
        return;
    Project project = psiClass.getProject();
    GlobalSearchScope scope = getCandidatesScope(project);
    PsiSearchHelper.SERVICE.getInstance(project).processUsagesInNonJavaFiles(name, new PsiNonJavaFileReferenceProcessor() {

        @Override
        public boolean process(PsiFile file, int startOffset, int endOffset) {
            PsiElement element = file.findElementAt(startOffset);
            processExtensionPointCandidate(element, list);
            return true;
        }
    }, scope);
}
Also used : Project(com.intellij.openapi.project.Project) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) PsiNonJavaFileReferenceProcessor(com.intellij.psi.search.PsiNonJavaFileReferenceProcessor) ExtensionPoint(org.jetbrains.idea.devkit.dom.ExtensionPoint)

Aggregations

Project (com.intellij.openapi.project.Project)4 PsiNonJavaFileReferenceProcessor (com.intellij.psi.search.PsiNonJavaFileReferenceProcessor)4 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)3 XmlTag (com.intellij.psi.xml.XmlTag)2 NotNull (org.jetbrains.annotations.NotNull)2 ToolExtensionPoints (com.intellij.ToolExtensionPoints)1 AnalysisScope (com.intellij.analysis.AnalysisScope)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 com.intellij.codeInspection (com.intellij.codeInspection)1 EntryPointsManager (com.intellij.codeInspection.ex.EntryPointsManager)1 GlobalInspectionContextBase (com.intellij.codeInspection.ex.GlobalInspectionContextBase)1 JobDescriptor (com.intellij.codeInspection.ex.JobDescriptor)1 com.intellij.codeInspection.reference (com.intellij.codeInspection.reference)1 UnusedSymbolLocalInspectionBase (com.intellij.codeInspection.unusedSymbol.UnusedSymbolLocalInspectionBase)1 RefFilter (com.intellij.codeInspection.util.RefFilter)1 JavaFileType (com.intellij.ide.highlighter.JavaFileType)1 ApplicationManager (com.intellij.openapi.application.ApplicationManager)1 Logger (com.intellij.openapi.diagnostic.Logger)1