Search in sources :

Example 31 with ModuleRootManager

use of com.intellij.openapi.roots.ModuleRootManager in project intellij-community by JetBrains.

the class AbstractProjectViewPane method getSelectedDirectoriesInAmbiguousCase.

@NotNull
protected PsiDirectory[] getSelectedDirectoriesInAmbiguousCase(@NotNull final DefaultMutableTreeNode node) {
    final Object userObject = node.getUserObject();
    if (userObject instanceof AbstractModuleNode) {
        final Module module = ((AbstractModuleNode) userObject).getValue();
        if (module != null) {
            final ModuleRootManager moduleRootManager = ModuleRootManager.getInstance(module);
            final VirtualFile[] sourceRoots = moduleRootManager.getSourceRoots();
            List<PsiDirectory> dirs = new ArrayList<>(sourceRoots.length);
            final PsiManager psiManager = PsiManager.getInstance(myProject);
            for (final VirtualFile sourceRoot : sourceRoots) {
                final PsiDirectory directory = psiManager.findDirectory(sourceRoot);
                if (directory != null) {
                    dirs.add(directory);
                }
            }
            return dirs.toArray(new PsiDirectory[dirs.size()]);
        }
    }
    return PsiDirectory.EMPTY_ARRAY;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) AbstractModuleNode(com.intellij.ide.projectView.impl.nodes.AbstractModuleNode) ArrayList(java.util.ArrayList) ModuleRootManager(com.intellij.openapi.roots.ModuleRootManager) Module(com.intellij.openapi.module.Module) NotNull(org.jetbrains.annotations.NotNull)

Example 32 with ModuleRootManager

use of com.intellij.openapi.roots.ModuleRootManager in project intellij-community by JetBrains.

the class CompilingEvaluatorImpl method compile.

@Override
@NotNull
protected Collection<ClassObject> compile(@Nullable JavaSdkVersion debuggeeVersion) throws EvaluateException {
    if (myCompiledClasses == null) {
        Module module = ReadAction.compute(() -> ModuleUtilCore.findModuleForPsiElement(myPsiContext));
        List<String> options = new ArrayList<>();
        options.add("-encoding");
        options.add("UTF-8");
        List<File> platformClasspath = new ArrayList<>();
        List<File> classpath = new ArrayList<>();
        AnnotationProcessingConfiguration profile = null;
        if (module != null) {
            assert myProject.equals(module.getProject()) : module + " is from another project";
            profile = CompilerConfiguration.getInstance(myProject).getAnnotationProcessingConfiguration(module);
            ModuleRootManager rootManager = ModuleRootManager.getInstance(module);
            for (String s : rootManager.orderEntries().compileOnly().recursively().exportedOnly().withoutSdk().getPathsList().getPathList()) {
                classpath.add(new File(s));
            }
            for (String s : rootManager.orderEntries().compileOnly().sdkOnly().getPathsList().getPathList()) {
                platformClasspath.add(new File(s));
            }
        }
        JavaBuilder.addAnnotationProcessingOptions(options, profile);
        Pair<Sdk, JavaSdkVersion> runtime = BuildManager.getJavacRuntimeSdk(myProject);
        JavaSdkVersion buildRuntimeVersion = runtime.getSecond();
        // if compiler or debuggee version or both are unknown, let source and target be the compiler's defaults
        if (buildRuntimeVersion != null && debuggeeVersion != null) {
            JavaSdkVersion minVersion = buildRuntimeVersion.ordinal() > debuggeeVersion.ordinal() ? debuggeeVersion : buildRuntimeVersion;
            String sourceOption = getSourceOption(minVersion.getMaxLanguageLevel());
            options.add("-source");
            options.add(sourceOption);
            options.add("-target");
            options.add(sourceOption);
        }
        CompilerManager compilerManager = CompilerManager.getInstance(myProject);
        File sourceFile = null;
        try {
            sourceFile = generateTempSourceFile(compilerManager.getJavacCompilerWorkingDir());
            File srcDir = sourceFile.getParentFile();
            List<File> sourcePath = Collections.emptyList();
            Set<File> sources = Collections.singleton(sourceFile);
            myCompiledClasses = compilerManager.compileJavaCode(options, platformClasspath, classpath, Collections.emptyList(), sourcePath, sources, srcDir);
        } catch (CompilationException e) {
            StringBuilder res = new StringBuilder("Compilation failed:\n");
            for (CompilationException.Message m : e.getMessages()) {
                if (m.getCategory() == CompilerMessageCategory.ERROR) {
                    res.append(m.getText()).append("\n");
                }
            }
            throw new EvaluateException(res.toString());
        } catch (Exception e) {
            throw new EvaluateException(e.getMessage());
        } finally {
            if (sourceFile != null) {
                FileUtil.delete(sourceFile);
            }
        }
    }
    return myCompiledClasses;
}
Also used : CompilationException(com.intellij.openapi.compiler.CompilationException) JavaSdkVersion(com.intellij.openapi.projectRoots.JavaSdkVersion) CompilerManager(com.intellij.openapi.compiler.CompilerManager) ModuleRootManager(com.intellij.openapi.roots.ModuleRootManager) AnnotationProcessingConfiguration(org.jetbrains.jps.model.java.compiler.AnnotationProcessingConfiguration) CompilationException(com.intellij.openapi.compiler.CompilationException) EvaluateException(com.intellij.debugger.engine.evaluation.EvaluateException) IOException(java.io.IOException) PrepareFailedException(com.intellij.refactoring.extractMethod.PrepareFailedException) EvaluateException(com.intellij.debugger.engine.evaluation.EvaluateException) Sdk(com.intellij.openapi.projectRoots.Sdk) Module(com.intellij.openapi.module.Module) PsiFile(com.intellij.psi.PsiFile) File(java.io.File) NotNull(org.jetbrains.annotations.NotNull)

