Search in sources :

Example 46 with OrderEntry

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

the class PyModuleDependenciesConfigurable method apply.

@Override
public void apply() throws ConfigurationException {
    ApplicationManager.getApplication().runWriteAction(() -> {
        ModifiableRootModel model = ModuleRootManager.getInstance(myModule).getModifiableModel();
        List<ModuleOrderEntry> entries = new ArrayList<>();
        for (OrderEntry entry : model.getOrderEntries()) {
            if (entry instanceof ModuleOrderEntry) {
                entries.add((ModuleOrderEntry) entry);
            }
        }
        for (ModuleOrderEntry entry : entries) {
            model.removeOrderEntry(entry);
        }
        for (Module module : collectDependencies()) {
            model.addModuleOrderEntry(module);
        }
        model.commit();
    });
}
Also used : ModifiableRootModel(com.intellij.openapi.roots.ModifiableRootModel) OrderEntry(com.intellij.openapi.roots.OrderEntry) ModuleOrderEntry(com.intellij.openapi.roots.ModuleOrderEntry) ArrayList(java.util.ArrayList) ModuleOrderEntry(com.intellij.openapi.roots.ModuleOrderEntry) Module(com.intellij.openapi.module.Module)

Example 47 with OrderEntry

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

the class XmlIndex method createFilter.

protected static VirtualFileFilter createFilter(@NotNull final Module module) {
    final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(module.getProject()).getFileIndex();
    return new VirtualFileFilter() {

        @Override
        public boolean accept(final VirtualFile file) {
            Module moduleForFile = fileIndex.getModuleForFile(file);
            if (moduleForFile != null) {
                // in module content
                return module.equals(moduleForFile);
            }
            if (fileIndex.isInLibraryClasses(file)) {
                List<OrderEntry> orderEntries = fileIndex.getOrderEntriesForFile(file);
                if (orderEntries.isEmpty()) {
                    return false;
                }
                for (OrderEntry orderEntry : orderEntries) {
                    Module ownerModule = orderEntry.getOwnerModule();
                    if (ownerModule.equals(module)) {
                        return true;
                    }
                }
            }
            final VirtualFile parent = file.getParent();
            assert parent != null;
            return parent.getName().equals("standardSchemas");
        }
    };
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) VirtualFileFilter(com.intellij.openapi.vfs.VirtualFileFilter) OrderEntry(com.intellij.openapi.roots.OrderEntry) ProjectFileIndex(com.intellij.openapi.roots.ProjectFileIndex) Module(com.intellij.openapi.module.Module)

Example 48 with OrderEntry

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

the class BundleManifestCache method getManifest.

@Nullable
public BundleManifest getManifest(@NotNull PsiFileSystemItem item) {
    VirtualFile file = item.getVirtualFile();
    if (file != null) {
        List<OrderEntry> entries = myIndex.getOrderEntriesForFile(file);
        if (entries.size() == 1 && entries.get(0) instanceof JdkOrderEntry) {
            return new JdkBundleManifest();
        }
        Module module = myIndex.getModuleForFile(file);
        if (module != null) {
            return getManifest(module);
        }
        VirtualFile libRoot = myIndex.getClassRootForFile(file);
        if (libRoot != null) {
            return getManifest(libRoot);
        }
    }
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) JdkOrderEntry(com.intellij.openapi.roots.JdkOrderEntry) OrderEntry(com.intellij.openapi.roots.OrderEntry) JdkOrderEntry(com.intellij.openapi.roots.JdkOrderEntry) Module(com.intellij.openapi.module.Module) Nullable(org.jetbrains.annotations.Nullable)

Example 49 with OrderEntry

use of com.intellij.openapi.roots.OrderEntry in project google-cloud-intellij by GoogleCloudPlatform.

the class AppEngineForbiddenCodeInspection method checkFile.

