public int compare(@NotNull VirtualFile file1, @NotNull VirtualFile file2) {
    List<OrderEntry> entries1 = myProjectFileIndex.getOrderEntriesForFile(file1);
    List<OrderEntry> entries2 = myProjectFileIndex.getOrderEntriesForFile(file2);
    if (entries1.size() != entries2.size()) return 0;

    int res = 0;
    for (OrderEntry entry1 : entries1) {
      Module module = entry1.getOwnerModule();
      ModuleFileIndex moduleFileIndex = ModuleRootManager.getInstance(module).getFileIndex();
      OrderEntry entry2 = moduleFileIndex.getOrderEntryForFile(file2);
      if (entry2 == null) {
        return 0;
      } else {
        int aRes = entry2.compareTo(entry1);
        if (aRes == 0) return 0;
        if (res == 0) {
          res = aRes;
        } else if (res != aRes) {
          return 0;
        }
      }
    }

    return res;
  }
  @Nullable
  @Override
  public PsiPackage resolvePackage(
      @NotNull PsiPackageManager packageManager,
      @NotNull VirtualFile virtualFile,
      @NotNull Class<? extends ModuleExtension> extensionClass,
      String qualifiedName) {
    ProjectFileIndex fileIndexFacade =
        ProjectFileIndex.SERVICE.getInstance(packageManager.getProject());
    PsiManager psiManager = PsiManager.getInstance(packageManager.getProject());
    if (fileIndexFacade.isInLibraryClasses(virtualFile)) {

      List<OrderEntry> orderEntriesForFile = fileIndexFacade.getOrderEntriesForFile(virtualFile);
      for (OrderEntry orderEntry : orderEntriesForFile) {
        Module ownerModule = orderEntry.getOwnerModule();
        ModuleExtension extension = ModuleUtilCore.getExtension(ownerModule, extensionClass);
        if (extension != null) {
          for (PsiPackageSupportProvider p : PsiPackageSupportProvider.EP_NAME.getExtensions()) {
            if (p.isSupported(extension)) {
              return p.createPackage(psiManager, packageManager, extensionClass, qualifiedName);
            }
          }
        }
      }
    }
    return null;
  }
 private static void addSourceDirectoriesFromLibraries(
     @NotNull Project project,
     @NotNull VirtualFile directory,
     @NotNull Collection<VirtualFile> outSourceRoots) {
   ProjectFileIndex index = ProjectFileIndex.SERVICE.getInstance(project);
   VirtualFile classRoot = index.getClassRootForFile(directory);
   if (classRoot == null) return;
   String relativePath = VfsUtilCore.getRelativePath(directory, classRoot);
   if (relativePath == null) return;
   for (OrderEntry orderEntry : index.getOrderEntriesForFile(directory)) {
     for (VirtualFile sourceRoot : orderEntry.getFiles(OrderRootType.SOURCES)) {
       VirtualFile sourceFile = sourceRoot.findFileByRelativePath(relativePath);
       if (sourceFile != null) {
         outSourceRoots.add(sourceFile);
       }
     }
   }
 }
  @Override
  public OrderEntry getLibraryEntry() {
    if (!isValid()) return null;
    PsiFile psiFile = getPsiFile();
    VirtualFile virtualFile = getFile();
    if (virtualFile == null) return null;

    ProjectRootManager projectRootManager = ProjectRootManager.getInstance(getProject());
    ProjectFileIndex fileIndex = projectRootManager.getFileIndex();

    if (psiFile instanceof PsiCompiledElement || fileIndex.isInLibrarySource(virtualFile)) {
      List<OrderEntry> orders = fileIndex.getOrderEntriesForFile(virtualFile);
      for (OrderEntry order : orders) {
        if (order instanceof LibraryOrderEntry || order instanceof JdkOrderEntry) {
          return order;
        }
      }
    }

    return null;
  }
