Search in sources :

Example 21 with SearchScope

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

the class JavaFindUsagesHelper method addElementUsages.

private static boolean addElementUsages(@NotNull final PsiElement element, @NotNull final FindUsagesOptions options, @NotNull final Processor<UsageInfo> processor) {
    final SearchScope searchScope = options.searchScope;
    final PsiClass[] parentClass = new PsiClass[1];
    if (element instanceof PsiMethod && ReadAction.compute(() -> {
        PsiMethod method = (PsiMethod) element;
        parentClass[0] = method.getContainingClass();
        return method.isConstructor();
    })) {
        PsiMethod method = (PsiMethod) element;
        if (parentClass[0] != null) {
            boolean strictSignatureSearch = !(options instanceof JavaMethodFindUsagesOptions) || !((JavaMethodFindUsagesOptions) options).isIncludeOverloadUsages;
            return MethodReferencesSearch.search(new MethodReferencesSearch.SearchParameters(method, searchScope, strictSignatureSearch, options.fastTrack)).forEach(new ReadActionProcessor<PsiReference>() {

                @Override
                public boolean processInReadAction(final PsiReference ref) {
                    return addResult(ref, options, processor);
                }
            });
        }
        return true;
    }
    final ReadActionProcessor<PsiReference> consumer = new ReadActionProcessor<PsiReference>() {

        @Override
        public boolean processInReadAction(final PsiReference ref) {
            return addResult(ref, options, processor);
        }
    };
    if (element instanceof PsiMethod) {
        final boolean strictSignatureSearch = // field with getter
        !(options instanceof JavaMethodFindUsagesOptions) || !((JavaMethodFindUsagesOptions) options).isIncludeOverloadUsages;
        return MethodReferencesSearch.search(new MethodReferencesSearch.SearchParameters((PsiMethod) element, searchScope, strictSignatureSearch, options.fastTrack)).forEach(consumer);
    }
    return ReferencesSearch.search(new ReferencesSearch.SearchParameters(element, searchScope, false, options.fastTrack)).forEach(consumer);
}
Also used : SearchScope(com.intellij.psi.search.SearchScope) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) ReadActionProcessor(com.intellij.openapi.application.ReadActionProcessor)

Example 22 with SearchScope

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

the class SystemBuilder method addUsage.

