Search in sources :

Example 21 with SmartList

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

the class CompilerPathsEx method getOutputPaths.

@NotNull
public static String[] getOutputPaths(@NotNull Module[] modules) {
    Set<String> outputPaths = new OrderedSet<>();
    for (Module module : modules) {
        CompilerModuleExtension compilerModuleExtension = !module.isDisposed() ? CompilerModuleExtension.getInstance(module) : null;
        if (compilerModuleExtension == null)
            continue;
        String outputPathUrl = compilerModuleExtension.getCompilerOutputUrl();
        if (outputPathUrl != null) {
            outputPaths.add(VirtualFileManager.extractPath(outputPathUrl).replace('/', File.separatorChar));
        }
        String outputPathForTestsUrl = compilerModuleExtension.getCompilerOutputUrlForTests();
        if (outputPathForTestsUrl != null) {
            outputPaths.add(VirtualFileManager.extractPath(outputPathForTestsUrl).replace('/', File.separatorChar));
        }
        ModuleRootManager moduleRootManager = ModuleRootManager.getInstance(module);
        for (OrderEnumerationHandler.Factory handlerFactory : OrderEnumerationHandler.EP_NAME.getExtensions()) {
            if (handlerFactory.isApplicable(module)) {
                OrderEnumerationHandler handler = handlerFactory.createHandler(module);
                List<String> outputUrls = new SmartList<>();
                handler.addCustomModuleRoots(OrderRootType.CLASSES, moduleRootManager, outputUrls, true, true);
                for (String outputUrl : outputUrls) {
                    outputPaths.add(VirtualFileManager.extractPath(outputUrl).replace('/', File.separatorChar));
                }
            }
        }
    }
    return ArrayUtil.toStringArray(outputPaths);
}
Also used : OrderedSet(com.intellij.util.containers.OrderedSet) Module(com.intellij.openapi.module.Module) SmartList(com.intellij.util.SmartList) NotNull(org.jetbrains.annotations.NotNull)

Example 22 with SmartList

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

the class ProjectTaskManagerImpl method run.

