@Override
  public void findTargets(
      TargetKind kind,
      Collection<SModel> scope,
      Consumer<NavigationTarget> consumer,
      Consumer<SModel> processedConsumer) {
    final ID<Integer, List<SNodeDescriptor>> indexName = RootNodeNameIndex.NAME;
    final FileBasedIndex fileBasedIndex = FileBasedIndex.getInstance();

    for (SModel sm : scope) {
      if (sm instanceof EditableSModel && ((EditableSModel) sm).isChanged()) {
        continue;
      }

      DataSource source = sm.getSource();
      if (!(source instanceof FileDataSource)) {
        continue;
      }

      IFile modelFile = ((FileDataSource) source).getFile();
      String ext = FileUtil.getExtension(modelFile.getName());
      if (ext == null
          || modelFile.isDirectory()
          || !(supportedExtensions.contains(ext.toLowerCase()))) {
        continue;
      }

      VirtualFile vf = VirtualFileUtils.getVirtualFile(modelFile);
      if (vf == null) continue; // e.g. model was deleted

      int fileId = FileBasedIndex.getFileId(vf);
      ConcreteFilesGlobalSearchScope fileScope =
          new ConcreteFilesGlobalSearchScope(Collections.singleton(vf));
      List<List<SNodeDescriptor>> descriptors =
          fileBasedIndex.getValues(indexName, fileId, fileScope);
      if (descriptors.isEmpty()) continue;

      boolean needToLoad = false;
      for (NavigationTarget snd : descriptors.get(0)) {
        PropertyConstraintsDescriptor descriptor =
            ConceptRegistry.getInstance()
                .getConstraintsDescriptor(snd.getConcept().getQualifiedName())
                .getProperty(SNodeUtil.property_INamedConcept_name);
        if (descriptor instanceof BasePropertyConstraintsDescriptor
            && !((BasePropertyConstraintsDescriptor) descriptor).isGetterDefault()) {
          needToLoad = true;
          break;
        }
      }

      if (!needToLoad) {
        for (SNodeDescriptor desc : descriptors.get(0)) {
          consumer.consume(desc);
        }
        processedConsumer.consume(sm);
      }
    }
  }
  @Override
  @Nullable
  public ObjectStubTree readFromVFile(Project project, final VirtualFile vFile) {
    if (DumbService.getInstance(project).isDumb()) {
      return null;
    }

    final int id = Math.abs(FileBasedIndex.getFileId(vFile));
    if (id <= 0) {
      return null;
    }

    boolean wasIndexedAlready = FileBasedIndexImpl.isFileIndexed(vFile, StubUpdatingIndex.INDEX_ID);

    final List<SerializedStubTree> datas =
        FileBasedIndex.getInstance()
            .getValues(StubUpdatingIndex.INDEX_ID, id, GlobalSearchScope.fileScope(project, vFile));
    final int size = datas.size();

    if (size == 1) {
      Stub stub;
      try {
        stub = datas.get(0).getStub(false);
      } catch (SerializerNotFoundException e) {
        return processError(
            vFile, "No stub serializer: " + vFile.getPresentableUrl() + ": " + e.getMessage(), e);
      }
      ObjectStubTree tree =
          stub instanceof PsiFileStub
              ? new StubTree((PsiFileStub) stub)
              : new ObjectStubTree((ObjectStubBase) stub, true);
      tree.setDebugInfo(
          "created from index: "
              + StubUpdatingIndex.getIndexingStampInfo(vFile)
              + ", wasIndexedAlready="
              + wasIndexedAlready
              + ", queried at "
              + vFile.getTimeStamp());
      return tree;
    } else if (size != 0) {
      return processError(
          vFile,
          "Twin stubs: "
              + vFile.getPresentableUrl()
              + " has "
              + size
              + " stub versions. Should only have one. id="
              + id,
          null);
    }

    return null;
  }
 private void doIndexing(
     @NotNull final VirtualFile virtualFile, @NotNull final ProgressIndicator indicator) {
   final int fileId = FileBasedIndex.getFileId(virtualFile);
   indicator.setText("IntelliJence Advisor plugin indexing...");
   indicator.setText2(virtualFile.getCanonicalPath());
   final Long timestamp = getTimestamp(fileId);
   ProgressManager.checkCanceled();
   final long currentTimeStamp = virtualFile.getModificationStamp();
   if (timestamp == null || timestamp != currentTimeStamp) {
     putTimestamp(fileId, currentTimeStamp);
     final ClassReader reader;
     try {
       reader = new ClassReader(virtualFile.contentsToByteArray());
     } catch (IOException e) {
       removeTimestamp(fileId);
       return;
     }
     try {
       for (final AdvisorBaseIndex index : indexes) {
         index.update(fileId, reader);
       }
     } catch (RuntimeException e) {
       log.error(String.format("can't index file: %s", virtualFile.getCanonicalPath()));
       // TODO delete it
       e.printStackTrace();
       throw e;
     }
   }
 }
 @Override
 public Collection<VirtualFile> getDomFileCandidates(
     Class<? extends DomElement> rootElementClass,
     Project project,
     final GlobalSearchScope scope) {
   return FileBasedIndex.getInstance()
       .getContainingFiles(DomFileIndex.NAME, rootElementClass.getName(), scope);
 }
  private static boolean notJetAnnotationEntry(final PsiElement found) {
    if (found instanceof JetAnnotationEntry) return false;

    VirtualFile faultyContainer = PsiUtilCore.getVirtualFile(found);
    LOG.error("Non annotation in annotations list: " + faultyContainer + "; element:" + found);
    if (faultyContainer != null && faultyContainer.isValid()) {
      FileBasedIndex.getInstance().requestReindex(faultyContainer);
    }

    return true;
  }
  public void testExternalFileModificationWhileProjectClosed() throws Exception {
    VirtualFile root = ProjectRootManager.getInstance(myProject).getContentRoots()[0];

    PsiClass objectClass =
        myJavaFacade.findClass(
            CommonClassNames.JAVA_LANG_OBJECT, GlobalSearchScope.allScope(getProject()));
    assertNotNull(objectClass);
    checkUsages(objectClass, new String[] {});
    FileBasedIndex.getInstance()
        .getContainingFiles(
            TodoIndex.NAME,
            new TodoIndexEntry("todo", true),
            GlobalSearchScope.allScope(getProject()));

    final String projectLocation = myProject.getPresentableUrl();
    assert projectLocation != null : myProject;
    PlatformTestUtil.saveProject(myProject);
    final VirtualFile content = ModuleRootManager.getInstance(getModule()).getContentRoots()[0];
    Project project = myProject;
    ProjectUtil.closeAndDispose(project);
    InjectedLanguageManagerImpl.checkInjectorsAreDisposed(project);

    assertTrue("Project was not disposed", myProject.isDisposed());
    myModule = null;

    final File file = new File(root.getPath(), "1.java");
    assertTrue(file.exists());

    FileUtil.writeToFile(file, "class A{ Object o;}".getBytes(CharsetToolkit.UTF8_CHARSET));
    root.refresh(false, true);

    LocalFileSystem.getInstance().refresh(false);

    myProject = ProjectManager.getInstance().loadAndOpenProject(projectLocation);
    InjectedLanguageManagerImpl.pushInjectors(getProject());

    setUpModule();
    setUpJdk();
    ProjectManagerEx.getInstanceEx().openTestProject(myProject);
    UIUtil.dispatchAllInvocationEvents(); // startup activities

    runStartupActivities();
    PsiTestUtil.addSourceContentToRoots(getModule(), content);

    assertNotNull(myProject);
    myPsiManager = (PsiManagerImpl) PsiManager.getInstance(myProject);
    myJavaFacade = JavaPsiFacadeEx.getInstanceEx(myProject);

    objectClass =
        myJavaFacade.findClass(
            CommonClassNames.JAVA_LANG_OBJECT, GlobalSearchScope.allScope(getProject()));
    assertNotNull(objectClass);
    checkUsages(objectClass, new String[] {"1.java"});
  }
 public MyIndex(
     final ID<Integer, SerializedStubTree> indexId,
     final IndexStorage<Integer, SerializedStubTree> storage,
     final DataIndexer<Integer, SerializedStubTree, FileContent> indexer) {
   super(indexId, indexer, storage);
   try {
     checkNameStorage();
   } catch (StorageException e) {
     LOG.info(e);
     FileBasedIndex.requestRebuild(INDEX_ID);
   }
 }
  public static Collection<JSClass> searchClassInheritors(
      ID<String, Void> indexId, String name, Project project, final GlobalSearchScope scope) {
    final Collection<VirtualFile> files =
        FileBasedIndex.getInstance().getContainingFiles(indexId, name, scope);
    final PsiManager psiManager = PsiManager.getInstance(project);
    Collection<JSClass> classes = new ArrayList<JSClass>(files.size());
    for (VirtualFile file : files) {
      PsiFile psifile = psiManager.findFile(file);
      if (!(psifile instanceof XmlFile)) continue;
      classes.addAll(XmlBackedJSClassImpl.getClasses((XmlFile) psifile));
    }

    if (FlexNameAlias.CONTAINER_TYPE_NAME.equals(name)) {
      classes.addAll(
          searchClassInheritors(indexId, FlexNameAlias.COMPONENT_TYPE_NAME, project, scope));
    }
    return classes;
  }
  public boolean processPropertiesFiles(
      @NotNull final GlobalSearchScope searchScope,
      @NotNull final PropertiesFileProcessor processor,
      @NotNull final BundleNameEvaluator evaluator) {
    for (VirtualFile file : FileTypeIndex.getFiles(PropertiesFileType.INSTANCE, searchScope)) {
      if (!processFile(file, evaluator, processor)) return false;
    }
    if (!myDumbService.isDumb()) {
      for (VirtualFile file :
          FileBasedIndex.getInstance()
              .getContainingFiles(
                  XmlPropertiesIndex.NAME, XmlPropertiesIndex.MARKER_KEY, searchScope)) {
        if (!processFile(file, evaluator, processor)) return false;
      }
    }

    return true;
  }
  @Override
  public <K> Collection<K> getAllKeys(final StubIndexKey<K, ?> indexKey, @NotNull Project project) {
    FileBasedIndex.getInstance()
        .ensureUpToDate(StubUpdatingIndex.INDEX_ID, project, GlobalSearchScope.allScope(project));

    final MyIndex<K> index = (MyIndex<K>) myIndices.get(indexKey);
    try {
      return index.getAllKeys();
    } catch (StorageException e) {
      forceRebuild(e);
    } catch (RuntimeException e) {
      final Throwable cause = e.getCause();
      if (cause instanceof IOException || cause instanceof StorageException) {
        forceRebuild(e);
      }
      throw e;
    }
    return Collections.emptyList();
  }
 @NotNull
 public static List<PyFile> find(
     @NotNull String name, @NotNull Project project, boolean includeNonProjectItems) {
   final List<PyFile> results = new ArrayList<PyFile>();
   final GlobalSearchScope scope =
       includeNonProjectItems
           ? PyProjectScopeBuilder.excludeSdkTestsScope(project)
           : GlobalSearchScope.projectScope(project);
   final Collection<VirtualFile> files =
       FileBasedIndex.getInstance().getContainingFiles(NAME, name, scope);
   for (VirtualFile virtualFile : files) {
     final PsiFile psiFile = PsiManager.getInstance(project).findFile(virtualFile);
     if (psiFile instanceof PyFile) {
       if (!PyUserSkeletonsUtil.isUnderUserSkeletonsDirectory(psiFile)) {
         results.add((PyFile) psiFile);
       }
     }
   }
   return results;
 }
  private static ObjectStubTree processError(
      final VirtualFile vFile, String message, @Nullable Exception e) {
    LOG.error(message, e);

    ApplicationManager.getApplication()
        .invokeLater(
            new Runnable() {
              @Override
              public void run() {
                final Document doc = FileDocumentManager.getInstance().getCachedDocument(vFile);
                if (doc != null) {
                  FileDocumentManager.getInstance().saveDocument(doc);
                }
              }
            },
            ModalityState.NON_MODAL);

    FileBasedIndex.getInstance().requestReindex(vFile);
    return null;
  }
 @Override
 public void processNames(
     @NotNull final Processor<String> processor,
     @NotNull GlobalSearchScope scope,
     IdFilter filter) {
   long started = System.currentTimeMillis();
   FileBasedIndex.getInstance()
       .processAllKeys(
           FilenameIndex.NAME,
           new Processor<String>() {
             @Override
             public boolean process(String s) {
               return processor.process(s);
             }
           },
           scope,
           filter);
   if (IdFilter.LOG.isDebugEnabled()) {
     IdFilter.LOG.debug("All names retrieved:" + (System.currentTimeMillis() - started));
   }
 }
 @Override
 public boolean isGenerated(VirtualFile file) {
   if (myGeneratedSources.contains(FileBasedIndex.getFileId(file))) {
     return true;
   }
   if (isUnderRoots(myRootToModuleMap.keySet(), file)) {
     return true;
   }
   final Module module = getModuleByFile(file);
   if (module != null) {
     for (AdditionalOutputDirectoriesProvider provider :
         AdditionalOutputDirectoriesProvider.EP_NAME.getExtensions()) {
       for (String path : provider.getOutputDirectories(getProject(), module)) {
         if (path != null
             && VfsUtilCore.isAncestor(new File(path), new File(file.getPath()), true)) {
           return true;
         }
       }
     }
   }
   return false;
 }
  public static <Key, Psi extends PsiElement> Collection<Psi> safeGet(
      @NotNull StubIndexKey<Key, Psi> indexKey,
      @NotNull Key key,
      final Project project,
      final GlobalSearchScope scope,
      Class<Psi> requiredClass) {
    Collection<Psi> collection = getInstance().get(indexKey, key, project, scope);
    for (Iterator<Psi> iterator = collection.iterator(); iterator.hasNext(); ) {
      Psi psi = iterator.next();
      if (!requiredClass.isInstance(psi)) {
        iterator.remove();

        VirtualFile faultyContainer = PsiUtilCore.getVirtualFile(psi);
        LOG.error("Invalid stub element type in index: " + faultyContainer + ". found: " + psi);
        if (faultyContainer != null && faultyContainer.isValid()) {
          FileBasedIndex.getInstance().requestReindex(faultyContainer);
        }
      }
    }

    return collection;
  }
 public void projectClosing(Project project) {
   myIndex.removeIndexableSet(MPSFileBasedIndexProjectHandler.this);
 }
  public JComponent createComponent() {
    final JPanel panel = new JPanel(new GridBagLayout());
    panel.setBorder(IdeBorderFactory.createTitledBorder("File Context", false));
    myCombo = new ComboBox();
    myCombo.putClientProperty(CONTEXTS_COMBO_KEY, Boolean.TRUE);
    panel.add(
        new JLabel("Included into:"),
        new GridBagConstraints(
            0,
            0,
            1,
            1,
            0.0,
            0.0,
            GridBagConstraints.WEST,
            GridBagConstraints.NONE,
            new Insets(5, 0, 5, 0),
            0,
            0));
    panel.add(
        myCombo,
        new GridBagConstraints(
            1,
            0,
            1,
            1,
            1.0,
            0.0,
            GridBagConstraints.WEST,
            GridBagConstraints.HORIZONTAL,
            new Insets(5, 5, 5, 0),
            0,
            0));

    final PsiManager psiManager = PsiManager.getInstance(myProject);
    final FileBasedIndex fbi = FileBasedIndex.getInstance();
    final Collection<VirtualFile> antFiles =
        fbi.getContainingFiles(
            AntImportsIndex.INDEX_NAME, AntImportsIndex.ANT_FILES_WITH_IMPORTS_KEY, myFileFilter);

    for (VirtualFile file : antFiles) {
      final PsiFile psiFile = psiManager.findFile(file);
      if (!(psiFile instanceof XmlFile)) {
        continue;
      }
      final XmlFile xmlFile = (XmlFile) psiFile;
      if (!xmlFile.equals(myFile) && AntDomFileDescription.isAntFile(xmlFile)) {
        final String path = PathUtil.getLocalPath(file);
        final XmlFile previous = myPathToFileMap.put(path, xmlFile);
        assert previous == null;
      }
    }

    final List<String> paths = new ArrayList<String>(myPathToFileMap.keySet());
    Collections.sort(
        paths,
        new Comparator<String>() {
          public int compare(final String o1, final String o2) {
            return o1.compareTo(o2);
          }
        });

    myCombo.addItem(NONE);
    for (String path : paths) {
      myCombo.addItem(path);
    }

    final AntConfigurationBase antConfig = AntConfigurationBase.getInstance(myProject);
    final XmlFile currentContext = antConfig.getContextFile(myFile);
    if (currentContext != null) {
      final VirtualFile vFile = currentContext.getVirtualFile();

      assert vFile != null;

      final String path = PathUtil.getLocalPath(vFile);
      if (!FileUtil.pathsEqual(path, myLocalPath)) {
        myOriginalContext = path;
      }
    }
    myCombo.setSelectedItem(myOriginalContext);

    return panel;
  }
 private static void forceRebuild(Throwable e) {
   LOG.info(e);
   requestRebuild();
   FileBasedIndex.getInstance().scheduleRebuild(StubUpdatingIndex.INDEX_ID, e);
 }
 @Override
 public void markGenerated(Collection<VirtualFile> files) {
   for (final VirtualFile file : files) {
     myGeneratedSources.add(FileBasedIndex.getFileId(file));
   }
 }
  public static VirtualFile[] configureByFiles(
      @Nullable VirtualFile rawProjectRoot,
      VirtualFile[] files,
      @Nullable VirtualFile[] auxiliaryFiles,
      Module module,
      @Nullable
          TripleFunction<ModifiableRootModel, VirtualFile, List<String>, Void> moduleInitializer)
      throws Exception {
    return WriteAction.compute(
        () -> {
          VirtualFile dummyRoot = VirtualFileManager.getInstance().findFileByUrl("temp:///");
          //noinspection ConstantConditions
          dummyRoot.refresh(false, false);
          final VirtualFile sourceDir = dummyRoot.createChildDirectory(DesignerTests.class, "s");
          assert sourceDir != null;

          final IndexableFileSet indexableFileSet =
              new IndexableFileSet() {
                @Override
                public boolean isInSet(@NotNull final VirtualFile file) {
                  return file.getFileSystem() == sourceDir.getFileSystem();
                }

                @Override
                public void iterateIndexableFilesIn(
                    @NotNull final VirtualFile file, @NotNull final ContentIterator iterator) {
                  if (file.isDirectory()) {
                    for (VirtualFile child : file.getChildren()) {
                      iterateIndexableFilesIn(child, iterator);
                    }
                  } else {
                    iterator.processFile(file);
                  }
                }
              };
          FileBasedIndex.getInstance().registerIndexableSet(indexableFileSet, module.getProject());

          Disposer.register(
              module,
              new Disposable() {
                @Override
                public void dispose() {
                  FileBasedIndex.getInstance().removeIndexableSet(indexableFileSet);
                  ApplicationManager.getApplication()
                      .runWriteAction(
                          () -> {
                            try {
                              sourceDir.delete(null);
                            } catch (IOException e) {
                              throw new RuntimeException(e);
                            }
                          });
                }
              });

          final ModifiableRootModel rootModel =
              ModuleRootManager.getInstance(module).getModifiableModel();

          VirtualFile[] toFiles = copyFiles(files, sourceDir, rawProjectRoot);
          if (auxiliaryFiles != null) {
            copyFiles(auxiliaryFiles, sourceDir, rawProjectRoot);
          }

          rootModel.addContentEntry(sourceDir).addSourceFolder(sourceDir, false);
          final List<String> libs = new ArrayList<>();
          if (moduleInitializer != null) {
            moduleInitializer.fun(rootModel, sourceDir, libs);
          }
          rootModel.commit();

          for (String path : libs) {
            VirtualFile virtualFile = path.charAt(0) != '/' ? getFile("lib", path) : getFile(path);
            FlexTestUtils.addLibrary(
                module, path, virtualFile.getParent().getPath(), virtualFile.getName(), null, null);
          }
          return toFiles;
        });
  }
  @Override
  @Nullable
  public ObjectStubTree readFromVFile(Project project, final VirtualFile vFile) {
    if (DumbService.getInstance(project).isDumb()) {
      return null;
    }

    final int id = Math.abs(FileBasedIndex.getFileId(vFile));
    if (id <= 0) {
      return null;
    }

    boolean wasIndexedAlready =
        ((FileBasedIndexImpl) FileBasedIndex.getInstance()).isFileUpToDate(vFile);

    Document document = FileDocumentManager.getInstance().getCachedDocument(vFile);
    boolean saved =
        document == null || !FileDocumentManager.getInstance().isDocumentUnsaved(document);

    final List<SerializedStubTree> datas =
        FileBasedIndex.getInstance()
            .getValues(StubUpdatingIndex.INDEX_ID, id, GlobalSearchScope.fileScope(project, vFile));
    final int size = datas.size();

    if (size == 1) {
      SerializedStubTree stubTree = datas.get(0);

      if (!stubTree.contentLengthMatches(
          vFile.getLength(), getCurrentTextContentLength(project, vFile, document))) {
        return processError(
            vFile,
            "Outdated stub in index: "
                + StubUpdatingIndex.getIndexingStampInfo(vFile)
                + ", doc="
                + document
                + ", docSaved="
                + saved
                + ", wasIndexedAlready="
                + wasIndexedAlready
                + ", queried at "
                + vFile.getTimeStamp(),
            null);
      }

      Stub stub;
      try {
        stub = stubTree.getStub(false);
      } catch (SerializerNotFoundException e) {
        return processError(
            vFile, "No stub serializer: " + vFile.getPresentableUrl() + ": " + e.getMessage(), e);
      }
      ObjectStubTree tree =
          stub instanceof PsiFileStub
              ? new StubTree((PsiFileStub) stub)
              : new ObjectStubTree((ObjectStubBase) stub, true);
      tree.setDebugInfo("created from index");
      return tree;
    } else if (size != 0) {
      return processError(
          vFile,
          "Twin stubs: "
              + vFile.getPresentableUrl()
              + " has "
              + size
              + " stub versions. Should only have one. id="
              + id,
          null);
    }

    return null;
  }
 @Override
 public void rebuildStubTree(VirtualFile virtualFile) {
   FileBasedIndex.getInstance().requestReindex(virtualFile);
 }