private void addUsage(final ReductionSystem system, final PsiElement element) {
    if (element instanceof PsiVariable) {
        final PsiExpression initializer = ((PsiVariable) element).getInitializer();
        if (initializer != null) {
            final PsiExpression core = PsiUtil.deparenthesizeExpression(initializer);
            if (core instanceof PsiArrayInitializerExpression) {
                final PsiExpression[] inits = ((PsiArrayInitializerExpression) core).getInitializers();
                final PsiType type = getType(element);
                for (PsiExpression init : inits) {
                    system.addSubtypeConstraint(evaluateType(init, system).createArrayType(), type);
                }
            } else if (core instanceof PsiNewExpression) {
                final PsiArrayInitializerExpression init = ((PsiNewExpression) core).getArrayInitializer();
                if (init != null) {
                    final PsiExpression[] inits = init.getInitializers();
                    final PsiType type = getType(element);
                    for (PsiExpression init1 : inits) {
                        system.addSubtypeConstraint(evaluateType(init1, system).createArrayType(), type);
                    }
                }
                system.addSubtypeConstraint(evaluateType(core, system), getType(element));
            } else {
                system.addSubtypeConstraint(evaluateType(core, system), getType(element));
            }
        }
        if (element instanceof PsiParameter) {
            PsiParameter parameter = (PsiParameter) element;
            final PsiElement declarationScope = parameter.getDeclarationScope();
            if (declarationScope instanceof PsiMethod) {
                final PsiMethod method = (PsiMethod) declarationScope;
                final PsiSearchHelper helper = PsiSearchHelper.SERVICE.getInstance(myManager.getProject());
                SearchScope scope = getScope(helper, method);
                for (PsiReference ref : ReferencesSearch.search(method, scope, true)) {
                    final PsiElement elt = ref.getElement();
                    if (elt != null) {
                        final PsiCallExpression call = PsiTreeUtil.getParentOfType(elt, PsiCallExpression.class);
                        if (call != null) {
                            PsiExpressionList argList = call.getArgumentList();
                            if (argList != null) {
                                PsiExpression[] args = argList.getExpressions();
                                int index = method.getParameterList().getParameterIndex(parameter);
                                if (index < args.length) {
                                    system.addSubtypeConstraint(evaluateType(args[index], system), myTypes.get(element));
                                }
                            }
                        }
                    }
                }
            } else if (declarationScope instanceof PsiForeachStatement) {
                addForEachConstraint(system, (PsiForeachStatement) declarationScope);
            }
        }
        return;
    } else if (element instanceof PsiMethod) {
        final PsiType reType = getType(element);
        element.accept(new JavaRecursiveElementWalkingVisitor() {

            @Override
            public void visitReturnStatement(final PsiReturnStatement statement) {
                super.visitReturnStatement(statement);
                final PsiExpression retExpr = statement.getReturnValue();
                if (retExpr != null) {
                    system.addSubtypeConstraint(evaluateType(retExpr, system), reType);
                }
            }

            @Override
            public void visitClass(PsiClass aClass) {
            }

            @Override
            public void visitLambdaExpression(PsiLambdaExpression expression) {
            }
        });
        return;
    }
    final PsiElement root = PsiTreeUtil.getParentOfType(element, PsiStatement.class, PsiField.class);
    if (root != null) {
        final PsiAnchor anchor = PsiAnchor.create(root);
        if (!myVisitedConstructions.contains(anchor)) {
            root.accept(new JavaRecursiveElementWalkingVisitor() {

                @Override
                public void visitAssignmentExpression(final PsiAssignmentExpression expression) {
                    super.visitAssignmentExpression(expression);
                    system.addSubtypeConstraint(evaluateType(expression.getRExpression(), system), evaluateType(expression.getLExpression(), system));
                }

                @Override
                public void visitConditionalExpression(final PsiConditionalExpression expression) {
                    super.visitConditionalExpression(expression);
                    system.addSubtypeConstraint(evaluateType(expression.getThenExpression(), system), evaluateType(expression.getElseExpression(), system));
                    system.addSubtypeConstraint(evaluateType(expression.getElseExpression(), system), evaluateType(expression.getThenExpression(), system));
                }

                @Override
                public void visitCallExpression(final PsiCallExpression expression) {
                    super.visitCallExpression(expression);
                    evaluateType(expression, system);
                }

                @Override
                public void visitReturnStatement(final PsiReturnStatement statement) {
                    super.visitReturnStatement(statement);
                    final PsiMethod method = PsiTreeUtil.getParentOfType(statement, PsiMethod.class, true, PsiLambdaExpression.class);
                    if (method != null) {
                        system.addSubtypeConstraint(evaluateType(statement.getReturnValue(), system), getType(method));
                    }
                }

                @Override
                public void visitTypeCastExpression(final PsiTypeCastExpression expression) {
                    super.visitTypeCastExpression(expression);
                    final PsiType operandType = evaluateType(expression.getOperand(), system);
                    final PsiType castType = evaluateType(expression, system);
                    if (operandType == null || castType == null)
                        return;
                    if (Util.bindsTypeVariables(operandType)) {
                        system.addCast(expression, operandType);
                    }
                    if (operandType.getDeepComponentType() instanceof PsiTypeVariable || castType.getDeepComponentType() instanceof PsiTypeVariable) {
                        system.addSubtypeConstraint(operandType, castType);
                    } else {
                        final PsiClassType.ClassResolveResult operandResult = Util.resolveType(operandType);
                        final PsiClassType.ClassResolveResult castResult = Util.resolveType(castType);
                        final PsiClass operandClass = operandResult.getElement();
                        final PsiClass castClass = castResult.getElement();
                        if (operandClass != null && castClass != null) {
                            if (InheritanceUtil.isInheritorOrSelf(operandClass, castClass, true)) {
                                system.addSubtypeConstraint(operandType, castType);
                            }
                        }
                    }
                }

                @Override
                public void visitVariable(final PsiVariable variable) {
                    super.visitVariable(variable);
                    final PsiExpression init = variable.getInitializer();
                    if (init != null) {
                        system.addSubtypeConstraint(evaluateType(init, system), getType(variable));
                    }
                }

                @Override
                public void visitNewExpression(final PsiNewExpression expression) {
                    super.visitNewExpression(expression);
                    final PsiArrayInitializerExpression init = expression.getArrayInitializer();
                    if (init != null) {
                        final PsiExpression[] inits = init.getInitializers();
                        final PsiType type = getType(expression);
                        for (PsiExpression init1 : inits) {
                            system.addSubtypeConstraint(evaluateType(init1, system).createArrayType(), type);
                        }
                    }
                }

                @Override
                public void visitReferenceExpression(final PsiReferenceExpression expression) {
                    final PsiExpression qualifierExpression = expression.getQualifierExpression();
                    if (qualifierExpression != null) {
                        qualifierExpression.accept(this);
                    }
                }
            });
            myVisitedConstructions.add(anchor);
        }
    }
}
Also used : PsiSearchHelper(com.intellij.psi.search.PsiSearchHelper) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) SearchScope(com.intellij.psi.search.SearchScope)

