Search in sources :

Example 46 with HashMap

use of com.intellij.util.containers.HashMap in project intellij-community by JetBrains.

the class EncapsulateFieldsProcessor method processUsagesPerFile.

private void processUsagesPerFile(UsageInfo[] usages) {
    Map<PsiFile, List<EncapsulateFieldUsageInfo>> usagesInFiles = new HashMap<>();
    for (UsageInfo usage : usages) {
        PsiElement element = usage.getElement();
        if (element == null)
            continue;
        final PsiFile file = element.getContainingFile();
        List<EncapsulateFieldUsageInfo> usagesInFile = usagesInFiles.get(file);
        if (usagesInFile == null) {
            usagesInFile = new ArrayList<>();
            usagesInFiles.put(file, usagesInFile);
        }
        usagesInFile.add(((EncapsulateFieldUsageInfo) usage));
    }
    for (List<EncapsulateFieldUsageInfo> usageInfos : usagesInFiles.values()) {
        //this is to avoid elements to become invalid as a result of processUsage
        final EncapsulateFieldUsageInfo[] infos = usageInfos.toArray(new EncapsulateFieldUsageInfo[usageInfos.size()]);
        CommonRefactoringUtil.sortDepthFirstRightLeftOrder(infos);
        for (EncapsulateFieldUsageInfo info : infos) {
            final PsiElement element = info.getElement();
            if (element == null)
                continue;
            EncapsulateFieldHelper helper = EncapsulateFieldHelper.getHelper(element.getLanguage());
            helper.processUsage(info, myDescriptor, myNameToSetter.get(info.getFieldDescriptor().getSetterName()), myNameToGetter.get(info.getFieldDescriptor().getGetterName()));
        }
    }
}
Also used : HashMap(com.intellij.util.containers.HashMap) UsageInfo(com.intellij.usageView.UsageInfo)

Example 47 with HashMap

use of com.intellij.util.containers.HashMap in project intellij-community by JetBrains.

the class JavaDirectInheritorsSearcher method calculateDirectSubClasses.

@NotNull
private static PsiClass[] calculateDirectSubClasses(@NotNull Project project, @NotNull PsiClass baseClass, @NotNull String baseClassName, @NotNull SearchScope useScope) {
    DumbService dumbService = DumbService.getInstance(project);
    GlobalSearchScope globalUseScope = dumbService.runReadActionInSmartMode(() -> StubHierarchyInheritorSearcher.restrictScope(GlobalSearchScopeUtil.toGlobalSearchScope(useScope, project)));
    Collection<PsiReferenceList> candidates = dumbService.runReadActionInSmartMode(() -> JavaSuperClassNameOccurenceIndex.getInstance().get(baseClassName, project, globalUseScope));
    // memory/speed optimisation: it really is a map(string -> PsiClass or List<PsiClass>)
    final Map<String, Object> classesWithFqn = new HashMap<>();
    processConcurrentlyIfTooMany(candidates, referenceList -> {
        ProgressManager.checkCanceled();
        ApplicationManager.getApplication().runReadAction(() -> {
            final PsiClass candidate = (PsiClass) referenceList.getParent();
            boolean isInheritor = candidate.isInheritor(baseClass, false);
            if (isInheritor) {
                String fqn = candidate.getQualifiedName();
                synchronized (classesWithFqn) {
                    Object value = classesWithFqn.get(fqn);
                    if (value == null) {
                        classesWithFqn.put(fqn, candidate);
                    } else if (value instanceof PsiClass) {
                        List<PsiClass> list = new ArrayList<>();
                        list.add((PsiClass) value);
                        list.add(candidate);
                        classesWithFqn.put(fqn, list);
                    } else {
                        @SuppressWarnings("unchecked") List<PsiClass> list = (List<PsiClass>) value;
                        list.add(candidate);
                    }
                }
            }
        });
        return true;
    });
    final List<PsiClass> result = new ArrayList<>();
    for (Object value : classesWithFqn.values()) {
        if (value instanceof PsiClass) {
            result.add((PsiClass) value);
        } else {
            @SuppressWarnings("unchecked") List<PsiClass> list = (List<PsiClass>) value;
            result.addAll(list);
        }
    }
    Collection<PsiAnonymousClass> anonymousCandidates = dumbService.runReadActionInSmartMode(() -> JavaAnonymousClassBaseRefOccurenceIndex.getInstance().get(baseClassName, project, globalUseScope));
    processConcurrentlyIfTooMany(anonymousCandidates, candidate -> {
        boolean isInheritor = dumbService.runReadActionInSmartMode(() -> candidate.isInheritor(baseClass, false));
        if (isInheritor) {
            synchronized (result) {
                result.add(candidate);
            }
        }
        return true;
    });
    boolean isEnum = ReadAction.compute(baseClass::isEnum);
    if (isEnum) {
        // abstract enum can be subclassed in the body
        PsiField[] fields = ReadAction.compute(baseClass::getFields);
        for (final PsiField field : fields) {
            ProgressManager.checkCanceled();
            if (field instanceof PsiEnumConstant) {
                PsiEnumConstantInitializer initializingClass = ReadAction.compute(((PsiEnumConstant) field)::getInitializingClass);
                if (initializingClass != null) {
                    // it surely is an inheritor
                    result.add(initializingClass);
                }
            }
        }
    }
    return result.isEmpty() ? PsiClass.EMPTY_ARRAY : result.toArray(new PsiClass[result.size()]);
}
Also used : HashMap(com.intellij.util.containers.HashMap) ArrayList(java.util.ArrayList) DumbService(com.intellij.openapi.project.DumbService) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) ArrayList(java.util.ArrayList) List(java.util.List) NotNull(org.jetbrains.annotations.NotNull)