Example #23
0
  @NotNull
  public static List<LinkReferenceResult> getReferencedFiles(
      @NotNull final VirtualFile _file, @NotNull final Project project) {
    final List<LinkReferenceResult> result = new ArrayList<LinkReferenceResult>();
    if (!(_file.getFileSystem() instanceof LocalFileSystem)) {
      return result;
    }

    FileBasedIndex.getInstance()
        .processValues(
            INDEX_ID,
            FileBasedIndex.getFileId(_file),
            null,
            new FileBasedIndex.ValueProcessor<InfoHolder<LinkInfo>>() {
              public boolean process(final VirtualFile file, final InfoHolder<LinkInfo> value) {
                final PsiManager psiManager = PsiManager.getInstance(project);
                final PsiFile psiFile = psiManager.findFile(file);
                if (psiFile != null) {
                  for (final LinkInfo linkInfo : value.myValues) {
                    if (linkInfo.value != null || linkInfo.scripted) {
                      final PsiFileSystemItem[] item = new PsiFileSystemItem[] {null};
                      if (linkInfo.value != null) {
                        final LeafElement newValueElement =
                            Factory.createSingleLeafElement(
                                XmlTokenType.XML_ATTRIBUTE_VALUE_TOKEN,
                                "\"" + linkInfo.value + "\"",
                                0,
                                linkInfo.value.length() + 2,
                                null,
                                psiManager,
                                psiFile);
                        final PsiElement element = newValueElement.getPsi();
                        final FileReferenceSet set =
                            new FileReferenceSet(
                                StringUtil.stripQuotesAroundValue(element.getText()),
                                element,
                                1,
                                null,
                                true);

                        final FileReference lastReference = set.getLastReference();

                        if (lastReference != null) {
                          final PsiFileSystemItem resolved = lastReference.resolve();
                          if (resolved instanceof PsiFile) {
                            item[0] = resolved;
                          }
                        }
                      }

                      result.add(new MyLinkReferenceResult(item, linkInfo, psiFile));
                    }
                  }
                }
                return true;
              }
            },
            GlobalSearchScope.allScope(project));

    return result;
  }
 private static void requestRebuild() {
   FileBasedIndex.requestRebuild(StubUpdatingIndex.INDEX_ID);
 }
  @Override
  public <Key, Psi extends PsiElement> Collection<Psi> get(
      @NotNull final StubIndexKey<Key, Psi> indexKey,
      @NotNull final Key key,
      final Project project,
      final GlobalSearchScope scope) {
    FileBasedIndex.getInstance().ensureUpToDate(StubUpdatingIndex.INDEX_ID, project, scope);

    final PersistentFS fs = (PersistentFS) ManagingFS.getInstance();
    final PsiManager psiManager = PsiManager.getInstance(project);

    final List<Psi> result = new ArrayList<Psi>();
    final MyIndex<Key> index = (MyIndex<Key>) myIndices.get(indexKey);

    try {
      try {
        // disable up-to-date check to avoid locks on attempt to acquire index write lock while
        // holding at the same time the readLock for this index
        FileBasedIndex.disableUpToDateCheckForCurrentThread();
        index.getReadLock().lock();
        final ValueContainer<TIntArrayList> container = index.getData(key);

        container.forEach(
            new ValueContainer.ContainerAction<TIntArrayList>() {
              @Override
              public void perform(final int id, final TIntArrayList value) {
                final VirtualFile file = IndexInfrastructure.findFileByIdIfCached(fs, id);
                if (file != null && (scope == null || scope.contains(file))) {
                  StubTree stubTree = null;

                  final PsiFile _psifile = psiManager.findFile(file);
                  PsiFileWithStubSupport psiFile = null;

                  if (_psifile != null && !(_psifile instanceof PsiPlainTextFile)) {
                    if (_psifile instanceof PsiFileWithStubSupport) {
                      psiFile = (PsiFileWithStubSupport) _psifile;
                      stubTree = psiFile.getStubTree();
                      if (stubTree == null && psiFile instanceof PsiFileImpl) {
                        stubTree = ((PsiFileImpl) psiFile).calcStubTree();
                      }
                    }
                  }

                  if (stubTree != null || psiFile != null) {
                    if (stubTree == null) {
                      stubTree = StubTreeLoader.getInstance().readFromVFile(project, file);
                      if (stubTree != null) {
                        final List<StubElement<?>> plained = stubTree.getPlainList();
                        for (int i = 0; i < value.size(); i++) {
                          final StubElement<?> stub = plained.get(value.get(i));
                          final ASTNode tree = psiFile.findTreeForStub(stubTree, stub);

                          if (tree != null) {
                            if (tree.getElementType() == stubType(stub)) {
                              result.add((Psi) tree.getPsi());
                            } else {
                              String persistedStubTree =
                                  ((PsiFileStubImpl) stubTree.getRoot()).printTree();

                              String stubTreeJustBuilt =
                                  ((PsiFileStubImpl)
                                          ((IStubFileElementType)
                                                  ((PsiFileImpl) psiFile).getContentElementType())
                                              .getBuilder()
                                              .buildStubTree(psiFile))
                                      .printTree();

                              StringBuilder builder = new StringBuilder();
                              builder.append("Oops\n");

                              builder.append("Recorded stub:-----------------------------------\n");
                              builder.append(persistedStubTree);
                              builder.append(
                                  "\nAST built stub: ------------------------------------\n");
                              builder.append(stubTreeJustBuilt);
                              builder.append("\n");
                              LOG.info(builder.toString());

                              // requestReindex() may want to acquire write lock (for indices not
                              // requiring content loading)
                              // thus, because here we are under read lock, need to use invoke later
                              ApplicationManager.getApplication()
                                  .invokeLater(
                                      new Runnable() {
                                        @Override
                                        public void run() {
                                          FileBasedIndex.getInstance().requestReindex(file);
                                        }
                                      },
                                      ModalityState.NON_MODAL);
                            }
                          }
                        }
                      }
                    } else {
                      final List<StubElement<?>> plained = stubTree.getPlainList();
                      for (int i = 0; i < value.size(); i++) {
                        result.add((Psi) plained.get(value.get(i)).getPsi());
                      }
                    }
                  }
                }
              }
            });
      } finally {
        index.getReadLock().unlock();
        FileBasedIndex.enableUpToDateCheckForCurrentThread();
      }
    } catch (StorageException e) {
      forceRebuild(e);
    } catch (RuntimeException e) {
      final Throwable cause = FileBasedIndex.getCauseToRebuildIndex(e);
      if (cause != null) {
        forceRebuild(cause);
      } else {
        throw e;
      }
    }

    return result;
  }
 @NotNull
 public static Collection<String> getAllKeys(@NotNull Project project) {
   return FileBasedIndex.getInstance().getAllKeys(NAME, project);
 }
 @Nullable
 public static String getNamespace(@NotNull VirtualFile file, final Project project) {
   final List<String> list =
       FileBasedIndex.getInstance().getValues(NAME, file.getUrl(), createFilter(project));
   return list.size() == 0 ? null : list.get(0);
 }
 public Collection<VirtualFile> getDomFileCandidates(
     Class<? extends DomElement> description, Project project) {
   return FileBasedIndex.getInstance()
       .getContainingFiles(
           DomFileIndex.NAME, description.getName(), GlobalSearchScope.allScope(project));
 }