Пример #5
0
  @Nullable
  public static PsiElement getOriginalElement(PsiClass clazz, PsiFile containingFile) {
    VirtualFile vFile = containingFile.getVirtualFile();
    final JavaPsiFacade facade = JavaPsiFacade.getInstance(clazz.getProject());
    final ProjectFileIndex idx = ProjectRootManager.getInstance(facade.getProject()).getFileIndex();

    if (vFile == null || !idx.isInLibrarySource(vFile)) return clazz;
    final String qName = clazz.getQualifiedName();
    if (qName == null) return null;
    final List<OrderEntry> orderEntries = idx.getOrderEntriesForFile(vFile);
    PsiClass original =
        facade.findClass(
            qName,
            new GlobalSearchScope(facade.getProject()) {
              public int compare(VirtualFile file1, VirtualFile file2) {
                return 0;
              }

              public boolean contains(VirtualFile file) {
                // order for file and vFile has non empty intersection.
                List<OrderEntry> entries = idx.getOrderEntriesForFile(file);
                //noinspection ForLoopReplaceableByForEach
                for (int i = 0; i < entries.size(); i++) {
                  final OrderEntry entry = entries.get(i);
                  if (orderEntries.contains(entry)) return true;
                }
                return false;
              }

              public boolean isSearchInModuleContent(@NotNull Module aModule) {
                return false;
              }

              public boolean isSearchInLibraries() {
                return true;
              }
            });

    return original != null ? original : clazz;
  }
  @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;
  }
Пример #7
0
  public static void customizeElementLabel(final PsiElement element, final JLabel label) {
    if (element != null) {
      PsiFile file = element.getContainingFile();
      VirtualFile vfile = file == null ? null : file.getVirtualFile();

      if (vfile == null) {
        label.setText("");
        label.setIcon(null);

        return;
      }

      final ProjectFileIndex fileIndex =
          ProjectRootManager.getInstance(element.getProject()).getFileIndex();
      final Module module = fileIndex.getModuleForFile(vfile);

      if (module != null) {
        label.setText(module.getName());
        label.setIcon(AllIcons.Nodes.Module);
      } else {
        final List<OrderEntry> entries = fileIndex.getOrderEntriesForFile(vfile);

        OrderEntry entry = null;

        for (OrderEntry order : entries) {
          if (order instanceof LibraryOrderEntry || order instanceof SdkOrderEntry) {
            entry = order;
            break;
          }
        }

        if (entry != null) {
          label.setText(entry.getPresentableName());
          label.setIcon(AllIcons.Nodes.PpLibFolder);
        }
      }
    }
  }
