Java Code Examples for com.intellij.openapi.roots.libraries.Library#getName()

The following examples show how to use com.intellij.openapi.roots.libraries.Library#getName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OrderEntryUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
public static boolean equals(Library library1, Library library2) {
  if (library1 == library2) return true;
  if (library1 == null || library2 == null) return false;

  final LibraryTable table = library1.getTable();
  if (table != null) {
    if (library2.getTable() != table) return false;
    final String name = library1.getName();
    return name != null && name.equals(library2.getName());
  }

  if (library2.getTable() != null) return false;

  for (OrderRootType type : OrderRootType.getAllTypes()) {
    if (!Comparing.equal(library1.getUrls(type), library2.getUrls(type))) {
      return false;
    }
  }
  return true;
}
 
Example 2
Source File: LibraryClasspathTableItem.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public String getTooltipText() {
  final Library library = myEntry.getLibrary();
  if (library == null) return null;

  final String name = library.getName();
  if (name != null) {
    final List<String> invalidUrls = ((LibraryEx)library).getInvalidRootUrls(BinariesOrderRootType.getInstance());
    if (!invalidUrls.isEmpty()) {
      return ProjectBundle.message("project.roots.tooltip.library.has.broken.paths", name, invalidUrls.size());
    }
  }

  final List<String> descriptions = LibraryPresentationManager.getInstance().getDescriptions(library, myContext);
  if (descriptions.isEmpty()) return null;

  return XmlStringUtil.wrapInHtml(StringUtil.join(descriptions, "<br>"));
}
 
Example 3
Source File: ArtifactEditorContextImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public void selectLibrary(@Nonnull Library library) {
  final LibraryTable table = library.getTable();
  if (table != null) {
    ProjectStructureConfigurable.getInstance(getProject()).selectProjectOrGlobalLibrary(library, true);
  }
  else {
    final Module module = ((LibraryImpl)library).getModule();
    if (module != null) {
      final ModuleRootModel rootModel = myParent.getModulesProvider().getRootModel(module);
      final String libraryName = library.getName();
      for (OrderEntry entry : rootModel.getOrderEntries()) {
        if (entry instanceof ModuleLibraryOrderEntryImpl) {
          final ModuleLibraryOrderEntryImpl libraryEntry = (ModuleLibraryOrderEntryImpl)entry;
          if (libraryName != null && libraryName.equals(libraryEntry.getLibraryName())
             || libraryName == null && library.equals(libraryEntry.getLibrary())) {
            ProjectStructureConfigurable.getInstance(getProject()).selectOrderEntry(module, libraryEntry);
            return;
          }
        }
      }
    }
  }
}
 
Example 4
Source File: OrderEntryAppearanceServiceImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
@Override
public CellAppearanceEx forLibrary(Project project, @Nonnull final Library library, final boolean hasInvalidRoots) {
  final StructureConfigurableContext context = ProjectStructureConfigurable.getInstance(project).getContext();
  final Image icon = LibraryPresentationManager.getInstance().getCustomIcon(library, context);

  final String name = library.getName();
  if (name != null) {
    return normalOrRedWaved(name, (icon != null ? icon :  AllIcons.Nodes.PpLib), hasInvalidRoots);
  }

  final String[] files = library.getUrls(BinariesOrderRootType.getInstance());
  if (files.length == 0) {
    return SimpleTextCellAppearance.invalid(ProjectBundle.message("library.empty.library.item"),  AllIcons.Nodes.PpLib);
  }
  else if (files.length == 1) {
    return forVirtualFilePointer(new LightFilePointer(files[0]));
  }

  final String url = StringUtil.trimEnd(files[0], URLUtil.ARCHIVE_SEPARATOR);
  return SimpleTextCellAppearance.regular(PathUtil.getFileName(url),  AllIcons.Nodes.PpLib);
}
 
