Child pages
  • Structure of IntelliJ IDEA Project

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
ModuleRootManager moduleRootManager = ModuleRootManager.getInstance(module);
Sdk SDK = moduleRootManager.getSdk();
String jdkInfo = "Module: " + module.getName() + " SDK: " + SDK.getName() + " SDK version: "
                    + SDK.getVersionString() + " SDK home directory: " + SDK.getHomePath();

How do I get a list of modules on which this module directly depends?

Use the ModuleRootManager.getDependencies() method to get an array of the Module type values or the ModuleRootManager.getDependencyModuleNames() to get an array of module names. To clarify, consider the following code snippet:

Code Block

ModuleRootManager moduleRootManager = ModuleRootManager.getInstance(module);
Module[] dependentModules = moduleRootManager.getDependencies();
String[] dependentModulesNames = moduleRootManager.getDependencyModuleNames();

How do I get a list of modules that depend on this module?

Use the ModuleManager.getModuleDependentModules(module) method.

Note that you can also check whether a module (module1) depends on another specified module (module2) using the ModuleManager.isModuleDependent method in the following way:
boolean isDependent = ModuleManager.getInstance(project).isModuleDependent(module1,module2);

How do I get a module to which the specified file or PSI element belongs?

  • To get the project module to which the specified file belongs, use the ModelUtil.findModuleForFile() static method. To clarify, consider the following code snippet:
Code Block

String pathToFile = "C:\\users\\firstName.LastName\\plugins\\myPlugin\src\myAction.java"; 
VirtualFile virtualFile = LocalFileSystem.getInstance().findFileByPath(pathToFile);
Module module = ModuleUtil.findModuleForFile(virtualFile,myProject);
String moduleName = (module == null) ? "Module not found" : module.getName();

  • To get the project module to which the specified PSI element belongs, use the ModelUtil.findModuleForPsiElement(psiElement) method.

How do I work with libraries available within a module?

How do I get a list of libraries available within a module?

To get the list of libraries, you can enumerate all order entries for the module of interest (see "How do I get order entries for a module?" earlier in this document), and then select the order entries that are instances of the LibraryOrderEntry interface. To clarify this, consider the following code snippet that illustrates how to output the list of libraries for the specified module:

...

This sample code outputs a list of libraries for the module module. In the list, each entry includes the library name and level (such as module, project or application level).

How do I get

...

the library content?

The Library.ModifiableModel class provides the getUrls method you can use to get a list of source roots and classes the library includes. To clarify, consider the following code snippet:

Code Block
ModuleRootManager moduleRootManager Library.ModifiableModel model = ModuleRootManagerlib.getInstancegetModifiableModel(module);
Module[] dependentModules String strRoots = moduleRootManager.getDependencies();
String[] dependentModulesNames = moduleRootManager.getDependencyModuleNames();

How do I get a list of modules that depend on this module?

Use the ModuleManager.getModuleDependentModules(module) method.

Note that you can also check whether a module (module1) depends on another specified module (module2) using the ModuleManager.isModuleDependent method in the following way:
boolean isDependent = ModuleManager.getInstance(project).isModuleDependent(module1,module2);

How do I get a module to which the specified file or PSI element belongs?

  • To get the project module to which the specified file belongs, use the ModelUtil.findModuleForFile() static method. To clarify, consider the following code snippet:
Code Block

String pathToFile = "C:\\users\\firstName.LastName\\plugins\\myPlugin\src\myAction.java"; 
VirtualFile virtualFile = LocalFileSystem.getInstance().findFileByPath(pathToFile);
Module module = ModuleUtil.findModuleForFile(virtualFile,myProject);
String moduleName = (module == null) ? "Module not found" : module.getName();

...

"The " + lib.getName() + " library includes:\n";
  strRoots += "Sources:\n";
  for (String each : myLibModel.getUrls(OrderRootType.SOURCES)) {
           strRoots += each + "\n";
        }
  strRoots += "Classes:\n";
  for (String each : myLibModel.getUrls(OrderRootType.CLASSES)) {
            strRoots += each + "\n";
        }
  Messages.showInfoMessage(strRoots, "Library Info");


In this sample code, lib is of the Library type.

How do I get a set of facets the module includes?

...