Search in sources :

Example 41 with THashSet

use of gnu.trove.THashSet in project intellij-community by JetBrains.

the class JavaBuilderUtil method updateMappings.

/**
   * @param filesToCompile   files compiled in this round
   * @param markDirtyRound   compilation round at which dirty files should be visible to builders
   * @return true if additional compilation pass is required, false otherwise
   * @throws Exception
   */
private static boolean updateMappings(CompileContext context, final Mappings delta, DirtyFilesHolder<JavaSourceRootDescriptor, ModuleBuildTarget> dirtyFilesHolder, ModuleChunk chunk, Collection<File> filesToCompile, Collection<File> successfullyCompiled, final CompilationRound markDirtyRound, @Nullable FileFilter skipMarkingDirtyFilter) throws IOException {
    try {
        boolean performIntegrate = true;
        boolean additionalPassRequired = false;
        final Set<String> removedPaths = getRemovedPaths(chunk, dirtyFilesHolder);
        final Mappings globalMappings = context.getProjectDescriptor().dataManager.getMappings();
        final boolean errorsDetected = Utils.errorsDetected(context);
        if (!isForcedRecompilationAllJavaModules(context)) {
            if (context.shouldDifferentiate(chunk)) {
                context.processMessage(new ProgressMessage("Checking dependencies... [" + chunk.getPresentableShortName() + "]"));
                final Set<File> allCompiledFiles = getFilesContainer(context, ALL_COMPILED_FILES_KEY);
                final Set<File> allAffectedFiles = getFilesContainer(context, ALL_AFFECTED_FILES_KEY);
                // mark as affected all files that were dirty before compilation
                allAffectedFiles.addAll(filesToCompile);
                // accumulate all successfully compiled in this round
                allCompiledFiles.addAll(successfullyCompiled);
                // unmark as affected all successfully compiled
                allAffectedFiles.removeAll(successfullyCompiled);
                final Set<File> affectedBeforeDif = new THashSet<>(FileUtil.FILE_HASHING_STRATEGY);
                affectedBeforeDif.addAll(allAffectedFiles);
                final Set<File> compiledWithErrors = getFilesContainer(context, COMPILED_WITH_ERRORS_KEY);
                COMPILED_WITH_ERRORS_KEY.set(context, null);
                final ModulesBasedFileFilter moduleBasedFilter = new ModulesBasedFileFilter(context, chunk);
                final boolean incremental = globalMappings.differentiateOnIncrementalMake(delta, removedPaths, filesToCompile, compiledWithErrors, allCompiledFiles, allAffectedFiles, moduleBasedFilter, CONSTANT_SEARCH_SERVICE.get(context));
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Differentiate Results:");
                    LOG.debug("   Compiled Files:");
                    for (final File c : allCompiledFiles) {
                        LOG.debug("      " + c.getAbsolutePath());
                    }
                    LOG.debug("   Affected Files:");
                    for (final File c : allAffectedFiles) {
                        LOG.debug("      " + c.getAbsolutePath());
                    }
                    LOG.debug("End Of Differentiate Results.");
                }
                if (incremental) {
                    final Set<File> newlyAffectedFiles = new HashSet<>(allAffectedFiles);
                    newlyAffectedFiles.removeAll(affectedBeforeDif);
                    final String infoMessage = "Dependency analysis found " + newlyAffectedFiles.size() + " affected files";
                    LOG.info(infoMessage);
                    context.processMessage(new ProgressMessage(infoMessage));
                    removeFilesAcceptedByFilter(newlyAffectedFiles, skipMarkingDirtyFilter);
                    if (!newlyAffectedFiles.isEmpty()) {
                        if (LOG.isDebugEnabled()) {
                            for (File file : newlyAffectedFiles) {
                                LOG.debug("affected file: " + file.getPath());
                            }
                            final List<Pair<File, JpsModule>> wrongFiles = checkAffectedFilesInCorrectModules(context, newlyAffectedFiles, moduleBasedFilter);
                            if (!wrongFiles.isEmpty()) {
                                LOG.debug("Wrong affected files for module chunk " + chunk.getName() + ": ");
                                for (Pair<File, JpsModule> pair : wrongFiles) {
                                    final String name = pair.second != null ? pair.second.getName() : "null";
                                    LOG.debug("\t[" + name + "] " + pair.first.getPath());
                                }
                            }
                        }
                        for (File file : newlyAffectedFiles) {
                            FSOperations.markDirtyIfNotDeleted(context, markDirtyRound, file);
                        }
                        additionalPassRequired = isCompileJavaIncrementally(context) && chunkContainsAffectedFiles(context, chunk, newlyAffectedFiles);
                    }
                } else {
                    // non-incremental mode
                    final String messageText = "Marking " + chunk.getPresentableShortName() + " and direct dependants for recompilation";
                    LOG.info("Non-incremental mode: " + messageText);
                    context.processMessage(new ProgressMessage(messageText));
                    final boolean alreadyMarkedDirty = FSOperations.isMarkedDirty(context, chunk);
                    additionalPassRequired = isCompileJavaIncrementally(context) && !alreadyMarkedDirty;
                    if (alreadyMarkedDirty) {
                        // need this to make sure changes data stored in Delta is complete
                        globalMappings.differentiateOnNonIncrementalMake(delta, removedPaths, filesToCompile);
                    } else {
                        performIntegrate = false;
                    }
                    FileFilter toBeMarkedFilter = skipMarkingDirtyFilter == null ? null : new NegationFileFilter(skipMarkingDirtyFilter);
                    FSOperations.markDirtyRecursively(context, markDirtyRound, chunk, toBeMarkedFilter);
                }
            } else {
                if (!errorsDetected) {
                    // makes sense only if we are going to integrate changes
                    globalMappings.differentiateOnNonIncrementalMake(delta, removedPaths, filesToCompile);
                }
            }
        } else {
            if (!errorsDetected) {
                // makes sense only if we are going to integrate changes
                globalMappings.differentiateOnRebuild(delta);
            }
        }
        if (errorsDetected) {
            // will be compiled during the first phase of the next make
            return false;
        }
        if (performIntegrate) {
            context.processMessage(new ProgressMessage("Updating dependency information... [" + chunk.getPresentableShortName() + "]"));
            globalMappings.integrate(delta);
        }
        return additionalPassRequired;
    } catch (BuildDataCorruptedException e) {
        throw e.getCause();
    } finally {
        // clean progress messages
        context.processMessage(new ProgressMessage(""));
    }
}
Also used : ProgressMessage(org.jetbrains.jps.incremental.messages.ProgressMessage) THashSet(gnu.trove.THashSet) JpsModule(org.jetbrains.jps.model.module.JpsModule) BuildDataCorruptedException(org.jetbrains.jps.builders.storage.BuildDataCorruptedException) Mappings(org.jetbrains.jps.builders.java.dependencyView.Mappings) FileFilter(java.io.FileFilter) File(java.io.File) THashSet(gnu.trove.THashSet) Pair(com.intellij.openapi.util.Pair)

