Search in sources :

Example 66 with LocalSearchScope

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

the class RenameJavaMemberProcessor method findCollisionsAgainstNewName.

protected static void findCollisionsAgainstNewName(final PsiMember memberToRename, final String newName, final List<UsageInfo> result) {
    if (!memberToRename.isPhysical()) {
        return;
    }
    final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(memberToRename.getProject());
    final List<PsiReference> potentialConflicts = new ArrayList<>();
    for (UsageInfo info : result) {
        final PsiElement element = info.getElement();
        if (element instanceof PsiReferenceExpression) {
            if (((PsiReferenceExpression) element).advancedResolve(false).getCurrentFileResolveScope() instanceof PsiImportStaticStatement && referencesLocalMember(memberToRename, newName, elementFactory, element)) {
                potentialConflicts.add(info.getReference());
            }
        }
    }
    final PsiFile containingFile = memberToRename.getContainingFile();
    if (containingFile instanceof PsiJavaFile) {
        final PsiImportList importList = ((PsiJavaFile) containingFile).getImportList();
        if (importList != null) {
            for (PsiImportStaticStatement staticImport : importList.getImportStaticStatements()) {
                final String referenceName = staticImport.getReferenceName();
                if (referenceName != null && !referenceName.equals(newName)) {
                    continue;
                }
                final PsiClass targetClass = staticImport.resolveTargetClass();
                if (targetClass != null) {
                    final Set<PsiMember> importedMembers = new HashSet<>();
                    if (memberToRename instanceof PsiMethod) {
                        for (PsiMethod method : targetClass.findMethodsByName(newName, true)) {
                            if (method.getModifierList().hasModifierProperty(PsiModifier.STATIC)) {
                                importedMembers.add(method);
                            }
                        }
                    } else if (memberToRename instanceof PsiField) {
                        final PsiField fieldByName = targetClass.findFieldByName(newName, true);
                        if (fieldByName != null) {
                            importedMembers.add(fieldByName);
                        }
                    }
                    for (PsiMember member : importedMembers) {
                        ReferencesSearch.search(member, new LocalSearchScope(containingFile), true).forEach(psiReference -> {
                            potentialConflicts.add(psiReference);
                            return true;
                        });
                    }
                }
            }
        }
    }
    for (PsiReference potentialConflict : potentialConflicts) {
        if (potentialConflict instanceof PsiJavaReference) {
            final JavaResolveResult resolveResult = ((PsiJavaReference) potentialConflict).advancedResolve(false);
            final PsiElement conflictElement = resolveResult.getElement();
            if (conflictElement != null) {
                final PsiElement scope = resolveResult.getCurrentFileResolveScope();
                if (scope instanceof PsiImportStaticStatement) {
                    result.add(new MemberHidesStaticImportUsageInfo(potentialConflict.getElement(), conflictElement, memberToRename));
                }
            }
        }
    }
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) ArrayList(java.util.ArrayList) UsageInfo(com.intellij.usageView.UsageInfo) HashSet(com.intellij.util.containers.HashSet)

Example 67 with LocalSearchScope

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

the class SafeDeleteJavaCallerChooser method isTheOnlyOneParameterUsage.

/**
   * @return parameter if it is used inside method only as argument in nodeMethod call at parameterIndex
   */
