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;
}
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;
}
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();
}
});
}
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));
}
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;
}
Aggregations