Example 23 with SearchScope

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

the class GuessManagerImpl method addTypesByVariable.

private void addTypesByVariable(HashSet<PsiType> typesSet, PsiVariable var, PsiFile scopeFile, HashSet<PsiVariable> checkedVariables, int flags, TextRange rangeToIgnore) {
    if (!checkedVariables.add(var))
        return;
    //System.out.println("analyzing usages of " + var + " in file " + scopeFile);
    SearchScope searchScope = new LocalSearchScope(scopeFile);
    if (BitUtil.isSet(flags, CHECK_USAGE) || BitUtil.isSet(flags, CHECK_DOWN)) {
        for (PsiReference varRef : ReferencesSearch.search(var, searchScope, false)) {
            PsiElement ref = varRef.getElement();
            if (BitUtil.isSet(flags, CHECK_USAGE)) {
                PsiType type = guessElementTypeFromReference(myMethodPatternMap, ref, rangeToIgnore);
                if (type != null && !(type instanceof PsiPrimitiveType)) {
                    typesSet.add(type);
                }
            }
            if (BitUtil.isSet(flags, CHECK_DOWN)) {
                if (ref.getParent() instanceof PsiExpressionList && ref.getParent().getParent() instanceof PsiMethodCallExpression) {
                    //TODO : new
                    PsiExpressionList list = (PsiExpressionList) ref.getParent();
                    PsiExpression[] args = list.getExpressions();
                    int argIndex = -1;
                    for (int j = 0; j < args.length; j++) {
                        PsiExpression arg = args[j];
                        if (arg.equals(ref)) {
                            argIndex = j;
                            break;
                        }
                    }
                    PsiMethodCallExpression methodCall = (PsiMethodCallExpression) list.getParent();
                    PsiMethod method = (PsiMethod) methodCall.getMethodExpression().resolve();
                    if (method != null) {
                        PsiParameter[] parameters = method.getParameterList().getParameters();
                        if (argIndex < parameters.length) {
                            addTypesByVariable(typesSet, parameters[argIndex], method.getContainingFile(), checkedVariables, flags | CHECK_USAGE, rangeToIgnore);
                        }
                    }
                }
            }
        }
    }
    if (BitUtil.isSet(flags, CHECK_UP)) {
        if (var instanceof PsiParameter && var.getParent() instanceof PsiParameterList && var.getParent().getParent() instanceof PsiMethod) {
            PsiParameterList list = (PsiParameterList) var.getParent();
            PsiParameter[] parameters = list.getParameters();
            int argIndex = -1;
            for (int i = 0; i < parameters.length; i++) {
                PsiParameter parameter = parameters[i];
                if (parameter.equals(var)) {
                    argIndex = i;
                    break;
                }
            }
            PsiMethod method = (PsiMethod) var.getParent().getParent();
            //System.out.println("analyzing usages of " + method + " in file " + scopeFile);
            for (PsiReference methodRef : ReferencesSearch.search(method, searchScope, false)) {
                PsiElement ref = methodRef.getElement();
                if (ref.getParent() instanceof PsiMethodCallExpression) {
                    PsiMethodCallExpression methodCall = (PsiMethodCallExpression) ref.getParent();
                    PsiExpression[] args = methodCall.getArgumentList().getExpressions();
                    if (args.length <= argIndex)
                        continue;
                    PsiExpression arg = args[argIndex];
                    if (arg instanceof PsiReferenceExpression) {
                        PsiElement refElement = ((PsiReferenceExpression) arg).resolve();
                        if (refElement instanceof PsiVariable) {
                            addTypesByVariable(typesSet, (PsiVariable) refElement, scopeFile, checkedVariables, flags | CHECK_USAGE, rangeToIgnore);
                        }
                    }
                //TODO : constructor
                }
            }
        }
    }
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) SearchScope(com.intellij.psi.search.SearchScope) LocalSearchScope(com.intellij.psi.search.LocalSearchScope)

Example 24 with SearchScope

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

the class EnumTypeConversionRule method findConversion.