Example 5
Source File: PackagingElementFactoryImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
@Override
public List<? extends PackagingElement<?>> createLibraryElements(@Nonnull Library library) {
  final LibraryTable table = library.getTable();
  final String libraryName = library.getName();
  if (table != null) {
    return Collections.singletonList(createLibraryFiles(libraryName, table.getTableLevel(), null));
  }
  if (libraryName != null) {
    final Module module = ((LibraryImpl)library).getModule();
    if (module != null) {
      return Collections.singletonList(createLibraryFiles(libraryName, LibraryTableImplUtil.MODULE_LEVEL, module.getName()));
    }
  }
  final List<PackagingElement<?>> elements = new ArrayList<>();
  for (VirtualFile file : library.getFiles(BinariesOrderRootType.getInstance())) {
    final String path = FileUtil.toSystemIndependentName(PathUtil.getLocalPath(file));
    elements.add(file.isDirectory() && file.isInLocalFileSystem() ? new DirectoryCopyPackagingElement(path) : new FileCopyPackagingElement(path));
  }
  return elements;
}
 
Example 6
Source File: BlazeScalaSyncPlugin.java    From intellij with Apache License 2.0 6 votes vote down vote up
@Override
public void updateProjectStructure(
    Project project,
    BlazeContext context,
    WorkspaceRoot workspaceRoot,
    ProjectViewSet projectViewSet,
    BlazeProjectData blazeProjectData,
    @Nullable BlazeProjectData oldBlazeProjectData,
    ModuleEditor moduleEditor,
    Module workspaceModule,
    ModifiableRootModel workspaceModifiableModel) {
  if (!blazeProjectData.getWorkspaceLanguageSettings().isLanguageActive(LanguageClass.SCALA)) {
    return;
  }
  for (Library library : ProjectLibraryTable.getInstance(project).getLibraries()) {
    // Convert the type of the SDK library to prevent the scala plugin from
    // showing the missing SDK notification.
    // TODO: use a canonical class in the SDK (e.g., scala.App) instead of the name?
    if (library.getName() != null && library.getName().startsWith("scala-library")) {
      ExistingLibraryEditor editor = new ExistingLibraryEditor(library, null);
      editor.setType(ScalaLibraryType.apply());
      editor.commit();
      return;
    }
  }
}
 
Example 7
Source File: HaxelibUtil.java    From intellij-haxe with Apache License 2.0 6 votes vote down vote up
/**
 * Get the list of libraries specified on the project.  Managed haxelib
 * (of the form "haxelib|<lib_name>") libraries are included unless
 * filterManagedLibs is true.
 *
 *
 * @param project to get the classpath for.
 * @return a (possibly empty) list of the classpaths for all of the libraries
 *         that are specified on the project (in the library pane).
 */
@NotNull
public static HaxeLibraryList getProjectLibraries(@NotNull Project project, boolean filterManagedLibs, boolean filterUnmanagedLibs) {
  LibraryTable libraryTable = ProjectLibraryTable.getInstance(project);
  if (null == libraryTable || (filterManagedLibs && filterUnmanagedLibs)) {
    return new HaxeLibraryList(HaxelibSdkUtils.lookupSdk(project));
  }

  HaxeLibraryList libs = new HaxeLibraryList(HaxelibSdkUtils.lookupSdk(project));
  Library[] libraries = libraryTable.getLibraries();
  for (Library library : libraries) {
    String name = library.getName();
    if (name == null) continue;

    boolean isManaged = HaxelibNameUtil.isManagedLibrary(name);
    if (filterManagedLibs && isManaged) continue;
    if (filterUnmanagedLibs && !isManaged) continue;

    libs.add(HaxeLibraryReference.create(project, name));
  }
  return libs;
}
 
Example 8
Source File: MavenImportingTestCase.java    From intellij-quarkus with Eclipse Public License 2.0 5 votes vote down vote up
public void assertProjectLibraries(String... expectedNames) {
  List<String> actualNames = new ArrayList<>();
  for (Library each : LibraryTablesRegistrar.getInstance().getLibraryTable(myProject).getLibraries()) {
    String name = each.getName();
    actualNames.add(name == null ? "<unnamed>" : name);
  }
  assertUnorderedElementsAreEqual(actualNames, expectedNames);
}
 