static PsiParameter isTheOnlyOneParameterUsage(PsiElement call, final int parameterIndex, final PsiMethod nodeMethod) {
    if (call instanceof PsiCallExpression) {
        final PsiExpressionList argumentList = ((PsiCallExpression) call).getArgumentList();
        if (argumentList != null) {
            final PsiExpression[] expressions = argumentList.getExpressions();
            if (expressions.length > parameterIndex) {
                final PsiExpression expression = PsiUtil.deparenthesizeExpression(expressions[parameterIndex]);
                if (expression != null) {
                    final Set<PsiParameter> paramRefs = new HashSet<>();
                    expression.accept(new JavaRecursiveElementWalkingVisitor() {

                        @Override
                        public void visitReferenceExpression(PsiReferenceExpression expression) {
                            super.visitReferenceExpression(expression);
                            final PsiElement resolve = expression.resolve();
                            if (resolve instanceof PsiParameter) {
                                paramRefs.add((PsiParameter) resolve);
                            }
                        }
                    });
                    final PsiParameter parameter = ContainerUtil.getFirstItem(paramRefs);
                    if (parameter != null && !parameter.isVarArgs()) {
                        final PsiElement scope = parameter.getDeclarationScope();
                        if (scope instanceof PsiMethod && ((PsiMethod) scope).findDeepestSuperMethods().length == 0 && OverridingMethodsSearch.search((PsiMethod) scope).findFirst() == null) {
                            final int scopeParamIdx = ((PsiMethod) scope).getParameterList().getParameterIndex(parameter);
                            final Ref<Boolean> ref = new Ref<>(false);
                            if (ReferencesSearch.search(parameter, new LocalSearchScope(scope)).forEach(new Processor<PsiReference>() {

                                @Override
                                public boolean process(PsiReference reference) {
                                    final PsiElement element = reference.getElement();
                                    if (element instanceof PsiReferenceExpression) {
                                        PsiCallExpression parent = PsiTreeUtil.getParentOfType(element, PsiCallExpression.class);
                                        while (parent != null) {
                                            final PsiMethod resolved = parent.resolveMethod();
                                            if (scope.equals(resolved)) {
                                                if (usedInQualifier(element, parent, scopeParamIdx))
                                                    return false;
                                                return true;
                                            }
                                            if (nodeMethod.equals(resolved)) {
                                                if (usedInQualifier(element, parent, parameterIndex))
                                                    return false;
                                                ref.set(true);
                                                return true;
                                            }
                                            parent = PsiTreeUtil.getParentOfType(parent, PsiCallExpression.class, true);
                                        }
                                        return false;
                                    }
                                    return true;
                                }

                                private boolean usedInQualifier(PsiElement element, PsiCallExpression parent, int parameterIndex) {
                                    PsiExpression qualifier = null;
                                    if (parent instanceof PsiMethodCallExpression) {
                                        qualifier = ((PsiMethodCallExpression) parent).getMethodExpression();
                                    } else if (parent instanceof PsiNewExpression) {
                                        qualifier = ((PsiNewExpression) parent).getQualifier();
                                    }
                                    if (PsiTreeUtil.isAncestor(qualifier, element, true)) {
                                        return true;
                                    }
                                    final PsiExpressionList list = parent.getArgumentList();
                                    return list != null && !PsiTreeUtil.isAncestor(list.getExpressions()[parameterIndex], element, false);
                                }
                            }) && ref.get()) {
                                return parameter;
                            }
                        }
                    }
                }
            }
        }
    }
    return null;
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) Processor(com.intellij.util.Processor) Ref(com.intellij.openapi.util.Ref) HashSet(java.util.HashSet)

Example 68 with LocalSearchScope

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

the class MoveJavaMemberHandler method getAnchor.

@Override
@Nullable
public PsiElement getAnchor(@NotNull final PsiMember member, @NotNull final PsiClass targetClass, final Set<PsiMember> membersToMove) {
    if (member instanceof PsiField && member.hasModifierProperty(PsiModifier.STATIC)) {
        final List<PsiField> afterFields = new ArrayList<>();
        final PsiExpression psiExpression = ((PsiField) member).getInitializer();
        if (psiExpression != null) {
            psiExpression.accept(new JavaRecursiveElementWalkingVisitor() {

                @Override
                public void visitReferenceExpression(final PsiReferenceExpression expression) {
                    super.visitReferenceExpression(expression);
                    final PsiElement psiElement = expression.resolve();
                    if (psiElement instanceof PsiField) {
                        final PsiField psiField = (PsiField) psiElement;
                        if ((psiField.getContainingClass() == targetClass || membersToMove.contains(psiField)) && !afterFields.contains(psiField)) {
                            afterFields.add(psiField);
                        }
                    }
                }
            });
        }
        if (!afterFields.isEmpty()) {
            Collections.sort(afterFields, (o1, o2) -> -PsiUtilCore.compareElementsByPosition(o1, o2));
            return afterFields.get(0);
        }
        final List<PsiField> beforeFields = new ArrayList<>();
        for (PsiReference psiReference : ReferencesSearch.search(member, new LocalSearchScope(targetClass))) {
            final PsiField fieldWithReference = PsiTreeUtil.getParentOfType(psiReference.getElement(), PsiField.class);
            if (fieldWithReference != null && !afterFields.contains(fieldWithReference) && fieldWithReference.getContainingClass() == targetClass) {
                beforeFields.add(fieldWithReference);
            }
        }
        Collections.sort(beforeFields, PsiUtil.BY_POSITION);
        if (!beforeFields.isEmpty()) {
            return beforeFields.get(0).getPrevSibling();
        }
    }
    return null;
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) ArrayList(java.util.ArrayList) Nullable(org.jetbrains.annotations.Nullable)