@Override
public void run(@NotNull ProjectTaskContext context, @NotNull ProjectTask projectTask, @Nullable ProjectTaskNotification callback) {
    List<Pair<ProjectTaskRunner, Collection<? extends ProjectTask>>> toRun = new SmartList<>();
    Consumer<Collection<? extends ProjectTask>> taskClassifier = tasks -> {
        Map<ProjectTaskRunner, ? extends List<? extends ProjectTask>> toBuild = tasks.stream().collect(Collectors.groupingBy(aTask -> {
            for (ProjectTaskRunner runner : getTaskRunners()) {
                if (runner.canRun(aTask))
                    return runner;
            }
            return myDefaultProjectTaskRunner;
        }));
        for (Map.Entry<ProjectTaskRunner, ? extends List<? extends ProjectTask>> entry : toBuild.entrySet()) {
            toRun.add(Pair.create(entry.getKey(), entry.getValue()));
        }
    };
    visitTasks(projectTask instanceof ProjectTaskList ? (ProjectTaskList) projectTask : Collections.singleton(projectTask), taskClassifier);
    if (toRun.isEmpty()) {
        sendSuccessNotify(callback);
        return;
    }
    AtomicInteger inProgressCounter = new AtomicInteger(toRun.size());
    AtomicInteger errorsCounter = new AtomicInteger();
    AtomicInteger warningsCounter = new AtomicInteger();
    AtomicBoolean abortedFlag = new AtomicBoolean(false);
    ProjectTaskNotification chunkStatusNotification = callback == null ? null : new ProjectTaskNotification() {

        @Override
        public void finished(@NotNull ProjectTaskResult executionResult) {
            int inProgress = inProgressCounter.decrementAndGet();
            int allErrors = errorsCounter.addAndGet(executionResult.getErrors());
            int allWarnings = warningsCounter.addAndGet(executionResult.getWarnings());
            if (executionResult.isAborted()) {
                abortedFlag.set(true);
            }
            if (inProgress == 0) {
                callback.finished(new ProjectTaskResult(abortedFlag.get(), allErrors, allWarnings));
            }
        }
    };
    toRun.forEach(pair -> {
        if (pair.second.isEmpty()) {
            sendSuccessNotify(chunkStatusNotification);
        } else {
            pair.first.run(myProject, context, chunkStatusNotification, pair.second);
        }
    });
}
Also used : java.util(java.util) ModuleManager(com.intellij.openapi.module.ModuleManager) VirtualFile(com.intellij.openapi.vfs.VirtualFile) ProjectFileIndex(com.intellij.openapi.roots.ProjectFileIndex) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Artifact(com.intellij.packaging.artifacts.Artifact) ContainerUtil(com.intellij.util.containers.ContainerUtil) Collectors(java.util.stream.Collectors) Nullable(org.jetbrains.annotations.Nullable) ContainerUtil.list(com.intellij.util.containers.ContainerUtil.list) SmartList(com.intellij.util.SmartList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ContainerUtil.map(com.intellij.util.containers.ContainerUtil.map) Pair(com.intellij.openapi.util.Pair) Project(com.intellij.openapi.project.Project) Module(com.intellij.openapi.module.Module) NotNull(org.jetbrains.annotations.NotNull) Consumer(com.intellij.util.Consumer) com.intellij.task(com.intellij.task) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SmartList(com.intellij.util.SmartList) SmartList(com.intellij.util.SmartList) Pair(com.intellij.openapi.util.Pair)

Example 23 with SmartList

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

the class CompilerManagerImpl method compileJavaCode.

@Override
public Collection<ClassObject> compileJavaCode(List<String> options, Collection<File> platformCp, Collection<File> classpath, Collection<File> modulePath, Collection<File> sourcePath, Collection<File> files, File outputDir) throws IOException, CompilationException {
    final Pair<Sdk, JavaSdkVersion> runtime = BuildManager.getJavacRuntimeSdk(myProject);
    final Sdk sdk = runtime.getFirst();
    final SdkTypeId type = sdk.getSdkType();
    String javaHome = null;
    if (type instanceof JavaSdkType) {
        javaHome = sdk.getHomePath();
        if (!isJdkOrJre(javaHome)) {
            // this can be a java-dependent SDK, implementing JavaSdkType
            // hack, because there is no direct way to obtain the java sdk, this sdk depends on
            final String binPath = ((JavaSdkType) type).getBinPath(sdk);
            javaHome = binPath != null ? new File(binPath).getParent() : null;
            if (!isJdkOrJre(javaHome)) {
                javaHome = null;
            }
        }
    }
    if (javaHome == null) {
        throw new IOException("Was not able to determine JDK for project " + myProject.getName());
    }
    final OutputCollector outputCollector = new OutputCollector();
    DiagnosticCollector diagnostic = new DiagnosticCollector();
    final Set<File> sourceRoots = new THashSet<>(FileUtil.FILE_HASHING_STRATEGY);
    if (!sourcePath.isEmpty()) {
        for (File file : sourcePath) {
            sourceRoots.add(file);
        }
    } else {
        for (File file : files) {
            final File parentFile = file.getParentFile();
            if (parentFile != null) {
                sourceRoots.add(parentFile);
            }
        }
    }
    final Map<File, Set<File>> outs = Collections.singletonMap(outputDir, sourceRoots);
    final ExternalJavacManager javacManager = getJavacManager();
    boolean compiledOk = javacManager != null && javacManager.forkJavac(javaHome, -1, Collections.emptyList(), options, platformCp, classpath, modulePath, sourcePath, files, outs, diagnostic, outputCollector, new JavacCompilerTool(), CanceledStatus.NULL);
    if (!compiledOk) {
        final List<CompilationException.Message> messages = new SmartList<>();
        for (Diagnostic<? extends JavaFileObject> d : diagnostic.getDiagnostics()) {
            final JavaFileObject source = d.getSource();
            final URI uri = source != null ? source.toUri() : null;
            messages.add(new CompilationException.Message(kindToCategory(d.getKind()), d.getMessage(Locale.US), uri != null ? uri.toURL().toString() : null, (int) d.getLineNumber(), (int) d.getColumnNumber()));
        }
        throw new CompilationException("Compilation failed", messages);
    }
    final List<ClassObject> result = new ArrayList<>();
    for (OutputFileObject fileObject : outputCollector.getCompiledClasses()) {
        final BinaryContent content = fileObject.getContent();
        result.add(new CompiledClass(fileObject.getName(), fileObject.getClassName(), content != null ? content.toByteArray() : null));
    }
    return result;
}
Also used : JavacCompilerTool(org.jetbrains.jps.builders.impl.java.JavacCompilerTool) THashSet(gnu.trove.THashSet) BinaryContent(org.jetbrains.jps.incremental.BinaryContent) URI(java.net.URI) JavaFileObject(javax.tools.JavaFileObject) OutputFileObject(org.jetbrains.jps.javac.OutputFileObject) IOException(java.io.IOException) THashSet(gnu.trove.THashSet) ExternalJavacManager(org.jetbrains.jps.javac.ExternalJavacManager) SmartList(com.intellij.util.SmartList) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File)