Example 9
Source File: LibraryGroupNode.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
@Nonnull
public Collection<AbstractTreeNode> getChildren() {
  Module module = getValue().getModule();
  final ModuleRootManager moduleRootManager = ModuleRootManager.getInstance(module);
  final List<AbstractTreeNode> children = new ArrayList<AbstractTreeNode>();
  final OrderEntry[] orderEntries = moduleRootManager.getOrderEntries();
  for (final OrderEntry orderEntry : orderEntries) {
    if (orderEntry instanceof LibraryOrderEntry) {
      final LibraryOrderEntry libraryOrderEntry = (LibraryOrderEntry)orderEntry;
      final Library library = libraryOrderEntry.getLibrary();
      if (library == null) {
        continue;
      }
      final String libraryName = library.getName();
      if (libraryName == null || libraryName.length() == 0) {
        addLibraryChildren(libraryOrderEntry, children, getProject(), this);
      }
      else {
        children.add(new NamedLibraryElementNode(getProject(), new NamedLibraryElement(module, libraryOrderEntry), getSettings()));
      }
    }
    else if (orderEntry instanceof ModuleExtensionWithSdkOrderEntry) {
      final ModuleExtensionWithSdkOrderEntry sdkOrderEntry = (ModuleExtensionWithSdkOrderEntry)orderEntry;
      final Sdk jdk = sdkOrderEntry.getSdk();
      if (jdk != null) {
        children.add(new NamedLibraryElementNode(getProject(), new NamedLibraryElement(module, sdkOrderEntry), getSettings()));
      }
    }
  }
  return children;
}
 
Example 10
Source File: LibraryElementPresentation.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static String getLibraryItemText(final @Nonnull Library library, final boolean includeTableName) {
  String name = library.getName();
  VirtualFile[] files = library.getFiles(BinariesOrderRootType.getInstance());
  if (name != null) {
    return name + (includeTableName ? getLibraryTableComment(library) : "");
  }
  else if (files.length > 0) {
    return files[0].getName() + (includeTableName ? getLibraryTableComment(library) : "");
  }
  else {
    return ProjectBundle.message("library.empty.item");
  }
}
 
Example 11
Source File: BlazeGoLibrarySource.java    From intellij with Apache License 2.0 5 votes vote down vote up
static boolean isGoLibrary(Library library) {
  String name = library.getName();
  if (name == null) {
    return false;
  }
  for (String prefix : BlazeGoSyncPlugin.GO_LIBRARY_PREFIXES) {
    if (name.startsWith(prefix)) {
      return true;
    }
  }
  return false;
}
 
Example 12
Source File: LibraryActionHelper.java    From intellij with Apache License 2.0 5 votes vote down vote up
@Nullable
public static BlazeJarLibrary findLibraryFromIntellijLibrary(
    Project project, BlazeProjectData blazeProjectData, Library library) {
  String libName = library.getName();
  if (libName == null) {
    return null;
  }
  LibraryKey libraryKey = LibraryKey.fromIntelliJLibraryName(libName);
  BlazeJavaSyncData syncData = blazeProjectData.getSyncState().get(BlazeJavaSyncData.class);
  if (syncData == null) {
    Messages.showErrorDialog(project, "Project isn't synced. Please resync project.", "Error");
    return null;
  }
  return syncData.getImportResult().libraries.get(libraryKey);
}
 
Example 13
Source File: CamelService.java    From camel-idea-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * @return true if the library name is cached
 */
public boolean containsLibrary(String lib, boolean quickCheck) {
    boolean answer = processedLibraries.contains(lib);
    if (!answer && !quickCheck) {
        for (Library l : projectLibraries) {
            String name = l.getName();
            if (name != null && name.contains(lib)) {
                answer = true;
                break;
            }
        }
    }
    return answer;
}
 
