Search in sources :

Example 11 with PsiShortNamesCache

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

the class JavaCompilingVisitor method buildDescendants.

private static List<PsiClass> buildDescendants(String className, boolean includeSelf, OptimizingSearchHelper searchHelper, CompileContext context) {
    if (!searchHelper.doOptimizing())
        return Collections.emptyList();
    final SearchScope scope = context.getOptions().getScope();
    if (!(scope instanceof GlobalSearchScope))
        return Collections.emptyList();
    final PsiShortNamesCache cache = PsiShortNamesCache.getInstance(context.getProject());
    final PsiClass[] classes = cache.getClassesByName(className, (GlobalSearchScope) scope);
    final List<PsiClass> results = new ArrayList<>();
    final Processor<PsiClass> processor = aClass -> {
        results.add(aClass);
        return true;
    };
    for (PsiClass aClass : classes) {
        ClassInheritorsSearch.search(aClass, scope, true).forEach(processor);
    }
    if (includeSelf) {
        Collections.addAll(results, classes);
    }
    return results;
}
Also used : MalformedPatternException(com.intellij.structuralsearch.MalformedPatternException) CommentMatchingStrategy(com.intellij.structuralsearch.impl.matcher.strategies.CommentMatchingStrategy) ExprMatchingStrategy(com.intellij.structuralsearch.impl.matcher.strategies.ExprMatchingStrategy) JavaDocMatchingStrategy(com.intellij.structuralsearch.impl.matcher.strategies.JavaDocMatchingStrategy) NonNls(org.jetbrains.annotations.NonNls) SearchScope(com.intellij.psi.search.SearchScope) com.intellij.structuralsearch.impl.matcher.handlers(com.intellij.structuralsearch.impl.matcher.handlers) com.intellij.structuralsearch.impl.matcher.filters(com.intellij.structuralsearch.impl.matcher.filters) ArrayList(java.util.ArrayList) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) RegExpPredicate(com.intellij.structuralsearch.impl.matcher.predicates.RegExpPredicate) Matcher(java.util.regex.Matcher) JavaCompiledPattern(com.intellij.structuralsearch.impl.matcher.JavaCompiledPattern) PsiShortNamesCache(com.intellij.psi.search.PsiShortNamesCache) DocValuesIterator(com.intellij.structuralsearch.impl.matcher.iterators.DocValuesIterator) NodeIterator(com.intellij.dupLocator.iterators.NodeIterator) MatchingStrategy(com.intellij.structuralsearch.impl.matcher.strategies.MatchingStrategy) SSRBundle(com.intellij.structuralsearch.SSRBundle) CompiledPattern(com.intellij.structuralsearch.impl.matcher.CompiledPattern) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) PsiDocTag(com.intellij.psi.javadoc.PsiDocTag) Nullable(org.jetbrains.annotations.Nullable) PsiDocComment(com.intellij.psi.javadoc.PsiDocComment) List(java.util.List) Processor(com.intellij.util.Processor) com.intellij.psi(com.intellij.psi) ClassInheritorsSearch(com.intellij.psi.search.searches.ClassInheritorsSearch) UnsupportedPatternException(com.intellij.structuralsearch.UnsupportedPatternException) Pattern(java.util.regex.Pattern) Collections(java.util.Collections) PsiShortNamesCache(com.intellij.psi.search.PsiShortNamesCache) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) SearchScope(com.intellij.psi.search.SearchScope) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) ArrayList(java.util.ArrayList)

Example 12 with PsiShortNamesCache

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

the class GenericsHighlightUtil method registerVariableParameterizedTypeFixes.

private static void registerVariableParameterizedTypeFixes(@Nullable HighlightInfo highlightInfo, @NotNull PsiVariable variable, @NotNull PsiReferenceParameterList parameterList, @NotNull JavaSdkVersion version) {
    PsiType type = variable.getType();
    if (!(type instanceof PsiClassType) || highlightInfo == null)
        return;
    if (DumbService.getInstance(variable.getProject()).isDumb())
        return;
    String shortName = ((PsiClassType) type).getClassName();
    PsiManager manager = parameterList.getManager();
    final JavaPsiFacade facade = JavaPsiFacade.getInstance(manager.getProject());
    PsiShortNamesCache shortNamesCache = PsiShortNamesCache.getInstance(parameterList.getProject());
    PsiClass[] classes = shortNamesCache.getClassesByName(shortName, GlobalSearchScope.allScope(manager.getProject()));
    PsiElementFactory factory = facade.getElementFactory();
    for (PsiClass aClass : classes) {
        if (checkReferenceTypeArgumentList(aClass, parameterList, PsiSubstitutor.EMPTY, false, version) == null) {
            PsiType[] actualTypeParameters = parameterList.getTypeArguments();
            PsiTypeParameter[] classTypeParameters = aClass.getTypeParameters();
            Map<PsiTypeParameter, PsiType> map = new java.util.HashMap<>();
            for (int j = 0; j < classTypeParameters.length; j++) {
                PsiTypeParameter classTypeParameter = classTypeParameters[j];
                PsiType actualTypeParameter = actualTypeParameters[j];
                map.put(classTypeParameter, actualTypeParameter);
            }
            PsiSubstitutor substitutor = factory.createSubstitutor(map);
            PsiType suggestedType = factory.createType(aClass, substitutor);
            HighlightUtil.registerChangeVariableTypeFixes(variable, suggestedType, variable.getInitializer(), highlightInfo);
        }
    }
}
Also used : HashMap(com.intellij.util.containers.HashMap) THashMap(gnu.trove.THashMap) PsiShortNamesCache(com.intellij.psi.search.PsiShortNamesCache)