@Override
public ProblemDescriptor[] checkFile(@NotNull PsiFile file, @NotNull final InspectionManager manager, final boolean isOnTheFly) {
    final Project project = manager.getProject();
    Module module = ModuleUtilCore.findModuleForPsiElement(file);
    final AppEngineStandardFacet appEngineStandardFacet = AppEngineStandardFacet.getAppEngineFacetByModule(module);
    if (appEngineStandardFacet == null) {
        return null;
    }
    if (appEngineStandardFacet.getRuntimeJavaVersion().atLeast(JavaVersion.JAVA_1_8)) {
        return null;
    }
    final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();
    final List<ProblemDescriptor> problems = new ArrayList<ProblemDescriptor>();
    file.accept(new JavaRecursiveElementWalkingVisitor() {

        CloudSdkInternals sdkInternals = CloudSdkInternals.getInstance();

        @Override
        public void visitDocComment(PsiDocComment comment) {
        }

        @Override
        public void visitMethod(PsiMethod method) {
            final PsiModifierList modifierList = method.getModifierList();
            if (modifierList.hasModifierProperty(PsiModifier.NATIVE)) {
                if (!isNativeMethodAllowed(method)) {
                    problems.add(manager.createProblemDescriptor(modifierList, "Native methods aren't allowed in App Engine application", isOnTheFly, LocalQuickFix.EMPTY_ARRAY, ProblemHighlightType.GENERIC_ERROR_OR_WARNING));
                }
            }
            super.visitMethod(method);
        }

        @Override
        public void visitNewExpression(PsiNewExpression expression) {
            final PsiJavaCodeReferenceElement classReference = expression.getClassReference();
            if (classReference != null) {
                final PsiElement resolved = classReference.resolve();
                if (resolved instanceof PsiClass) {
                    final String qualifiedName = ((PsiClass) resolved).getQualifiedName();
                    if (qualifiedName != null && sdkInternals.isMethodInBlacklist(qualifiedName, "new")) {
                        final String message = "App Engine application should not create new instances of '" + qualifiedName + "' class";
                        problems.add(manager.createProblemDescriptor(classReference, message, isOnTheFly, LocalQuickFix.EMPTY_ARRAY, ProblemHighlightType.GENERIC_ERROR_OR_WARNING));
                    }
                }
            }
            super.visitNewExpression(expression);
        }

        @Override
        public void visitMethodCallExpression(PsiMethodCallExpression expression) {
            final PsiReferenceExpression methodExpression = expression.getMethodExpression();
            final PsiElement element = methodExpression.resolve();
            if (element instanceof PsiMethod) {
                final PsiMethod method = (PsiMethod) element;
                final PsiClass psiClass = method.getContainingClass();
                if (psiClass != null) {
                    final String qualifiedName = psiClass.getQualifiedName();
                    final String methodName = method.getName();
                    if (qualifiedName != null && sdkInternals.isMethodInBlacklist(qualifiedName, methodName)) {
                        final String message = "AppEngine application should not call '" + StringUtil.getShortName(qualifiedName) + "" + methodName + "' method";
                        problems.add(manager.createProblemDescriptor(methodExpression, message, isOnTheFly, LocalQuickFix.EMPTY_ARRAY, ProblemHighlightType.GENERIC_ERROR_OR_WARNING));
                    }
                }
            }
            super.visitMethodCallExpression(expression);
        }

        @Override
        public void visitReferenceElement(PsiJavaCodeReferenceElement reference) {
            final PsiElement resolved = reference.resolve();
            if (resolved instanceof PsiClass) {
                final PsiFile psiFile = resolved.getContainingFile();
                if (psiFile != null) {
                    final VirtualFile virtualFile = psiFile.getVirtualFile();
                    if (virtualFile != null && !fileIndex.isInSource(virtualFile)) {
                        final List<OrderEntry> list = fileIndex.getOrderEntriesForFile(virtualFile);
                        for (OrderEntry entry : list) {
                            if (entry instanceof JdkOrderEntry) {
                                final String className = ClassUtil.getJVMClassName((PsiClass) resolved);
                                if (className != null && !sdkInternals.isClassInWhiteList(className)) {
                                    problems.add(manager.createProblemDescriptor(reference, "Class '" + className + "' is not included in App Engine JRE White List", isOnTheFly, LocalQuickFix.EMPTY_ARRAY, ProblemHighlightType.GENERIC_ERROR_OR_WARNING));
                                }
                            }
                        }
                    }
                }
            }
            super.visitReferenceElement(reference);
        }
    });
    return problems.toArray(new ProblemDescriptor[problems.size()]);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiMethod(com.intellij.psi.PsiMethod) ProblemDescriptor(com.intellij.codeInspection.ProblemDescriptor) PsiJavaCodeReferenceElement(com.intellij.psi.PsiJavaCodeReferenceElement) PsiReferenceExpression(com.intellij.psi.PsiReferenceExpression) ArrayList(java.util.ArrayList) CloudSdkInternals(com.google.cloud.tools.intellij.appengine.sdk.CloudSdkInternals) JdkOrderEntry(com.intellij.openapi.roots.JdkOrderEntry) OrderEntry(com.intellij.openapi.roots.OrderEntry) PsiFile(com.intellij.psi.PsiFile) ArrayList(java.util.ArrayList) PsiModifierList(com.intellij.psi.PsiModifierList) List(java.util.List) PsiElement(com.intellij.psi.PsiElement) JavaRecursiveElementWalkingVisitor(com.intellij.psi.JavaRecursiveElementWalkingVisitor) PsiDocComment(com.intellij.psi.javadoc.PsiDocComment) JdkOrderEntry(com.intellij.openapi.roots.JdkOrderEntry) PsiClass(com.intellij.psi.PsiClass) PsiNewExpression(com.intellij.psi.PsiNewExpression) PsiModifierList(com.intellij.psi.PsiModifierList) PsiMethodCallExpression(com.intellij.psi.PsiMethodCallExpression) Project(com.intellij.openapi.project.Project) ProjectFileIndex(com.intellij.openapi.roots.ProjectFileIndex) AppEngineStandardFacet(com.google.cloud.tools.intellij.appengine.facet.standard.AppEngineStandardFacet) Module(com.intellij.openapi.module.Module)

Example 50 with OrderEntry

use of com.intellij.openapi.roots.OrderEntry in project google-cloud-intellij by GoogleCloudPlatform.

the class AppEngineStandardSupportProvider method removeMavenLibraries.

static void removeMavenLibraries(final Set<AppEngineStandardMavenLibrary> librariesToRemove, final Module module) {
    final ModuleRootManager manager = ModuleRootManager.getInstance(module);
    final ModifiableRootModel model = manager.getModifiableModel();
    final LibraryTable libraryTable = ProjectLibraryTable.getInstance(module.getProject());
    ApplicationManager.getApplication().invokeLater(() -> {
        new WriteAction() {

            @Override
            protected void run(@NotNull Result result) throws Throwable {
                for (AppEngineStandardMavenLibrary libraryToRemove : librariesToRemove) {
                    final String displayName = libraryToRemove.toMavenDisplayVersion();
                    final Library library = libraryTable.getLibraryByName(displayName);
                    if (library != null) {
                        libraryTable.removeLibrary(library);
                        for (OrderEntry orderEntry : model.getOrderEntries()) {
                            if (orderEntry.getPresentableName().equals(library.getName())) {
                                model.removeOrderEntry(orderEntry);
                            }
                        }
                    }
                }
                model.commit();
            }
        }.execute();
    }, ModalityState.NON_MODAL);
}
Also used : ModifiableRootModel(com.intellij.openapi.roots.ModifiableRootModel) ProjectLibraryTable(com.intellij.openapi.roots.impl.libraries.ProjectLibraryTable) LibraryTable(com.intellij.openapi.roots.libraries.LibraryTable) OrderEntry(com.intellij.openapi.roots.OrderEntry) WriteAction(com.intellij.openapi.application.WriteAction) ModuleRootManager(com.intellij.openapi.roots.ModuleRootManager) Library(com.intellij.openapi.roots.libraries.Library) Result(com.intellij.openapi.application.Result)

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