Example 48 with HashMap

use of com.intellij.util.containers.HashMap in project kotlin by JetBrains.

the class AndroidLintGlobalInspectionContext method performPreRunActivities.

@Override
public void performPreRunActivities(@NotNull List<Tools> globalTools, @NotNull List<Tools> localTools, @NotNull final GlobalInspectionContext context) {
    final Project project = context.getProject();
    if (!ProjectFacetManager.getInstance(project).hasFacets(AndroidFacet.ID)) {
        return;
    }
    final List<Issue> issues = AndroidLintExternalAnnotator.getIssuesFromInspections(project, null);
    if (issues.size() == 0) {
        return;
    }
    final Map<Issue, Map<File, List<ProblemData>>> problemMap = new HashMap<Issue, Map<File, List<ProblemData>>>();
    final AnalysisScope scope = context.getRefManager().getScope();
    if (scope == null) {
        return;
    }
    final IntellijLintClient client = IntellijLintClient.forBatch(project, problemMap, scope, issues);
    final LintDriver lint = new LintDriver(new IntellijLintIssueRegistry(), client);
    final ProgressIndicator indicator = ProgressManager.getInstance().getProgressIndicator();
    if (indicator != null) {
        ProgressWrapper.unwrap(indicator).setText("Running Android Lint");
    }
    EnumSet<Scope> lintScope;
    //noinspection ConstantConditions
    if (!IntellijLintProject.SUPPORT_CLASS_FILES) {
        lintScope = EnumSet.copyOf(Scope.ALL);
        // Can't run class file based checks
        lintScope.remove(Scope.CLASS_FILE);
        lintScope.remove(Scope.ALL_CLASS_FILES);
        lintScope.remove(Scope.JAVA_LIBRARIES);
    } else {
        lintScope = Scope.ALL;
    }
    List<VirtualFile> files = null;
    final List<Module> modules = Lists.newArrayList();
    int scopeType = scope.getScopeType();
    switch(scopeType) {
        case AnalysisScope.MODULE:
            {
                SearchScope searchScope = scope.toSearchScope();
                if (searchScope instanceof ModuleWithDependenciesScope) {
                    ModuleWithDependenciesScope s = (ModuleWithDependenciesScope) searchScope;
                    if (!s.isSearchInLibraries()) {
                        modules.add(s.getModule());
                    }
                }
                break;
            }
        case AnalysisScope.FILE:
        case AnalysisScope.VIRTUAL_FILES:
        case AnalysisScope.UNCOMMITTED_FILES:
            {
                files = Lists.newArrayList();
                SearchScope searchScope = scope.toSearchScope();
                if (searchScope instanceof LocalSearchScope) {
                    final LocalSearchScope localSearchScope = (LocalSearchScope) searchScope;
                    final PsiElement[] elements = localSearchScope.getScope();
                    final List<VirtualFile> finalFiles = files;
                    ApplicationManager.getApplication().runReadAction(new Runnable() {

                        @Override
                        public void run() {
                            for (PsiElement element : elements) {
                                if (element instanceof PsiFile) {
                                    // should be the case since scope type is FILE
                                    Module module = ModuleUtilCore.findModuleForPsiElement(element);
                                    if (module != null && !modules.contains(module)) {
                                        modules.add(module);
                                    }
                                    VirtualFile virtualFile = ((PsiFile) element).getVirtualFile();
                                    if (virtualFile != null) {
                                        finalFiles.add(virtualFile);
                                    }
                                }
                            }
                        }
                    });
                } else {
                    final List<VirtualFile> finalList = files;
                    scope.accept(new PsiElementVisitor() {

                        @Override
                        public void visitFile(PsiFile file) {
                            VirtualFile virtualFile = file.getVirtualFile();
                            if (virtualFile != null) {
                                finalList.add(virtualFile);
                            }
                        }
                    });
                }
                if (files.isEmpty()) {
                    files = null;
                } else {
                    // Lint will compute it lazily based on actual files in the request
                    lintScope = null;
                }
                break;
            }
        case AnalysisScope.PROJECT:
            {
                modules.addAll(Arrays.asList(ModuleManager.getInstance(project).getModules()));
                break;
            }
        case AnalysisScope.CUSTOM:
        case AnalysisScope.MODULES:
        case AnalysisScope.DIRECTORY:
            {
                // Handled by the getNarrowedComplementaryScope case below
                break;
            }
        case AnalysisScope.INVALID:
            break;
        default:
            Logger.getInstance(this.getClass()).warn("Unexpected inspection scope " + scope + ", " + scopeType);
    }
    if (modules.isEmpty()) {
        for (Module module : ModuleManager.getInstance(project).getModules()) {
            if (scope.containsModule(module)) {
                modules.add(module);
            }
        }
        if (modules.isEmpty() && files != null) {
            for (VirtualFile file : files) {
                Module module = ModuleUtilCore.findModuleForFile(file, project);
                if (module != null && !modules.contains(module)) {
                    modules.add(module);
                }
            }
        }
        if (modules.isEmpty()) {
            AnalysisScope narrowed = scope.getNarrowedComplementaryScope(project);
            for (Module module : ModuleManager.getInstance(project).getModules()) {
                if (narrowed.containsModule(module)) {
                    modules.add(module);
                }
            }
        }
    }
    LintRequest request = new IntellijLintRequest(client, project, files, modules, false);
    request.setScope(lintScope);
    lint.analyze(request);
    myResults = problemMap;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Issue(com.android.tools.klint.detector.api.Issue) HashMap(com.intellij.util.containers.HashMap) PsiElementVisitor(com.intellij.psi.PsiElementVisitor) AnalysisScope(com.intellij.analysis.AnalysisScope) LintRequest(com.android.tools.klint.client.api.LintRequest) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) LintDriver(com.android.tools.klint.client.api.LintDriver) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) ModuleWithDependenciesScope(com.intellij.openapi.module.impl.scopes.ModuleWithDependenciesScope) Project(com.intellij.openapi.project.Project) ModuleWithDependenciesScope(com.intellij.openapi.module.impl.scopes.ModuleWithDependenciesScope) SearchScope(com.intellij.psi.search.SearchScope) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) AnalysisScope(com.intellij.analysis.AnalysisScope) Scope(com.android.tools.klint.detector.api.Scope) SearchScope(com.intellij.psi.search.SearchScope) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) Module(com.intellij.openapi.module.Module) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) File(java.io.File) HashMap(com.intellij.util.containers.HashMap)