Example 42 with THashSet

use of gnu.trove.THashSet in project intellij-community by JetBrains.

the class JavaRunConfigurationModule method getModulesForClass.

public static Collection<Module> getModulesForClass(@NotNull final Project project, final String className) {
    if (project.isDefault())
        return Arrays.asList(ModuleManager.getInstance(project).getModules());
    PsiDocumentManager.getInstance(project).commitAllDocuments();
    final PsiClass[] possibleClasses = JavaPsiFacade.getInstance(project).findClasses(className, GlobalSearchScope.projectScope(project));
    final Set<Module> modules = new THashSet<>();
    for (PsiClass aClass : possibleClasses) {
        Module module = ModuleUtilCore.findModuleForPsiElement(aClass);
        if (module != null) {
            modules.add(module);
        }
    }
    if (modules.isEmpty()) {
        return Arrays.asList(ModuleManager.getInstance(project).getModules());
    } else {
        final Set<Module> result = new HashSet<>();
        for (Module module : modules) {
            ModuleUtilCore.collectModulesDependsOn(module, result);
        }
        return result;
    }
}
Also used : PsiClass(com.intellij.psi.PsiClass) Module(com.intellij.openapi.module.Module) THashSet(gnu.trove.THashSet) THashSet(gnu.trove.THashSet) HashSet(java.util.HashSet)

Example 43 with THashSet

use of gnu.trove.THashSet in project intellij-community by JetBrains.

the class TypeConversionUtil method getAllBoxedTypeSupers.