Пример #8
0
  @Nullable
  public static List<LocalQuickFix> registerFixes(
      @NotNull final QuickFixActionRegistrar registrar, @NotNull final PsiReference reference) {
    final PsiElement psiElement = reference.getElement();
    @NonNls
    final String shortReferenceName = reference.getRangeInElement().substring(psiElement.getText());

    Project project = psiElement.getProject();
    PsiFile containingFile = psiElement.getContainingFile();
    if (containingFile == null) return null;

    final VirtualFile classVFile = containingFile.getVirtualFile();
    if (classVFile == null) return null;

    final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();
    final Module currentModule = fileIndex.getModuleForFile(classVFile);
    if (currentModule == null) return null;

    final List<LocalQuickFix> providedFixes =
        findFixes(
            new Function<MissingDependencyFixProvider, List<LocalQuickFix>>() {
              @Override
              public List<LocalQuickFix> fun(MissingDependencyFixProvider provider) {
                return provider.registerFixes(registrar, reference);
              }
            });
    if (providedFixes != null) {
      return providedFixes;
    }

    List<LocalQuickFix> result = new ArrayList<LocalQuickFix>();
    JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project);
    String fullReferenceText = reference.getCanonicalText();
    for (ExternalLibraryResolver resolver : ExternalLibraryResolver.EP_NAME.getExtensions()) {
      final ExternalClassResolveResult resolveResult =
          resolver.resolveClass(shortReferenceName, isReferenceToAnnotation(psiElement));
      OrderEntryFix fix = null;
      if (resolveResult != null
          && psiFacade.findClass(
                  resolveResult.getQualifiedClassName(),
                  currentModule.getModuleWithDependenciesAndLibrariesScope(true))
              == null) {
        fix =
            new AddExternalLibraryToDependenciesQuickFix(
                currentModule,
                resolveResult.getLibrary(),
                reference,
                resolveResult.getQualifiedClassName());
      } else if (!fullReferenceText.equals(shortReferenceName)) {
        ExternalLibraryDescriptor descriptor = resolver.resolvePackage(fullReferenceText);
        if (descriptor != null) {
          fix =
              new AddExternalLibraryToDependenciesQuickFix(
                  currentModule, descriptor, reference, null);
        }
      }
      if (fix != null) {
        registrar.register(fix);
        result.add(fix);
      }
    }
    if (!result.isEmpty()) {
      return result;
    }

    Set<Object> librariesToAdd = new THashSet<Object>();
    final JavaPsiFacade facade = JavaPsiFacade.getInstance(psiElement.getProject());
    PsiClass[] classes =
        PsiShortNamesCache.getInstance(project)
            .getClassesByName(shortReferenceName, GlobalSearchScope.allScope(project));
    List<PsiClass> allowedDependencies = filterAllowedDependencies(psiElement, classes);
    if (allowedDependencies.isEmpty()) {
      return result;
    }
    classes = allowedDependencies.toArray(new PsiClass[allowedDependencies.size()]);
    OrderEntryFix moduleDependencyFix =
        new AddModuleDependencyFix(currentModule, classVFile, classes, reference);

    final PsiClass[] finalClasses = classes;
    final OrderEntryFix finalModuleDependencyFix = moduleDependencyFix;
    final OrderEntryFix providedModuleDependencyFix =
        provideFix(
            new Function<MissingDependencyFixProvider, OrderEntryFix>() {
              @Override
              public OrderEntryFix fun(MissingDependencyFixProvider provider) {
                return provider.getAddModuleDependencyFix(
                    reference, finalModuleDependencyFix, currentModule, classVFile, finalClasses);
              }
            });
    moduleDependencyFix = ObjectUtils.notNull(providedModuleDependencyFix, moduleDependencyFix);

    registrar.register(moduleDependencyFix);
    result.add(moduleDependencyFix);
    for (final PsiClass aClass : classes) {
      if (!facade.getResolveHelper().isAccessible(aClass, psiElement, aClass)) continue;
      PsiFile psiFile = aClass.getContainingFile();
      if (psiFile == null) continue;
      VirtualFile virtualFile = psiFile.getVirtualFile();
      if (virtualFile == null) continue;
      ModuleFileIndex moduleFileIndex = ModuleRootManager.getInstance(currentModule).getFileIndex();
      for (OrderEntry orderEntry : fileIndex.getOrderEntriesForFile(virtualFile)) {
        if (orderEntry instanceof LibraryOrderEntry) {
          final LibraryOrderEntry libraryEntry = (LibraryOrderEntry) orderEntry;
          final Library library = libraryEntry.getLibrary();
          if (library == null) continue;
          VirtualFile[] files = library.getFiles(OrderRootType.CLASSES);
          if (files.length == 0) continue;
          final VirtualFile jar = files[0];

          if (jar == null
              || libraryEntry.isModuleLevel() && !librariesToAdd.add(jar)
              || !librariesToAdd.add(library)) continue;
          OrderEntry entryForFile = moduleFileIndex.getOrderEntryForFile(virtualFile);
          if (entryForFile != null
              && !(entryForFile instanceof ExportableOrderEntry
                  && ((ExportableOrderEntry) entryForFile).getScope() == DependencyScope.TEST
                  && !ModuleRootManager.getInstance(currentModule)
                      .getFileIndex()
                      .isInTestSourceContent(classVFile))) {
            continue;
          }
          final OrderEntryFix platformFix =
              new OrderEntryFix() {
                @Override
                @NotNull
                public String getText() {
                  return QuickFixBundle.message(
                      "orderEntry.fix.add.library.to.classpath", libraryEntry.getPresentableName());
                }

                @Override
                @NotNull
                public String getFamilyName() {
                  return QuickFixBundle.message("orderEntry.fix.family.add.library.to.classpath");
                }

                @Override
                public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
                  return !project.isDisposed()
                      && !currentModule.isDisposed()
                      && libraryEntry.isValid();
                }

                @Override
                public void invoke(
                    @NotNull final Project project, @Nullable final Editor editor, PsiFile file) {
                  OrderEntryUtil.addLibraryToRoots(libraryEntry, currentModule);
                  if (editor != null) {
                    DumbService.getInstance(project)
                        .withAlternativeResolveEnabled(
                            new Runnable() {
                              @Override
                              public void run() {
                                new AddImportAction(project, reference, editor, aClass).execute();
                              }
                            });
                  }
                }
              };

          final OrderEntryFix providedFix =
              provideFix(
                  new Function<MissingDependencyFixProvider, OrderEntryFix>() {
                    @Override
                    public OrderEntryFix fun(MissingDependencyFixProvider provider) {
                      return provider.getAddLibraryToClasspathFix(
                          reference, platformFix, currentModule, libraryEntry, aClass);
                    }
                  });
          final OrderEntryFix fix = ObjectUtils.notNull(providedFix, platformFix);
          registrar.register(fix);
          result.add(fix);
        }
      }
    }
    return result;
  }