Example 49 with HashMap

use of com.intellij.util.containers.HashMap in project intellij-plugins by JetBrains.

the class FlexXmlBackedClassesIndex method getIndexer.

@Override
@NotNull
public DataIndexer<String, Void, FileContent> getIndexer() {
    return new DataIndexer<String, Void, FileContent>() {

        @Override
        @NotNull
        public Map<String, Void> map(@NotNull FileContent inputData) {
            final XmlFile file = (XmlFile) inputData.getPsiFile();
            final Map<String, Void> result = new HashMap<>();
            for (JSClass clazz : XmlBackedJSClassImpl.getClasses(file)) {
                JSReferenceList supers = getSupers(clazz);
                if (supers != null) {
                    final JSExpression[] expressions = supers.getExpressions();
                    for (JSExpression expr : expressions) {
                        String s = expr instanceof JSReferenceExpression ? ((JSReferenceExpression) expr).getReferenceName() : null;
                        if (s != null)
                            result.put(s, null);
                    }
                }
            }
            return result;
        }
    };
}
Also used : XmlFile(com.intellij.psi.xml.XmlFile) HashMap(com.intellij.util.containers.HashMap) JSExpression(com.intellij.lang.javascript.psi.JSExpression) NotNull(org.jetbrains.annotations.NotNull) JSReferenceExpression(com.intellij.lang.javascript.psi.JSReferenceExpression) JSClass(com.intellij.lang.javascript.psi.ecmal4.JSClass) JSReferenceList(com.intellij.lang.javascript.psi.ecmal4.JSReferenceList) NotNull(org.jetbrains.annotations.NotNull)

