Search in sources :

Example 31 with OrderEntry

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

the class LibraryPackagingElement method findLibrary.

@Nullable
public Library findLibrary(@NotNull PackagingElementResolvingContext context) {
    if (myModuleName == null) {
        return context.findLibrary(myLevel, myLibraryName);
    }
    final ModulesProvider modulesProvider = context.getModulesProvider();
    final Module module = modulesProvider.getModule(myModuleName);
    if (module != null) {
        for (OrderEntry entry : modulesProvider.getRootModel(module).getOrderEntries()) {
            if (entry instanceof LibraryOrderEntry) {
                final LibraryOrderEntry libraryEntry = (LibraryOrderEntry) entry;
                if (libraryEntry.isModuleLevel()) {
                    final String libraryName = libraryEntry.getLibraryName();
                    if (libraryName != null && libraryName.equals(myLibraryName)) {
                        return libraryEntry.getLibrary();
                    }
                }
            }
        }
    }
    return null;
}
Also used : ModulesProvider(com.intellij.openapi.roots.ui.configuration.ModulesProvider) OrderEntry(com.intellij.openapi.roots.OrderEntry) LibraryOrderEntry(com.intellij.openapi.roots.LibraryOrderEntry) LibraryOrderEntry(com.intellij.openapi.roots.LibraryOrderEntry) Module(com.intellij.openapi.module.Module) Nullable(org.jetbrains.annotations.Nullable)

Example 32 with OrderEntry

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

the class ChooseLibrariesDialogBase method collectChildren.

protected void collectChildren(Object element, final List<Object> result) {
    if (element instanceof Application) {
        Collections.addAll(result, ProjectManager.getInstance().getOpenProjects());
        final LibraryTablesRegistrar instance = LibraryTablesRegistrar.getInstance();
        //1
        result.add(instance.getLibraryTable());
        //2
        result.addAll(instance.getCustomLibraryTables());
    } else if (element instanceof Project) {
        Collections.addAll(result, ModuleManager.getInstance((Project) element).getModules());
        result.add(LibraryTablesRegistrar.getInstance().getLibraryTable((Project) element));
    } else if (element instanceof LibraryTable) {
        Collections.addAll(result, ((LibraryTable) element).getLibraries());
    } else if (element instanceof Module) {
        for (OrderEntry entry : ModuleRootManager.getInstance((Module) element).getOrderEntries()) {
            if (entry instanceof LibraryOrderEntry) {
                final LibraryOrderEntry libraryOrderEntry = (LibraryOrderEntry) entry;
                if (LibraryTableImplUtil.MODULE_LEVEL.equals(libraryOrderEntry.getLibraryLevel())) {
                    final Library library = libraryOrderEntry.getLibrary();
                    result.add(library);
                }
            }
        }
    }
}
Also used : Project(com.intellij.openapi.project.Project) LibraryTable(com.intellij.openapi.roots.libraries.LibraryTable) OrderEntry(com.intellij.openapi.roots.OrderEntry) LibraryOrderEntry(com.intellij.openapi.roots.LibraryOrderEntry) LibraryTablesRegistrar(com.intellij.openapi.roots.libraries.LibraryTablesRegistrar) LibraryOrderEntry(com.intellij.openapi.roots.LibraryOrderEntry) Library(com.intellij.openapi.roots.libraries.Library) Module(com.intellij.openapi.module.Module) Application(com.intellij.openapi.application.Application)

Example 33 with OrderEntry

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

the class MagicConstantInspection method checkAnnotationsJarAttached.