Example 14
Source File: AndroidModuleLibraryManager.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void addAndroidLibraryDependencies(@NotNull Project androidProject,
                                           @NotNull Module androidModule,
                                           @NotNull Module flutterModule) {
  AndroidSdkUtils.setupAndroidPlatformIfNecessary(androidModule, true);
  Sdk currentSdk = ModuleRootManager.getInstance(androidModule).getSdk();
  if (currentSdk != null) {
    // TODO(messick) Add sdk dependency on currentSdk if not already set
  }
  LibraryTable androidProjectLibraryTable = LibraryTablesRegistrar.getInstance().getLibraryTable(androidProject);
  Library[] androidProjectLibraries = androidProjectLibraryTable.getLibraries();
  LibraryTable flutterProjectLibraryTable = LibraryTablesRegistrar.getInstance().getLibraryTable(getProject());
  Library[] flutterProjectLibraries = flutterProjectLibraryTable.getLibraries();
  Set<String> knownLibraryNames = new HashSet<>(flutterProjectLibraries.length);
  for (Library lib : flutterProjectLibraries) {
    if (lib.getName() != null) {
      knownLibraryNames.add(lib.getName());
    }
  }
  for (Library library : androidProjectLibraries) {
    if (library.getName() != null && !knownLibraryNames.contains(library.getName())) {

      List<String> roots = Arrays.asList(library.getRootProvider().getUrls(OrderRootType.CLASSES));
      Set<String> filteredRoots = roots.stream().filter(s -> shouldIncludeRoot(s)).collect(Collectors.toSet());
      if (filteredRoots.isEmpty()) continue;

      HashSet<String> sources = new HashSet<>(Arrays.asList(library.getRootProvider().getUrls(OrderRootType.SOURCES)));

      updateLibraryContent(library.getName(), filteredRoots, sources);
      updateAndroidModuleLibraryDependencies(flutterModule);
    }
  }
}
 
Example 15
Source File: AndroidModuleLibraryManager.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void addAndroidLibraryDependencies(@NotNull Project androidProject,
                                           @NotNull Module androidModule,
                                           @NotNull Module flutterModule) {
  AndroidSdkUtils.setupAndroidPlatformIfNecessary(androidModule, true);
  Sdk currentSdk = ModuleRootManager.getInstance(androidModule).getSdk();
  if (currentSdk != null) {
    // TODO(messick) Add sdk dependency on currentSdk if not already set
  }
  LibraryTable androidProjectLibraryTable = LibraryTablesRegistrar.getInstance().getLibraryTable(androidProject);
  Library[] androidProjectLibraries = androidProjectLibraryTable.getLibraries();
  LibraryTable flutterProjectLibraryTable = LibraryTablesRegistrar.getInstance().getLibraryTable(getProject());
  Library[] flutterProjectLibraries = flutterProjectLibraryTable.getLibraries();
  Set<String> knownLibraryNames = new HashSet<>(flutterProjectLibraries.length);
  for (Library lib : flutterProjectLibraries) {
    if (lib.getName() != null) {
      knownLibraryNames.add(lib.getName());
    }
  }
  for (Library library : androidProjectLibraries) {
    if (library.getName() != null && !knownLibraryNames.contains(library.getName())) {

      List<String> roots = Arrays.asList(library.getRootProvider().getUrls(OrderRootType.CLASSES));
      Set<String> filteredRoots = roots.stream().filter(s -> shouldIncludeRoot(s)).collect(Collectors.toSet());
      if (filteredRoots.isEmpty()) continue;

      HashSet<String> sources = new HashSet<>(Arrays.asList(library.getRootProvider().getUrls(OrderRootType.SOURCES)));

      updateLibraryContent(library.getName(), filteredRoots, sources);
      updateAndroidModuleLibraryDependencies(flutterModule);
    }
  }
}
 
Example 16
Source File: ExternalSystemImportingTestCase.java    From intellij-quarkus with Eclipse Public License 2.0 5 votes vote down vote up
public void assertProjectLibraries(String... expectedNames) {
  List<String> actualNames = new ArrayList<>();
  for (Library each : ProjectLibraryTable.getInstance(myProject).getLibraries()) {
    String name = each.getName();
    actualNames.add(name == null ? "<unnamed>" : name);
  }
  assertUnorderedElementsAreEqual(actualNames, expectedNames);
}
 
