@NotNull List<OrderEntry> findAllOrderEntriesWithOwnerModule(@NotNull Module ownerModule) { OrderEntry[] entries = orderEntries; if (entries == null) { return Collections.emptyList(); } if (entries.length == 1) { OrderEntry entry = entries[0]; return entry.getOwnerModule() == ownerModule ? Arrays.asList(entries) : Collections.<OrderEntry>emptyList(); } int index = Arrays.binarySearch(entries, createFakeOrderEntry(ownerModule), BY_OWNER_MODULE); if (index < 0) { return Collections.emptyList(); } int firstIndex = index; while (firstIndex - 1 >= 0 && entries[firstIndex - 1].getOwnerModule() == ownerModule) { firstIndex--; } int lastIndex = index + 1; while (lastIndex < entries.length && entries[lastIndex].getOwnerModule() == ownerModule) { lastIndex++; } OrderEntry[] subArray = new OrderEntry[lastIndex - firstIndex]; System.arraycopy(entries, firstIndex, subArray, 0, lastIndex - firstIndex); return Arrays.asList(subArray); }
@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 void appendChosenAnnotationsRoot( @NotNull final OrderEntry entry, @NotNull final VirtualFile vFile) { if (entry instanceof LibraryOrderEntry) { Library library = ((LibraryOrderEntry) entry).getLibrary(); LOG.assertTrue(library != null); final ModifiableRootModel rootModel = ModuleRootManager.getInstance(entry.getOwnerModule()).getModifiableModel(); final Library.ModifiableModel model = library.getModifiableModel(); model.addRoot(vFile, AnnotationOrderRootType.getInstance()); model.commit(); rootModel.commit(); } else if (entry instanceof ModuleSourceOrderEntry) { final ModifiableRootModel model = ModuleRootManager.getInstance(entry.getOwnerModule()).getModifiableModel(); final JavaModuleExternalPaths extension = model.getModuleExtension(JavaModuleExternalPaths.class); extension.setExternalAnnotationUrls( ArrayUtil.mergeArrays(extension.getExternalAnnotationsUrls(), vFile.getUrl())); model.commit(); } else if (entry instanceof JdkOrderEntry) { final SdkModificator sdkModificator = ((JdkOrderEntry) entry).getJdk().getSdkModificator(); sdkModificator.addRoot(vFile, AnnotationOrderRootType.getInstance()); sdkModificator.commitChanges(); } myExternalAnnotations.clear(); }
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; }
@Override @Nullable protected ClasspathTableItem<?> createTableItem(final Library item) { // clear invalid order entry corresponding to added library if any final ModifiableRootModel rootModel = myClasspathPanel.getRootModel(); final OrderEntry[] orderEntries = rootModel.getOrderEntries(); for (OrderEntry orderEntry : orderEntries) { if (orderEntry instanceof LibraryOrderEntry) { final LibraryOrderEntry libraryOrderEntry = (LibraryOrderEntry) orderEntry; if (item.equals(libraryOrderEntry.getLibrary())) { return ClasspathTableItem.createItem(libraryOrderEntry, myContext); } String name = item.getName(); if (name != null && name.equals(libraryOrderEntry.getLibraryName())) { if (orderEntry.isValid()) { Messages.showErrorDialog( ProjectBundle.message("classpath.message.library.already.added", item.getName()), ProjectBundle.message("classpath.title.adding.dependency")); return null; } else { rootModel.removeOrderEntry(orderEntry); } } } } final LibraryOrderEntry orderEntry = rootModel.addLibraryEntry(item); DependencyScope defaultScope = getDefaultScope(item); if (defaultScope != null) { orderEntry.setScope(defaultScope); } return ClasspathTableItem.createItem(orderEntry, myContext); }
@Nullable @Override public OrderEntry findIdeModuleOrderEntry(@NotNull DependencyData data) { Module ownerIdeModule = findIdeModule(data.getOwnerModule()); if (ownerIdeModule == null) return null; LibraryDependencyData libraryDependencyData = null; ModuleDependencyData moduleDependencyData = null; if (data instanceof LibraryDependencyData) { libraryDependencyData = (LibraryDependencyData) data; } else if (data instanceof ModuleDependencyData) { moduleDependencyData = (ModuleDependencyData) data; } else { return null; } for (OrderEntry entry : getOrderEntries(ownerIdeModule)) { if (entry instanceof LibraryOrderEntry && libraryDependencyData != null) { if (((LibraryOrderEntry) entry).isModuleLevel() && libraryDependencyData.getLevel() != LibraryLevel.MODULE) continue; if (StringUtil.isEmpty(((LibraryOrderEntry) entry).getLibraryName())) { final Set<String> paths = ContainerUtil.map2Set( libraryDependencyData.getTarget().getPaths(LibraryPathType.BINARY), new Function<String, String>() { @Override public String fun(String path) { return PathUtil.getLocalPath(path); } }); final Set<String> entryPaths = ContainerUtil.map2Set( entry.getUrls(OrderRootType.CLASSES), new Function<String, String>() { @Override public String fun(String s) { return PathUtil.getLocalPath(VfsUtilCore.urlToPath(s)); } }); if (entryPaths.equals(paths) && ((LibraryOrderEntry) entry).getScope() == data.getScope()) return entry; continue; } } String entryName = libraryDependencyData != null ? libraryDependencyData.getInternalName() : moduleDependencyData.getInternalName(); if (entryName.equals(entry.getPresentableName()) && (!(entry instanceof ExportableOrderEntry) || ((ExportableOrderEntry) entry).getScope() == data.getScope())) { return entry; } } return null; }
@NotNull private Map<VirtualFile, OrderEntry[]> getOrderEntries() { Map<VirtualFile, OrderEntry[]> result = myOrderEntries; if (result != null) return result; MultiMap<VirtualFile, OrderEntry> libClassRootEntries = MultiMap.createSmart(); MultiMap<VirtualFile, OrderEntry> libSourceRootEntries = MultiMap.createSmart(); MultiMap<VirtualFile, OrderEntry> depEntries = MultiMap.createSmart(); for (final Module module : ModuleManager.getInstance(myProject).getModules()) { final ModuleRootManager moduleRootManager = ModuleRootManager.getInstance(module); for (OrderEntry orderEntry : moduleRootManager.getOrderEntries()) { if (orderEntry instanceof ModuleOrderEntry) { final Module depModule = ((ModuleOrderEntry) orderEntry).getModule(); if (depModule != null) { VirtualFile[] importedClassRoots = OrderEnumerator.orderEntries(depModule) .exportedOnly() .recursively() .classes() .usingCache() .getRoots(); for (VirtualFile importedClassRoot : importedClassRoots) { depEntries.putValue(importedClassRoot, orderEntry); } } for (VirtualFile sourceRoot : orderEntry.getFiles(OrderRootType.SOURCES)) { depEntries.putValue(sourceRoot, orderEntry); } } else if (orderEntry instanceof LibraryOrSdkOrderEntry) { final LibraryOrSdkOrderEntry entry = (LibraryOrSdkOrderEntry) orderEntry; for (final VirtualFile sourceRoot : entry.getRootFiles(OrderRootType.SOURCES)) { libSourceRootEntries.putValue(sourceRoot, orderEntry); } for (final VirtualFile classRoot : entry.getRootFiles(OrderRootType.CLASSES)) { libClassRootEntries.putValue(classRoot, orderEntry); } } } } RootInfo rootInfo = buildRootInfo(myProject); result = ContainerUtil.newHashMap(); Set<VirtualFile> allRoots = rootInfo.getAllRoots(); for (VirtualFile file : allRoots) { List<VirtualFile> hierarchy = getHierarchy(file, allRoots, rootInfo); result.put( file, hierarchy == null ? OrderEntry.EMPTY_ARRAY : calcOrderEntries( rootInfo, depEntries, libClassRootEntries, libSourceRootEntries, hierarchy)); } myOrderEntries = result; return result; }
private List<String> collectModuleDepsNames(String moduleName, Class clazz) { List<String> actual = new ArrayList<String>(); for (OrderEntry e : getRootManager(moduleName).getOrderEntries()) { if (clazz.isInstance(e)) { actual.add(e.getPresentableName()); } } return actual; }
public void selectOrderEntry(@NotNull OrderEntry entry) { for (int row = 0; row < myModel.getRowCount(); row++) { final OrderEntry orderEntry = getItemAt(row).getEntry(); if (orderEntry != null && entry.getPresentableName().equals(orderEntry.getPresentableName())) { myEntryTable.getSelectionModel().setSelectionInterval(row, row); TableUtil.scrollSelectionToVisible(myEntryTable); } } IdeFocusManager.getInstance(myState.getProject()).requestFocus(myEntryTable, true); }
private static int rearrangeOrderEntryOfType( ModifiableRootModel rootModel, Class<? extends OrderEntry> orderEntryClass) { OrderEntry[] orderEntries = rootModel.getOrderEntries(); int moduleSourcesIdx = 0; for (OrderEntry orderEntry : orderEntries) { if (orderEntryClass.isAssignableFrom(orderEntry.getClass())) { break; } moduleSourcesIdx++; } orderEntries = ArrayUtil.append(orderEntries, orderEntries[moduleSourcesIdx]); orderEntries = ArrayUtil.remove(orderEntries, moduleSourcesIdx); rootModel.rearrangeOrderEntries(orderEntries); return orderEntries.length - 1; }
@Nullable OrderEntry findOrderEntryWithOwnerModule(@NotNull Module ownerModule) { OrderEntry[] entries = orderEntries; if (entries == null) { return null; } if (entries.length < 10) { for (OrderEntry entry : entries) { if (entry.getOwnerModule() == ownerModule) return entry; } return null; } int index = Arrays.binarySearch(entries, createFakeOrderEntry(ownerModule), BY_OWNER_MODULE); return index < 0 ? null : entries[index]; }
public Collection<OrderEntry> getOrderEntries(@NotNull Module module) { if (orderEntries == null) { return Collections.emptyList(); } MultiMap<Module, OrderEntry> tmp = moduleOrderEntries; if (tmp == null) { tmp = new MultiMap<Module, OrderEntry>(); for (final OrderEntry orderEntry : orderEntries) { tmp.putValue(orderEntry.getOwnerModule(), orderEntry); } moduleOrderEntries = tmp; } return tmp.get(module); }
private <T> T getModuleDep(String moduleName, String depName, Class<T> clazz) { T dep = null; for (OrderEntry e : getRootManager(moduleName).getOrderEntries()) { if (clazz.isInstance(e) && e.getPresentableName().equals(depName)) { dep = (T) e; } } assertNotNull( "Dependency not found: " + depName + "\namong: " + collectModuleDepsNames(moduleName, clazz), dep); return dep; }
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); } } } }
@Nullable private static VirtualFile getVcsRootForLibraryFile( @NotNull Project project, @NotNull VirtualFile file) { ProjectLevelVcsManager vcsManager = ProjectLevelVcsManager.getInstance(project); // for a file inside .jar/.zip consider the .jar/.zip file itself VirtualFile root = vcsManager.getVcsRootFor(VfsUtilCore.getVirtualFileForJar(file)); if (root != null) { LOGGER.debug("Found root for zip/jar file: " + root); return root; } // for other libs which don't have jars inside the project dir (such as JDK) take the owner // module of the lib List<OrderEntry> entries = ProjectRootManager.getInstance(project).getFileIndex().getOrderEntriesForFile(file); Set<VirtualFile> libraryRoots = new HashSet<VirtualFile>(); for (OrderEntry entry : entries) { if (entry instanceof LibraryOrderEntry || entry instanceof JdkOrderEntry) { VirtualFile moduleRoot = vcsManager.getVcsRootFor(entry.getOwnerModule().getModuleFile()); if (moduleRoot != null) { libraryRoots.add(moduleRoot); } } } if (libraryRoots.size() == 0) { LOGGER.debug("No library roots"); return null; } // if the lib is used in several modules, take the top module // (for modules of the same level we can't guess anything => take the first one) Iterator<VirtualFile> libIterator = libraryRoots.iterator(); VirtualFile topLibraryRoot = libIterator.next(); while (libIterator.hasNext()) { VirtualFile libRoot = libIterator.next(); if (VfsUtilCore.isAncestor(libRoot, topLibraryRoot, true)) { topLibraryRoot = libRoot; } } LOGGER.debug("Several library roots, returning " + topLibraryRoot); return topLibraryRoot; }
/** * Allows to answer if target library dependency is still available at the target project. * * @param id target library id * @return <code>true</code> if target library dependency is still available at the target * project; <code>false</code> otherwise */ public boolean isIntellijLibraryDependencyExist(@NotNull final GradleLibraryDependencyId id) { final Module module = findIntellijModuleByName(id.getModuleName()); if (module == null) { return false; } RootPolicy<Boolean> visitor = new RootPolicy<Boolean>() { @Override public Boolean visitLibraryOrderEntry( LibraryOrderEntry libraryOrderEntry, Boolean value) { return id.getLibraryName().equals(libraryOrderEntry.getLibraryName()); } }; for (OrderEntry entry : myFacade.getOrderEntries(module)) { if (entry.accept(visitor, false)) { return true; } } return false; }
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); } } } }
// entries must be sorted BY_OWNER_MODULE @NotNull private OrderEntry[] mergeWith(@NotNull OrderEntry[] entries) { OrderEntry[] orderEntries = this.orderEntries; OrderEntry[] result = new OrderEntry[orderEntries.length + entries.length]; int i = 0; int j = 0; // remove equals entries in the process int o = 0; while (i != orderEntries.length || j != entries.length) { OrderEntry m = i != orderEntries.length && (j == entries.length || BY_OWNER_MODULE.compare(orderEntries[i], entries[j]) < 0) ? orderEntries[i++] : entries[j++]; if (o == 0 || !m.equals(result[o - 1])) { result[o++] = m; } } if (o != result.length) { result = ArrayUtil.realloc(result, o, ORDER_ENTRY_ARRAY_FACTORY); } return result; }
private void setupRootAndAnnotateExternally( @NotNull final OrderEntry entry, @NotNull final Project project, @NotNull final PsiModifierListOwner listOwner, @NotNull final String annotationFQName, @NotNull final PsiFile fromFile, @NotNull final String packageName, @Nullable final PsiNameValuePair[] value) { final FileChooserDescriptor descriptor = FileChooserDescriptorFactory.createSingleFolderDescriptor(); descriptor.setTitle( ProjectBundle.message( "external.annotations.root.chooser.title", entry.getPresentableName())); descriptor.setDescription( ProjectBundle.message("external.annotations.root.chooser.description")); final VirtualFile newRoot = FileChooser.chooseFile(descriptor, project, null); if (newRoot == null) { notifyAfterAnnotationChanging(listOwner, annotationFQName, false); return; } new WriteCommandAction(project) { @Override protected void run(final Result result) throws Throwable { appendChosenAnnotationsRoot(entry, newRoot); XmlFile xmlFileInRoot = findXmlFileInRoot(findExternalAnnotationsXmlFiles(listOwner), newRoot); if (xmlFileInRoot != null) { // file already exists under appeared content root if (!CodeInsightUtilBase.preparePsiElementForWrite(xmlFileInRoot)) { notifyAfterAnnotationChanging(listOwner, annotationFQName, false); return; } annotateExternally(listOwner, annotationFQName, xmlFileInRoot, fromFile, value); } else { final XmlFile annotationsXml = createAnnotationsXml(newRoot, packageName); if (annotationsXml != null) { final List<PsiFile> createdFiles = new ArrayList<PsiFile>(); createdFiles.add(annotationsXml); String fqn = getFQN(packageName, fromFile); if (fqn != null) { myExternalAnnotations.put(fqn, createdFiles); } } annotateExternally(listOwner, annotationFQName, annotationsXml, fromFile, value); } } }.execute(); }
public boolean contains(@Nullable final OrderEntry e) { if (e == null) return false; return e.accept(CONTAINS, false); }
public void addDependency(@NotNull final OrderEntry e) { if (!DependenciesFilter.REMOVABLE_DEPENDENCY.apply(e)) return; e.accept(ADD, null); }
@Override public int compare(OrderEntry o1, OrderEntry o2) { String name1 = o1.getOwnerModule().getName(); String name2 = o2.getOwnerModule().getName(); return name1.compareTo(name2); }