@Override
public TypeConversionDescriptorBase findConversion(PsiType from, PsiType to, PsiMember member, PsiExpression context, TypeMigrationLabeler labeler) {
    final PsiMethodCallExpression callExpression = PsiTreeUtil.getParentOfType(context, PsiMethodCallExpression.class, false);
    if (callExpression != null) {
        final PsiMethod resolved = callExpression.resolveMethod();
        if (resolved != null) {
            final SearchScope searchScope = labeler.getRules().getSearchScope();
            if (!PsiSearchScopeUtil.isInScope(searchScope, resolved)) {
                return null;
            }
        }
    }
    final PsiField field = PsiTreeUtil.getParentOfType(context, PsiField.class);
    if (field != null && !myEnumConstants.contains(field) && field.hasModifierProperty(PsiModifier.STATIC) && field.hasModifierProperty(PsiModifier.FINAL) && field.hasInitializer()) {
        return null;
    }
    final PsiClass toClass = PsiUtil.resolveClassInType(to);
    if (toClass != null && toClass.isEnum()) {
        final PsiMethod[] constructors = toClass.getConstructors();
        if (constructors.length == 1) {
            final PsiMethod constructor = constructors[0];
            final PsiParameter[] parameters = constructor.getParameterList().getParameters();
            if (parameters.length == 1) {
                if (TypeConversionUtil.isAssignable(parameters[0].getType(), from)) {
                    return new TypeConversionDescriptorBase();
                }
            }
        }
    }
    return null;
}
Also used : TypeConversionDescriptorBase(com.intellij.refactoring.typeMigration.TypeConversionDescriptorBase) SearchScope(com.intellij.psi.search.SearchScope)

Example 25 with SearchScope

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

the class SPIReferencesSearcher method processQuery.

@Override
public void processQuery(@NotNull final ReferencesSearch.SearchParameters p, @NotNull final Processor<PsiReference> consumer) {
    final PsiElement element = p.getElementToSearch();
    if (!element.isValid())
        return;
    final SearchScope scope = p.getEffectiveSearchScope();
    if (!(scope instanceof GlobalSearchScope))
        return;
    if (element instanceof PsiClass) {
        final PsiClass aClass = (PsiClass) element;
        final String jvmClassName = ClassUtil.getJVMClassName(aClass);
        if (jvmClassName == null)
            return;
        final PsiFile[] files = FilenameIndex.getFilesByName(aClass.getProject(), jvmClassName, (GlobalSearchScope) scope);
        for (PsiFile file : files) {
            if (file.getLanguage() == SPILanguage.INSTANCE) {
                final PsiReference reference = file.getReference();
                if (reference != null) {
                    consumer.process(reference);
                }
            }
        }
    } else if (element instanceof PsiPackage) {
        final String qualifiedName = ((PsiPackage) element).getQualifiedName();
        final Project project = element.getProject();
        final String[] filenames = FilenameIndex.getAllFilenames(project);
        for (final String filename : filenames) {
            if (filename.startsWith(qualifiedName + ".")) {
                final PsiFile[] files = FilenameIndex.getFilesByName(project, filename, (GlobalSearchScope) scope);
                for (PsiFile file : files) {
                    if (file.getLanguage() == SPILanguage.INSTANCE) {
                        final PsiReference[] references = file.getReferences();
                        for (final PsiReference reference : references) {
                            if (reference.getCanonicalText().equals(qualifiedName)) {
                                consumer.process(reference);
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : Project(com.intellij.openapi.project.Project) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) SearchScope(com.intellij.psi.search.SearchScope)

Aggregations

SearchScope (com.intellij.psi.search.SearchScope)90 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)39 LocalSearchScope (com.intellij.psi.search.LocalSearchScope)36 Project (com.intellij.openapi.project.Project)21 NotNull (org.jetbrains.annotations.NotNull)21 VirtualFile (com.intellij.openapi.vfs.VirtualFile)18 PsiElement (com.intellij.psi.PsiElement)16 PsiClass (com.intellij.psi.PsiClass)8 Processor (com.intellij.util.Processor)8 com.intellij.psi (com.intellij.psi)6 PsiMethod (com.intellij.psi.PsiMethod)6 HashMap (com.intellij.util.containers.HashMap)6 ClassInheritorsSearch (com.intellij.psi.search.searches.ClassInheritorsSearch)5 Nullable (org.jetbrains.annotations.Nullable)5 AnalysisScope (com.intellij.analysis.AnalysisScope)4 ProgressManager (com.intellij.openapi.progress.ProgressManager)4 TextRange (com.intellij.openapi.util.TextRange)4 PsiFile (com.intellij.psi.PsiFile)4 PsiSearchHelper (com.intellij.psi.search.PsiSearchHelper)4 QueryExecutor (com.intellij.util.QueryExecutor)4