@NotNull
private static Set<String> getAllBoxedTypeSupers(@NotNull PsiClass psiClass) {
    PsiManager manager = psiClass.getManager();
    final Project project = psiClass.getProject();
    CachedValue<Set<String>> boxedHolderTypes = project.getUserData(POSSIBLE_BOXED_HOLDER_TYPES);
    if (boxedHolderTypes == null) {
        project.putUserData(POSSIBLE_BOXED_HOLDER_TYPES, boxedHolderTypes = CachedValuesManager.getManager(manager.getProject()).createCachedValue(() -> {
            final JavaPsiFacade facade = JavaPsiFacade.getInstance(project);
            final Set<String> set = new THashSet<>();
            for (final String qname : PsiPrimitiveType.getAllBoxedTypeNames()) {
                final PsiClass boxedClass = facade.findClass(qname, GlobalSearchScope.allScope(project));
                InheritanceUtil.processSupers(boxedClass, true, psiClass1 -> {
                    ContainerUtil.addIfNotNull(set, psiClass1.getQualifiedName());
                    return true;
                });
            }
            return CachedValueProvider.Result.create(set, ProjectRootModificationTracker.getInstance(project));
        }, false));
    }
    return boxedHolderTypes.getValue();
}
Also used : Project(com.intellij.openapi.project.Project) THashSet(gnu.trove.THashSet) THashSet(gnu.trove.THashSet) NotNull(org.jetbrains.annotations.NotNull)

Example 44 with THashSet

use of gnu.trove.THashSet in project intellij-community by JetBrains.

the class AnnotationUtil method getAllAnnotations.

@NotNull
public static PsiAnnotation[] getAllAnnotations(@NotNull PsiModifierListOwner owner, boolean inHierarchy, @Nullable Set<PsiModifierListOwner> visited, boolean withInferred) {
    final PsiModifierList list = owner.getModifierList();
    PsiAnnotation[] annotations = PsiAnnotation.EMPTY_ARRAY;
    if (list != null) {
        annotations = list.getAnnotations();
    }
    final Project project = owner.getProject();
    final PsiAnnotation[] externalAnnotations = ExternalAnnotationsManager.getInstance(project).findExternalAnnotations(owner);
    if (externalAnnotations != null) {
        annotations = ArrayUtil.mergeArrays(annotations, externalAnnotations, PsiAnnotation.ARRAY_FACTORY);
    }
    if (withInferred) {
        final PsiAnnotation[] inferredAnnotations = InferredAnnotationsManager.getInstance(project).findInferredAnnotations(owner);
        annotations = ArrayUtil.mergeArrays(annotations, inferredAnnotations, PsiAnnotation.ARRAY_FACTORY);
    }
    if (inHierarchy) {
        if (owner instanceof PsiClass) {
            for (PsiClass superClass : ((PsiClass) owner).getSupers()) {
                if (visited == null)
                    visited = new THashSet<>();
                if (visited.add(superClass))
                    annotations = ArrayUtil.mergeArrays(annotations, getAllAnnotations(superClass, true, visited, withInferred));
            }
        } else if (owner instanceof PsiMethod) {
            PsiMethod method = (PsiMethod) owner;
            PsiClass aClass = method.getContainingClass();
            if (aClass != null) {
                HierarchicalMethodSignature methodSignature = method.getHierarchicalMethodSignature();
                final List<HierarchicalMethodSignature> superSignatures = methodSignature.getSuperSignatures();
                PsiResolveHelper resolveHelper = PsiResolveHelper.SERVICE.getInstance(aClass.getProject());
                for (final HierarchicalMethodSignature superSignature : superSignatures) {
                    final PsiMethod superMethod = superSignature.getMethod();
                    if (visited == null)
                        visited = new THashSet<>();
                    if (!visited.add(superMethod))
                        continue;
                    if (!resolveHelper.isAccessible(superMethod, owner, null))
                        continue;
                    annotations = ArrayUtil.mergeArrays(annotations, getAllAnnotations(superMethod, true, visited, withInferred));
                }
            }
        } else if (owner instanceof PsiParameter) {
            PsiParameter parameter = (PsiParameter) owner;
            PsiElement scope = parameter.getDeclarationScope();
            if (scope instanceof PsiMethod) {
                PsiMethod method = (PsiMethod) scope;
                PsiClass aClass = method.getContainingClass();
                PsiElement parent = parameter.getParent();
                if (aClass != null && parent instanceof PsiParameterList) {
                    int index = ((PsiParameterList) parent).getParameterIndex(parameter);
                    HierarchicalMethodSignature methodSignature = method.getHierarchicalMethodSignature();
                    final List<HierarchicalMethodSignature> superSignatures = methodSignature.getSuperSignatures();
                    PsiResolveHelper resolveHelper = PsiResolveHelper.SERVICE.getInstance(aClass.getProject());
                    for (final HierarchicalMethodSignature superSignature : superSignatures) {
                        final PsiMethod superMethod = superSignature.getMethod();
                        if (visited == null)
                            visited = new THashSet<>();
                        if (!visited.add(superMethod))
                            continue;
                        if (!resolveHelper.isAccessible(superMethod, owner, null))
                            continue;
                        PsiParameter[] superParameters = superMethod.getParameterList().getParameters();
                        if (index < superParameters.length) {
                            annotations = ArrayUtil.mergeArrays(annotations, getAllAnnotations(superParameters[index], true, visited, withInferred));
                        }
                    }
                }
            }
        }
    }
    return annotations;
}
Also used : THashSet(gnu.trove.THashSet) Project(com.intellij.openapi.project.Project) NotNull(org.jetbrains.annotations.NotNull)