Example 50 with HashMap

use of com.intellij.util.containers.HashMap in project intellij-plugins by JetBrains.

the class FlexXmlBackedMembersIndex method getIndexer.

@Override
@NotNull
public DataIndexer<String, Void, FileContent> getIndexer() {
    return new DataIndexer<String, Void, FileContent>() {

        @Override
        @NotNull
        public Map<String, Void> map(@NotNull FileContent inputData) {
            final XmlFile file = (XmlFile) inputData.getPsiFile();
            final Map<String, Void> result = new HashMap<>();
            process(file, element -> {
                String name = getName(element);
                if (name != null) {
                    result.put(name, null);
                }
            }, false);
            return result;
        }
    };
}
Also used : XmlFile(com.intellij.psi.xml.XmlFile) HashMap(com.intellij.util.containers.HashMap) NotNull(org.jetbrains.annotations.NotNull) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

HashMap (com.intellij.util.containers.HashMap)118 NotNull (org.jetbrains.annotations.NotNull)23 VirtualFile (com.intellij.openapi.vfs.VirtualFile)22 File (java.io.File)22 Nullable (org.jetbrains.annotations.Nullable)18 Map (java.util.Map)17 Module (com.intellij.openapi.module.Module)15 Project (com.intellij.openapi.project.Project)15 ArrayList (java.util.ArrayList)14 PsiElement (com.intellij.psi.PsiElement)11 HashSet (com.intellij.util.containers.HashSet)10 List (java.util.List)9 IncorrectOperationException (com.intellij.util.IncorrectOperationException)8 IOException (java.io.IOException)8 Pair (com.intellij.openapi.util.Pair)6 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)6 SearchScope (com.intellij.psi.search.SearchScope)6 Logger (com.intellij.openapi.diagnostic.Logger)5 PsiFile (com.intellij.psi.PsiFile)5 UsageInfo (com.intellij.usageView.UsageInfo)5