Example 33 with ModuleRootManager

use of com.intellij.openapi.roots.ModuleRootManager in project intellij-community by JetBrains.

the class OrderEntryTest method removeLibs.

private void removeLibs() {
    ApplicationManager.getApplication().runWriteAction(() -> {
        try {
            for (Module module : ModuleManager.getInstance(getProject()).getModules()) {
                ModuleRootManager rootManager = ModuleRootManager.getInstance(module);
                ModifiableRootModel model = rootManager.getModifiableModel();
                for (OrderEntry orderEntry : model.getOrderEntries()) {
                    model.removeOrderEntry(orderEntry);
                }
                model.commit();
            }
        } catch (Throwable e) {
            // when running test from within IDEA it would fail because junit.jar cache is locked by host IDEA instance
            e.printStackTrace();
        }
    });
}
Also used : ModifiableRootModel(com.intellij.openapi.roots.ModifiableRootModel) OrderEntry(com.intellij.openapi.roots.OrderEntry) ModuleRootManager(com.intellij.openapi.roots.ModuleRootManager) Module(com.intellij.openapi.module.Module)

Example 34 with ModuleRootManager

use of com.intellij.openapi.roots.ModuleRootManager in project intellij-community by JetBrains.

the class RootsTest method testTest1.

public void testTest1() {
    final String rootPath = PathManagerEx.getTestDataPath() + "/moduleRootManager/roots/" + "test1";
    final VirtualFile[] rootFileBox = new VirtualFile[1];
    ApplicationManager.getApplication().runWriteAction(() -> {
        rootFileBox[0] = LocalFileSystem.getInstance().refreshAndFindFileByPath(rootPath.replace(File.separatorChar, '/'));
    });
    final VirtualFile rootFile = rootFileBox[0];
    final VirtualFile classesFile = rootFile.findChild("classes");
    assertNotNull(classesFile);
    final VirtualFile childOfContent = rootFile.findChild("x.txt");
    assertNotNull(childOfContent);
    final VirtualFile childOfClasses = classesFile.findChild("y.txt");
    assertNotNull(childOfClasses);
    final ModuleRootManager rootManager = ModuleRootManager.getInstance(myModule);
    PsiTestUtil.addContentRoot(myModule, rootFile);
    PsiTestUtil.setCompilerOutputPath(myModule, classesFile.getUrl(), false);
    PsiTestUtil.setExcludeCompileOutput(myModule, false);
    assertTrue(rootManager.getFileIndex().isInContent(childOfContent));
    assertTrue(rootManager.getFileIndex().isInContent(childOfClasses));
    PsiTestUtil.setExcludeCompileOutput(myModule, true);
    assertTrue(rootManager.getFileIndex().isInContent(childOfContent));
    assertFalse(rootManager.getFileIndex().isInContent(childOfClasses));
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ModuleRootManager(com.intellij.openapi.roots.ModuleRootManager)

Example 35 with ModuleRootManager

use of com.intellij.openapi.roots.ModuleRootManager in project intellij-community by JetBrains.

the class UnnecessaryModuleDependencyInspection method checkElement.

@Override
public CommonProblemDescriptor[] checkElement(@NotNull RefEntity refEntity, @NotNull AnalysisScope scope, @NotNull InspectionManager manager, @NotNull final GlobalInspectionContext globalContext) {
    if (refEntity instanceof RefModule) {
        final RefModule refModule = (RefModule) refEntity;
        final Module module = refModule.getModule();
        if (module.isDisposed() || !scope.containsModule(module))
            return CommonProblemDescriptor.EMPTY_ARRAY;
        final ModuleRootManager moduleRootManager = ModuleRootManager.getInstance(module);
        final OrderEntry[] declaredDependencies = moduleRootManager.getOrderEntries();
        final Module[] declaredModuleDependencies = moduleRootManager.getDependencies();
        List<CommonProblemDescriptor> descriptors = new ArrayList<>();
        final Set<Module> modules = refModule.getUserData(UnnecessaryModuleDependencyAnnotator.DEPENDENCIES);
        Graph<Module> graph = myGraph.get();
        if (graph == null) {
            graph = ModuleManager.getInstance(globalContext.getProject()).moduleGraph();
            myGraph = new SoftReference<>(graph);
        }
        final RefManager refManager = globalContext.getRefManager();
        for (final OrderEntry entry : declaredDependencies) {
            if (entry instanceof ModuleOrderEntry) {
                final Module dependency = ((ModuleOrderEntry) entry).getModule();
                if (dependency != null) {
                    if (modules == null || !modules.contains(dependency)) {
                        List<String> dependenciesThroughExported = null;
                        if (((ModuleOrderEntry) entry).isExported()) {
                            final Iterator<Module> iterator = graph.getOut(module);
                            while (iterator.hasNext()) {
                                final Module dep = iterator.next();
                                final RefModule depRefModule = refManager.getRefModule(dep);
                                if (depRefModule != null) {
                                    final Set<Module> neededModules = depRefModule.getUserData(UnnecessaryModuleDependencyAnnotator.DEPENDENCIES);
                                    if (neededModules != null && neededModules.contains(dependency)) {
                                        if (dependenciesThroughExported == null) {
                                            dependenciesThroughExported = new ArrayList<>();
                                        }
                                        dependenciesThroughExported.add(dep.getName());
                                    }
                                }
                            }
                        }
                        if (modules != null) {
                            List<String> transitiveDependencies = new ArrayList<>();
                            final OrderEntry[] dependenciesOfDependencies = ModuleRootManager.getInstance(dependency).getOrderEntries();
                            for (OrderEntry secondDependency : dependenciesOfDependencies) {
                                if (secondDependency instanceof ModuleOrderEntry && ((ModuleOrderEntry) secondDependency).isExported()) {
                                    final Module mod = ((ModuleOrderEntry) secondDependency).getModule();
                                    if (mod != null && modules.contains(mod) && ArrayUtil.find(declaredModuleDependencies, mod) < 0) {
                                        transitiveDependencies.add(mod.getName());
                                    }
                                }
                            }
                            if (!transitiveDependencies.isEmpty()) {
                                final String exported = StringUtil.join(transitiveDependencies, ", ");
                                descriptors.add(manager.createProblemDescriptor(InspectionsBundle.message("unnecessary.module.dependency.exported.problem.descriptor1", module.getName(), dependency.getName(), exported)));
                                continue;
                            }
                        }
                        descriptors.add(createDescriptor(scope, manager, module, dependency, dependenciesThroughExported));
                    }
                }
            }
        }
        return descriptors.isEmpty() ? null : descriptors.toArray(new CommonProblemDescriptor[descriptors.size()]);
    }
    return null;
}
Also used : RefManager(com.intellij.codeInspection.reference.RefManager) ArrayList(java.util.ArrayList) ModuleRootManager(com.intellij.openapi.roots.ModuleRootManager) RefModule(com.intellij.codeInspection.reference.RefModule) ModuleOrderEntry(com.intellij.openapi.roots.ModuleOrderEntry) OrderEntry(com.intellij.openapi.roots.OrderEntry) ModuleOrderEntry(com.intellij.openapi.roots.ModuleOrderEntry) RefModule(com.intellij.codeInspection.reference.RefModule) Module(com.intellij.openapi.module.Module)

Aggregations

ModuleRootManager (com.intellij.openapi.roots.ModuleRootManager)47 VirtualFile (com.intellij.openapi.vfs.VirtualFile)26 Module (com.intellij.openapi.module.Module)19 ModifiableRootModel (com.intellij.openapi.roots.ModifiableRootModel)13 ContentEntry (com.intellij.openapi.roots.ContentEntry)11 Sdk (com.intellij.openapi.projectRoots.Sdk)10 File (java.io.File)7 NotNull (org.jetbrains.annotations.NotNull)7 Nullable (org.jetbrains.annotations.Nullable)6 SourceFolder (com.intellij.openapi.roots.SourceFolder)5 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 AndroidModuleModel (com.android.tools.idea.gradle.project.model.AndroidModuleModel)2 ModuleManager (com.intellij.openapi.module.ModuleManager)2 OrderEntry (com.intellij.openapi.roots.OrderEntry)2 ProjectRootManager (com.intellij.openapi.roots.ProjectRootManager)2 ProjectRootManagerEx (com.intellij.openapi.roots.ex.ProjectRootManagerEx)2 FilePaths.findParentContentEntry (com.android.tools.idea.gradle.util.FilePaths.findParentContentEntry)1 GradleUtil.getGradleBuildFile (com.android.tools.idea.gradle.util.GradleUtil.getGradleBuildFile)1 CodeInsightTestCase (com.intellij.codeInsight.CodeInsightTestCase)1