Example 13 with PsiShortNamesCache

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

the class StaticImportConstantFix method getMembersToImport.

@NotNull
@Override
protected List<PsiField> getMembersToImport(boolean applicableOnly) {
    final Project project = myRef.getProject();
    PsiShortNamesCache cache = PsiShortNamesCache.getInstance(project);
    final PsiJavaCodeReferenceElement element = myRef.getElement();
    String name = element != null ? element.getReferenceName() : null;
    if (name == null)
        return Collections.emptyList();
    if (element instanceof PsiExpression && PsiUtil.isAccessedForWriting((PsiExpression) element) || element.getParent() instanceof PsiTypeElement) {
        return Collections.emptyList();
    }
    final StaticMembersProcessor<PsiField> processor = new StaticMembersProcessor<PsiField>(element) {

        @Override
        protected boolean isApplicable(PsiField field, PsiElement place) {
            final PsiType expectedType = getExpectedType();
            return expectedType == null || TypeConversionUtil.isAssignable(expectedType, field.getType());
        }
    };
    cache.processFieldsWithName(name, processor, element.getResolveScope(), null);
    return processor.getMembersToImport(applicableOnly);
}
Also used : Project(com.intellij.openapi.project.Project) PsiShortNamesCache(com.intellij.psi.search.PsiShortNamesCache) NotNull(org.jetbrains.annotations.NotNull)

Example 14 with PsiShortNamesCache

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

the class CreateFromUsageUtils method handleObjectMethod.

private static boolean handleObjectMethod(Set<String> possibleClassNames, final JavaPsiFacade facade, final GlobalSearchScope searchScope, final boolean method, final String memberName, final boolean staticAccess, boolean addInheritors) {
    final PsiShortNamesCache cache = PsiShortNamesCache.getInstance(facade.getProject());
    final boolean[] allClasses = { false };
    ApplicationManager.getApplication().runReadAction(() -> {
        final PsiClass objectClass = facade.findClass(CommonClassNames.JAVA_LANG_OBJECT, searchScope);
        if (objectClass != null) {
            if (method && objectClass.findMethodsByName(memberName, false).length > 0) {
                allClasses[0] = true;
            } else if (!method) {
                final PsiField field = objectClass.findFieldByName(memberName, false);
                if (hasCorrectModifiers(field, staticAccess)) {
                    allClasses[0] = true;
                }
            }
        }
    });
    if (allClasses[0]) {
        possibleClassNames.add(CommonClassNames.JAVA_LANG_OBJECT);
        if (!addInheritors) {
            return true;
        }
        final String[] strings = ApplicationManager.getApplication().runReadAction(new Computable<String[]>() {

            @Override
            public String[] compute() {
                return cache.getAllClassNames();
            }
        });
        for (final String className : strings) {
            final PsiClass[] classes = ApplicationManager.getApplication().runReadAction(new Computable<PsiClass[]>() {

                @Override
                public PsiClass[] compute() {
                    return cache.getClassesByName(className, searchScope);
                }
            });
            for (final PsiClass aClass : classes) {
                final String qname = getQualifiedName(aClass);
                ContainerUtil.addIfNotNull(possibleClassNames, qname);
            }
        }
        return true;
    }
    return false;
}
Also used : PsiShortNamesCache(com.intellij.psi.search.PsiShortNamesCache)

Example 15 with PsiShortNamesCache

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

the class CreateFromUsageUtils method guessType.