Example 45 with THashSet

use of gnu.trove.THashSet in project intellij-community by JetBrains.

the class BaseExternalAnnotationsManager method findExternalAnnotationsFiles.

@Override
@Nullable
public List<PsiFile> findExternalAnnotationsFiles(@NotNull PsiModifierListOwner listOwner) {
    final PsiFile containingFile = PsiUtil.preferCompiledElement(listOwner).getContainingFile();
    if (!(containingFile instanceof PsiJavaFile))
        return null;
    final VirtualFile virtualFile = containingFile.getVirtualFile();
    if (virtualFile == null)
        return null;
    final List<PsiFile> files = myExternalAnnotationsCache.get(virtualFile);
    if (files == NULL_LIST)
        return null;
    if (files != null) {
        boolean allValid = true;
        for (PsiFile file : files) {
            if (!file.isValid()) {
                allValid = false;
                break;
            }
        }
        if (allValid) {
            return files;
        }
    }
    Set<PsiFile> possibleAnnotationXmls = new THashSet<>();
    String relativePath = ((PsiJavaFile) containingFile).getPackageName().replace('.', '/') + '/' + ANNOTATIONS_XML;
    for (VirtualFile root : getExternalAnnotationsRoots(virtualFile)) {
        VirtualFile ext = root.findFileByRelativePath(relativePath);
        if (ext != null && ext.isValid()) {
            PsiFile psiFile = myPsiManager.findFile(ext);
            if (psiFile != null) {
                possibleAnnotationXmls.add(psiFile);
            }
        }
    }
    if (possibleAnnotationXmls.isEmpty()) {
        myExternalAnnotationsCache.put(virtualFile, NULL_LIST);
        return null;
    }
    List<PsiFile> result = new SmartList<>(possibleAnnotationXmls);
    // writable go first
    result.sort((f1, f2) -> {
        boolean w1 = f1.isWritable();
        boolean w2 = f2.isWritable();
        return w1 == w2 ? 0 : w1 ? -1 : 1;
    });
    myExternalAnnotationsCache.put(virtualFile, result);
    return result;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) LightVirtualFile(com.intellij.testFramework.LightVirtualFile) SmartList(com.intellij.util.SmartList) THashSet(gnu.trove.THashSet) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

THashSet (gnu.trove.THashSet)239 NotNull (org.jetbrains.annotations.NotNull)65 VirtualFile (com.intellij.openapi.vfs.VirtualFile)62 Project (com.intellij.openapi.project.Project)35 File (java.io.File)35 THashMap (gnu.trove.THashMap)31 Nullable (org.jetbrains.annotations.Nullable)31 Module (com.intellij.openapi.module.Module)29 IOException (java.io.IOException)24 PsiElement (com.intellij.psi.PsiElement)21 PsiFile (com.intellij.psi.PsiFile)18 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)16 java.util (java.util)16 Element (org.jdom.Element)14 Pair (com.intellij.openapi.util.Pair)13 Logger (com.intellij.openapi.diagnostic.Logger)12 ContainerUtil (com.intellij.util.containers.ContainerUtil)12 Document (com.intellij.openapi.editor.Document)11 Library (com.intellij.openapi.roots.libraries.Library)11 StringUtil (com.intellij.openapi.util.text.StringUtil)10