@Override
  public String getQualifiedName(RefEntity refEntity) {
    if (refEntity == null || refEntity instanceof RefElementImpl && !refEntity.isValid()) {
      return InspectionsBundle.message("inspection.reference.invalid");
    }

    return refEntity.getQualifiedName();
  }
  @Override
  @Nullable
  public RefClass getOwnerClass(RefElement refElement) {
    RefEntity parent = refElement.getOwner();

    while (!(parent instanceof RefClass) && parent instanceof RefElement) {
      parent = parent.getOwner();
    }

    if (parent instanceof RefClass) return (RefClass) parent;

    return null;
  }
  @Override
  public RefClass getTopLevelClass(RefElement refElement) {
    RefEntity refParent = refElement.getOwner();

    while (refParent != null
        && refParent instanceof RefElement
        && !(refParent instanceof RefFile)) {
      refElement = (RefElementImpl) refParent;
      refParent = refParent.getOwner();
    }

    return refElement instanceof RefClass ? (RefClass) refElement : null;
  }
 @Nullable
 public String getQualifiedName(final RefEntity refEntity) {
   if (refEntity instanceof RefJavaElement && ((RefJavaElement) refEntity).isSyntheticJSP()) {
     return XmlStringUtil.escapeString(refEntity.getName());
   } else if (refEntity instanceof RefMethod) {
     PsiMethod psiMethod = (PsiMethod) ((RefMethod) refEntity).getElement();
     if (psiMethod != null) {
       return psiMethod.getName();
     } else {
       return refEntity.getName();
     }
   }
   return null;
 }
  @Override
  @Nullable
  public String getGroupName(final RefElement entity) {
    for (RefManagerExtension extension : myExtensions.values()) {
      final String groupName = extension.getGroupName(entity);
      if (groupName != null) return groupName;
    }

    final LinkedList<String> containingDirs = new LinkedList<>();
    RefEntity parent = entity.getOwner();
    while (parent != null && !(parent instanceof RefDirectory)) {
      parent = parent.getOwner();
    }
    while (parent instanceof RefDirectory) {
      containingDirs.addFirst(parent.getName());
      parent = parent.getOwner();
    }
    return containingDirs.isEmpty() ? null : StringUtil.join(containingDirs, File.separator);
  }
  @Override
  public Element export(
      @NotNull RefEntity refEntity, @NotNull final Element element, final int actualLine) {
    refEntity = getRefinedElement(refEntity);

    Element problem = new Element("problem");

    if (refEntity instanceof RefElement) {
      final RefElement refElement = (RefElement) refEntity;
      final SmartPsiElementPointer pointer = refElement.getPointer();
      PsiFile psiFile = pointer.getContainingFile();
      if (psiFile == null) return null;

      Element fileElement = new Element("file");
      Element lineElement = new Element("line");
      final VirtualFile virtualFile = psiFile.getVirtualFile();
      LOG.assertTrue(virtualFile != null);
      fileElement.addContent(virtualFile.getUrl());

      if (actualLine == -1) {
        final Document document =
            PsiDocumentManager.getInstance(pointer.getProject()).getDocument(psiFile);
        LOG.assertTrue(document != null);
        final Segment range = pointer.getRange();
        lineElement.addContent(
            String.valueOf(
                range != null ? document.getLineNumber(range.getStartOffset()) + 1 : -1));
      } else {
        lineElement.addContent(String.valueOf(actualLine));
      }

      problem.addContent(fileElement);
      problem.addContent(lineElement);

      appendModule(problem, refElement.getModule());
    } else if (refEntity instanceof RefModule) {
      final RefModule refModule = (RefModule) refEntity;
      final VirtualFile moduleFile = refModule.getModule().getModuleFile();
      final Element fileElement = new Element("file");
      fileElement.addContent(moduleFile != null ? moduleFile.getUrl() : refEntity.getName());
      problem.addContent(fileElement);
      appendModule(problem, refModule);
    }

    for (RefManagerExtension extension : myExtensions.values()) {
      extension.export(refEntity, problem);
    }

    new SmartRefElementPointerImpl(refEntity, true).writeExternal(problem);
    element.addContent(problem);
    return problem;
  }
  private static void ignoreElement(
      @NotNull ProblemDescriptionsProcessor processor, @NotNull RefEntity refElement) {
    processor.ignoreElement(refElement);

    if (refElement instanceof RefClass) {
      RefClass refClass = (RefClass) refElement;
      RefMethod defaultConstructor = refClass.getDefaultConstructor();
      if (defaultConstructor != null) {
        processor.ignoreElement(defaultConstructor);
        return;
      }
    }

    RefEntity owner = refElement.getOwner();
    if (owner instanceof RefElement) {
      processor.ignoreElement(owner);
    }
  }
 public void appendLocation(final RefEntity entity, final StringBuffer buf) {
   RefEntity owner = entity.getOwner();
   if (owner instanceof RefPackage) {
     buf.append(InspectionsBundle.message("inspection.export.results.package"));
     buf.append(HTMLComposerImpl.NBSP).append(HTMLComposerImpl.CODE_OPENING);
     buf.append(RefJavaUtil.getInstance().getPackageName(entity));
     buf.append(HTMLComposerImpl.CODE_CLOSING);
   } else if (owner instanceof RefMethod) {
     buf.append(InspectionsBundle.message("inspection.export.results.method"));
     buf.append(HTMLComposerImpl.NBSP);
     myComposer.appendElementReference(buf, (RefElement) owner);
   } else if (owner instanceof RefField) {
     buf.append(InspectionsBundle.message("inspection.export.results.field"));
     buf.append(HTMLComposerImpl.NBSP);
     myComposer.appendElementReference(buf, (RefElement) owner);
     buf.append(HTMLComposerImpl.NBSP);
     buf.append(InspectionsBundle.message("inspection.export.results.initializer"));
   } else if (owner instanceof RefClass) {
     appendClassOrInterface(buf, (RefClass) owner, false);
     buf.append(HTMLComposerImpl.NBSP);
     myComposer.appendElementReference(buf, (RefElement) owner);
   }
 }
  public void appendReferencePresentation(
      RefEntity refElement, final StringBuffer buf, final boolean isPackageIncluded) {
    if (refElement instanceof RefImplicitConstructor) {
      buf.append(InspectionsBundle.message("inspection.export.results.implicit.constructor"));
      refElement = ((RefImplicitConstructor) refElement).getOwnerClass();
    }

    buf.append(HTMLComposerImpl.CODE_OPENING);

    if (refElement instanceof RefField) {
      RefField field = (RefField) refElement;
      PsiField psiField = field.getElement();
      buf.append(psiField.getType().getPresentableText());
      buf.append(HTMLComposerImpl.NBSP);
    } else if (refElement instanceof RefMethod) {
      RefMethod method = (RefMethod) refElement;
      PsiMethod psiMethod = (PsiMethod) method.getElement();
      PsiType returnType = psiMethod.getReturnType();

      if (returnType != null) {
        buf.append(returnType.getPresentableText());
        buf.append(HTMLComposerImpl.NBSP);
      }
    }

    buf.append(HTMLComposerImpl.A_HREF_OPENING);

    if (myComposer.myExporter == null) {
      buf.append(((RefElementImpl) refElement).getURL());
    } else {
      buf.append(myComposer.myExporter.getURL(refElement));
    }

    buf.append("\">");

    if (refElement instanceof RefClass && ((RefClass) refElement).isAnonymous()) {
      buf.append(InspectionsBundle.message("inspection.reference.anonymous"));
    } else if (refElement instanceof RefJavaElement
        && ((RefJavaElement) refElement).isSyntheticJSP()) {
      buf.append(XmlStringUtil.escapeString(refElement.getName()));
    } else if (refElement instanceof RefMethod) {
      PsiMethod psiMethod = (PsiMethod) ((RefMethod) refElement).getElement();
      buf.append(psiMethod.getName());
    } else {
      buf.append(refElement.getName());
    }

    buf.append(HTMLComposerImpl.A_CLOSING);

    if (refElement instanceof RefMethod) {
      PsiMethod psiMethod = (PsiMethod) ((RefMethod) refElement).getElement();
      appendMethodParameters(buf, psiMethod, false);
    }

    buf.append(HTMLComposerImpl.CODE_CLOSING);

    if (refElement instanceof RefClass && ((RefClass) refElement).isAnonymous()) {
      buf.append(" ");
      buf.append(InspectionsBundle.message("inspection.export.results.anonymous.ref.in.owner"));
      buf.append(" ");
      myComposer.appendElementReference(
          buf, ((RefElement) refElement.getOwner()), isPackageIncluded);
    } else if (isPackageIncluded) {
      buf.append(" ").append(HTMLComposerImpl.CODE_OPENING).append("(");
      myComposer.appendQualifiedName(buf, refElement.getOwner());
      //      buf.append(RefUtil.getPackageName(refElement));
      buf.append(")").append(HTMLComposerImpl.CODE_CLOSING);
    }
  }
  public void appendShortName(final RefEntity refElement, final StringBuffer buf) {
    if (refElement instanceof RefJavaElement) {
      String modifier = ((RefJavaElement) refElement).getAccessModifier();
      if (modifier != null && modifier != PsiModifier.PACKAGE_LOCAL) {
        buf.append(modifier);
        buf.append(HTMLComposerImpl.NBSP);
      }
    }
    refElement.accept(
        new RefJavaVisitor() {
          @Override
          public void visitClass(@NotNull RefClass refClass) {
            if (refClass.isStatic()) {
              buf.append(InspectionsBundle.message("inspection.export.results.static"));
              buf.append(HTMLComposerImpl.NBSP);
            }

            appendClassOrInterface(buf, refClass, false);
            buf.append(HTMLComposerImpl.NBSP)
                .append(HTMLComposerImpl.B_OPENING)
                .append(HTMLComposerImpl.CODE_OPENING);
            final String name = refClass.getName();
            buf.append(refClass.isSyntheticJSP() ? XmlStringUtil.escapeString(name) : name);
            buf.append(HTMLComposerImpl.CODE_CLOSING).append(HTMLComposerImpl.B_CLOSING);
          }

          @Override
          public void visitField(@NotNull RefField field) {
            PsiField psiField = field.getElement();
            if (psiField != null) {
              if (field.isStatic()) {
                buf.append(InspectionsBundle.message("inspection.export.results.static"));
                buf.append(HTMLComposerImpl.NBSP);
              }

              buf.append(InspectionsBundle.message("inspection.export.results.field"));
              buf.append(HTMLComposerImpl.NBSP).append(HTMLComposerImpl.CODE_OPENING);

              buf.append(psiField.getType().getPresentableText());
              buf.append(HTMLComposerImpl.NBSP).append(HTMLComposerImpl.B_OPENING);
              buf.append(psiField.getName());
              buf.append(HTMLComposerImpl.B_CLOSING).append(HTMLComposerImpl.CODE_CLOSING);
            }
          }

          @Override
          public void visitMethod(@NotNull RefMethod method) {
            PsiMethod psiMethod = (PsiMethod) method.getElement();
            if (psiMethod != null) {
              PsiType returnType = psiMethod.getReturnType();

              if (method.isStatic()) {
                buf.append(InspectionsBundle.message("inspection.export.results.static"));
                buf.append(HTMLComposerImpl.NBSP);
              } else if (method.isAbstract()) {
                buf.append(InspectionsBundle.message("inspection.export.results.abstract"));
                buf.append(HTMLComposerImpl.NBSP);
              }
              buf.append(
                  method.isConstructor()
                      ? InspectionsBundle.message("inspection.export.results.constructor")
                      : InspectionsBundle.message("inspection.export.results.method"));
              buf.append(HTMLComposerImpl.NBSP).append(HTMLComposerImpl.CODE_OPENING);

              if (returnType != null) {
                buf.append(returnType.getPresentableText());
                buf.append(HTMLComposerImpl.NBSP);
              }

              buf.append(HTMLComposerImpl.B_OPENING);
              buf.append(psiMethod.getName());
              buf.append(HTMLComposerImpl.B_CLOSING);
              appendMethodParameters(buf, psiMethod, true);
              buf.append(HTMLComposerImpl.CODE_CLOSING);
            }
          }

          @Override
          public void visitFile(@NotNull RefFile file) {
            final PsiFile psiFile = file.getElement();
            buf.append(HTMLComposerImpl.B_OPENING);
            buf.append(psiFile.getName());
            buf.append(HTMLComposerImpl.B_CLOSING);
          }
        });
  }