@Nullable
static PsiType[] guessType(PsiExpression expression, final boolean allowVoidType) {
    final PsiManager manager = expression.getManager();
    final GlobalSearchScope resolveScope = expression.getResolveScope();
    List<ExpectedTypeInfo[]> typesList = new ArrayList<>();
    final List<String> expectedMethodNames = new ArrayList<>();
    final List<String> expectedFieldNames = new ArrayList<>();
    getExpectedInformation(expression, typesList, expectedMethodNames, expectedFieldNames);
    if (typesList.size() == 1 && (!expectedFieldNames.isEmpty() || !expectedMethodNames.isEmpty())) {
        ExpectedTypeInfo[] infos = typesList.get(0);
        if (infos.length == 1 && infos[0].getKind() == ExpectedTypeInfo.TYPE_OR_SUBTYPE && infos[0].getType().equals(PsiType.getJavaLangObject(manager, resolveScope))) {
            typesList.clear();
        }
    }
    if (typesList.isEmpty()) {
        final JavaPsiFacade facade = JavaPsiFacade.getInstance(manager.getProject());
        final PsiShortNamesCache cache = PsiShortNamesCache.getInstance(expression.getProject());
        PsiElementFactory factory = facade.getElementFactory();
        for (String fieldName : expectedFieldNames) {
            PsiField[] fields = cache.getFieldsByNameIfNotMoreThan(fieldName, resolveScope, MAX_RAW_GUESSED_MEMBERS_COUNT);
            addMemberInfo(fields, expression, typesList, factory);
        }
        for (String methodName : expectedMethodNames) {
            PsiMethod[] methods = cache.getMethodsByNameIfNotMoreThan(methodName, resolveScope, MAX_RAW_GUESSED_MEMBERS_COUNT);
            addMemberInfo(methods, expression, typesList, factory);
        }
    }
    ExpectedTypeInfo[] expectedTypes = ExpectedTypeUtil.intersect(typesList);
    if (expectedTypes.length == 0 && !typesList.isEmpty()) {
        List<ExpectedTypeInfo> union = new ArrayList<>();
        for (ExpectedTypeInfo[] aTypesList : typesList) {
            ContainerUtil.addAll(union, (ExpectedTypeInfo[]) aTypesList);
        }
        expectedTypes = union.toArray(new ExpectedTypeInfo[union.size()]);
    }
    if (expectedTypes.length == 0) {
        return allowVoidType ? new PsiType[] { PsiType.VOID } : new PsiType[] { PsiType.getJavaLangObject(manager, resolveScope) };
    } else {
        //Double check to avoid expensive operations on PsiClassTypes
        final Set<PsiType> typesSet = new HashSet<>();
        PsiTypeVisitor<PsiType> visitor = new PsiTypeVisitor<PsiType>() {

            @Override
            @Nullable
            public PsiType visitType(PsiType type) {
                if (PsiType.NULL.equals(type) || PsiType.VOID.equals(type) && !allowVoidType) {
                    type = PsiType.getJavaLangObject(manager, resolveScope);
                }
                if (!typesSet.contains(type)) {
                    if (type instanceof PsiClassType && (!expectedFieldNames.isEmpty() || !expectedMethodNames.isEmpty())) {
                        PsiClass aClass = ((PsiClassType) type).resolve();
                        if (aClass != null) {
                            for (String fieldName : expectedFieldNames) {
                                if (aClass.findFieldByName(fieldName, true) == null)
                                    return null;
                            }
                            for (String methodName : expectedMethodNames) {
                                PsiMethod[] methods = aClass.findMethodsByName(methodName, true);
                                if (methods.length == 0)
                                    return null;
                            }
                        }
                    }
                    typesSet.add(type);
                    return type;
                }
                return null;
            }

            @Override
            public PsiType visitCapturedWildcardType(PsiCapturedWildcardType capturedWildcardType) {
                return capturedWildcardType.getUpperBound().accept(this);
            }
        };
        PsiType[] types = ExpectedTypesProvider.processExpectedTypes(expectedTypes, visitor, manager.getProject());
        if (types.length == 0) {
            return allowVoidType ? new PsiType[] { PsiType.VOID } : new PsiType[] { PsiType.getJavaLangObject(manager, resolveScope) };
        }
        return types;
    }
}
Also used : PsiShortNamesCache(com.intellij.psi.search.PsiShortNamesCache) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

PsiShortNamesCache (com.intellij.psi.search.PsiShortNamesCache)30 NotNull (org.jetbrains.annotations.NotNull)20 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)11 Project (com.intellij.openapi.project.Project)6 PsiClass (com.intellij.psi.PsiClass)5 ArrayList (java.util.ArrayList)4 Nullable (org.jetbrains.annotations.Nullable)4 com.intellij.psi (com.intellij.psi)3 Processor (com.intellij.util.Processor)3 HashSet (com.intellij.util.containers.HashSet)3 Module (com.intellij.openapi.module.Module)2 PsiField (com.intellij.psi.PsiField)2 PsiMethod (com.intellij.psi.PsiMethod)2 THashSet (gnu.trove.THashSet)2 List (java.util.List)2 Matcher (java.util.regex.Matcher)2 Pattern (java.util.regex.Pattern)2 HighlightInfo (com.intellij.codeInsight.daemon.impl.HighlightInfo)1 NodeIterator (com.intellij.dupLocator.iterators.NodeIterator)1 DefaultPsiElementCellRenderer (com.intellij.ide.util.DefaultPsiElementCellRenderer)1