private static void checkAnnotationsJarAttached(@NotNull PsiFile file, @NotNull ProblemsHolder holder) {
    final Project project = file.getProject();
    if (!holder.isOnTheFly()) {
        final Boolean found = project.getUserData(NO_ANNOTATIONS_FOUND);
        if (found != null)
            return;
    }
    PsiClass event = JavaPsiFacade.getInstance(project).findClass("java.awt.event.InputEvent", GlobalSearchScope.allScope(project));
    // no jdk to attach
    if (event == null)
        return;
    PsiMethod[] methods = event.findMethodsByName("getModifiers", false);
    // no jdk to attach
    if (methods.length != 1)
        return;
    PsiMethod getModifiers = methods[0];
    PsiAnnotation annotation = ExternalAnnotationsManager.getInstance(project).findExternalAnnotation(getModifiers, MagicConstant.class.getName());
    if (annotation != null)
        return;
    final VirtualFile virtualFile = PsiUtilCore.getVirtualFile(getModifiers);
    // no jdk to attach
    if (virtualFile == null)
        return;
    final List<OrderEntry> entries = ProjectRootManager.getInstance(project).getFileIndex().getOrderEntriesForFile(virtualFile);
    Sdk jdk = null;
    for (OrderEntry orderEntry : entries) {
        if (orderEntry instanceof JdkOrderEntry) {
            jdk = ((JdkOrderEntry) orderEntry).getJdk();
            if (jdk != null)
                break;
        }
    }
    // no jdk to attach
    if (jdk == null)
        return;
    if (!holder.isOnTheFly()) {
        project.putUserData(NO_ANNOTATIONS_FOUND, Boolean.TRUE);
    }
    final Sdk finalJdk = jdk;
    String path = finalJdk.getHomePath();
    String text = "No IDEA annotations attached to the JDK " + finalJdk.getName() + (path == null ? "" : " (" + FileUtil.toSystemDependentName(path) + ")") + ", some issues will not be found";
    holder.registerProblem(file, text, ProblemHighlightType.GENERIC_ERROR_OR_WARNING, new LocalQuickFix() {

        @NotNull
        @Override
        public String getFamilyName() {
            return "Attach annotations";
        }

        @Nullable
        @Override
        public PsiElement getElementToMakeWritable(@NotNull PsiFile file) {
            return null;
        }

        @Override
        public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
            SdkModificator modificator = finalJdk.getSdkModificator();
            JavaSdkImpl.attachJdkAnnotations(modificator);
            modificator.commitChanges();
        }
    });
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) MagicConstant(org.intellij.lang.annotations.MagicConstant) JdkOrderEntry(com.intellij.openapi.roots.JdkOrderEntry) SdkModificator(com.intellij.openapi.projectRoots.SdkModificator) NotNull(org.jetbrains.annotations.NotNull) Project(com.intellij.openapi.project.Project) JdkOrderEntry(com.intellij.openapi.roots.JdkOrderEntry) OrderEntry(com.intellij.openapi.roots.OrderEntry) Sdk(com.intellij.openapi.projectRoots.Sdk) Nullable(org.jetbrains.annotations.Nullable)

Example 34 with OrderEntry

use of com.intellij.openapi.roots.OrderEntry 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 35 with OrderEntry

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

the class FindClassUtil method findModulesWithClass.

/**
   * Searches the project for modules that contain the class with the specified full-qualified name within
   * the module dependencies or libraries.
   *
   * @param qualifiedName the full-qualified name of the class to find.
   * @return the modules that contain the given class in dependencies or libraries.
   */
@NotNull
public static Collection<Module> findModulesWithClass(@NotNull Project project, @NonNls @NotNull String qualifiedName) {
    GlobalSearchScope allScope = GlobalSearchScope.allScope(project);
    JavaPsiFacade facade = JavaPsiFacade.getInstance(project);
    PsiClass[] possibleClasses = facade.findClasses(qualifiedName, allScope);
    if (possibleClasses.length == 0) {
        return Collections.emptyList();
    }
    Set<Module> relevantModules = ContainerUtil.newLinkedHashSet();
    ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();
    for (PsiClass aClass : possibleClasses) {
        VirtualFile classFile = aClass.getContainingFile().getVirtualFile();
        for (OrderEntry orderEntry : fileIndex.getOrderEntriesForFile(classFile)) {
            relevantModules.add(orderEntry.getOwnerModule());
        }
    }
    return relevantModules;
}
Also used : JavaPsiFacade(com.intellij.psi.JavaPsiFacade) VirtualFile(com.intellij.openapi.vfs.VirtualFile) OrderEntry(com.intellij.openapi.roots.OrderEntry) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) ProjectFileIndex(com.intellij.openapi.roots.ProjectFileIndex) PsiClass(com.intellij.psi.PsiClass) Module(com.intellij.openapi.module.Module) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

OrderEntry (com.intellij.openapi.roots.OrderEntry)54 Module (com.intellij.openapi.module.Module)24 VirtualFile (com.intellij.openapi.vfs.VirtualFile)23 LibraryOrderEntry (com.intellij.openapi.roots.LibraryOrderEntry)18 Project (com.intellij.openapi.project.Project)14 Library (com.intellij.openapi.roots.libraries.Library)12 ArrayList (java.util.ArrayList)11 JdkOrderEntry (com.intellij.openapi.roots.JdkOrderEntry)10 ProjectFileIndex (com.intellij.openapi.roots.ProjectFileIndex)10 ModifiableRootModel (com.intellij.openapi.roots.ModifiableRootModel)8 ModuleOrderEntry (com.intellij.openapi.roots.ModuleOrderEntry)8 Nullable (org.jetbrains.annotations.Nullable)8 NotNull (org.jetbrains.annotations.NotNull)7 ModuleLibraryOrderEntryImpl (com.intellij.openapi.roots.impl.ModuleLibraryOrderEntryImpl)5 List (java.util.List)5 Sdk (com.intellij.openapi.projectRoots.Sdk)4 ModuleRootManager (com.intellij.openapi.roots.ModuleRootManager)4 AccessToken (com.intellij.openapi.application.AccessToken)3 LibraryEx (com.intellij.openapi.roots.impl.libraries.LibraryEx)3 LibraryTable (com.intellij.openapi.roots.libraries.LibraryTable)3