public void removeEntryPoint(RefElement anEntryPoint) {
    if (anEntryPoint instanceof RefClass) {
      RefClass refClass = (RefClass) anEntryPoint;
      if (!refClass.isInterface()) {
        anEntryPoint = refClass.getDefaultConstructor();
      }
    }

    if (anEntryPoint == null) return;

    myTemporaryEntryPoints.remove(anEntryPoint);

    Set<Map.Entry<String, SmartRefElementPointer>> set = myPersistentEntryPoints.entrySet();
    String key = null;
    for (Map.Entry<String, SmartRefElementPointer> entry : set) {
      SmartRefElementPointer value = entry.getValue();
      if (value.getRefElement() == anEntryPoint) {
        key = entry.getKey();
        break;
      }
    }

    if (key != null) {
      myPersistentEntryPoints.remove(key);
      ((RefElementImpl) anEntryPoint).setEntry(false);
    }

    if (anEntryPoint.isPermanentEntry() && anEntryPoint.isValid()) {
      final Project project = anEntryPoint.getElement().getProject();
      final EntryPointsManagerImpl entryPointsManager = getInstance(project);
      if (this != entryPointsManager) {
        entryPointsManager.removeEntryPoint(anEntryPoint);
      }
    }
  }
 @Override
 public void ignoreProblem(RefEntity refEntity, CommonProblemDescriptor problem, int idx) {
   if (refEntity == null) return;
   final Set<QuickFix> localQuickFixes = getQuickFixActions().get(refEntity);
   final QuickFix[] fixes = problem.getFixes();
   if (isIgnoreProblem(fixes, localQuickFixes, idx)) {
     getProblemToElements().remove(problem);
     Map<RefEntity, CommonProblemDescriptor[]> problemElements = getProblemElements();
     synchronized (lock) {
       CommonProblemDescriptor[] descriptors = problemElements.get(refEntity);
       if (descriptors != null) {
         ArrayList<CommonProblemDescriptor> newDescriptors =
             new ArrayList<CommonProblemDescriptor>(Arrays.asList(descriptors));
         newDescriptors.remove(problem);
         getQuickFixActions().put(refEntity, null);
         if (!newDescriptors.isEmpty()) {
           problemElements.put(
               refEntity,
               newDescriptors.toArray(new CommonProblemDescriptor[newDescriptors.size()]));
           for (CommonProblemDescriptor descriptor : newDescriptors) {
             collectQuickFixes(descriptor.getFixes(), refEntity);
           }
         } else {
           ignoreProblemElement(refEntity);
         }
       }
     }
   }
 }
  @Override
  public void addEntryPoint(@NotNull RefElement newEntryPoint, boolean isPersistent) {
    if (!newEntryPoint.isValid()) return;
    if (isPersistent) {
      if (newEntryPoint instanceof RefMethod && ((RefMethod) newEntryPoint).isConstructor()
          || newEntryPoint instanceof RefClass) {
        final ClassPattern classPattern = new ClassPattern();
        classPattern.pattern = new SmartRefElementPointerImpl(newEntryPoint, true).getFQName();
        getPatterns().add(classPattern);

        final EntryPointsManager entryPointsManager =
            getInstance(newEntryPoint.getElement().getProject());
        if (this != entryPointsManager) {
          entryPointsManager.addEntryPoint(newEntryPoint, true);
        }

        return;
      }
    }

    if (newEntryPoint instanceof RefClass) {
      RefClass refClass = (RefClass) newEntryPoint;

      if (refClass.isAnonymous()) {
        // Anonymous class cannot be an entry point.
        return;
      }

      List<RefMethod> refConstructors = refClass.getConstructors();
      if (refConstructors.size() == 1) {
        addEntryPoint(refConstructors.get(0), isPersistent);
      } else if (refConstructors.size() > 1) {
        // Many constructors here. Need to ask user which ones are used
        for (RefMethod refConstructor : refConstructors) {
          addEntryPoint(refConstructor, isPersistent);
        }
      }
    }

    if (!isPersistent) {
      myTemporaryEntryPoints.add(newEntryPoint);
      ((RefElementImpl) newEntryPoint).setEntry(true);
    } else {
      if (myPersistentEntryPoints.get(newEntryPoint.getExternalName()) == null) {
        final SmartRefElementPointerImpl entry =
            new SmartRefElementPointerImpl(newEntryPoint, true);
        myPersistentEntryPoints.put(entry.getFQName(), entry);
        ((RefElementImpl) newEntryPoint).setEntry(true);
        ((RefElementImpl) newEntryPoint).setPermanentEntry(true);
        if (entry.isPersistent()) { // do save entry points
          final EntryPointsManager entryPointsManager =
              getInstance(newEntryPoint.getElement().getProject());
          if (this != entryPointsManager) {
            entryPointsManager.addEntryPoint(newEntryPoint, true);
          }
        }
      }
    }
  }
  @Override
  public void cleanup() {
    myOldProblemElements = null;

    synchronized (lock) {
      myProblemElements.clear();
      myProblemToElements.clear();
      myQuickFixActions.clear();
      myIgnoredElements.clear();
      myContents.clear();
      myModulesProblems.clear();
    }

    isDisposed = true;
  }
  @Override
  public void resolveEntryPoints(@NotNull final RefManager manager) {
    if (!myResolved) {
      myResolved = true;
      cleanup();
      validateEntryPoints();

      ApplicationManager.getApplication()
          .runReadAction(
              () -> {
                for (SmartRefElementPointer entryPoint : myPersistentEntryPoints.values()) {
                  if (entryPoint.resolve(manager)) {
                    RefEntity refElement = entryPoint.getRefElement();
                    ((RefElementImpl) refElement).setEntry(true);
                    ((RefElementImpl) refElement).setPermanentEntry(entryPoint.isPersistent());
                  }
                }

                for (ClassPattern pattern : myPatterns) {
                  final RefEntity refClass =
                      manager.getReference(RefJavaManager.CLASS, pattern.pattern);
                  if (refClass != null) {
                    for (RefMethod constructor : ((RefClass) refClass).getConstructors()) {
                      ((RefMethodImpl) constructor).setEntry(true);
                      ((RefMethodImpl) constructor).setPermanentEntry(true);
                    }
                  }
                }
              });
    }
  }
 @Override
 public Map<String, Set<RefEntity>> getOldContent() {
   if (myOldProblemElements == null) return null;
   final Map<String, Set<RefEntity>> oldContents =
       new com.intellij.util.containers.HashMap<String, Set<RefEntity>>();
   final Set<RefEntity> elements = myOldProblemElements.keySet();
   for (RefEntity element : elements) {
     String groupName =
         element instanceof RefElement
             ? element.getRefManager().getGroupName((RefElement) element)
             : element.getName();
     final Set<RefEntity> collection = myContents.get(groupName);
     if (collection != null) {
       final Set<RefEntity> currentElements = new HashSet<RefEntity>(collection);
       if (RefUtil.contains(element, currentElements)) continue;
     }
     Set<RefEntity> oldContent = oldContents.get(groupName);
     if (oldContent == null) {
       oldContent = new HashSet<RefEntity>();
       oldContents.put(groupName, oldContent);
     }
     oldContent.add(element);
   }
   return oldContents;
 }
  @Override
  @SuppressWarnings({"HardCodedStringLiteral"})
  public void loadState(Element element) {
    Element entryPointsElement = element.getChild("entry_points");
    if (entryPointsElement != null) {
      final String version = entryPointsElement.getAttributeValue(VERSION_ATTR);
      if (!Comparing.strEqual(version, VERSION)) {
        convert(entryPointsElement, myPersistentEntryPoints);
      } else {
        List content = entryPointsElement.getChildren();
        for (final Object aContent : content) {
          Element entryElement = (Element) aContent;
          if (ENTRY_POINT_ATTR.equals(entryElement.getName())) {
            SmartRefElementPointerImpl entryPoint = new SmartRefElementPointerImpl(entryElement);
            myPersistentEntryPoints.put(entryPoint.getFQName(), entryPoint);
          }
        }
      }
    }
    try {
      ADDITIONAL_ANNOTATIONS.readExternal(element);
    } catch (Throwable ignored) {
    }

    getPatterns().clear();
    for (Element pattern : element.getChildren("pattern")) {
      final ClassPattern classPattern = new ClassPattern();
      XmlSerializer.deserializeInto(classPattern, pattern);
      getPatterns().add(classPattern);
    }
  }
 public void cleanup() {
   purgeTemporaryEntryPoints();
   Collection<SmartRefElementPointer> entries = myPersistentEntryPoints.values();
   for (SmartRefElementPointer entry : entries) {
     entry.freeReference();
   }
 }
  @Override
  public void removeEntryPoint(@NotNull RefElement anEntryPoint) {
    myTemporaryEntryPoints.remove(anEntryPoint);

    Set<Map.Entry<String, SmartRefElementPointer>> set = myPersistentEntryPoints.entrySet();
    String key = null;
    for (Map.Entry<String, SmartRefElementPointer> entry : set) {
      SmartRefElementPointer value = entry.getValue();
      if (value.getRefElement() == anEntryPoint) {
        key = entry.getKey();
        break;
      }
    }

    if (key != null) {
      myPersistentEntryPoints.remove(key);
    }
    ((RefElementImpl) anEntryPoint).setEntry(false);

    if (anEntryPoint.isPermanentEntry() && anEntryPoint.isValid()) {
      final Project project = anEntryPoint.getElement().getProject();
      final EntryPointsManager entryPointsManager = getInstance(project);
      if (this != entryPointsManager) {
        entryPointsManager.removeEntryPoint(anEntryPoint);
      }
    }

    if (anEntryPoint instanceof RefMethod && ((RefMethod) anEntryPoint).isConstructor()
        || anEntryPoint instanceof RefClass) {
      final RefClass aClass =
          anEntryPoint instanceof RefClass
              ? (RefClass) anEntryPoint
              : ((RefMethod) anEntryPoint).getOwnerClass();
      final String qualifiedName = aClass.getQualifiedName();
      for (Iterator<ClassPattern> iterator = getPatterns().iterator(); iterator.hasNext(); ) {
        if (Comparing.equal(iterator.next().pattern, qualifiedName)) {
          // todo if inheritance or pattern?
          iterator.remove();
        }
      }
    }
  }
 @NotNull
 @Override
 public FileStatus getElementStatus(final RefEntity element) {
   final GlobalInspectionContextImpl context = getContext();
   if (!isDisposed() && context.getUIOptions().SHOW_DIFF_WITH_PREVIOUS_RUN) {
     if (myOldProblemElements != null) {
       final boolean old = RefUtil.contains(element, myOldProblemElements.keySet());
       final boolean current = RefUtil.contains(element, getProblemElements().keySet());
       return calcStatus(old, current);
     }
   }
   return FileStatus.NOT_CHANGED;
 }
  private void validateEntryPoints() {
    long count = PsiManager.getInstance(myProject).getModificationTracker().getModificationCount();
    if (count != myLastModificationCount) {
      myLastModificationCount = count;
      Collection<SmartRefElementPointer> collection = myPersistentEntryPoints.values();
      SmartRefElementPointer[] entries =
          collection.toArray(new SmartRefElementPointer[collection.size()]);
      for (SmartRefElementPointer entry : entries) {
        RefElement refElement = (RefElement) entry.getRefElement();
        if (refElement != null && !refElement.isValid()) {
          myPersistentEntryPoints.remove(entry.getFQName());
        }
      }

      final Iterator<RefElement> it = myTemporaryEntryPoints.iterator();
      while (it.hasNext()) {
        RefElement refElement = it.next();
        if (!refElement.isValid()) {
          it.remove();
        }
      }
    }
  }
  public RefElement[] getEntryPoints() {
    validateEntryPoints();
    List<RefElement> entries = new ArrayList<RefElement>();
    Collection<SmartRefElementPointer> collection = myPersistentEntryPoints.values();
    for (SmartRefElementPointer refElementPointer : collection) {
      final RefEntity elt = refElementPointer.getRefElement();
      if (elt instanceof RefElement) {
        entries.add((RefElement) elt);
      }
    }
    entries.addAll(myTemporaryEntryPoints);

    return entries.toArray(new RefElement[entries.size()]);
  }
 @Override
 public boolean hasReportedProblems() {
   final GlobalInspectionContextImpl context = getContext();
   if (!isDisposed() && context.getUIOptions().SHOW_ONLY_DIFF) {
     for (CommonProblemDescriptor descriptor : getProblemToElements().keySet()) {
       if (getProblemStatus(descriptor) != FileStatus.NOT_CHANGED) {
         return true;
       }
     }
     if (myOldProblemElements != null) {
       for (RefEntity entity : myOldProblemElements.keySet()) {
         if (getElementStatus(entity) != FileStatus.NOT_CHANGED) {
           return true;
         }
       }
     }
     return false;
   }
   if (!getProblemElements().isEmpty()) return true;
   return !isDisposed()
       && context.getUIOptions().SHOW_DIFF_WITH_PREVIOUS_RUN
       && myOldProblemElements != null
       && !myOldProblemElements.isEmpty();
 }
  @SuppressWarnings({"HardCodedStringLiteral"})
  public static void writeExternal(
      final Element element,
      final Map<String, SmartRefElementPointer> persistentEntryPoints,
      final JDOMExternalizableStringList additional_annotations) {
    Element entryPointsElement = new Element("entry_points");
    entryPointsElement.setAttribute(VERSION_ATTR, VERSION);
    for (SmartRefElementPointer entryPoint : persistentEntryPoints.values()) {
      assert entryPoint.isPersistent();
      entryPoint.writeExternal(entryPointsElement);
    }

    element.addContent(entryPointsElement);
    if (!additional_annotations.isEmpty()) {
      additional_annotations.writeExternal(element);
    }
  }
  public static void convert(
      Element element, final Map<String, SmartRefElementPointer> persistentEntryPoints) {
    List content = element.getChildren();
    for (final Object aContent : content) {
      Element entryElement = (Element) aContent;
      if (ENTRY_POINT_ATTR.equals(entryElement.getName())) {
        String fqName = entryElement.getAttributeValue(SmartRefElementPointerImpl.FQNAME_ATTR);
        final String type = entryElement.getAttributeValue(SmartRefElementPointerImpl.TYPE_ATTR);
        if (Comparing.strEqual(type, RefJavaManager.METHOD)) {

          int spaceIdx = fqName.indexOf(' ');
          int lastDotIdx = fqName.lastIndexOf('.');

          int parenIndex = fqName.indexOf('(');

          while (lastDotIdx > parenIndex) lastDotIdx = fqName.lastIndexOf('.', lastDotIdx - 1);

          boolean notype = false;
          if (spaceIdx < 0 || spaceIdx + 1 > lastDotIdx || spaceIdx > parenIndex) {
            notype = true;
          }

          final String className = fqName.substring(notype ? 0 : spaceIdx + 1, lastDotIdx);
          final String methodSignature =
              notype
                  ? fqName.substring(lastDotIdx + 1)
                  : fqName.substring(0, spaceIdx) + ' ' + fqName.substring(lastDotIdx + 1);

          fqName = className + " " + methodSignature;
        } else if (Comparing.strEqual(type, RefJavaManager.FIELD)) {
          final int lastDotIdx = fqName.lastIndexOf('.');
          if (lastDotIdx > 0 && lastDotIdx < fqName.length() - 2) {
            String className = fqName.substring(0, lastDotIdx);
            String fieldName = fqName.substring(lastDotIdx + 1);
            fqName = className + " " + fieldName;
          } else {
            continue;
          }
        }
        SmartRefElementPointerImpl entryPoint = new SmartRefElementPointerImpl(type, fqName);
        persistentEntryPoints.put(entryPoint.getFQName(), entryPoint);
      }
    }
  }
 @Override
 @NotNull
 public FileStatus getProblemStatus(@NotNull final CommonProblemDescriptor descriptor) {
   final GlobalInspectionContextImpl context = getContext();
   if (!isDisposed() && context.getUIOptions().SHOW_DIFF_WITH_PREVIOUS_RUN) {
     if (myOldProblemElements != null) {
       final Set<CommonProblemDescriptor> allAvailable = new HashSet<CommonProblemDescriptor>();
       for (CommonProblemDescriptor[] descriptors : myOldProblemElements.values()) {
         if (descriptors != null) {
           ContainerUtil.addAll(allAvailable, descriptors);
         }
       }
       final boolean old = containsDescriptor(descriptor, allAvailable);
       final boolean current = containsDescriptor(descriptor, getProblemToElements().keySet());
       return calcStatus(old, current);
     }
   }
   return FileStatus.NOT_CHANGED;
 }
 @SuppressWarnings({"HardCodedStringLiteral"})
 public void loadState(Element element) {
   Element entryPointsElement = element.getChild("entry_points");
   if (entryPointsElement != null) {
     final String version = entryPointsElement.getAttributeValue(VERSION_ATTR);
     if (!Comparing.strEqual(version, VERSION)) {
       convert(entryPointsElement, myPersistentEntryPoints);
     } else {
       List content = entryPointsElement.getChildren();
       for (final Object aContent : content) {
         Element entryElement = (Element) aContent;
         if (ENTRY_POINT_ATTR.equals(entryElement.getName())) {
           SmartRefElementPointerImpl entryPoint = new SmartRefElementPointerImpl(entryElement);
           myPersistentEntryPoints.put(entryPoint.getFQName(), entryPoint);
         }
       }
     }
   }
   try {
     ADDITIONAL_ANNOTATIONS.readExternal(element);
   } catch (InvalidDataException ignored) {
   }
 }
 @Override
 @Nullable
 public QuickFixAction[] extractActiveFixes(
     @NotNull RefEntity[] refElements, @NotNull Map<RefEntity, Set<QuickFix>> actions) {
   Map<Class, QuickFixAction> result =
       new com.intellij.util.containers.HashMap<Class, QuickFixAction>();
   for (RefEntity refElement : refElements) {
     final Set<QuickFix> localQuickFixes = actions.get(refElement);
     if (localQuickFixes == null) continue;
     for (QuickFix fix : localQuickFixes) {
       if (fix == null) continue;
       final Class klass =
           fix instanceof ActionClassHolder
               ? ((ActionClassHolder) fix).getActionClass()
               : fix.getClass();
       final QuickFixAction quickFixAction = result.get(klass);
       if (quickFixAction != null) {
         try {
           String familyName = fix.getFamilyName();
           familyName = !familyName.isEmpty() ? "\'" + familyName + "\'" : familyName;
           ((LocalQuickFixWrapper) quickFixAction)
               .setText(
                   InspectionsBundle.message(
                       "inspection.descriptor.provider.apply.fix", familyName));
         } catch (AbstractMethodError e) {
           // for plugin compatibility
           ((LocalQuickFixWrapper) quickFixAction)
               .setText(InspectionsBundle.message("inspection.descriptor.provider.apply.fix", ""));
         }
       } else {
         LocalQuickFixWrapper quickFixWrapper = new LocalQuickFixWrapper(fix, myToolWrapper);
         result.put(klass, quickFixWrapper);
       }
     }
   }
   return result.values().isEmpty()
       ? null
       : result.values().toArray(new QuickFixAction[result.size()]);
 }
 public void addAllPersistentEntries(EntryPointsManagerImpl manager) {
   myPersistentEntryPoints.putAll(manager.myPersistentEntryPoints);
 }
 public void addAllPersistentEntries(EntryPointsManagerBase manager) {
   myPersistentEntryPoints.putAll(manager.myPersistentEntryPoints);
   myPatterns.addAll(manager.getPatterns());
 }
  @Override
  public void addProblemElement(
      final RefEntity refElement,
      boolean filterSuppressed,
      @NotNull final CommonProblemDescriptor... descriptors) {
    if (refElement == null) return;
    if (descriptors.length == 0) return;
    if (filterSuppressed) {
      if (!isOutputPathSet() || !(myToolWrapper instanceof LocalInspectionToolWrapper)) {
        synchronized (lock) {
          Map<RefEntity, CommonProblemDescriptor[]> problemElements = getProblemElements();
          CommonProblemDescriptor[] problems = problemElements.get(refElement);
          problems = problems == null ? descriptors : mergeDescriptors(problems, descriptors);
          problemElements.put(refElement, problems);
        }
        for (CommonProblemDescriptor description : descriptors) {
          getProblemToElements().put(description, refElement);
          collectQuickFixes(description.getFixes(), refElement);
        }
      } else {
        writeOutput(descriptors, refElement);
      }
    } else { // just need to collect problems
      for (CommonProblemDescriptor descriptor : descriptors) {
        getProblemToElements().put(descriptor, refElement);
      }
    }

    final GlobalInspectionContextImpl context = getContext();
    if (myToolWrapper instanceof LocalInspectionToolWrapper) {
      final InspectionResultsView view = context.getView();
      if (view == null || !(refElement instanceof RefElement)) {
        return;
      }
      UIUtil.invokeLaterIfNeeded(
          new Runnable() {
            @Override
            public void run() {
              if (!isDisposed()) {
                final InspectionNode toolNode;
                synchronized (myToolLock) {
                  if (myToolNode == null) {
                    final HighlightSeverity currentSeverity = getSeverity((RefElement) refElement);
                    toolNode =
                        view.addTool(
                            myToolWrapper,
                            HighlightDisplayLevel.find(currentSeverity),
                            context.getUIOptions().GROUP_BY_SEVERITY);
                  } else {
                    toolNode = myToolNode;
                    if (toolNode.isTooBigForOnlineRefresh()) {
                      return;
                    }
                  }
                }
                final Map<RefEntity, CommonProblemDescriptor[]> problems =
                    new HashMap<RefEntity, CommonProblemDescriptor[]>();
                problems.put(refElement, descriptors);
                final Map<String, Set<RefEntity>> contents = new HashMap<String, Set<RefEntity>>();
                final String groupName =
                    refElement.getRefManager().getGroupName((RefElement) refElement);
                Set<RefEntity> content = contents.get(groupName);
                if (content == null) {
                  content = new HashSet<RefEntity>();
                  contents.put(groupName, content);
                }
                content.add(refElement);

                view.getProvider()
                    .appendToolNodeContent(
                        context,
                        toolNode,
                        (InspectionTreeNode) toolNode.getParent(),
                        context.getUIOptions().SHOW_STRUCTURE,
                        contents,
                        problems,
                        (DefaultTreeModel) view.getTree().getModel());
                context.addView(view);
              }
            }
          });
    }
  }