Example 17
Source File: ExternalSystemApiUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
public static boolean isExternalSystemLibrary(@Nonnull Library library, @Nonnull ProjectSystemId externalSystemId) {
  return library.getName() != null && StringUtil.startsWith(library.getName(), externalSystemId.getReadableName() + ": ");
}
 
Example 18
Source File: BlazeAttachSourceProvider.java    From intellij with Apache License 2.0 4 votes vote down vote up
@Override
public Collection<AttachSourcesAction> getActions(
    List<LibraryOrderEntry> orderEntries, final PsiFile psiFile) {
  Project project = psiFile.getProject();
  BlazeProjectData blazeProjectData =
      BlazeProjectDataManager.getInstance(project).getBlazeProjectData();
  if (blazeProjectData == null) {
    return ImmutableList.of();
  }

  List<BlazeLibrary> librariesToAttachSourceTo = Lists.newArrayList();
  for (LibraryOrderEntry orderEntry : orderEntries) {
    Library library = orderEntry.getLibrary();
    if (library == null) {
      continue;
    }
    String name = library.getName();
    if (name == null) {
      continue;
    }
    LibraryKey libraryKey = LibraryKey.fromIntelliJLibraryName(name);
    if (AttachedSourceJarManager.getInstance(project).hasSourceJarAttached(libraryKey)) {
      continue;
    }
    BlazeJarLibrary blazeLibrary =
        LibraryActionHelper.findLibraryFromIntellijLibrary(project, blazeProjectData, library);
    if (blazeLibrary == null) {
      continue;
    }
    LibraryArtifact libraryArtifact = blazeLibrary.libraryArtifact;
    if (libraryArtifact.getSourceJars().isEmpty()) {
      continue;
    }
    librariesToAttachSourceTo.add(blazeLibrary);
  }

  if (librariesToAttachSourceTo.isEmpty()) {
    return ImmutableList.of();
  }

  // Hack: When sources are requested and we have them, we attach them automatically in the
  // background.
  if (attachAutomatically.getValue()) {
    TransactionGuard.getInstance()
        .submitTransactionLater(
            project,
            () -> {
              attachSources(project, blazeProjectData, librariesToAttachSourceTo);
            });
    return ImmutableList.of();
  }

  return ImmutableList.of(
      new AttachSourcesAction() {
        @Override
        public String getName() {
          return "Attach Blaze Source Jars";
        }

        @Override
        public String getBusyText() {
          return "Attaching source jars...";
        }

        @Override
        public ActionCallback perform(List<LibraryOrderEntry> orderEntriesContainingFile) {
          ActionCallback callback =
              new ActionCallback().doWhenDone(() -> navigateToSource(psiFile));
          Transactions.submitTransaction(
              project,
              () -> {
                attachSources(project, blazeProjectData, librariesToAttachSourceTo);
                callback.setDone();
              });
          return callback;
        }
      });
}
 