Example 69 with LocalSearchScope

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

the class FindManagerTest method testFindInClassHierarchy.

public void testFindInClassHierarchy() {
    initProject("findInClassHierarchy", "src");
    FindModel findModel = new FindModel();
    findModel.setStringToFind("instanceof");
    findModel.setWholeWordsOnly(true);
    findModel.setFromCursor(false);
    findModel.setGlobal(true);
    findModel.setMultipleFiles(true);
    final JavaPsiFacade facade = JavaPsiFacade.getInstance(getProject());
    final GlobalSearchScope scope = GlobalSearchScope.allScope(getProject());
    final PsiClass baseClass = facade.findClass("A", scope);
    final PsiClass implClass = facade.findClass("AImpl", scope);
    findModel.setCustomScope(new LocalSearchScope(new PsiElement[] { baseClass, implClass }));
    List<UsageInfo> usages = findUsages(findModel);
    assertEquals(2, usages.size());
    final PsiClass aClass = facade.findClass("B", scope);
    findModel.setCustomScope(new LocalSearchScope(aClass));
    assertSize(1, findUsages(findModel));
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) UsageInfo(com.intellij.usageView.UsageInfo)

Example 70 with LocalSearchScope

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

the class JavaRefactoringSupportProvider method mayRenameInplace.

public static boolean mayRenameInplace(PsiElement elementToRename, final PsiElement nameSuggestionContext) {
    if (nameSuggestionContext != null && nameSuggestionContext.getContainingFile() != elementToRename.getContainingFile())
        return false;
    if (!(elementToRename instanceof PsiLocalVariable) && !(elementToRename instanceof PsiParameter) && !(elementToRename instanceof PsiLabeledStatement)) {
        return false;
    }
    SearchScope useScope = PsiSearchHelper.SERVICE.getInstance(elementToRename.getProject()).getUseScope(elementToRename);
    if (!(useScope instanceof LocalSearchScope))
        return false;
    PsiElement[] scopeElements = ((LocalSearchScope) useScope).getScope();
    if (// assume there are no elements with use scopes with holes in them
    scopeElements.length > 1 && // ... except a case of element and it's doc comment
    !isElementWithComment(scopeElements) && !isResourceVariable(scopeElements)) {
        // ... and badly scoped resource variables
        return false;
    }
    PsiFile containingFile = elementToRename.getContainingFile();
    return PsiTreeUtil.isAncestor(containingFile, scopeElements[0], false);
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) SearchScope(com.intellij.psi.search.SearchScope) LocalSearchScope(com.intellij.psi.search.LocalSearchScope)

Aggregations

LocalSearchScope (com.intellij.psi.search.LocalSearchScope)113 SearchScope (com.intellij.psi.search.SearchScope)31 NotNull (org.jetbrains.annotations.NotNull)22 PsiElement (com.intellij.psi.PsiElement)19 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)19 Project (com.intellij.openapi.project.Project)18 Nullable (org.jetbrains.annotations.Nullable)13 ArrayList (java.util.ArrayList)12 VirtualFile (com.intellij.openapi.vfs.VirtualFile)11 UsageInfo (com.intellij.usageView.UsageInfo)11 TextRange (com.intellij.openapi.util.TextRange)9 IncorrectOperationException (com.intellij.util.IncorrectOperationException)9 com.intellij.psi (com.intellij.psi)8 PsiReference (com.intellij.psi.PsiReference)8 PsiFile (com.intellij.psi.PsiFile)7 ReferencesSearch (com.intellij.psi.search.searches.ReferencesSearch)7 AnalysisScope (com.intellij.analysis.AnalysisScope)6 Processor (com.intellij.util.Processor)6 PsiTreeUtil (com.intellij.psi.util.PsiTreeUtil)5 Ref (com.intellij.openapi.util.Ref)4