Example 24 with SmartList

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

the class AppletConfiguration method readExternal.

@Override
public void readExternal(final Element parentNode) throws InvalidDataException {
    mySerializer.readExternal(this, parentNode);
    List<Element> paramList = parentNode.getChildren(PARAMETER_ELEMENT_NAME);
    if (paramList.isEmpty()) {
        myAppletParameters = null;
    } else {
        List<AppletParameter> parameters = new SmartList<>();
        for (Element element : paramList) {
            parameters.add(new AppletParameter(element.getAttributeValue(NAME_ATTR), element.getAttributeValue(VALUE_ATTR)));
        }
        myAppletParameters = parameters.toArray(new AppletParameter[parameters.size()]);
    }
}
Also used : PsiElement(com.intellij.psi.PsiElement) Element(org.jdom.Element) SmartList(com.intellij.util.SmartList)

Example 25 with SmartList

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

the class PsiSearchHelperImpl method processFilesConcurrentlyDespiteWriteActions.

// Tries to run {@code localProcessor} for each file in {@code files} concurrently on ForkJoinPool.
// When encounters write action request, stops all threads, waits for write action to finish and re-starts all threads again.
// {@localProcessor} must be as idempotent as possible.
public static boolean processFilesConcurrentlyDespiteWriteActions(@NotNull Project project, @NotNull List<VirtualFile> files, @NotNull final ProgressIndicator progress, @NotNull final Processor<VirtualFile> localProcessor) {
    ApplicationEx app = (ApplicationEx) ApplicationManager.getApplication();
    final AtomicBoolean canceled = new AtomicBoolean(false);
    while (true) {
        List<VirtualFile> failedList = new SmartList<>();
        final List<VirtualFile> failedFiles = Collections.synchronizedList(failedList);
        final Processor<VirtualFile> processor = vfile -> {
            try {
                boolean result = localProcessor.process(vfile);
                if (!result) {
                    canceled.set(true);
                }
                return result;
            } catch (ApplicationUtil.CannotRunReadActionException action) {
                failedFiles.add(vfile);
            }
            return !canceled.get();
        };
        boolean completed;
        if (app.isWriteAccessAllowed() || app.isReadAccessAllowed() && app.isWriteActionPending()) {
            // no point in processing in separate threads - they are doomed to fail to obtain read action anyway
            completed = ContainerUtil.process(files, processor);
        } else if (app.isWriteActionPending()) {
            completed = true;
            // we don't have read action now so wait for write action to complete
            failedFiles.addAll(files);
        } else {
            // try to run parallel read actions but fail as soon as possible
            completed = JobLauncher.getInstance().invokeConcurrentlyUnderProgress(files, progress, false, true, processor);
        }
        if (!completed) {
            return false;
        }
        if (failedFiles.isEmpty()) {
            break;
        }
        // we failed to run read action in job launcher thread
        // run read action in our thread instead to wait for a write action to complete and resume parallel processing
        DumbService.getInstance(project).runReadActionInSmartMode(EmptyRunnable.getInstance());
        files = failedList;
    }
    return true;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) com.intellij.openapi.util(com.intellij.openapi.util) java.util(java.util) CommentUtilCore(com.intellij.util.codeInsight.CommentUtilCore) JobLauncher(com.intellij.concurrency.JobLauncher) VirtualFile(com.intellij.openapi.vfs.VirtualFile) ApplicationUtil(com.intellij.openapi.application.ex.ApplicationUtil) PsiManagerEx(com.intellij.psi.impl.PsiManagerEx) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) UsageInfo(com.intellij.usageView.UsageInfo) THashSet(gnu.trove.THashSet) ContainerUtil(com.intellij.util.containers.ContainerUtil) THashMap(gnu.trove.THashMap) ReadAction(com.intellij.openapi.application.ReadAction) ApplicationEx(com.intellij.openapi.application.ex.ApplicationEx) StringSearcher(com.intellij.util.text.StringSearcher) ReadActionProcessor(com.intellij.openapi.application.ReadActionProcessor) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException) TooManyUsagesStatus(com.intellij.openapi.progress.util.TooManyUsagesStatus) SmartList(com.intellij.util.SmartList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CacheManager(com.intellij.psi.impl.cache.CacheManager) Project(com.intellij.openapi.project.Project) FileIndexFacade(com.intellij.openapi.roots.FileIndexFacade) Logger(com.intellij.openapi.diagnostic.Logger) MultiMap(com.intellij.util.containers.MultiMap) FileBasedIndex(com.intellij.util.indexing.FileBasedIndex) DumbService(com.intellij.openapi.project.DumbService) StringUtil(com.intellij.openapi.util.text.StringUtil) Processors(com.intellij.util.Processors) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) IOException(java.io.IOException) IdIndexEntry(com.intellij.psi.impl.cache.impl.id.IdIndexEntry) ProgressIndicatorProvider(com.intellij.openapi.progress.ProgressIndicatorProvider) ExtensionPointName(com.intellij.openapi.extensions.ExtensionPointName) AsyncFuture(com.intellij.concurrency.AsyncFuture) EmptyProgressIndicator(com.intellij.openapi.progress.EmptyProgressIndicator) Nullable(org.jetbrains.annotations.Nullable) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) PsiUtilCore(com.intellij.psi.util.PsiUtilCore) com.intellij.psi.search(com.intellij.psi.search) Processor(com.intellij.util.Processor) ApplicationManager(com.intellij.openapi.application.ApplicationManager) IdIndex(com.intellij.psi.impl.cache.impl.id.IdIndex) com.intellij.psi(com.intellij.psi) UsageInfoFactory(com.intellij.usageView.UsageInfoFactory) AsyncUtil(com.intellij.concurrency.AsyncUtil) NotNull(org.jetbrains.annotations.NotNull) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ApplicationEx(com.intellij.openapi.application.ex.ApplicationEx) SmartList(com.intellij.util.SmartList)

Aggregations

SmartList (com.intellij.util.SmartList)163 NotNull (org.jetbrains.annotations.NotNull)70 Nullable (org.jetbrains.annotations.Nullable)25 VirtualFile (com.intellij.openapi.vfs.VirtualFile)24 Module (com.intellij.openapi.module.Module)15 Project (com.intellij.openapi.project.Project)14 TextRange (com.intellij.openapi.util.TextRange)12 PsiElement (com.intellij.psi.PsiElement)12 List (java.util.List)12 Element (org.jdom.Element)12 File (java.io.File)11 THashSet (gnu.trove.THashSet)9 ContainerUtil (com.intellij.util.containers.ContainerUtil)8 ArrayList (java.util.ArrayList)8 Collection (java.util.Collection)8 Pair (com.intellij.openapi.util.Pair)7 PsiFile (com.intellij.psi.PsiFile)6 IOException (java.io.IOException)6 PropertiesFile (com.intellij.lang.properties.psi.PropertiesFile)5 IElementType (com.intellij.psi.tree.IElementType)5