Example 19
Source File: PsiBasedClassFileFinder.java    From intellij with Apache License 2.0 4 votes vote down vote up
@Nullable
private VirtualFile findClassFileForIJarClass(
    BlazeProjectData projectData, VirtualFile ijarClass, String fqcn) {
  OrderEntry orderEntry = LibraryUtil.findLibraryEntry(ijarClass, project);
  if (!(orderEntry instanceof LibraryOrderEntry)) {
    return null;
  }

  Library intellijLibrary = ((LibraryOrderEntry) orderEntry).getLibrary();
  if (intellijLibrary == null || intellijLibrary.getName() == null) {
    return null;
  }

  BlazeJavaSyncData syncData = projectData.getSyncState().get(BlazeJavaSyncData.class);
  if (syncData == null) {
    return null;
  }

  LibraryKey libraryKey = LibraryKey.fromIntelliJLibraryName(intellijLibrary.getName());
  BlazeJarLibrary blazeLibrary = syncData.getImportResult().libraries.get(libraryKey);
  if (blazeLibrary == null) {
    LOG.warn(
        "Couldn't find BlazeJarLibrary corresponding to IJar for library "
            + intellijLibrary.getName());
    return null;
  }
  if (blazeLibrary.libraryArtifact.getClassJar() == null) {
    LOG.warn(
        "No class jar specified in BlazeJarLibrary for library " + intellijLibrary.getName());
    return null;
  }

  ArtifactLocation classJarArtifactLocation = blazeLibrary.libraryArtifact.getClassJar();
  File classJarFile =
      Preconditions.checkNotNull(
          OutputArtifactResolver.resolve(
              project, projectData.getArtifactLocationDecoder(), classJarArtifactLocation),
          "Fail to find file %s",
          classJarArtifactLocation.getRelativePath());
  VirtualFile classJar =
      VirtualFileSystemProvider.getInstance().getSystem().findFileByIoFile(classJarFile);
  if (classJar == null) {
    LOG.warn("Missing class jar: " + classJarFile);
    return null;
  }

  return findClassInJar(classJar, fqcn);
}
 
Example 20
Source File: ExternalLibrariesNode.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nonnull
@Override
@RequiredUIAccess
public Collection<? extends AbstractTreeNode> getChildren() {
  final List<AbstractTreeNode> children = new ArrayList<AbstractTreeNode>();
  ProjectFileIndex fileIndex = ProjectRootManager.getInstance(getProject()).getFileIndex();
  Module[] modules = ModuleManager.getInstance(getProject()).getModules();
  Set<Library> processedLibraries = new THashSet<Library>();
  Set<Sdk> processedSdk = new THashSet<Sdk>();
  Set<OrderEntry> processedCustomOrderEntries = new THashSet<OrderEntry>();

  for (Module module : modules) {
    final ModuleRootManager moduleRootManager = ModuleRootManager.getInstance(module);
    final OrderEntry[] orderEntries = moduleRootManager.getOrderEntries();
    loop:for (final OrderEntry orderEntry : orderEntries) {
      if (orderEntry instanceof LibraryOrderEntry) {
        final LibraryOrderEntry libraryOrderEntry = (LibraryOrderEntry)orderEntry;
        final Library library = libraryOrderEntry.getLibrary();
        if (library == null) continue;
        if (processedLibraries.contains(library)) continue;
        processedLibraries.add(library);

        if (!hasExternalEntries(fileIndex, libraryOrderEntry)) continue;

        final String libraryName = library.getName();
        if (libraryName == null || libraryName.length() == 0) {
          addLibraryChildren(libraryOrderEntry, children, getProject(), this);
        }
        else {
          children.add(new NamedLibraryElementNode(getProject(), new NamedLibraryElement(null, libraryOrderEntry), getSettings()));
        }
      }
      else if (orderEntry instanceof ModuleExtensionWithSdkOrderEntry) {
        final ModuleExtensionWithSdkOrderEntry sdkOrderEntry = (ModuleExtensionWithSdkOrderEntry)orderEntry;
        final Sdk sdk = sdkOrderEntry.getSdk();
        if (sdk != null) {
          if (processedSdk.contains(sdk)) continue;
          processedSdk.add(sdk);
          children.add(new NamedLibraryElementNode(getProject(), new NamedLibraryElement(null, sdkOrderEntry), getSettings()));
        }
      }
      else if(orderEntry instanceof OrderEntryWithTracking) {
        if(!orderEntry.isValid()) {
          continue;
        }
        for (OrderEntry processedCustomOrderEntry : processedCustomOrderEntries) {
          if(processedCustomOrderEntry.isEquivalentTo(orderEntry)) {
            continue loop;
          }
        }
        processedCustomOrderEntries.add(orderEntry);
        children.add(new NamedLibraryElementNode(getProject(), new NamedLibraryElement(null, orderEntry), getSettings()));
      }
    }
  }
  return children;
}